Skip to content

Commit

Permalink
DBC parsing working for test and main dbc
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiran Jojare committed Jul 19, 2024
1 parent 176704d commit df87c04
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 101 deletions.
50 changes: 0 additions & 50 deletions test/can_parser.py

This file was deleted.

101 changes: 101 additions & 0 deletions test/data/test.dbc
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
VERSION ""


NS_ :
NS_DESC_
CM_
BA_DEF_
BA_
VAL_
CAT_DEF_
CAT_
FILTER
BA_DEF_DEF_
EV_DATA_
ENVVAR_DATA_
SGTYPE_
SGTYPE_VAL_
BA_DEF_SGTYPE_
BA_SGTYPE_
SIG_TYPE_REF_
VAL_TABLE_
SIG_GROUP_
SIG_VALTYPE_
SIGTYPE_VALTYPE_
BO_TX_BU_
BA_DEF_REL_
BA_REL_
BA_DEF_DEF_REL_
BU_SG_REL_
BU_EV_REL_
BU_BO_REL_
SG_MUL_VAL_

BS_:

BU_: Node1 Node2 Node3


BO_ 100 AccelerationCommandTest: 8 Node1
SG_ ACC_AccelerationTest : 0|1@1+ (1,0) [0|1] "Bool" Node1,Node2
SG_ ACC_CurrentTest : 1|1@1+ (1,0) [0|1] "Bool" Node2,Node1
SG_ ACC_SurgeTest : 16|16@1+ (1,0) [0|32767] "RPM" Node1,Node2
SG_ ACC_AccelTest : 32|16@1+ (1,0) [0|32767] "RPM/sec" Node1,Node2
SG_ ACC_MaxCurrentTest : 48|8@1+ (5,0) [0|1275] "Amp" Node1,Node2

BO_ 101 FeedbackTest: 8 Node2
SG_ FB_SafetyModeTest : 0|1@1+ (1,0) [0|1] "Enum" Node1,Node2
SG_ FB_TorqueTest : 1|15@1+ (0.2,-3212.8) [-3212.8|3212.8] "Nm" Node2,Node1
SG_ FB_VoltageTest : 16|16@1+ (0.1,-3212.8) [-3212.8|3212.8] "Volts" Node2,Node1
SG_ FB_CurrentTest : 32|16@1+ (0.1,-3212.8) [-3212.8|3212.8] "Amps" Node2,Node1
SG_ FB_SpeedTest : 48|16@1+ (0.5,-16064) [-16064|16064] "rpm" Node2,Node1

BO_ 102 SafetyFeedbackTest: 8 Node3
SG_ SAF_SafetyModeTest : 0|1@1+ (1,0) [0|1] "Enum" Node1,Node3
SG_ SAF_TorqueTest : 1|12@1+ (1,-2048) [-2048|2047] "Nm" Node1,Node3
SG_ SAF_VoltageTest : 13|12@1+ (1,0) [0|4095] "Volts" Node1,Node3
SG_ SAF_CurrentTest : 25|12@1+ (1,-2048) [-2048|2047] "Amps" Node1,Node3
SG_ SAF_SpeedTest : 37|15@1+ (1,-16384) [-16384|16383] "rpm" Node1,Node3

BO_ 103 CommandTest: 8 Node1
SG_ CMD_ControlModeTest : 0|4@1+ (1,0) [0|6] "Enum" Node2,Node1,Node3
SG_ CMD_CommandCounterTest : 4|4@1+ (1,0) [0|15] "Integer" Node2,Node1,Node3
SG_ CMD_ActiveDischargeTest : 8|1@1+ (1,0) [0|1] "Bool" Node3,Node2
SG_ CMD_UseRawSpeedTest : 10|1@1+ (1,0) [0|1] "Bool" Node1,Node2,Node3
SG_ CMD_DirOfRotationTest : 11|1@1+ (1,0) [0|1] "Enum" Node1,Node2,Node3

BO_ 104 StatusTest: 8 Node2
SG_ STS_NotEnabledTest : 0|1@1+ (1,0) [0|1] "Bool" Node2,Node1
SG_ STS_OverCurrentTest : 1|1@1+ (1,0) [0|1] "Bool" Node2,Node1
SG_ STS_OverBusTest : 2|1@1+ (1,0) [0|1] "Bool" Node2,Node1
SG_ STS_OverPhaseTest : 3|1@1+ (1,0) [0|1] "Bool" Node2,Node1
SG_ STS_UnderVoltageTest : 4|1@1+ (1,0) [0|1] "Bool" Node2,Node1

