-
Notifications
You must be signed in to change notification settings - Fork 3
/
mock_can_data.py
79 lines (59 loc) · 2.09 KB
/
mock_can_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
""" Script to mock CAN data """
# This script automatically generates and sends CAN messages based off of the DBC file
# Ensure that you have can and cantools installed, which can be accomplished by running
# pip install -r requirements.txt
# Alternatively, you could also just run
# pip install can && pip install cantools
# If you are running this script with virtual CAN ensure that it is set up first
# You can run the below commands
# sudo modprobe vcan
# sudo ip link add dev vcan0 type vcan
# sudo ip link set up vcan0
import time
import random
import sys
import cantools
import can
# A negative value for NUM_MESSAGES will cause the script to send CAN messages forever
NUM_MESSAGES = -1
SLEEP_TIME_S = 1
CAN_MESSAGES = []
try:
DB = cantools.database.load_file('system_can.dbc')
# pylint: disable=broad-except
except BaseException:
print("ERROR: Must generate DBC file first")
print("Run make codegen && make codegen_dbc")
sys.exit(1)
# This can be edited depending on the CAN interface
CAN_BUS = can.interface.Bus('vcan0', bustype='socketcan')
def main():
""" Main function """
get_messages()
iterate_message_and_signal()
def get_messages():
""" Gets messages """
for msg in DB.messages:
CAN_MESSAGES.append(msg)
def iterate_message_and_signal():
""" Iterates through messages and signals """
num_messages_sent = 0
while NUM_MESSAGES < 0 or NUM_MESSAGES > num_messages_sent:
try:
send_message()
num_messages_sent += 1
time.sleep(SLEEP_TIME_S)
except KeyboardInterrupt:
break
print("\n" + str(num_messages_sent) + " CAN messages have been sent")
def send_message():
""" Sends messages """
msg = random.choice(CAN_MESSAGES)
data = {}
for signal in msg.signals:
data[signal.name] = random.randint(0, pow(2, signal.length) - 1)
new_data = msg.encode(data)
message = can.Message(arbitration_id=msg.frame_id, data=new_data)
CAN_BUS.send(message)
if __name__ == "__main__":
main()