Skip to content

Commit

Permalink
Command Line Version for Test and Data Working Fine
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiran Jojare committed Jul 19, 2024
1 parent 588a219 commit f25e394
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 43 deletions.
53 changes: 42 additions & 11 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,43 @@ def parse_log(db, log_file_path):
with open(log_file_path, 'r') as log_file:
current_message = None
for line in log_file:
# print(f"Reading line: {line.strip()}") # Commented for clean output
if line.startswith("CAN"):
if current_message:
process_message(db, current_message, parsed_data)
current_message = [line.strip()]
# print("Detected start of a new message") # Commented for clean output
elif line.strip().startswith("->"):
# print("Detected start of line with ->")
current_message.append(line.strip())
# print(f"Appending signal line: {line.strip()}") # Commented for clean output
if current_message:
process_message(db, current_message, parsed_data)
print_parsed_data(parsed_data)
return parsed_data
except Exception as e:
print(f"Error parsing log file {log_file_path}: {e}")
return None

def process_message(db, message_lines, parsed_data):
try:
# print(f"Processing message lines: {message_lines}") # Commented for clean output
main_line = message_lines[0].split()
can_id = int(main_line[2], 16)
message_name = main_line[5]
timestamp = float(main_line[6])
direction = main_line[7]
# print(f"Main line parts: {main_line}") # Commented for clean output
can_id = main_line[2]
# if can_id in ["00000000", "0000040A"]:
# return
ecu_name, message_name = main_line[6].split('.')
timestamp = main_line[7]
direction = main_line[8]

signals = []

for signal_line in message_lines[1:]:
# print("Signal Line ---- ", signal_line)
parts = signal_line.split()
signal_name = parts[1]
signal_value = float(parts[2])
signal_value = parts[2]
signals.append((signal_name, signal_value, timestamp))

parsed_data.append({
Expand All @@ -54,16 +64,32 @@ def process_message(db, message_lines, parsed_data):
"direction": direction,
"signals": signals
})

except Exception as e:
print(f"Error processing message: {e}, for CAN ID (Error Frames): {can_id}")
return

def print_parsed_data(parsed_data):
for data in parsed_data:
#print(f"Message: {data['message_name']}, CAN ID: {data['can_id']}, Timestamp: {data['timestamp']}, Direction: {data['direction']}")
for signal in data['signals']:
signal_name, signal_value, signal_timestamp = signal
#print(f" Signal: {signal_name}, Value: {signal_value}, Timestamp: {signal_timestamp}")


def plot_signals(parsed_data, signal_name, start_time, end_time):
timestamps = []
values = []
for data in parsed_data:
for signal in data['signals']:
s_name, s_value, s_timestamp = signal
try:
s_value = float(s_value)
s_timestamp = float(s_timestamp) / 1000 # Convert timestamp to seconds here
except ValueError as e:
print(f"Error converting value: {e}")
continue

if s_name == signal_name and (start_time is None or s_timestamp >= start_time) and (end_time is None or s_timestamp <= end_time):
timestamps.append(s_timestamp)
values.append(s_value)
Expand All @@ -72,12 +98,16 @@ def plot_signals(parsed_data, signal_name, start_time, end_time):
print(f"No data found for signal: {signal_name}")
return

plt.figure()
plt.plot(timestamps, values, marker='o')
plt.figure(figsize=(30, 15)) # Adjust the size as needed
plt.plot(timestamps, values)
plt.xlabel('Time (s)')
plt.ylabel('Value')
plt.title(f'Signal: {signal_name}')
plt.grid(True)

# Format the x-axis to display timestamps correctly in seconds
plt.gca().xaxis.set_major_formatter(plt.FuncFormatter(lambda x, _: f'{x:.2f}'))

plt.tight_layout()
plt.show()

Expand All @@ -101,14 +131,15 @@ def plot_signals(parsed_data, signal_name, start_time, end_time):
if db:
parsed_data = parse_log(db, log_file)
if parsed_data:
print("Done parsing log file")
if args.start is None or args.end is None:
# Determine the start and end times from the log if not provided
all_timestamps = [data['timestamp'] for data in parsed_data]
all_timestamps = [float(data['timestamp']) / 1000 for data in parsed_data for signal in data['signals']]
start_time = min(all_timestamps) if args.start is None else args.start
end_time = max(all_timestamps) if args.end is None else args.end
else:
start_time = args.start
end_time = args.end