CM_ "Test CAN database definition.";
CM_ BU_ Node1 "Test Node 1";
CM_ BU_ Node2 "Test Node 2";
CM_ BU_ Node3 "Test Node 3";
CM_ BO_ 100 "Test command message for acceleration limits.";
CM_ SG_ 100 ACC_AccelerationTest "Test signal for updating acceleration limits.";
CM_ SG_ 100 ACC_CurrentTest "Test signal for updating current limits.";
CM_ SG_ 100 ACC_SurgeTest "Test signal for controlling surges in acceleration.";
CM_ SG_ 100 ACC_AccelTest "Test signal for maximum rate of acceleration.";
CM_ SG_ 100 ACC_MaxCurrentTest "Test signal for maximum current in drive mode.";

BA_DEF_ SG_ "GenSigSendType" ENUM "Cyclic","OnWrite","OnWriteWithRepetition","OnChange","OnChangeWithRepetition","IfActive","IfActiveWithRepetition","NoSigSendType","NotUsed";
BA_DEF_ SG_ "GenSigCycleTime" INT 0 0;
BA_DEF_ BO_ "GenMsgSendType" ENUM "Cyclic","OnWrite","OnWriteWithRepetition","OnChange","OnChangeWithRepetition","IfActive","IfActiveWithRepetition","NoMsgSendType","NotUsed";
BA_DEF_ BO_ "GenMsgCycleTime" INT 0 0;
BA_DEF_ "BusType" STRING ;
BA_DEF_DEF_ "GenSigSendType" "Cyclic";
BA_DEF_DEF_ "GenSigCycleTime" 0;
BA_DEF_DEF_ "GenMsgSendType" "Cyclic";
BA_DEF_DEF_ "GenMsgCycleTime" 0;
BA_DEF_DEF_ "BusType" "";
BA_ "BusType" "CAN";
BA_ "GenMsgCycleTime" BO_ 101 100;
BA_ "GenSigCycleTime" SG_ 101 FB_SafetyModeTest 100;
BA_ "GenSigCycleTime" SG_ 101 FB_TorqueTest 100;
BA_ "GenSigCycleTime" SG_ 101 FB_VoltageTest 100;
BA_ "GenSigCycleTime" SG_ 101 FB_CurrentTest 100;
BA_ "GenSigCycleTime" SG_ 101 FB_SpeedTest 100;
7 changes: 0 additions & 7 deletions test/data/test_dbc.dbc

This file was deleted.

41 changes: 13 additions & 28 deletions test/data/test_log.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
CAN 1 14EF6A5A X 8 Safety Processor_toyota_a.UniversalCommand 44709.992868 R
-> UC_ControlMode 5.0000 Enum
-> UC_CommandCounter 5.0000 Integer
-> UC_ActiveDischarge 0.0000 Bool
-> UC_UseRawSpeed 0.0000 Bool
-> UCS_DirOfRotation 1.0000 Enum
-> UC_Enable 1.0000 Bool
CAN 1 14EF6A5A X 8 Safety Processor_toyota_a.UniversalCommand 44710.002938 R
-> UC_ControlMode 6.0000 Enum
-> UC_CommandCounter 6.0000 Integer
-> UC_ActiveDischarge 1.0000 Bool
-> UC_UseRawSpeed 1.0000 Bool
-> UCS_DirOfRotation 0.0000 Enum
-> UC_Enable 0.0000 Bool
CAN 1 14EF6A5A X 8 Safety Processor_toyota_a.UniversalCommand 44710.012918 R
-> UC_ControlMode 7.0000 Enum
-> UC_CommandCounter 7.0000 Integer
-> UC_ActiveDischarge 0.0000 Bool
-> UC_UseRawSpeed 1.0000 Bool
-> UCS_DirOfRotation 1.0000 Enum
-> UC_Enable 1.0000 Bool
CAN 1 14EF6A5A X 8 Safety Processor_toyota_a.UniversalCommand 44710.022898 R
-> UC_ControlMode 5.0000 Enum
-> UC_CommandCounter 8.0000 Integer
-> UC_ActiveDischarge 1.0000 Bool
-> UC_UseRawSpeed 0.0000 Bool
-> UCS_DirOfRotation 0.0000 Enum
-> UC_Enable 0.0000 Bool
WinNo Identifier Flg Dlc Name Time Dir
CAN 1 123456 X 8 ECU1.Message1 44709.992868 R
-> Signal1 100.0000 units
-> Signal2 200.0000 units
CAN 1 123456 X 8 ECU1.Message1 44710.002938 R
-> Signal1 110.0000 units
-> Signal2 210.0000 units
CAN 1 654321 X 8 ECU1.Message2 44710.012918 R
-> Signal3 300.0000 units
-> Signal4 400.0000 units
CAN 1 654321 X 8 ECU1.Message2 44710.022908 R
-> Signal3 310.0000 units
-> Signal4 410.0000 units
16 changes: 0 additions & 16 deletions test/main.py

