-
Notifications
You must be signed in to change notification settings - Fork 2
/
radio.py
160 lines (129 loc) · 4.7 KB
/
radio.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
import serial
import time
from threading import Thread
class radioPacket():
def __init__(self, data):
self.type = data[0]
self.checksum = data[1]
self.seqNum = self.interpret16Bit(data[2:4])
self.payload = data[4:12]
def interpret24Bit(self, data):
low = data[0]
mid = data[1]
high = data[2]
return (low) + (mid * (2**8)) + (high * (2**16))
def interpret16Bit(self, data):
low = data[0]
high = data[1]
return (low) + (high * (2**8))
class setupPacket(radioPacket):
def __init__(self, data):
super().__init__(data)
self.force = self.interpret24Bit(self.payload[0:])
self.pressure = self.interpret24Bit(self.payload[3:])
self.continuity = bool(self.payload[6])
def __str__(self):
out = "Force: {}, Pressure: {}, Continuity: {}".format(self.force, self.pressure, self.continuity)
return out
class errorPacket(radioPacket):
def __init__(self, data):
super().__init__(data)
self.storageError = self.payload[0]
self.adcError = self.payload[1]
def __str__(self):
out = "Storage: {}, ADC: {}".format(self.storageError, self.adcError)
return out
class resultPacket(radioPacket):
def __init__(self, data):
super().__init__(data)
self.time = self.interpret16Bit(self.payload)
self.force = self.interpret24Bit(self.payload[2:])
self.pressure = self.interpret24Bit(self.payload[5:])
def __str__(self):
out = "#: {}, Time: {}, Force: {}, Pressure: {}".format(self.seqNum, self.time, self.force, self.pressure)
return out
class RadioManager():
PACKET_SIZE = 12
PREAMBLE = [0xAA, 0xBB]
ESCAPE = 0x11
PACKET_TYPE_MAP = {
0: setupPacket,
1: errorPacket,
2: resultPacket
}
def __init__(self, port):
self.toSend = []
self.port = port
self.serialThread = Thread(target=self._serialThread)
self.results = {}
@staticmethod
def checkPacket(packet):
checksum = (sum(packet) % 256) == 0
rightLength = len(packet) == RadioManager.PACKET_SIZE
return checksum and rightLength
def sendPacket(self, packetType, seqNum, payload):
seqNumLow = seqNum & 0xFF
seqNumHigh = (seqNum >> 8) & 0xFF
pack = [packetType, 0, seqNumHigh, seqNumLow] + payload
pack[1] = (256 - sum(pack)) % 256
self.toSend.append(bytearray(RadioManager.PREAMBLE + pack))
def buildPacket(self, packetData):
packetCons = RadioManager.PACKET_TYPE_MAP[packetData[0]]
pack = packetCons(packetData)
if type(pack) is resultPacket:
self.results[pack.seqNum] = pack
else:
print(pack)
def _serialThread(self):
with serial.Serial(self.port, 9600) as serport:
escape = False
packetBuff = []
inPreamble = False
inPacket = False
while True:
while serport.in_waiting > 0 and len(self.toSend) == 0:
b = int.from_bytes(serport.read(), 'big')
if b == RadioManager.PREAMBLE[0] and not escape:
inPreamble = True
inPacket = False
elif b == RadioManager.PREAMBLE[1] and not escape and inPreamble:
packetBuff = []
inPacket = True
elif inPacket and (b != RadioManager.ESCAPE or escape):
packetBuff.append(b)
if self.checkPacket(packetBuff):
self.buildPacket(packetBuff)
inPacket = False
escape = b == RadioManager.ESCAPE
if len(self.toSend) > 0:
packet = self.toSend.pop(0)
serport.write(packet)
def run(self):
self.serialThread.start()
rm = RadioManager('/dev/ttyUSB0')
rm.run()
time.sleep(5)
for i in range(0, 5):
rm.sendPacket(128, 0, [0x00, 0x1B, 0, 0, 0, 0, 0, 0])
time.sleep(0.05)
def animate(i):
global lastLen
if len(rm.results) > 10 and len(rm.results) > lastLen:
lastLen = len(rm.results)
x = []
y = []
recv = list(rm.results.keys())
recv.sort()
for p in recv:
x.append(rm.results[p].time)
y.append((rm.results[p].force - 110000) * 70 / 390000)
ax.clear()
plt.title('{}/1000'.format(len(rm.results)))
ax.plot(x, y)
import matplotlib.pyplot as plt
import matplotlib.animation as animation
lastLen = 0
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ani = animation.FuncAnimation(fig, animate, interval=250)
plt.show()