plot_signals(parsed_data, args.signal, start_time, end_time)
else:
print("Parsed data is empty or None.")
else:
print("DBC parsing failed.")
30 changes: 15 additions & 15 deletions test/data/test_log.txt
Original file line number Diff line number Diff line change
@@ -1,89 +1,89 @@
WinNo Identifier Flg Dlc Name Time Dir
CAN 1 000064 X 8 AccelerationCommandTest 44709.992868 R
CAN 1 000064 X 8 VehicleNode AccelerationCommand_ECU.AccelerationCommandTest 44709.992868 R
-> ACC_AccelerationTest 0.0000 Bool
-> ACC_CurrentTest 1.0000 Bool
-> ACC_SurgeTest 12345.0000 RPM
-> ACC_AccelTest 67890.0000 RPM/sec
-> ACC_MaxCurrentTest 100.0000 Amp
CAN 1 000065 X 8 FeedbackTest 44710.002938 R
CAN 1 000065 X 8 VehicleNode Feedback_ECU.FeedbackTest 44710.002938 R
-> FB_SafetyModeTest 1.0000 Enum
-> FB_TorqueTest 2345.6000 Nm
-> FB_VoltageTest 3456.7000 Volts
-> FB_CurrentTest 4567.8000 Amps
-> FB_SpeedTest 5678.9000 rpm
CAN 1 000066 X 8 SafetyFeedbackTest 44710.012918 R
CAN 1 000066 X 8 VehicleNode SafetyFeedback_ECU.SafetyFeedbackTest 44710.012918 R
-> SAF_SafetyModeTest 0.0000 Enum
-> SAF_TorqueTest 567.0000 Nm
-> SAF_VoltageTest 6789.0000 Volts
-> SAF_CurrentTest 7890.1000 Amps
-> SAF_SpeedTest 8901.2000 rpm
CAN 1 000067 X 8 CommandTest 44710.022908 R
CAN 1 000067 X 8 VehicleNode Command_ECU.CommandTest 44710.022908 R
-> CMD_ControlModeTest 2.0000 Enum
-> CMD_CommandCounterTest 3.0000 Integer
-> CMD_ActiveDischargeTest 0.0000 Bool
-> CMD_UseRawSpeedTest 1.0000 Bool
-> CMD_DirOfRotationTest 0.0000 Enum
CAN 1 000068 X 8 StatusTest 44710.032888 R
CAN 1 000068 X 8 VehicleNode Status_ECU.StatusTest 44710.032888 R
-> STS_NotEnabledTest 1.0000 Bool
-> STS_OverCurrentTest 0.0000 Bool
-> STS_OverBusTest 1.0000 Bool
-> STS_OverPhaseTest 0.0000 Bool
-> STS_UnderVoltageTest 1.0000 Bool
CAN 1 000064 X 8 AccelerationCommandTest 44710.042868 R
CAN 1 000064 X 8 VehicleNode AccelerationCommand_ECU.AccelerationCommandTest 44710.042868 R
-> ACC_AccelerationTest 1.0000 Bool
-> ACC_CurrentTest 0.0000 Bool
-> ACC_SurgeTest 54321.0000 RPM
-> ACC_AccelTest 98765.0000 RPM/sec
-> ACC_MaxCurrentTest 150.0000 Amp
CAN 1 000065 X 8 FeedbackTest 44710.052938 R
CAN 1 000065 X 8 VehicleNode Feedback_ECU.FeedbackTest 44710.052938 R
-> FB_SafetyModeTest 0.0000 Enum
-> FB_TorqueTest 1234.5000 Nm
-> FB_VoltageTest 4567.8000 Volts
-> FB_CurrentTest 5678.9000 Amps
-> FB_SpeedTest 6789.0000 rpm
CAN 1 000066 X 8 SafetyFeedbackTest 44710.062918 R
CAN 1 000066 X 8 VehicleNode SafetyFeedback_ECU.SafetyFeedbackTest 44710.062918 R
-> SAF_SafetyModeTest 1.0000 Enum
-> SAF_TorqueTest 890.0000 Nm
-> SAF_VoltageTest 5678.0000 Volts
-> SAF_CurrentTest 6789.1000 Amps
-> SAF_SpeedTest 7890.2000 rpm
CAN 1 000067 X 8 CommandTest 44710.072908 R
CAN 1 000067 X 8 VehicleNode Command_ECU.CommandTest 44710.072908 R
-> CMD_ControlModeTest 3.0000 Enum
-> CMD_CommandCounterTest 4.0000 Integer
-> CMD_ActiveDischargeTest 1.0000 Bool
-> CMD_UseRawSpeedTest 0.0000 Bool
-> CMD_DirOfRotationTest 1.0000 Enum
CAN 1 000068 X 8 StatusTest 44710.082888 R
CAN 1 000068 X 8 VehicleNode Status_ECU.StatusTest 44710.082888 R
-> STS_NotEnabledTest 0.0000 Bool
-> STS_OverCurrentTest 1.0000 Bool
-> STS_OverBusTest 0.0000 Bool
-> STS_OverPhaseTest 1.0000 Bool
-> STS_UnderVoltageTest 0.0000 Bool
CAN 1 000064 X 8 AccelerationCommandTest 44710.092868 R
CAN 1 000064 X 8 VehicleNode AccelerationCommand_ECU.AccelerationCommandTest 44710.092868 R
-> ACC_AccelerationTest 2.0000 Bool
-> ACC_CurrentTest 2.0000 Bool
-> ACC_SurgeTest 54322.0000 RPM
-> ACC_AccelTest 98766.0000 RPM/sec
-> ACC_MaxCurrentTest 151.0000 Amp
CAN 1 000065 X 8 FeedbackTest 44710.102938 R
CAN 1 000065 X 8 VehicleNode Feedback_ECU.FeedbackTest 44710.102938 R
-> FB_SafetyModeTest 1.0000 Enum
-> FB_TorqueTest 2346.5000 Nm
-> FB_VoltageTest 4568.8000 Volts
-> FB_CurrentTest 5679.9000 Amps
-> FB_SpeedTest 6780.0000 rpm
CAN 1 000066 X 8 SafetyFeedbackTest 44710.112918 R
CAN 1 000066 X 8 VehicleNode SafetyFeedback_ECU.SafetyFeedbackTest 44710.112918 R
-> SAF_SafetyModeTest 0.0000 Enum
-> SAF_TorqueTest 569.0000 Nm
-> SAF_VoltageTest 6782.0000 Volts
-> SAF_CurrentTest 7892.1000 Amps
-> SAF_SpeedTest 8912.2000 rpm
CAN 1 000067 X 8 CommandTest 44710.122908 R
CAN 1 000067 X 8 VehicleNode Command_ECU.CommandTest 44710.122908 R
-> CMD_ControlModeTest 2.0000 Enum
-> CMD_CommandCounterTest 5.0000 Integer
-> CMD_ActiveDischargeTest 0.0000 Bool
-> CMD_UseRawSpeedTest 1.0000 Bool
-> CMD_DirOfRotationTest 0.0000 Enum
CAN 1 000068 X 8 StatusTest 44710.132888 R
CAN 1 000068 X 8 VehicleNode Status_ECU.StatusTest 44710.132888 R
-> STS_NotEnabledTest 1.0000 Bool
-> STS_OverCurrentTest 0.0000 Bool
-> STS_OverBusTest 1.0000 Bool
Expand Down
5 changes: 2 additions & 3 deletions test/test_dbc_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ def parse_dbc(file_path):
sys.exit(1)

