From a103b95208964a3b1755ddbc4c43469ace28da18 Mon Sep 17 00:00:00 2001 From: samparent97 Date: Fri, 12 Apr 2024 10:16:57 -0400 Subject: [PATCH] Working tx --- .../projects/DemoCan/generated/can_messages.h | 20 +++--- firmware/projects/DemoCan/main.cc | 2 +- firmware/shared/comms/can/can_msg.h | 2 +- firmware/shared/comms/can/raw_can_msg.h | 14 ++++ scripts/canal/canParser.ipynb | 64 +++++++++++++++---- scripts/canal/canal_messages.jinja2 | 14 ++-- 6 files changed, 82 insertions(+), 34 deletions(-) diff --git a/firmware/projects/DemoCan/generated/can_messages.h b/firmware/projects/DemoCan/generated/can_messages.h index 09bf332e..1bdd17b7 100644 --- a/firmware/projects/DemoCan/generated/can_messages.h +++ b/firmware/projects/DemoCan/generated/can_messages.h @@ -102,11 +102,7 @@ class TempSensors : public shared::can::CanTxMsg { 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; - } - + void Pack(shared::can::RawCanMsg& raw_msg) const override { // temporary raw msg shared::can::RawCanMsg temp_raw_msg; temp_raw_msg.header = { @@ -116,12 +112,12 @@ class TempSensors : public shared::can::CanTxMsg { }; // 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; + int16_t temp_sensor6 = static_cast(static_cast(sensor6 - kSensor6Offset) / static_cast(kSensor6Scale)); + int16_t temp_sensor5 = static_cast(static_cast(sensor5 - kSensor5Offset) / static_cast(kSensor5Scale)); + int16_t temp_sensor4 = static_cast(static_cast(sensor4 - kSensor4Offset) / static_cast(kSensor4Scale)); + int16_t temp_sensor3 = static_cast(static_cast(sensor3 - kSensor3Offset) / static_cast(kSensor3Scale)); + int16_t temp_sensor2 = static_cast(static_cast(sensor2 - kSensor2Offset) / static_cast(kSensor2Scale)); + int16_t temp_sensor1 = static_cast(static_cast(sensor1 - kSensor1Offset) / static_cast(kSensor1Scale)); temp_raw_msg.data[0] |= pack_left_shift(temp_sensor6, 4U, 0xf0U); temp_raw_msg.data[1] |= pack_right_shift(temp_sensor6, 4U, 0x3fU); @@ -137,7 +133,7 @@ class TempSensors : public shared::can::CanTxMsg { temp_raw_msg.data[7] |= pack_right_shift(temp_sensor1, 2U, 0xffU); // Copy temp raw msg to raw msg - raw_msg = temp_raw_msg; + raw_msg.Copy(temp_raw_msg); } }; diff --git a/firmware/projects/DemoCan/main.cc b/firmware/projects/DemoCan/main.cc index 5e11eeec..0b973d9f 100644 --- a/firmware/projects/DemoCan/main.cc +++ b/firmware/projects/DemoCan/main.cc @@ -37,7 +37,7 @@ int main(void) { veh_can_bus.Read(veh_info_msg); std::cout << "requested speed: " << veh_info_msg.requested_speed - << "actual speed: " << veh_info_msg.wheel_speed << std::endl; + << " actual speed: " << veh_info_msg.wheel_speed << std::endl; temp_sens_msg.sensor1 = i++; temp_sens_msg.sensor2 = i++; diff --git a/firmware/shared/comms/can/can_msg.h b/firmware/shared/comms/can/can_msg.h index 5c04a952..9e261ffc 100644 --- a/firmware/shared/comms/can/can_msg.h +++ b/firmware/shared/comms/can/can_msg.h @@ -38,7 +38,7 @@ class CanRxMsg : public CanMsg { class CanTxMsg : public CanMsg { private: - virtual void Pack(RawCanMsg&) = 0; + virtual void Pack(RawCanMsg&) const = 0; protected: template diff --git a/firmware/shared/comms/can/raw_can_msg.h b/firmware/shared/comms/can/raw_can_msg.h index bf896d1f..276953e4 100644 --- a/firmware/shared/comms/can/raw_can_msg.h +++ b/firmware/shared/comms/can/raw_can_msg.h @@ -5,6 +5,9 @@ #include +#include +#include + namespace shared::can { constexpr uint8_t kMaxMsgBytes = 8; @@ -20,6 +23,17 @@ struct CanHeader { struct RawCanMsg { CanHeader header; uint8_t data[kMaxMsgBytes] = {0}; + + void Copy( + const shared::can::RawCanMsg& other) noexcept { + + header.id = other.header.id; + header.data_len = other.header.data_len; + header.is_extended_frame = other.header.is_extended_frame; + + std::copy(std::begin(other.data), std::end(other.data), + std::begin(data)); + } }; } // namespace shared::can diff --git a/scripts/canal/canParser.ipynb b/scripts/canal/canParser.ipynb index 164f94ff..b2d1c395 100644 --- a/scripts/canal/canParser.ipynb +++ b/scripts/canal/canParser.ipynb @@ -16,7 +16,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ @@ -54,7 +54,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ @@ -71,7 +71,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, "outputs": [], "source": [ @@ -88,9 +88,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2024-04-12 10:06:43,226 - root - INFO - adding dbc files (['../../firmware/dbcs/DEMO_CAN.dbc'])\n", + "2024-04-12 10:06:43,228 - root - INFO - successfully added dbc (../../firmware/dbcs/DEMO_CAN.dbc)\n" + ] + } + ], "source": [ "can_db = parse_dbc_files(dbc_files)" ] @@ -104,7 +113,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ @@ -160,9 +169,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2024-04-12 10:06:43,240 - root - INFO - filtered messages by node (FOO) num msgs: rx = 1, tx = 1\n" + ] + } + ], "source": [ "rx_msgs, tx_msgs = filter_messages_by_node(can_db.messages, our_node)" ] @@ -176,9 +193,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "big\n", + "big\n", + "little\n", + "shift amounts: [-4, 4, 12, 20, 28, 36, 44, 52]\n", + "little\n", + "shift amounts: [-14, -6, 2, 10, 18, 26, 34, 42]\n", + "little\n", + "shift amounts: [-24, -16, -8, 0, 8, 16, 24, 32]\n", + "little\n", + "shift amounts: [-34, -26, -18, -10, -2, 6, 14, 22]\n", + "little\n", + "shift amounts: [-44, -36, -28, -20, -12, -4, 4, 12]\n", + "little\n", + "shift amounts: [-54, -46, -38, -30, -22, -14, -6, 2]\n" + ] + } + ], "source": [ "def get_masks_shifts(msgs):\n", " masks_shifts_dict = {}\n", @@ -208,7 +246,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, "outputs": [], "source": [ @@ -225,7 +263,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ @@ -270,7 +308,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 36, "metadata": {}, "outputs": [ { diff --git a/scripts/canal/canal_messages.jinja2 b/scripts/canal/canal_messages.jinja2 index dd636390..6cf261ad 100644 --- a/scripts/canal/canal_messages.jinja2 +++ b/scripts/canal/canal_messages.jinja2 @@ -113,11 +113,7 @@ private: static constexpr {{ sig_var_type }} k{{ sig.name }}Offset = {{ sig.offset }}; {% endfor %} - void Pack(shared::can::RawCanMsg& raw_msg) override { - if (raw_msg.header.id != kCanId) { - return; - } - + void Pack(shared::can::RawCanMsg& raw_msg) const override { // temporary raw msg shared::can::RawCanMsg temp_raw_msg; temp_raw_msg.header = { @@ -129,7 +125,11 @@ private: // temporary signal variables {% for sig in msg.signals %} {% set sig_var = sig.name | camel_to_snake %} - {{ temp_signal_types[msg.name][sig.name] }} temp_{{ sig.name | camel_to_snake }} = {{ sig_var }}; + {% set sig_var_scale = "k" + sig.name + "Scale" %} + {% set sig_var_offset = "k" + sig.name + "Offset" %} + {% set temp_sig_var = "temp_" + (sig.name | camel_to_snake) %} + {% set temp_sig_var_type = temp_signal_types[msg.name][sig.name] %} + {{ temp_sig_var_type }} {{ temp_sig_var }} = static_cast<{{ temp_sig_var_type }}>(static_cast({{ sig_var }} - {{ sig_var_offset }}) / static_cast({{ sig_var_scale }})); {% endfor %} {% for sig in msg.signals %} @@ -153,7 +153,7 @@ private: {% endfor %} // Copy temp raw msg to raw msg - raw_msg = temp_raw_msg; + raw_msg.Copy(temp_raw_msg); } }; {% endfor %}