Skip to content

Commit

Permalink
Working tx
Browse files Browse the repository at this point in the history
  • Loading branch information
samparent97 committed Apr 12, 2024
1 parent dc0bc45 commit a103b95
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 34 deletions.
20 changes: 8 additions & 12 deletions firmware/projects/DemoCan/generated/can_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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<int16_t>(static_cast<double>(sensor6 - kSensor6Offset) / static_cast<double>(kSensor6Scale));
int16_t temp_sensor5 = static_cast<int16_t>(static_cast<double>(sensor5 - kSensor5Offset) / static_cast<double>(kSensor5Scale));
int16_t temp_sensor4 = static_cast<int16_t>(static_cast<double>(sensor4 - kSensor4Offset) / static_cast<double>(kSensor4Scale));
int16_t temp_sensor3 = static_cast<int16_t>(static_cast<double>(sensor3 - kSensor3Offset) / static_cast<double>(kSensor3Scale));
int16_t temp_sensor2 = static_cast<int16_t>(static_cast<double>(sensor2 - kSensor2Offset) / static_cast<double>(kSensor2Scale));
int16_t temp_sensor1 = static_cast<int16_t>(static_cast<double>(sensor1 - kSensor1Offset) / static_cast<double>(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);
Expand All @@ -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);
}
};

Expand Down
2 changes: 1 addition & 1 deletion firmware/projects/DemoCan/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
2 changes: 1 addition & 1 deletion firmware/shared/comms/can/can_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
Expand Down
14 changes: 14 additions & 0 deletions firmware/shared/comms/can/raw_can_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

#include <stdint.h>

#include <algorithm>
#include <iostream>

namespace shared::can {

constexpr uint8_t kMaxMsgBytes = 8;
Expand All @@ -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
64 changes: 51 additions & 13 deletions scripts/canal/canParser.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -54,7 +54,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -71,7 +71,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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)"
]
Expand All @@ -104,7 +113,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -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)"
]
Expand All @@ -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",
Expand Down Expand Up @@ -208,7 +246,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -225,7 +263,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -270,7 +308,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 36,
"metadata": {},
"outputs": [
{
Expand Down
14 changes: 7 additions & 7 deletions scripts/canal/canal_messages.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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<double>({{ sig_var }} - {{ sig_var_offset }}) / static_cast<double>({{ sig_var_scale }}));
{% endfor %}

{% for sig in msg.signals %}
Expand All @@ -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 %}
Expand Down

0 comments on commit a103b95

Please sign in to comment.