script_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(script_dir, '..', 'data')

dbc_type = sys.argv[1]
if dbc_type == 'test':
dbc_file_path = os.path.join(data_dir, 'test.dbc')
dbc_file_path = os.path.join(script_dir, 'data', 'test.dbc')
elif dbc_type == 'main':
dbc_file_path = os.path.join(data_dir, '1200G_CAN-DBC_v01.01.00.dbc')
dbc_file_path = os.path.join(script_dir, '..', 'data', '1200G_CAN-DBC_v01.01.00.dbc')
else:
print("Invalid argument. Use 'test' or 'main'.")
sys.exit(1)
Expand Down
6 changes: 3 additions & 3 deletions test/test_log_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def process_message(db, message_lines, parsed_data):
can_id = main_line[2]
# if can_id in ["00000000", "0000040A"]:
# return
message_name = main_line[5]
timestamp = main_line[6]
direction = main_line[7]
ecu_name, message_name = main_line[6].split('.')
timestamp = main_line[7]
direction = main_line[8]

signals = []

Expand Down
53 changes: 42 additions & 11 deletions test/test_signal_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,43 @@ def parse_log(db, log_file_path):
with open(log_file_path, 'r') as log_file:
current_message = None
for line in log_file:
# print(f"Reading line: {line.strip()}") # Commented for clean output
if line.startswith("CAN"):
if current_message:
process_message(db, current_message, parsed_data)
current_message = [line.strip()]
# print("Detected start of a new message") # Commented for clean output
elif line.strip().startswith("->"):
# print("Detected start of line with ->")
current_message.append(line.strip())
# print(f"Appending signal line: {line.strip()}") # Commented for clean output
if current_message:
process_message(db, current_message, parsed_data)
print_parsed_data(parsed_data)
return parsed_data
except Exception as e:
print(f"Error parsing log file {log_file_path}: {e}")
return None

