-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChassisInterface.py
170 lines (133 loc) · 6.17 KB
/
ChassisInterface.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from smbus2 import SMBus
import threading
from time import sleep
from enum import IntEnum
import logging
from sys import stdout
class Protocol:
class CmdId(IntEnum):
WheelCmd = 1
WheelResp = 2
UltrasonicQuery = 3
class DeviceId(IntEnum):
FrontAxis = 0
RearAxis = 1
Ultrasonic1 = 2
Ultrasonic2 = 3
Ultrasonic3 = 4
Ultrasonic4 = 5
SpeedMax = 1000
SteeringMax = 1000
def FormatOffset(cmdId, deviceId):
return ((int(cmdId) << 4) & 0xF0) | (int(deviceId) & 0x0F);
#speedRel и steeringRel в дипазоне [-1 .. 1]
def FormatWheelCmd(speedRel, steeringRel):
#todo : check parameter ranges
speedScaled = int(speedRel * Protocol.SpeedMax)
steeringScaled = int(steeringRel * Protocol.SteeringMax)
speedBuf = speedScaled.to_bytes(2, byteorder='big', signed=True)
steeringBuf = steeringScaled.to_bytes(2, byteorder='big', signed=True)
checksumBuf = []
for x, y in zip(speedBuf, steeringBuf):
xorByte = x ^ y
checksumBuf.append(xorByte)
return speedBuf + steeringBuf + bytes(checksumBuf)
def ParseWheelResp(respBuf):
if len(respBuf) < 4:
return None
speedL = int.from_bytes(respBuf[0:2], byteorder='big', signed=True)
speedR = int.from_bytes(respBuf[2:4], byteorder='big', signed=True)
return (speedL, speedR)
def ParseUltrasonicResp(respBuf):
if len(respBuf) < 2:
return None
return int.from_bytes(respBuf[0:2], byteorder='big', signed=True)
class SMBusStub:
def write_i2c_block_data(self, address, offset, data):
pass
def read_i2c_block_data(self, address, offset, dataSize):
return b'\xAB\xCD\xEF\x12'
class ChassisInterface:
LogFilename="chassis_interface.log"
LogRecordFormat="%(asctime)s %(levelname)s %(message)s"
ResendIntervalSec = 1.0 #Период передачи команд контроллеру
InterCmdPauseSeс = 0.05 #Интервал между I2C-транзакциями
I2cSlaveAddr = 80 #Адрес I2C slave (на arduino)
DefaultBusNum = 1 #id i2c-устройства (/dev/i2c-N)
def __init__(self, busNum = DefaultBusNum):
loggerHandlers = [
logging.StreamHandler(stream=stdout),
logging.FileHandler(filename=ChassisInterface.LogFilename)
]
logging.basicConfig(handlers=loggerHandlers, level=logging.DEBUG, format=ChassisInterface.LogRecordFormat)
self.bus = SMBus(busNum)
#self.bus = SMBusStub()
self.speed = 0.0
self.steering = 0.0
self.wheelCallback = None
self.ultrasonicCallback = None
self.thread = threading.Thread(target=self.__run)
self.thread.start()
#todo : interrupt and join the thread nicely
#def __del__(self):
#todo : set event to interrupt thread
#self.thread.join();
def setWheelCallback(self, callback):
self.wheelCallback = callback
def setUltrasonicCallback(self, callback):
self.ultrasonicCallback = callback
def setSpeed(self, speed_) :
self.speed = speed_
def setSteering(self, steering_) :
self.steering = steering_
def __sendWheelCmd(self, deviceId):
offs = Protocol.FormatOffset(Protocol.CmdId.WheelCmd, deviceId)
cmdBytes = Protocol.FormatWheelCmd(self.speed, self.steering)
logging.debug('Send to offset {0}: [0x{1}]'.format(hex(offs), cmdBytes.hex()))
try:
self.bus.write_i2c_block_data(ChassisInterface.I2cSlaveAddr, offs, cmdBytes)
except OSError as exc:
logging.error('I2C error : '.format(exc))
def __receiveWheelResponse(self, deviceId):
offs = Protocol.FormatOffset(Protocol.CmdId.WheelResp, deviceId)
try:
respList = self.bus.read_i2c_block_data(ChassisInterface.I2cSlaveAddr, offs, 4)
respBytes = bytes(respList)
logging.debug('Received from offset {0}: [0x{1}]'.format(hex(offs), respBytes.hex()))
respTuple = Protocol.ParseWheelResp(respBytes)
if (self.wheelCallback != None and respTuple != None) :
self.wheelCallback(deviceId, respTuple)
except OSError as exc:
logging.error('I2C error : '.format(exc))
def __receiveUltrasonicResponse(self, deviceId):
offs = Protocol.FormatOffset(Protocol.CmdId.UltrasonicQuery, deviceId)
try:
respList = self.bus.read_i2c_block_data(ChassisInterface.I2cSlaveAddr, offs, 2)
respBytes = bytes(respList)
logging.debug('Received from offset {0}: [0x{1}]'.format(hex(offs), respBytes.hex()))
respInt = Protocol.ParseUltrasonicResp(respBytes)
if (self.ultrasonicCallback != None and respInt != None) :
self.ultrasonicCallback(deviceId, respInt)
except OSError as exc:
logging.error('I2C error : '.format(exc))
def __run(self):
while True:
self.__sendWheelCmd(Protocol.DeviceId.FrontAxis)
self.__sendWheelCmd(Protocol.DeviceId.RearAxis)
sleep(2*ChassisInterface.InterCmdPauseSeс)
self.__receiveWheelResponse(Protocol.DeviceId.FrontAxis)
sleep(ChassisInterface.InterCmdPauseSeс)
self.__receiveWheelResponse(Protocol.DeviceId.RearAxis)
sleep(ChassisInterface.InterCmdPauseSeс)
for devId in range(Protocol.DeviceId.Ultrasonic1, Protocol.DeviceId.Ultrasonic4+1):
self.__receiveUltrasonicResponse(devId)
sleep(ChassisInterface.InterCmdPauseSeс)
sleep(ChassisInterface.ResendIntervalSec)
def logWheelResp(deviceId, respTuple):
logging.debug('Received wheel response from id {0} : L {1} R {2}'.format(deviceId, respTuple[0], respTuple[1]))
def logUltrasonicResp(deviceId, respVal):
logging.debug('Received ultrasonic response from id {0} : {1} '.format(deviceId, respVal))
if __name__ == '__main__':
d = ChassisInterface()
d.setWheelCallback(logWheelResp)
d.setUltrasonicCallback(logUltrasonicResp)