Skip to content

Commit

Permalink
Update democan project to compile with new autogen; not working
Browse files Browse the repository at this point in the history
  • Loading branch information
samparent97 committed Apr 12, 2024
1 parent 0035f5e commit dc0bc45
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 72 deletions.
169 changes: 120 additions & 49 deletions firmware/projects/DemoCan/generated/can_messages.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
/// @author Samuel Parent
/// @date 2023-01-17
/// @date 2024-04-12

// WARNING: DO NOT MODIFY THIS CODE. THIS IS AN AUTOGENERATED FILE.

/// @todo add override keyword
/// @todo support enums
/// @todo clang format
/// @todo make Id static inline
/// @todo make Clone work in a better way

// clang-format off

#pragma once

Expand All @@ -8,68 +18,129 @@

namespace generated::can {

class TmsBroadcast : public shared::can::CanTxMsg {
/* -------------------- RX MESSAGES -------------------- */

class VehicleInfo : public shared::can::CanRxMsg {
public:
float requested_speed = 0;
float wheel_speed = 0;

private:
static constexpr shared::can::CanId kCanId = 0x1839F380;
// Message properties
static constexpr shared::can::CanId kCanId = 0x245;
static constexpr uint8_t kDlc = 8;
static constexpr bool kIsExtFrame = true;

void Pack(shared::can::RawCanMsg& raw_msg) {
raw_msg.header.id = kCanId;
raw_msg.header.data_len = kDlc;
raw_msg.header.is_extended_frame = kIsExtFrame;

raw_msg.data[0] = static_cast<uint8_t>(therm_module_num & 0xFF);
raw_msg.data[1] = static_cast<uint8_t>(num_therm_enabled & 0xFF);
raw_msg.data[2] = static_cast<uint8_t>(low_therm_value & 0xFF);
raw_msg.data[3] = static_cast<uint8_t>(high_therm_value & 0xFF);
raw_msg.data[4] = static_cast<uint8_t>(avg_therm_value & 0xFF);
raw_msg.data[5] = static_cast<uint8_t>(high_therm_id & 0xFF);
raw_msg.data[6] = static_cast<uint8_t>(low_therm_id & 0xFF);
raw_msg.data[7] = static_cast<uint8_t>(checksum & 0xFF);
static constexpr bool kIsExtFrame = false;

// Signal properties
static constexpr float kRequestedSpeedScale = 0.00390625;
static constexpr float kRequestedSpeedOffset = 0;
static constexpr float kWheelSpeedScale = 0.00390625;
static constexpr float kWheelSpeedOffset = 0;

shared::can::CanId Id() const override { return kCanId; }

void Clone(shared::can::CanRxMsg& rx_msg) const override {
VehicleInfo* p_rx_msg = static_cast<VehicleInfo*>(&rx_msg);

p_rx_msg->requested_speed = requested_speed;
p_rx_msg->wheel_speed = wheel_speed;
}

public:
uint8_t therm_module_num = 0;
uint8_t num_therm_enabled = 0;
int8_t low_therm_value = 0;
int8_t high_therm_value = 0;
int8_t avg_therm_value = 0;
uint8_t high_therm_id = 0;
uint8_t low_therm_id = 0;
uint8_t checksum = 0;
void Unpack(const shared::can::RawCanMsg& raw_msg) override {
if (raw_msg.header.id != kCanId) {
return;
}

// temporary signal variables
uint32_t temp_requested_speed = 0;
uint32_t temp_wheel_speed = 0;

temp_requested_speed |= unpack_left_shift<uint32_t>(raw_msg.data[0], 20U, 0xfU);
temp_requested_speed |= unpack_left_shift<uint32_t>(raw_msg.data[1], 12U, 0xffU);
temp_requested_speed |= unpack_left_shift<uint32_t>(raw_msg.data[2], 4U, 0xffU);
temp_requested_speed |= unpack_right_shift<uint32_t>(raw_msg.data[3], 4U, 0xf0U);
requested_speed = (static_cast<float>(temp_requested_speed) * kRequestedSpeedScale) + kRequestedSpeedOffset;

temp_wheel_speed |= unpack_left_shift<uint32_t>(raw_msg.data[3], 29U, 0x7U);
temp_wheel_speed |= unpack_left_shift<uint32_t>(raw_msg.data[4], 21U, 0xffU);
temp_wheel_speed |= unpack_left_shift<uint32_t>(raw_msg.data[5], 13U, 0xffU);
temp_wheel_speed |= unpack_left_shift<uint32_t>(raw_msg.data[6], 5U, 0xffU);
temp_wheel_speed |= unpack_right_shift<uint32_t>(raw_msg.data[7], 3U, 0xf8U);
wheel_speed = (static_cast<float>(temp_wheel_speed) * kWheelSpeedScale) + kWheelSpeedOffset;

}
};

class DebugLedOverride : public shared::can::CanRxMsg {
private:
static constexpr shared::can::CanId kCanId = 0x645;
static constexpr uint8_t kDlc = 1;
static constexpr bool kIsExtFrame = true;
/* -------------------- TX MESSAGES -------------------- */

class TempSensors : public shared::can::CanTxMsg {
public:
void Clone(shared::can::CanRxMsg& rx_msg) const {
DebugLedOverride* p_rx_msg = static_cast<DebugLedOverride*>(&rx_msg);
float sensor6 = 0;
float sensor5 = 0;
float sensor4 = 0;
float sensor3 = 0;
float sensor2 = 0;
float sensor1 = 0;

p_rx_msg->set_green_led = set_green_led;
p_rx_msg->set_red_led = set_red_led;
}
private:
// Message properties
static constexpr shared::can::CanId kCanId = 0x3ac;
static constexpr uint8_t kDlc = 8;
static constexpr bool kIsExtFrame = false;

void Unpack(const shared::can::RawCanMsg& raw_msg) {
if (raw_msg.header.id == kCanId) {
set_green_led = static_cast<uint8_t>(raw_msg.data[0] & 0b00000001);
set_red_led = static_cast<uint8_t>(raw_msg.data[0] & 0b00000010);
// Signal properties
static constexpr float kSensor6Scale = 0.2;
static constexpr float kSensor6Offset = 0;
static constexpr float kSensor5Scale = 0.2;
static constexpr float kSensor5Offset = 0;
static constexpr float kSensor4Scale = 0.2;
static constexpr float kSensor4Offset = 0;
static constexpr float kSensor3Scale = 0.2;
static constexpr float kSensor3Offset = 0;
static constexpr float kSensor2Scale = 0.2;
static constexpr float kSensor2Offset = 0;
static constexpr float kSensor1Scale = 0.2;
static constexpr float kSensor1Offset = 0;

void Pack(shared::can::RawCanMsg& raw_msg) override {
if (raw_msg.header.id != kCanId) {
return;
}
}

shared::can::CanId Id() const {
return kCanId;
}
// temporary raw msg
shared::can::RawCanMsg temp_raw_msg;
temp_raw_msg.header = {
.id = kCanId,
.data_len = kDlc,
.is_extended_frame = kIsExtFrame,
};

DebugLedOverride(){};
~DebugLedOverride(){};
// temporary signal variables
int16_t temp_sensor6 = sensor6;
int16_t temp_sensor5 = sensor5;
int16_t temp_sensor4 = sensor4;
int16_t temp_sensor3 = sensor3;
int16_t temp_sensor2 = sensor2;
int16_t temp_sensor1 = sensor1;

bool set_green_led = false;
bool set_red_led = false;
temp_raw_msg.data[0] |= pack_left_shift(temp_sensor6, 4U, 0xf0U);
temp_raw_msg.data[1] |= pack_right_shift(temp_sensor6, 4U, 0x3fU);
temp_raw_msg.data[1] |= pack_left_shift(temp_sensor5, 6U, 0xc0U);
temp_raw_msg.data[2] |= pack_right_shift(temp_sensor5, 2U, 0xffU);
temp_raw_msg.data[3] |= pack_right_shift(temp_sensor4, 0U, 0xffU);
temp_raw_msg.data[4] |= pack_right_shift(temp_sensor4, 8U, 0x3U);
temp_raw_msg.data[4] |= pack_left_shift(temp_sensor3, 2U, 0xfcU);
temp_raw_msg.data[5] |= pack_right_shift(temp_sensor3, 6U, 0xfU);
temp_raw_msg.data[5] |= pack_left_shift(temp_sensor2, 4U, 0xf0U);
temp_raw_msg.data[6] |= pack_right_shift(temp_sensor2, 4U, 0x3fU);
temp_raw_msg.data[6] |= pack_left_shift(temp_sensor1, 6U, 0xc0U);
temp_raw_msg.data[7] |= pack_right_shift(temp_sensor1, 2U, 0xffU);

// Copy temp raw msg to raw msg
raw_msg = temp_raw_msg;
}
};

// clang-format on

} // namespace generated::can
15 changes: 9 additions & 6 deletions firmware/projects/DemoCan/generated/msg_registry.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// @author Samuel Parent
/// @date 2023-03-01
/// @date 2024-04-12

#pragma once

Expand All @@ -11,10 +11,8 @@

namespace generated::can {

class DemoCanVehMsgRegistry : public shared::can::MsgRegistry {
class VehMsgRegistry : public shared::can::MsgRegistry {
public:
DemoCanVehMsgRegistry(){};

bool SetMessage(const shared::can::RawCanMsg& raw_msg) {
bool msg_found = false;

Expand Down Expand Up @@ -44,11 +42,16 @@ class DemoCanVehMsgRegistry : public shared::can::MsgRegistry {
private:
static constexpr size_t kNumRxMsgs = 1;

// Message IDs
static constexpr shared::can::CanId kVehicleInfoCanId = 0x245;

// Can Rx Messages
DebugLedOverride debug_led_override_;
VehicleInfo vehicle_info_;

etl::unordered_map<shared::can::CanId, shared::can::CanRxMsg*, kNumRxMsgs>
rx_msg_map_ = {{0x645, &debug_led_override_}};
rx_msg_map_ = {
{ kVehicleInfoCanId, &vehicle_info_ },
};
};

} // namespace generated::can
30 changes: 14 additions & 16 deletions firmware/projects/DemoCan/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern shared::periph::CanBase& veh_can_base;
extern void Initialize();
} // namespace bindings

generated::can::DemoCanVehMsgRegistry veh_can_registry{};
generated::can::VehMsgRegistry veh_can_registry{};

shared::can::CanBus veh_can_bus{
bindings::veh_can_base,
Expand All @@ -23,32 +23,30 @@ shared::can::CanBus veh_can_bus{

int main(void) {
bindings::Initialize();
std::chrono::milliseconds duration(1000);
std::chrono::milliseconds duration(100);

generated::can::TmsBroadcast tms_msg;
generated::can::DebugLedOverride led_msg;
generated::can::TempSensors temp_sens_msg;
generated::can::VehicleInfo veh_info_msg;

int i = 0;
while (1) {
std::this_thread::sleep_for(duration);

veh_can_bus.Update();

veh_can_bus.Read(led_msg);
veh_can_bus.Read(veh_info_msg);

std::cout << "led green: " << led_msg.set_green_led
<< " led red: " << led_msg.set_red_led << std::endl;
std::cout << "requested speed: " << veh_info_msg.requested_speed
<< "actual speed: " << veh_info_msg.wheel_speed << std::endl;

tms_msg.therm_module_num = i++;
tms_msg.num_therm_enabled = i++;
tms_msg.low_therm_value = i++;
tms_msg.high_therm_value = i++;
tms_msg.avg_therm_value = i++;
tms_msg.high_therm_id = i++;
tms_msg.low_therm_id = i++;
tms_msg.checksum = i++;
temp_sens_msg.sensor1 = i++;
temp_sens_msg.sensor2 = i++;
temp_sens_msg.sensor3 = i++;
temp_sens_msg.sensor4 = i++;
temp_sens_msg.sensor5 = i++;
temp_sens_msg.sensor6 = i++;

veh_can_bus.Send(tms_msg);
veh_can_bus.Send(temp_sens_msg);
}

return 0;
Expand Down
1 change: 0 additions & 1 deletion scripts/canal/canal_messages.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ private:

// temporary raw msg
shared::can::RawCanMsg temp_raw_msg;
temp_raw_msg.data = {0};
temp_raw_msg.header = {
.id = kCanId,
.data_len = kDlc,
Expand Down

0 comments on commit dc0bc45

Please sign in to comment.