-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit with project structure and base scripts
- Loading branch information
Kiran Jojare
committed
Jul 18, 2024
1 parent
5a91bbc
commit 0f9e89b
Showing
4 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
cantools | ||
matplotlib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |