-
Notifications
You must be signed in to change notification settings - Fork 0
/
canbus_reader.py
300 lines (251 loc) · 11.8 KB
/
canbus_reader.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python
# coding: utf-8
"""
Created by Usama
Script for reading and processing CAN bus data from BLF files using a DBC file.
"""
import os
import can
import cantools
import pandas as pd
import numpy as np
def blf_to_df(blf_file_path, dbc_file_path):
"""
Reads and decodes CAN messages from a BLF file using a DBC file.
Parameters:
blf_file_path (str): Path to the BLF file.
dbc_file_path (str): Path to the DBC file.
Returns:
pd.DataFrame: DataFrame containing the decoded messages.
list: List of skipped message IDs.
"""
db = cantools.database.load_file(dbc_file_path)
reader = can.BLFReader(blf_file_path)
messages = []
skipped_ids = []
all_keys = set()
print("Messages from BLF file:")
for msg in reader:
print(f"ID: {msg.arbitration_id}, Hex ID: 0x{msg.arbitration_id:X}, Timestamp: {msg.timestamp}, Data: {msg.data}")
if msg.is_error_frame:
continue
try:
decoded_msg = db.decode_message(msg.arbitration_id, msg.data)
decoded_msg['timestamp'] = msg.timestamp
decoded_msg['arbitration_id'] = f"0x{msg.arbitration_id:X}"
print(f"Decoded message: {decoded_msg}")
messages.append(decoded_msg)
all_keys.update(decoded_msg.keys())
except KeyError:
skipped_ids.append(f"0x{msg.arbitration_id:X}")
print(f"Skipping message with ID 0x{msg.arbitration_id:X} - not defined in DBC file.")
continue
except Exception as e:
print(f"Error decoding message with ID 0x{msg.arbitration_id:X}: {e}")
continue
complete_messages = [{key: msg.get(key, np.nan) for key in all_keys} for msg in messages]
df = pd.DataFrame(complete_messages)
print(f"DataFrame shape: {df.shape}")
print(f"DataFrame head: {df.head()}")
return df, skipped_ids
def process_can_messages(blf_file_path, dbc_file_path, target_ids):
"""
Processes CAN messages and saves them to CSV files.
Parameters:
blf_file_path (str): Path to the BLF file.
dbc_file_path (str): Path to the DBC file.
target_ids (set): Set of target message IDs to process.
Returns:
dict: Dictionary of DataFrames indexed by arbitration ID.
list: List of skipped message IDs.
"""
db = cantools.database.load_file(dbc_file_path)
reader = can.BLFReader(blf_file_path)
dataframes = {}
skipped_ids = []
desired_signal_suffixes = {'HghstCellV', 'LwstCellV', 'BtryPckI', 'MxTmp', 'MinTmp', 'SOC'}
print("Messages from BLF file:")
for msg in reader:
if msg.arbitration_id not in target_ids:
continue
print(f"ID: {msg.arbitration_id}, Hex ID: 0x{msg.arbitration_id:X}, Timestamp: {msg.timestamp}, Data: {msg.data}")
if msg.is_error_frame:
continue
try:
decoded_msg = db.decode_message(msg.arbitration_id, msg.data)
filtered_msg = {key: value for key, value in decoded_msg.items() if any(key.endswith(suffix) for suffix in desired_signal_suffixes)}
if not filtered_msg:
skipped_ids.append(f"0x{msg.arbitration_id:X}")
continue
filtered_msg['timestamp'] = msg.timestamp
filtered_msg['arbitration_id'] = f"0x{msg.arbitration_id:X}"
print(f"Filtered message: {filtered_msg}")
arbitration_id = f"0x{msg.arbitration_id:X}"
if arbitration_id not in dataframes:
dataframes[arbitration_id] = []
dataframes[arbitration_id].append(filtered_msg)
except KeyError:
skipped_ids.append(f"0x{msg.arbitration_id:X}")
print(f"Skipping message with ID 0x{msg.arbitration_id:X} - not defined in DBC file.")
continue
except Exception as e:
print(f"Error decoding message with ID 0x{msg.arbitration_id:X}: {e}")
continue
for arb_id, messages in dataframes.items():
all_keys = set(key for msg in messages for key in msg.keys())
complete_messages = [{key: msg.get(key, np.nan) for key in all_keys} for msg in messages]
df = pd.DataFrame.from_records(complete_messages)
df = df.sort_values(by=['timestamp'])
df.to_csv(f'can_data_{arb_id}.csv', index=False)
print(f"Total unique IDs: {len(dataframes)}")
return dataframes, skipped_ids
def filter_columns(columns, suffixes):
"""Filter columns to include only those with the exact desired suffixes."""
filtered_columns = []
for col in columns:
for suffix in suffixes:
if col.endswith(suffix) and (col == suffix or col[-len(suffix)-1] in {'_', '-', '.'}):
filtered_columns.append(col)
break
return filtered_columns
def process_all_can_messages(blf_file_path, dbc_file_path, output_folder):
"""
Processes all CAN messages and saves them to CSV files.
Parameters:
blf_file_path (str): Path to the BLF file.
dbc_file_path (str): Path to the DBC file.
output_folder (str): Path to the output folder.
Returns:
dict: Dictionary of DataFrames indexed by arbitration ID.
list: List of skipped message IDs.
"""
db = cantools.database.load_file(dbc_file_path)
reader = can.BLFReader(blf_file_path)
dataframes = {}
skipped_ids = []
desired_signal_suffixes = {'HghstCellV', 'LwstCellV', 'BtryPckI', 'MxTmp', 'MinTmp', 'SOC'}
print("Messages from BLF file:")
for msg in reader:
print(f"ID: {msg.arbitration_id}, Hex ID: 0x{msg.arbitration_id:X}, Timestamp: {msg.timestamp}, Data: {msg.data}")
if msg.is_error_frame:
continue
try:
decoded_msg = db.decode_message(msg.arbitration_id, msg.data)
filtered_columns = filter_columns(decoded_msg.keys(), desired_signal_suffixes)
filtered_msg = {key: decoded_msg[key] for key in filtered_columns}
if not filtered_msg:
skipped_ids.append(f"0x{msg.arbitration_id:X}")
continue
filtered_msg['timestamp'] = msg.timestamp
filtered_msg['arbitration_id'] = f"0x{msg.arbitration_id:X}"
print(f"Filtered message: {filtered_msg}")
arbitration_id = f"0x{msg.arbitration_id:X}"
if arbitration_id not in dataframes:
dataframes[arbitration_id] = []
dataframes[arbitration_id].append(filtered_msg)
except KeyError:
skipped_ids.append(f"0x{msg.arbitration_id:X}")
print(f"Skipping message with ID 0x{msg.arbitration_id:X} - not defined in DBC file.")
continue
except Exception as e:
print(f"Error decoding message with ID 0x{msg.arbitration_id:X}: {e}")
continue
os.makedirs(output_folder, exist_ok=True)
for arb_id, messages in dataframes.items():
all_keys = set(key for msg in messages for key in msg.keys())
complete_messages = [{key: msg.get(key, np.nan) for key in all_keys} for msg in messages]
df = pd.DataFrame.from_records(complete_messages)
df = df.sort_values(by=['timestamp'])
df.to_csv(os.path.join(output_folder, f'can_data_{arb_id}.csv'), index=False)
print(f"Total unique IDs: {len(dataframes)}")
return dataframes, skipped_ids
def load_all_ids(blf_file_path, dbc_file_path):
"""
Loads all CAN message IDs from a BLF file using a DBC file.
Parameters:
blf_file_path (str): Path to the BLF file.
dbc_file_path (str): Path to the DBC file.
Returns:
pd.DataFrame: DataFrame containing the decoded messages.
list: List of skipped message IDs.
"""
db = cantools.database.load_file(dbc_file_path)
reader = can.BLFReader(blf_file_path)
messages = []
skipped_ids = []
print("Messages from BLF file:")
for msg in reader:
print(f"ID: {msg.arbitration_id}, Hex ID: 0x{msg.arbitration_id:X}, Timestamp: {msg.timestamp}, Data: {msg.data}")
if msg.is_error_frame:
continue
try:
decoded_msg = db.decode_message(msg.arbitration_id, msg.data)
decoded_msg['timestamp'] = msg.timestamp
decoded_msg['arbitration_id'] = f"0x{msg.arbitration_id:X}"
print(f"Decoded message: {decoded_msg}")
messages.append(decoded_msg)
except KeyError:
skipped_ids.append(f"0x{msg.arbitration_id:X}")
print(f"Skipping message with ID 0x{msg.arbitration_id:X} - not defined in DBC file.")
continue
except Exception as e:
print(f"Error decoding message with ID 0x{msg.arbitration_id:X}: {e}")
continue
df = pd.DataFrame.from_records(messages)
print(f"DataFrame shape: {df.shape}")
print(f"DataFrame head: {df.head()}")
return df, skipped_ids
def combine_signals_to_single_cell(file_paths, desired_signal_suffixes):
"""
Combines signals from multiple files into a single cell format.
Parameters:
file_paths (list): List of file paths to process.
desired_signal_suffixes (list): List of desired signal suffixes.
Returns:
pd.DataFrame: DataFrame containing the combined signals.
"""
combined_data = []
for file_path in file_paths:
df = pd.read_csv(file_path)
extracted_columns = {suffix: df[[col for col in df.columns if col.endswith(suffix)]] for suffix in desired_signal_suffixes}
extracted_df = pd.concat(extracted_columns, axis=1)
combined_data.append(extracted_df)
combined_df = pd.concat(combined_data, axis=1)
combined_df.columns = ['_'.join(col) if isinstance(col, tuple) else col for col in combined_df.columns]
single_cell_data = pd.DataFrame()
timestamp_cols = [col for col in combined_df.columns if 'timestamp' in col.lower()]
if timestamp_cols:
single_cell_data['timestamp'] = combined_df[timestamp_cols[0]]
suffixes = ['HghstCellV', 'LwstCellV', 'MxTmp', 'MinTmp', 'BtryPckI', 'SOC']
columns = extract_columns(combined_df, suffixes)
if 'HghstCellV' in columns and 'LwstCellV' in columns:
voltage = (columns['HghstCellV'].mean(axis=1) + columns['LwstCellV'].mean(axis=1)) / 2
single_cell_data['Voltage'] = voltage / 1000
if 'MxTmp' in columns and 'MinTmp' in columns:
temperature = (columns['MxTmp'].mean(axis=1) + columns['MinTmp'].mean(axis=1)) / 2
single_cell_data['Temperature'] = temperature
if 'BtryPckI' in columns:
current = columns['BtryPckI'].mean(axis=1)
single_cell_data['Current'] = current
if 'SOC' in columns:
single_cell_data['SOC'] = columns['SOC'].mean(axis=1)
single_cell_data.to_parquet('single_cell_data.parquet', index=False)
print(single_cell_data)
# usage for example
if __name__ == "__main__":
blf_file_path = "blf_file.blf"
dbc_file_path = "dbc_file.dbc"
output_folder = "output_can_data_3"
target_ids = {66052, 66050, 66048}
# Process CAN messages
dataframes, skipped_ids = process_can_messages(blf_file_path, dbc_file_path, target_ids)
# Display the DataFrames
for arb_id, df in dataframes.items():
print(f"DataFrame for {arb_id}:\n{df}\n")
print(f"Skipped IDs: {skipped_ids}")
# Combine signals to single cell data
file_paths = ['C:/Users/usama/output_can_data_2/can_data_0x10200.csv',
'C:/Users/usama/output_can_data_2/can_data_0x10202.csv',
'C:/Users/usama/output_can_data_2/can_data_0x10204.csv']
desired_signal_suffixes = ['timestamp', 'HghstCellV', 'LwstCellV', 'BtryPckI', 'MxTmp', 'MinTmp', 'SOC']
combine_signals_to_single_cell(file_paths, desired_signal_suffixes)