This file was deleted.

Empty file removed test/plot_signals.py
Empty file.
34 changes: 34 additions & 0 deletions test/test_dbc_parsing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import cantools
import os
import sys

def parse_dbc(file_path):
try:
db = cantools.database.load_file(file_path)
print(f"Successfully parsed {file_path}")
for message in db.messages:
print(f"Message ID: {message.frame_id}, Name: {message.name}")
for signal in message.signals:
print(f" Signal Name: {signal.name}, Start Bit: {signal.start}, Length: {signal.length}")
except Exception as e:
print(f"Error parsing {file_path}: {e}")

if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python test_dbc_parsing.py <test|main>")
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')
elif dbc_type == 'main':
dbc_file_path = os.path.join(data_dir, '1200G_CAN-DBC_v01.01.00.dbc')
else:
print("Invalid argument. Use 'test' or 'main'.")
sys.exit(1)

print(f"Parsing DBC: {dbc_file_path}")
parse_dbc(dbc_file_path)
12 changes: 12 additions & 0 deletions test/test_log_parsing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import re

def parse_log_file(log_path):
with open(log_path, 'r') as file:
log_data = file.readlines()
return log_data

if __name__ == "__main__":
log_path = 'test/data/test_log.txt'
log_data = parse_log_file(log_path)
for line in log_data:
print(line.strip())
35 changes: 35 additions & 0 deletions test/test_signal_decoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import cantools
import re

def parse_dbc_file(dbc_path):
db = cantools.database.load_file(dbc_path)
return db

def parse_log_file(log_path):
with open(log_path, 'r') as file:
log_data = file.readlines()
return log_data

def decode_signals(db, log_data):
signals = []
for line in log_data:
match = re.match(r'.*?(\w+)\s+(\w+)\s+\d+\s+(.*?)\s+(\d+\.\d+)\s+\w\s+->\s+(.*?)\s+(\d+\.?\d*)\s+.*', line)
if match:
message_id = int(match.group(2), 16)
timestamp = float(match.group(4))
signal_name = match.group(5)
signal_value = float(match.group(6))
if message_id in db.messages:
message = db.get_message_by_frame_id(message_id)
decoded = {signal.name: signal_value for signal in message.signals}
signals.append((timestamp, signal_name, decoded))
return signals

if __name__ == "__main__":
dbc_path = 'test/data/test.dbc'
log_path = 'test/data/test_log.txt'
db = parse_dbc_file(dbc_path)
log_data = parse_log_file(log_path)
decoded_signals = decode_signals(db, log_data)
for signal in decoded_signals:
print(signal)
54 changes: 54 additions & 0 deletions test/test_signal_plotting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import cantools
import re
import matplotlib.pyplot as plt

def parse_dbc_file(dbc_path):
db = cantools.database.load_file(dbc_path)
return db

def parse_log_file(log_path):
with open(log_path, 'r') as file:
log_data = file.readlines()
return log_data

def decode_signals(db, log_data):
signals = []
for line in log_data:
match = re.match(r'.*?(\w+)\s+(\w+)\s+\d+\s+(.*?)\s+(\d+\.\d+)\s+\w\s+->\s+(.*?)\s+(\d+\.?\d*)\s+.*', line)
if match:
message_id = int(match.group(2), 16)
timestamp = float(match.group(4))
signal_name = match.group(5)
signal_value = float(match.group(6))
if message_id in db.messages:
message = db.get_message_by_frame_id(message_id)
decoded = {signal.name: signal_value for signal in message.signals}
signals.append((timestamp, signal_name, decoded))
return signals

def plot_signals(signals):
timestamps, signal_names, values = zip(*signals)
signal_data = {}

for timestamp, signal_name, decoded in signals:
if signal_name not in signal_data:
signal_data[signal_name] = ([], [])
signal_data[signal_name][0].append(timestamp)
signal_data[signal_name][1].append(decoded[signal_name])

for signal_name, (times, vals) in signal_data.items():
plt.figure()
plt.plot(times, vals, label=signal_name)
plt.xlabel('Time (s)')
plt.ylabel(signal_name)
plt.title(f"Signal: {signal_name} Over Time")
plt.legend()
plt.show()

if __name__ == "__main__":
dbc_path = 'test/data/test.dbc'
log_path = 'test/data/test_log.txt'
db = parse_dbc_file(dbc_path)
log_data = parse_log_file(log_path)
decoded_signals = decode_signals(db, log_data)
plot_signals(decoded_signals)

0 comments on commit df87c04

Please sign in to comment.