Skip to content

Commit

Permalink
Add msg registry generation
Browse files Browse the repository at this point in the history
  • Loading branch information
samparent97 committed Apr 12, 2024
1 parent a2b7591 commit 0035f5e
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 15 deletions.
7 changes: 7 additions & 0 deletions firmware/shared/comms/can/can_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class CanTxMsg : public CanMsg {
mask);
}

template <typename T>
static inline uint8_t pack_right_shift(T value, uint8_t shift,
uint8_t mask) {
return static_cast<uint8_t>(static_cast<uint8_t>(value >> shift) &
mask);
}

friend class CanBus;
};

Expand Down
36 changes: 29 additions & 7 deletions scripts/canal/canParser.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 43,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Rendered code written to 'can_messages.h'\n",
"Rendered code written to 'msg_registry.h'\n"
]
}
],
"source": [
"# Read the template string from a file\n",
"with open('canal_messages.jinja2', 'r') as file:\n",
" template_str = file.read()\n",
" canal_messages_template = file.read()\n",
" \n",
"with open('canal_msg_registry.jinja2', 'r') as file:\n",
" message_registry_template = file.read()\n",
"\n",
"# Create the environment with trim_blocks and lstrip_blocks settings\n",
"env = Environment(trim_blocks=True, lstrip_blocks=True)\n",
Expand All @@ -286,7 +298,8 @@
"env.filters['decimal_to_hex'] = decimal_to_hex\n",
"\n",
"# Load the template from the string content\n",
"template = env.from_string(template_str)\n",
"msgs_template = env.from_string(canal_messages_template)\n",
"registry_template = env.from_string(message_registry_template)\n",
"\n",
"# Define your context dictionary\n",
"context = {\n",
Expand All @@ -297,16 +310,25 @@
" 'temp_signal_types': temp_signal_types,\n",
" 'unpack_info': unpack_info,\n",
" 'pack_info': pack_info,\n",
" 'bus_name': \"Veh\"\n",
"}\n",
"\n",
"# Render the template with the context\n",
"rendered_code = template.render(**context)\n",
"rendered_code = msgs_template.render(**context)\n",
"\n",
"# Write the rendered code to a file\n",
"with open('../../firmware/projects/DemoCan/generated/can_messages.h', 'w') as output_file:\n",
" output_file.write(rendered_code)\n",
"\n",
"print(f\"Rendered code written to 'can_messages.h'\")\n",
"\n",
"rendered_code = registry_template.render(**context)\n",
"\n",
"# Write the rendered code to a file\n",
"with open('can_messages.h', 'w') as output_file:\n",
"with open('../../firmware/projects/DemoCan/generated/msg_registry.h', 'w') as output_file:\n",
" output_file.write(rendered_code)\n",
"\n",
"print(f\"Rendered code written to 'can_messages.h'\")"
"print(f\"Rendered code written to 'msg_registry.h'\")"
]
}
],
Expand Down
16 changes: 8 additions & 8 deletions scripts/canal/canal_messages.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#pragma once

#include "shared/comms/can/can_msg.h"
#include "shared/comms/can/raw_can_msg.h
#include "shared/comms/can/raw_can_msg.h"

