forked from perdo1305/data_station
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbc2msg.py
More file actions
58 lines (43 loc) · 1.65 KB
/
Copy pathdbc2msg.py
File metadata and controls
58 lines (43 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
import re
import cantools
# Path to your DBC file
dbc_path = '/home/andre-lopes/Desktop/ros2_ws/src/can_bridge/include/CAN_DBC/Autonomous_temporary.dbc'
# Get directory of this script
script_dir = os.path.dirname(os.path.abspath(__file__))
# Output folder = ./msg relative to this script
output_dir = os.path.join(script_dir, "msg")
os.makedirs(output_dir, exist_ok=True)
# Load DBC
db = cantools.database.load_file(dbc_path)
def to_camel_case(name):
"""Convert string with spaces or hyphens to CamelCase."""
parts = re.split(r'[-_\s]+', name)
return ''.join(word.capitalize() for word in parts if word)
def dbc_to_rosmsg(message):
lines = ["std_msgs/Header header\n"] # Always include header
for signal in message.signals:
# Determine ROS data type
if signal.scale != 1 or signal.offset != 0:
dtype = "float32"
elif signal.length <= 8:
dtype = "int8"
elif signal.length <= 16:
dtype = "int16"
elif signal.length <= 32:
dtype = "int32"
else:
dtype = "int64"
# Clean up signal name for ROS compatibility (lowercase)
name = signal.name.replace(" ", "_").replace("-", "_").lower()
lines.append(f"{dtype} {name}")
return "\n".join(lines) + "\n"
# Generate one .msg per CAN message
for message in db.messages:
# Use CamelCase for filename
file_name = f"{to_camel_case(message.name)}.msg"
file_path = os.path.join(output_dir, file_name)
with open(file_path, "w") as f:
f.write(dbc_to_rosmsg(message))
print(f"✅ Generated {file_path}")
print(f"\nAll .msg files are in: {output_dir}")