-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
96 lines (71 loc) · 2.23 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python
# -*- coding:utf-8 -*-
from machine import UART,Pin,Timer
from rp2 import *
import time
# Define LED-Pin
led = Pin(25, Pin.OUT)
# LED ON
led.value(True)
# In-work indicator to false
inwork = True
# Loop counter
loop = 1
# Waiting for vehicle to poperly boot
print("=== Wait for 10s")
time.sleep(10)
# Filter IDs
id_filter_state = {0: CANID(0x227).get_id_filter()}
id_filter_toggle = {0: CANID(0x1A9).get_id_filter()}
# Repeat work until StSt is off
while inwork:
# Initialize CAN controller for reading StSt state
print("=== Initializing CAN")
c = CAN(profile=CAN.BITRATE_125K_75,id_filters=id_filter_state)
time.sleep(2)
# Read payload
frames = c.recv()
payload_state = frames[0].get_data()
# Output payload
print("=== StSt State frame 227")
print(payload_state)
# Check for Bit2 in Byte3
a = payload_state
b = b'\x00\x00\x00\x04\x00\x00\x00\x00'
a = (int.from_bytes(a, 'big') & int.from_bytes(b, 'big')).to_bytes(max(len(a), len(b)), 'big')
n = b'\x00\x00\x00\x00\x00\x00\x00\x00'
# If Bit 2 of Byte 3 is set = StSt OFF
if a != n:
print("=== StSt is now OFF")
# inwork = False # Uncomment if you want the Pi to stop checking the status to infinity
else:
# Initialize CAN controller for reading StSt toggle
c = CAN(profile=CAN.BITRATE_125K_75,id_filters=id_filter_toggle)
time.sleep(2)
# Read payload
frames = c.recv()
payload = frames[0].get_data()
# Output payload
print("=== StSt State frame 227")
print(payload)
# Set Bit 7 on Byte 6
a = payload
b = b'\x00\x00\x00\x00\x00\x00\x80\x00'
payload = (int.from_bytes(a, 'big') | int.from_bytes(b, 'big')).to_bytes(max(len(a), len(b)), 'big')
# Notify
print("=== New payload for Sending")
print(payload)
# Define CANFrame
f = CANFrame(CANID(0x1A9), data=payload)
# Send Frame
c.send_frame(f)
print("=== Sent message")
print("Loop:")
print(loop)
print("==============================")
loop += 1
while True:
# Go to infinite Standby
print("=== Standby [LED BLINK SLOW])")
led.toggle()
time.slaeep(2)