-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagvapi.py
217 lines (183 loc) · 6.43 KB
/
agvapi.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/python3
# -*- coding: utf-8 -*-
''' Interface for serial communication with the AGV driver
It contains a simple test program that sets constant motor velocities and outputs sensor readings.
'''
import serial
import struct
from time import sleep, time
from ams import wrapToPi
from math import pi
class Agv(object):
WHEEL_VEL_GAIN = 1000.0
AXIS_LENGTH = 0.075 # Axis length, in m
WHEEL_MAX_V = 0.3 # Max wheel velocity, in m/s
REPEAT = 10 # Number of start/stop command repeats
TIMEOUT = 0.02 # Start/stop command timeout
def __init__(self, port='/dev/ttyS0', baud=115200):
self._serial = serial.Serial()
self._serial.baudrate = baud
self._serial.port = port
self._serial.timeout = 0
self._buff = bytearray()
self._data = [0]*28
def __enter__(self):
self.open()
return self
def __exit__(self, *args, **kwargs):
self.close()
def open(self):
self._serial.open()
m, s = False, False
M, S = True, True
i = Agv.REPEAT
while m != M and s != S and i > 0:
self.setPowerMode(motors=M, sensors=S)
sleep(Agv.TIMEOUT)
self.readSensors()
t, m, s = self.getStatus()
i -= 1
if i==0:
raise SystemError('Unable to setup the AGV controller.')
def close(self):
for i in range(Agv.REPEAT):
self.setWheelVel() # Stop the robot
sleep(Agv.TIMEOUT)
for i in range(Agv.REPEAT):
self.setPowerMode(motors=False, sensors=False) # Enable power-saving mode
sleep(Agv.TIMEOUT)
self._serial.close()
#Set tangential and angular velocity, in m/s and rad/s, respectively
def setVel(self, v=0.0, omega=0.0):
vl = -v+Agv.AXIS_LENGTH/2.0*omega
vr = -v-Agv.AXIS_LENGTH/2.0*omega
self.setWheelVel(vl, vr)
# Set wheel velocities, in m/s
def setWheelVel(self, left=0.0, right=0.0):
cmd = [left, -right]
chk = 0
for i in range(len(cmd)):
cmd[i] = Agv._clamp(cmd[i], -Agv.WHEEL_MAX_V, Agv.WHEEL_MAX_V)
cmd[i] = int(cmd[i]*Agv.WHEEL_VEL_GAIN)
cmd[i] = Agv._clampShort(cmd[i])
chk += (cmd[i]&0xFF) + (cmd[i]>>8)
chk += 0x5A + 0x20 + 0x04
msg = struct.pack('<BBBhhB', 0x5A, 0x20, 0x04, cmd[0], cmd[1], (-chk) & 0xFF)
#print("TX: "+":".join("{:02X}".format(x) for x in msg)) # Debug
self._serial.write(msg)
def setPowerMode(self, motors=False, sensors=False):
chk = 0
a = int(motors)
b = int(sensors)
chk = 0x5A + 0x21 + 0x02 + a + b
msg = struct.pack('<BBBBBB', 0x5A, 0x21, 0x02, a, b, (-chk) & 0xFF)
#print("TX: "+":".join("{:02X}".format(x) for x in msg)) # Debug
self._serial.write(msg)
sleep(Agv.TIMEOUT)
def setWatchdog(self, period):
period = Agv._clampUShort(period)
chk = 0x5A + 0x22 + 0x01 + (period>>8) + (period&0xFF)
msg = struct.pack('<BBBBBB', 0x5A, 0x22, 0x01, period, (-chk) & 0xFF)
#print("TX: "+":".join("{:02X}".format(x) for x in msg)) # Debug
self._serial.write(msg)
sleep(Agv.TIMEOUT)
# Read sensors
def readSensors(self):
msg = self._serial.read((3+40+1)*20)
if len(msg):
#print("RX: "+":".join("{:02X}".format(x) for x in msg)) # Debug
self._buff += bytearray(msg)
n = len(self._buff)
i = 0
while n-i >= 4:
head, cmd, length = struct.unpack('<BBB', self._buff[i:i+3])
if head == 0x5A:
if n-i >= length + 1 + 3:
chk = sum(self._buff[i:i+3+length+1])
if chk & 0xFF == 0:
if cmd == 0x10:
self.data = struct.unpack('<Iiii8B8B8B', self._buff[i+3:i+3+length])
i += 3 + length + 1
continue
else:
break
i += 1
self._buff = self._buff[i:]
def getEncoders(self):
return self.data[1], self.data[2], self.data[3]
def getLineValues(self, mode=0):
mode %= 3
return self.data[4+8*mode:4+8*mode+7]
def getStatus(self):
return self.data[0] & 0x00FFFFFF, (self.data[0] & 0x01000000) != 0, (self.data[0] & 0x02000000) != 0
def printSensors(self):
print('\033c')
print('Status: t={} ms, motors={}, sensors={}'.format(*self.getStatus()))
print('Encoders: left={}, right={}, aux={}'.format(*self.data[1:4]))
print('Line sensor:')
print(' Individual: {:02X}, {:02X}, {:02X}, {:02X}, {:02X}, {:02X}, {:02X}, {:02X}'.format(*self.data[4:4+8]))
print(' All on: {:02X}, {:02X}, {:02X}, {:02X}, {:02X}, {:02X}, {:02X}, {:02X}'.format(*self.data[12:12+8]))
print(' All off: {:02X}, {:02X}, {:02X}, {:02X}, {:02X}, {:02X}, {:02X}, {:02X}'.format(*self.data[20:20+8]))
print(' Individual All on All off')
print(' ---------- ---------- ----------')
tmpl = ' {:10s} {:10s} {:10s}'
for i in range(8):
a = [int(x*10/255) for x in self.data[4+i:21+i:8]]
print(tmpl.format(*['o'*x for x in a]))
@staticmethod
def _clamp(num, low, high):
return max(min(high, num), low)
@staticmethod
def _clampShort(num):
return Agv._clamp(num, -32768, 32767)
@staticmethod
def _clampUShort(num):
return Agv._clamp(num, 0, 65536)
def findLineEdges(lineValues):
smin = min(lineValues)
smax = max(lineValues)
av = sum(lineValues) / 7
threshold = (smin + smax) / 2
edgeRight = None
edgeLeft = None
if smax - smin > 80:
lineEl = [i for i in range(len(lineValues)) if lineValues[i] > threshold]
# Right edge
if lineEl[0] == 0:
edgeRight = -1
else:
vs = lineValues[lineEl[0]-1:lineEl[0]+1]
if abs(vs[1] - vs[0]) > 0:
edgeRight = ((lineEl[0] + float((threshold - vs[0])) / (vs[1] - vs[0])) - 4) / 2.5
else:
edgeRight = (lineEl[0] + 0.5 - 4) / 2.5
# Left edge
if lineEl[-1] == 6:
edgeLeft = 1
else:
vs = lineValues[lineEl[-1]:lineEl[-1]+2]
if abs(vs[1] - vs[0]) > 0:
edgeLeft = ((lineEl[-1] + float((threshold - vs[1])) / (vs[1] - vs[0])) - 2.5) / 2.5
else:
edgeLeft = (lineEl[-1] - 2.5) / 2.5
elif smax > 150:
# On the line
edgeRight = -1.0
edgeLeft = 1.0
return edgeLeft, edgeRight
if __name__ == '__main__':
with Agv() as robot:
try:
k = 0
while True:
robot.readSensors()
lineValues = robot.getLineValues()
edgeLeft, edgeRight = findLineEdges(lineValues)
k += 1
if k % 20 == 0:
robot.printSensors()
print('Line edges: left={}, right={}'.format(edgeLeft, edgeRight))
robot.setVel(0.0, 1.0)
sleep(0.01)
except KeyboardInterrupt:
pass