Skip to content

Commit

Permalink
Initial commit with project structure and base scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiran Jojare committed Jul 18, 2024
1 parent 5a91bbc commit 0f9e89b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cantools
matplotlib
28 changes: 28 additions & 0 deletions src/can_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import cantools
import csv

def read_log_file(log_file):
can_messages = []
with open(log_file, 'r') as file:
reader = csv.reader(file, delimiter=' ')
for row in reader:
can_id = int(row[1], 16)
data = bytes.fromhex(row[2])
timestamp = float(row[0])
can_messages.append((timestamp, can_id, data))
return can_messages

def decode_signals(can_messages, dbc_file):
db = cantools.database.load_file(dbc_file)
signal_data = {}
for timestamp, can_id, data in can_messages:
try:
message = db.get_message_by_frame_id(can_id)
decoded_signals = message.decode(data)
for signal_name, value in decoded_signals.items():
if signal_name not in signal_data:
signal_data[signal_name] = []
signal_data[signal_name].append((timestamp, value))
except KeyError:
continue
return signal_data
13 changes: 13 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from can_parser import read_log_file, decode_signals
from plot_signals import plot_signals

def main():
dbc_file = 'data/your_dbc_file.dbc'
log_file = 'data/your_log_file.txt'

can_messages = read_log_file(log_file)
signal_data = decode_signals(can_messages, dbc_file)
plot_signals(signal_data)

if __name__ == '__main__':
main()
11 changes: 11 additions & 0 deletions src/plot_signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import matplotlib.pyplot as plt

def plot_signals(signal_data):
for signal_name, data in signal_data.items():
timestamps, values = zip(*data)
plt.figure()
plt.plot(timestamps, values)
plt.title(signal_name)
plt.xlabel('Time (s)')
plt.ylabel(signal_name)
plt.show()

0 comments on commit 0f9e89b

Please sign in to comment.