-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbc_msg_conversion.py
158 lines (137 loc) · 5.2 KB
/
dbc_msg_conversion.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from glob_def import CarEvent
class SimpleMsg:
can_id = 0
desc = ""
byte_len = 0
bit_start_pos = 0
bit_num = 0
scale = 0.0
offset = 0.0
unit = ""
def __init__(self, can_id, byte_len, bit_start_pos, bit_num,
scale, offset, desc="", unit=""):
self.can_id = can_id
self.byte_len = byte_len
self.bit_start_pos = bit_start_pos
self.bit_num = bit_num
self.scale = scale
self.offset = offset
self.desc = desc
self.unit = unit
DBC_DATABASE_TOYOTA_PRIUS = {
CarEvent.CAR_EVENT_GAS_ACC_VIA_ICE:
SimpleMsg(can_id=0x37, byte_len=7,
bit_start_pos=0, bit_num=40,
scale=1.0, offset=0.0,
desc="Acceleration via ICE", unit=""),
CarEvent.CAR_EVENT_QUERY_SPEED:
SimpleMsg(can_id=0xb4, byte_len=8,
bit_start_pos=47, bit_num=16,
scale=0.01, offset=0.0,
desc="Current speed of the automobile", unit="kmph"),
CarEvent.CAR_EVENT_QUERY_RPM:
SimpleMsg(can_id=0x01c4, byte_len=8,
bit_start_pos=15, bit_num=16,
scale=1.0, offset=-400.0,
desc="ICE RPM", unit=""),
CarEvent.CAR_EVENT_QUERY_TORQUE:
SimpleMsg(can_id=0x24, byte_len=8,
bit_start_pos=15, bit_num=16,
scale=1.0, offset=0.0,
desc="ICE Torque", unit=""),
CarEvent.CAR_EVENT_BRAKE_PEDAL:
SimpleMsg(can_id=0x0224, byte_len=8,
bit_start_pos=47, bit_num=16,
scale=0.01, offset=0.0,
desc="Brake pedal position sensor", unit=""),
CarEvent.CAR_EVENT_STEERING_WHEEL_ANGLE:
SimpleMsg(can_id=0x25, byte_len=8,
bit_start_pos=3, bit_num=12,
scale=1.5, offset=0.0,
desc="Steer wheel angle", unit="deg"),
CarEvent.CAR_EVENT_BROADCAST_GEAR_STATUS:
SimpleMsg(can_id=0x127, byte_len=8,
bit_start_pos=0, bit_num=64,
scale=1.0, offset=0.0,
desc="Broadcast gear status", unit=""),
CarEvent.CAR_EVENT_BRAKE_SENSOR:
SimpleMsg(can_id=0x0230, byte_len=7,
bit_start_pos=31, bit_num=8,
scale=1.0, offset=0.0,
desc="Brake sensor", unit=""),
CarEvent.CAR_EVENT_GAS_PEDAL:
SimpleMsg(can_id=0x0245, byte_len=5,
bit_start_pos=15, bit_num=16,
scale=0.0062, offset=0,
desc="Acceleration pedal position", unit="mph"),
CarEvent.CAR_EVENT_PCS_PRECOLLISION:
SimpleMsg(can_id=0x0283, byte_len=7,
bit_start_pos=16, bit_num=24,
scale=1.0, offset=0.0,
desc="Pre-collision action", unit=""),
CarEvent.CAR_EVENT_BUS_DIAG:
SimpleMsg(can_id=0x7df, byte_len=8,
bit_start_pos=15, bit_num=16,
scale=1.0, offset=0,
desc="Universal diagnostic message", unit="")
}
class DbcMsgConvertor:
vehicle_model = "unknown"
dbc_database = None
def __init__(self, model):
self.vehicle_model = model
database_name = "DBC_DATABASE_" + str(model).upper()
try:
self.dbc_database = eval(database_name)
except NameError:
print("DBC database %s doesn't exist!" % database_name)
# raise an error
assert(False)
def simple_msg_encode(self, id, value):
# convert value to pre-defined dbc format
# return value is string in hex represenation
if id in self.dbc_database:
msg = self.dbc_database[id]
else:
msg = SimpleMsg(can_id=id, byte_len=8, bit_start_pos=16,
bit_num=4, scale=1.0, offset=0.0)
binstr = '0' * msg.byte_len * 8
# convert value to binary
binfmt = '0'+str(msg.bit_num)+'b'
data_value = format(int((value-msg.offset)/msg.scale), binfmt)
assert(len(data_value) <= msg.bit_num)
# combine binary string
postzeronum = msg.byte_len*8-msg.bit_start_pos-msg.bit_num
binstr = '0'*msg.bit_start_pos + data_value + '0'*postzeronum
# convert binary to hex
hexfmt = '0'+str(msg.byte_len*2)+'x'
hexstr = format(int(binstr, 2), hexfmt)
return hexstr
def simple_msg_decode(self, id, data):
if id in self.dbc_database:
msg = self.dbc_database[id]
else:
msg = SimpleMsg(can_id=id, byte_len=8, bit_start_pos=16,
bit_num=4, scale=1.0, offset=0.0)
bin_str = format(int(data, 16), '064b')
bin_value = bin_str[msg.bit_start_pos:msg.bit_start_pos+msg.bit_num]
value = int(bin_value, 2)*msg.scale+msg.offset
return value
def get_msg_length_in_byte(self, id):
if id in self.dbc_database:
msg = self.dbc_database[id]
return msg.byte_len
else:
return 0
def get_msg_can_id(self, id):
if id in self.dbc_database:
msg = self.dbc_database[id]
return msg.can_id
else:
return 0xffff
def get_event_id_by_can_id(self, can_id):
for i in self.dbc_database:
msg = self.dbc_database[i]
if msg.can_id == can_id:
return i
return CarEvent.CAR_EVENT_UNKNOWN