diff --git a/firmware/projects/DemoCan/generated/can_messages.h b/firmware/projects/DemoCan/generated/can_messages.h index 6628af1b9..09bf332e8 100644 --- a/firmware/projects/DemoCan/generated/can_messages.h +++ b/firmware/projects/DemoCan/generated/can_messages.h @@ -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 @@ -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(therm_module_num & 0xFF); - raw_msg.data[1] = static_cast(num_therm_enabled & 0xFF); - raw_msg.data[2] = static_cast(low_therm_value & 0xFF); - raw_msg.data[3] = static_cast(high_therm_value & 0xFF); - raw_msg.data[4] = static_cast(avg_therm_value & 0xFF); - raw_msg.data[5] = static_cast(high_therm_id & 0xFF); - raw_msg.data[6] = static_cast(low_therm_id & 0xFF); - raw_msg.data[7] = static_cast(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(&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(raw_msg.data[0], 20U, 0xfU); + temp_requested_speed |= unpack_left_shift(raw_msg.data[1], 12U, 0xffU); + temp_requested_speed |= unpack_left_shift(raw_msg.data[2], 4U, 0xffU); + temp_requested_speed |= unpack_right_shift(raw_msg.data[3], 4U, 0xf0U); + requested_speed = (static_cast(temp_requested_speed) * kRequestedSpeedScale) + kRequestedSpeedOffset; + + temp_wheel_speed |= unpack_left_shift(raw_msg.data[3], 29U, 0x7U); + temp_wheel_speed |= unpack_left_shift(raw_msg.data[4], 21U, 0xffU); + temp_wheel_speed |= unpack_left_shift(raw_msg.data[5], 13U, 0xffU); + temp_wheel_speed |= unpack_left_shift(raw_msg.data[6], 5U, 0xffU); + temp_wheel_speed |= unpack_right_shift(raw_msg.data[7], 3U, 0xf8U); + wheel_speed = (static_cast(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(&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(raw_msg.data[0] & 0b00000001); - set_red_led = static_cast(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 \ No newline at end of file diff --git a/firmware/projects/DemoCan/generated/msg_registry.h b/firmware/projects/DemoCan/generated/msg_registry.h index c1c705d3a..f7fb9b77a 100644 --- a/firmware/projects/DemoCan/generated/msg_registry.h +++ b/firmware/projects/DemoCan/generated/msg_registry.h @@ -1,5 +1,5 @@ /// @author Samuel Parent -/// @date 2023-03-01 +/// @date 2024-04-12 #pragma once @@ -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; @@ -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 - rx_msg_map_ = {{0x645, &debug_led_override_}}; + rx_msg_map_ = { + { kVehicleInfoCanId, &vehicle_info_ }, + }; }; } // namespace generated::can \ No newline at end of file diff --git a/firmware/projects/DemoCan/main.cc b/firmware/projects/DemoCan/main.cc index 6c5cff4d8..5e11eeec8 100644 --- a/firmware/projects/DemoCan/main.cc +++ b/firmware/projects/DemoCan/main.cc @@ -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, @@ -23,10 +23,10 @@ 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) { @@ -34,21 +34,19 @@ int main(void) { 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; diff --git a/scripts/canal/canal_messages.jinja2 b/scripts/canal/canal_messages.jinja2 index 897e98513..dd6363905 100644 --- a/scripts/canal/canal_messages.jinja2 +++ b/scripts/canal/canal_messages.jinja2 @@ -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,