def process_message(db, message_lines, parsed_data):
try:
# print(f"Processing message lines: {message_lines}") # Commented for clean output
main_line = message_lines[0].split()
can_id = int(main_line[2], 16)
message_name = main_line[5]
timestamp = float(main_line[6])
direction = main_line[7]
# print(f"Main line parts: {main_line}") # Commented for clean output
can_id = main_line[2]
# if can_id in ["00000000", "0000040A"]:
# return
ecu_name, message_name = main_line[6].split('.')
timestamp = main_line[7]
direction = main_line[8]

signals = []

for signal_line in message_lines[1:]:
# print("Signal Line ---- ", signal_line)
parts = signal_line.split()
signal_name = parts[1]
signal_value = float(parts[2])
signal_value = parts[2]
signals.append((signal_name, signal_value, timestamp))

parsed_data.append({
Expand All @@ -54,16 +64,32 @@ def process_message(db, message_lines, parsed_data):
"direction": direction,
"signals": signals
})

except Exception as e:
print(f"Error processing message: {e}, for CAN ID (Error Frames): {can_id}")
return

def print_parsed_data(parsed_data):
for data in parsed_data:
#print(f"Message: {data['message_name']}, CAN ID: {data['can_id']}, Timestamp: {data['timestamp']}, Direction: {data['direction']}")
for signal in data['signals']:
signal_name, signal_value, signal_timestamp = signal
#print(f" Signal: {signal_name}, Value: {signal_value}, Timestamp: {signal_timestamp}")


def plot_signals(parsed_data, signal_name, start_time, end_time):
timestamps = []
values = []
for data in parsed_data:
for signal in data['signals']:
s_name, s_value, s_timestamp = signal
try:
s_value = float(s_value)
s_timestamp = float(s_timestamp) / 1000 # Convert timestamp to seconds here
except ValueError as e:
print(f"Error converting value: {e}")
continue

if s_name == signal_name and (start_time is None or s_timestamp >= start_time) and (end_time is None or s_timestamp <= end_time):
timestamps.append(s_timestamp)
values.append(s_value)
Expand All @@ -72,12 +98,16 @@ def plot_signals(parsed_data, signal_name, start_time, end_time):
print(f"No data found for signal: {signal_name}")
return

plt.figure()
plt.plot(timestamps, values, marker='o')
plt.figure(figsize=(30, 15)) # Adjust the size as needed
plt.plot(timestamps, values)
plt.xlabel('Time (s)')
plt.ylabel('Value')
plt.title(f'Signal: {signal_name}')
plt.grid(True)

# Format the x-axis to display timestamps correctly in seconds
plt.gca().xaxis.set_major_formatter(plt.FuncFormatter(lambda x, _: f'{x:.2f}'))

plt.tight_layout()
plt.show()

Expand All @@ -101,14 +131,15 @@ def plot_signals(parsed_data, signal_name, start_time, end_time):
if db:
parsed_data = parse_log(db, log_file)
if parsed_data:
print("Done parsing log file")
if args.start is None or args.end is None:
# Determine the start and end times from the log if not provided
all_timestamps = [data['timestamp'] for data in parsed_data]
all_timestamps = [float(data['timestamp']) / 1000 for data in parsed_data for signal in data['signals']]
start_time = min(all_timestamps) if args.start is None else args.start
end_time = max(all_timestamps) if args.end is None else args.end
else:
start_time = args.start
end_time = args.end

plot_signals(parsed_data, args.signal, start_time, end_time)
else:
print("Parsed data is empty or None.")
else:
print("DBC parsing failed.")

0 comments on commit f25e394

Please sign in to comment.