Skip to content

Commit

Permalink
Fixes timing issue in get iq callback
Browse files Browse the repository at this point in the history
Without this fix, my can calls wouls always fail for axis1.
They didn't fail for axis0. I don't know why.
  • Loading branch information
tobbelobb committed Apr 7, 2022
1 parent 4ec5a01 commit 87633b8
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions Firmware/communication/can/can_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,25 @@ bool CANSimple::get_iq_callback(const Axis& axis) {
txmsg.isExt = axis.config_.can.is_extended;
txmsg.len = 8;

std::optional<float2D> Idq_setpoint = axis.motor_.current_control_.Idq_setpoint_;
if (!Idq_setpoint.has_value()) {
Idq_setpoint = {0.0f, 0.0f};
}

static_assert(sizeof(float) == sizeof(Idq_setpoint->second));
static_assert(sizeof(float) == sizeof(axis.motor_.current_control_.Iq_measured_));
can_setSignal<float>(txmsg, Idq_setpoint->second, 0, 32, true);
can_setSignal<float>(txmsg, axis.motor_.current_control_.Iq_measured_, 32, 32, true);
auto& current_control = axis.motor_.current_control_;
auto [dummy, Idq_setpoint] = current_control.Idq_setpoint_.value();
float const Iq_measured = current_control.Iq_measured_;

uint32_t floatBytes;
static_assert(sizeof(Idq_setpoint) == sizeof(floatBytes));
std::memcpy(&floatBytes, &Idq_setpoint, sizeof(floatBytes));

txmsg.buf[0] = floatBytes;
txmsg.buf[1] = floatBytes >> 8;
txmsg.buf[2] = floatBytes >> 16;
txmsg.buf[3] = floatBytes >> 24;

static_assert(sizeof(floatBytes) == sizeof(Iq_measured));
std::memcpy(&floatBytes, &Iq_measured, sizeof(floatBytes));
txmsg.buf[4] = floatBytes;
txmsg.buf[5] = floatBytes >> 8;
txmsg.buf[6] = floatBytes >> 16;
txmsg.buf[7] = floatBytes >> 24;

return canbus_->send_message(txmsg);
}
Expand Down

0 comments on commit 87633b8

Please sign in to comment.