Skip to content

Commit ddb621a

Browse files
author
FILIPPINI Matteo (Iveco Group)
committed
Correzione script
1 parent 9d09311 commit ddb621a

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

converter.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ def process_blf_file(db, blf_file, msg_list, sgn_list, output, current_values):
6363
try:
6464
for msg in tqdm(can_log, desc=f"Reading signals in {os.path.basename(blf_file)}"):
6565
try:
66-
msg_name = db.get_message_by_frame_id(msg.arbitration_id).name
66+
message = db.get_message_by_frame_id(msg.arbitration_id)
67+
msg_name = message.name
6768
cur_frame = db.decode_message(
6869
msg.arbitration_id, msg.data, decode_choices=False
6970
)
@@ -73,16 +74,33 @@ def process_blf_file(db, blf_file, msg_list, sgn_list, output, current_values):
7374
start_abs = msg.timestamp
7475
output[0].append("Timestamp")
7576

77+
# Add signal names to the header with units if present
78+
for i in range(len(sgn_list)):
79+
signal_obj = message.get_signal_by_name(sgn_list[i])
80+
if signal_obj:
81+
# Append unit if available
82+
unit = signal_obj.unit if signal_obj.unit else ""
83+
if unit:
84+
output[0].append(f"{sgn_list[i]} [{unit}]")
85+
else:
86+
output[0].append(sgn_list[i])
87+
else:
88+
output[0].append(sgn_list[i])
89+
7690
timestamp = msg.timestamp - start_abs
7791
row = [timestamp] # Start with timestamp
7892
has_changes = False # Flag to track changes in signals
7993

8094
# Update current signal values and check for changes
8195
for i in range(len(sgn_list)):
8296
if sgn_list[i] in cur_frame and msg_name == msg_list[i]:
83-
current_value = cur_frame[sgn_list[i]]
84-
if current_value != previous_values[i]:
85-
current_values[i] = current_value # Update only if value changed
97+
# Apply gain (factor) and offset
98+
scaled_value = cur_frame[sgn_list[i]]
99+
signal_obj = message.get_signal_by_name(sgn_list[i])
100+
101+
# Update only if the value has changed
102+
if scaled_value != previous_values[i]:
103+
current_values[i] = scaled_value
86104
has_changes = True # Flag indicates change
87105
row.append(current_values[i])
88106

@@ -92,10 +110,10 @@ def process_blf_file(db, blf_file, msg_list, sgn_list, output, current_values):
92110
output[i].append(row[i])
93111

94112
except KeyError as e:
95-
print(f"Error decoding message: {e}")
113+
#print(f"Error decoding message: {e}")
96114
continue # Ignore undecodable messages
97115
except Exception as e:
98-
print(f"Unexpected error while processing message: {e}")
116+
#print(f"Unexpected error while processing message: {e}")
99117
continue # Ignore other general errors
100118
except Exception as e:
101119
print(f"Error processing BLF file '{blf_file}': {e}")
@@ -104,6 +122,8 @@ def process_blf_file(db, blf_file, msg_list, sgn_list, output, current_values):
104122
return output
105123

106124

125+
126+
107127
def write_csv(output, dbc_file, blf_file):
108128
"""Write the output structure to a CSV file with a name based on the DBC and BLF filenames."""
109129
try:

0 commit comments

Comments
 (0)