Skip to content

Commit

Permalink
Complete tx msg and rx msg
Browse files Browse the repository at this point in the history
  • Loading branch information
samparent97 committed Apr 12, 2024
1 parent 4940d27 commit a2b7591
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 151 deletions.
2 changes: 1 addition & 1 deletion firmware/shared/comms/can/raw_can_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct CanHeader {

struct RawCanMsg {
CanHeader header;
uint8_t data[kMaxMsgBytes];
uint8_t data[kMaxMsgBytes] = {0};
};

} // namespace shared::can
180 changes: 50 additions & 130 deletions scripts/canal/canParser.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import logging, cantools, os, math, re, datetime\n",
"from jinja2 import Environment\n",
"from canSupportLib import big_endian_mask, big_endian_shift_amounts, parse_dbc_files, filter_messages_by_node"
"from canSupportLib import little_endian_mask, little_endian_shift_amounts, big_endian_mask, big_endian_shift_amounts, parse_dbc_files, filter_messages_by_node"
]
},
{
Expand Down Expand Up @@ -54,7 +54,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -71,7 +71,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -88,18 +88,9 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-04-11 14:47:46,682 - root - INFO - adding dbc files (['../../firmware/dbcs/DEMO_CAN.dbc'])\n",
"2024-04-11 14:47:46,685 - root - INFO - successfully added dbc (../../firmware/dbcs/DEMO_CAN.dbc)\n"
]
}
],
"outputs": [],
"source": [
"can_db = parse_dbc_files(dbc_files)"
]
Expand All @@ -113,11 +104,11 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_signal_types(can_db):\n",
"def get_signal_types(can_db, allow_floating_point=True):\n",
" sig_types = {}\n",
"\n",
" for message in can_db.messages:\n",
Expand All @@ -132,8 +123,8 @@
" if not signal.is_signed:\n",
" sign = \"u\"\n",
" \n",
" if isinstance(signal.scale, float) or signal.is_float:\n",
" if num_bits < 32:\n",
" if (isinstance(signal.scale, float) or signal.is_float) and allow_floating_point:\n",
" if num_bits <= 32:\n",
" sig_types[message.name][signal.name] = \"float\"\n",
" continue\n",
" else:\n",
Expand All @@ -156,7 +147,8 @@
" \n",
" return sig_types\n",
"\n",
"signal_types = get_signal_types(can_db)"
"signal_types = get_signal_types(can_db)\n",
"temp_signal_types = get_signal_types(can_db, allow_floating_point=False)"
]
},
{
Expand All @@ -168,17 +160,9 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-04-11 14:47:46,704 - root - INFO - filtered messages by node (FOO) num msgs: rx = 1, tx = 1\n"
]
}
],
"outputs": [],
"source": [
"rx_msgs, tx_msgs = filter_messages_by_node(can_db.messages, our_node)"
]
Expand All @@ -192,35 +176,39 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'VehicleInfo': {'RequestedSpeed': ([15, 255, 255, 240, 0, 0, 0, 0], [20, 12, 4, -4, -12, -20, -28, -36]), 'WheelSpeed': ([0, 0, 0, 7, 255, 255, 255, 248], [53, 45, 37, 29, 21, 13, 5, -3])}}\n"
]
}
],
"outputs": [],
"source": [
"unpacking_info = {}\n",
"def get_masks_shifts(msgs):\n",
" masks_shifts_dict = {}\n",
"\n",
"for msg in rx_msgs:\n",
" unpacking_info[msg.name] = {}\n",
" for sig in msg.signals:\n",
" mask, num_trailing_zeros = big_endian_mask(sig.length, sig.start)\n",
" shift_amounts = big_endian_shift_amounts(num_trailing_zeros)\n",
" \n",
" unpacking_info[msg.name][sig.name] = ([int(byte) for byte in mask], shift_amounts)\n",
" for msg in msgs:\n",
" masks_shifts_dict[msg.name] = {}\n",
" for sig in msg.signals:\n",
" if sig.byte_order == 'little_endian':\n",
" print(\"little\")\n",
" mask, num_trailing_zeros = little_endian_mask(sig.length, sig.start)\n",
" shift_amounts = little_endian_shift_amounts(num_trailing_zeros)\n",
" elif sig.byte_order == 'big_endian':\n",
" print(\"big\")\n",
" mask, num_trailing_zeros = big_endian_mask(sig.length, sig.start)\n",
" shift_amounts = big_endian_shift_amounts(num_trailing_zeros)\n",
" else:\n",
" print(\"ERROR: invalid byte order\")\n",
" exit(1)\n",
" masks_shifts_dict[msg.name][sig.name] = ([int(byte) for byte in mask], shift_amounts)\n",
" \n",
" return masks_shifts_dict\n",
" \n",
" \n",
"print(unpacking_info)"
"unpack_info = get_masks_shifts(rx_msgs)\n",
"pack_info = get_masks_shifts(tx_msgs)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -237,11 +225,10 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#regex \n",
"def camel_to_snake(text):\n",
" '''\n",
" Converts UpperCamelCase to snake_case.\n",
Expand Down Expand Up @@ -283,78 +270,9 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/// @author Samuel Parent\n",
"/// @date 2024-04-11\n",
"\n",
"#pragma once\n",
"\n",
"#include \"shared/comms/can/can_msg.h\"\n",
"#include \"shared/comms/can/raw_can_msg.h\n",
"\n",
"class VehicleInfo : public shared::can::CanRxMsg {\n",
"public:\n",
" float requested_speed;\n",
" double wheel_speed;\n",
"\n",
"private:\n",
" static constexpr shared::can::CanId kCanId = 0x245;\n",
" static constexpr uint8_t kDlc = 8;\n",
" static constexpr bool kIsExtFrame = false;\n",
"\n",
" void Pack(shared::can::RawCanMsg& raw_msg) {\n",
" // temporary variables\n",
" float temp_requested_speed;\n",
" double temp_wheel_speed;\n",
" \n",
" raw_msg.header.id = kCanId;\n",
" raw_msg.header.data_len = kDlc;\n",
" raw_msg.header.is_extended_frame = kIsExtFrame;\n",
"\n",
" 0xf = 20 0\n",
" \n",
" 0xff = 12 1\n",
" \n",
" 0xff = 4 2\n",
" \n",
" 0xf0 = -4 3\n",
" \n",
" 0x = -12 4\n",
" \n",
" 0x = -20 5\n",
" \n",
" 0x = -28 6\n",
" \n",
" 0x = -36 7\n",
" \n",
" 0x = 53 0\n",
" \n",
" 0x = 45 1\n",
" \n",
" 0x = 37 2\n",
" \n",
" 0x7 = 29 3\n",
" \n",
" 0xff = 21 4\n",
" \n",
" 0xff = 13 5\n",
" \n",
" 0xff = 5 6\n",
" \n",
" 0xf8 = -3 7\n",
" \n",
"\n",
" }\n",
"}\n"
]
}
],
"outputs": [],
"source": [
"# Read the template string from a file\n",
"with open('canal_messages.jinja2', 'r') as file:\n",
Expand All @@ -374,20 +292,22 @@
"context = {\n",
" 'date': datetime.date.today().strftime(\"%Y-%m-%d\"),\n",
" 'rx_msgs': rx_msgs,\n",
" 'tx_msgs': tx_msgs,\n",
" 'signal_types': signal_types,\n",
" 'unpack_info': unpacking_info\n",
" 'temp_signal_types': temp_signal_types,\n",
" 'unpack_info': unpack_info,\n",
" 'pack_info': pack_info,\n",
"}\n",
"\n",
"# Render the template with the context\n",
"rendered_code = template.render(**context)\n",
"\n",
"print(rendered_code)"
"# Write the rendered code to a file\n",
"with open('can_messages.h', 'w') as output_file:\n",
" output_file.write(rendered_code)\n",
"\n",
"print(f\"Rendered code written to 'can_messages.h'\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
Expand Down
28 changes: 28 additions & 0 deletions scripts/canal/canSupportLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ def big_endian_mask(length: int, start: int) -> Tuple[bytearray, int]:

return mask, 63-end

def little_endian_mask(length: int, start: int) -> (bytearray, int):
mask = bytearray(8)
pos = start
end = start+length
while (pos < 64):
if (pos < end):
mask[pos//8] |= 1<<(pos%8)
pos += 1
else:
break
# # Convert each byte in the byte array to a binary string
# binary_strings = [format(byte, '08b') for byte in mask]

# # Join the binary strings and print
# binary_string = ' '.join(binary_strings)
# print("mask:", binary_string)
# print("num zeroes:", start%8)

return mask, start

def big_endian_shift_amounts(num_zeroes: int) -> List[int]:
shift_amounts = [0]*8
Expand All @@ -41,6 +60,15 @@ def big_endian_shift_amounts(num_zeroes: int) -> List[int]:

return shift_amounts

def little_endian_shift_amounts(num_zeroes: int) -> list:
shift_amounts = [0]*8
for i in range(0, 8):
shift_amounts[i] = i*8 - num_zeroes

print("shift amounts:", shift_amounts)

return shift_amounts

def parse_dbc_files(dbc_filepaths: List[str]) -> cantools.database.Database:
logger = logging.getLogger()
can_db = cantools.database.Database()
Expand Down
Loading

0 comments on commit a2b7591

Please sign in to comment.