namespace generated::can {

Expand Down Expand Up @@ -42,7 +42,7 @@ private:
static constexpr {{ sig_var_type }} k{{ sig.name }}Offset = {{ sig.offset }};
{% endfor %}

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

void Clone(shared::can::CanRxMsg& rx_msg) const override {
{{ class_name }}* p_rx_msg = static_cast<{{ class_name }}*>(&rx_msg);
Expand All @@ -53,7 +53,7 @@ private:
{% endfor %}
}

void Unpack(shared::can::RawCanMsg& raw_msg) override {
void Unpack(const shared::can::RawCanMsg& raw_msg) override {
if (raw_msg.header.id != kCanId) {
return;
}
Expand All @@ -66,6 +66,7 @@ private:
{% for sig in msg.signals %}
{% set masks, shifts = unpack_info[msg.name][sig.name] %}
{% set temp_sig_var = "temp_" + sig.name | camel_to_snake %}
{% set temp_sig_var_type = temp_signal_types[msg.name][sig.name] %}
{% set sig_var = sig.name | camel_to_snake %}
{% set sig_var_type = signal_types[msg.name][sig.name] %}
{% set sig_var_scale = "k" + sig.name + "Scale" %}
Expand All @@ -75,9 +76,9 @@ private:
{% set shift = shifts[i] %}
{% if mask != 0 %}
{% if shift >= 0 %}
{{ temp_sig_var }} |= unpack_left_shift(raw_msg.data[{{ i }}], {{ shift }}U, {{ mask | decimal_to_hex }}U);
{{ temp_sig_var }} |= unpack_left_shift<{{ temp_sig_var_type }}>(raw_msg.data[{{ i }}], {{ shift }}U, {{ mask | decimal_to_hex }}U);
{% else %}
{{ temp_sig_var }} |= unpack_right_shift(raw_msg.data[{{ i }}], {{ -shift }}U, {{ mask | decimal_to_hex }}U);
{{ temp_sig_var }} |= unpack_right_shift<{{ temp_sig_var_type }}>(raw_msg.data[{{ i }}], {{ -shift }}U, {{ mask | decimal_to_hex }}U);
{% endif %}
{% endif %}
{% endfor %}
Expand Down Expand Up @@ -144,9 +145,9 @@ private:
{% set shift = shifts[i] %}
{% if mask != 0 %}
{% if shift >= 0 %}
temp_raw_msg[{{ i }}] |= pack_right_shift({{ temp_sig_var }}, {{ shift }}U, {{ mask | decimal_to_hex }}U);
temp_raw_msg.data[{{ i }}] |= pack_right_shift({{ temp_sig_var }}, {{ shift }}U, {{ mask | decimal_to_hex }}U);
{% else %}
temp_raw_msg[{{ i }}] |= pack_left_shift({{ temp_sig_var }}, {{ -(shift) }}U, {{ mask | decimal_to_hex }}U);
temp_raw_msg.data[{{ i }}] |= pack_left_shift({{ temp_sig_var }}, {{ -(shift) }}U, {{ mask | decimal_to_hex }}U);
{% endif %}
{% endif %}
{% endfor %}
Expand All @@ -155,7 +156,6 @@ private:
// Copy temp raw msg to raw msg
raw_msg = temp_raw_msg;
}

};
{% endfor %}

Expand Down
72 changes: 72 additions & 0 deletions scripts/canal/canal_msg_registry.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/// @author Samuel Parent
/// @date {{ date }}

#pragma once

#include "can_messages.h"
#include "shared/comms/can/can_msg.h"
#include "shared/comms/can/msg_registry.h"
#include "shared/comms/can/raw_can_msg.h"
#include "third-party/etl/include/etl/unordered_map.h"

namespace generated::can {

{% set reg_class_name = bus_name + "MsgRegistry" %}
class {{ reg_class_name }} : public shared::can::MsgRegistry {
public:
bool SetMessage(const shared::can::RawCanMsg& raw_msg) {
bool msg_found = false;

if (rx_msg_map_.count(raw_msg.header.id) > 0) {
msg_found = true;

Unpack(rx_msg_map_[raw_msg.header.id], raw_msg);
}

return msg_found;
}

bool GetMessage(shared::can::CanRxMsg& rx_msg) {
bool msg_found = false;

shared::can::CanId mid = MsgId(&rx_msg);

if (rx_msg_map_.count(mid) > 0) {
msg_found = true;

Clone(rx_msg_map_[mid], rx_msg);
}

return msg_found;
}

private:
static constexpr size_t kNumRxMsgs = {{ rx_msgs | length }};

// Message IDs
{% for msg in rx_msgs %}
{% set class_name = msg.name %}
{% set class_const = "k" + class_name + "CanId" %}
{% set hex_id = msg.frame_id | decimal_to_hex %}
static constexpr shared::can::CanId {{ class_const }} = {{ hex_id }};
{% endfor %}

// Can Rx Messages
{% for msg in rx_msgs %}
{% set class_name = msg.name %}
{% set class_var = (class_name | camel_to_snake) + "_" %}
{{ class_name }} {{ class_var }};
{% endfor %}

etl::unordered_map<shared::can::CanId, shared::can::CanRxMsg*, kNumRxMsgs>
{% for msg in rx_msgs %}
{% set class_name = msg.name %}
{% set class_var = (class_name | camel_to_snake) + "_" %}
{% set msg_id = "k" + class_name + "CanId" %}
rx_msg_map_ = {
{ {{ msg_id }}, &{{class_var}} },
};
{% endfor %}
};

} // namespace generated::can

0 comments on commit 0035f5e

Please sign in to comment.