-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdriveAI.py
493 lines (389 loc) · 17.8 KB
/
driveAI.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import pika # needed to read messages out via RabbitMQ
#import threading # needed for multi threads
from gpiozero import Motor # needed for motor control
import time # needed for sleep
class DriveAI:
"""
Class that controls the motors and navigation for the rover
Note: currently doesn't navigate
"""
def __init__(self, maxSpeed=1):
"""
Initialization method
Args:
maxSpeed (int, optional): The max speed to use for the motors, should be between 0 and 1. Defaults to 1.
"""
# set up all four motors
#self.frontLeft = Motor(forward="BOARD29", backward="BOARD31")
#self.backLeft = Motor(forward="BOARD35", backward="BOARD37")
self.frontLeft = Motor(forward="BOARD31", backward="BOARD29")
self.backLeft = Motor(forward="BOARD37", backward="BOARD35")
self.frontRight = Motor(forward="BOARD32", backward="BOARD36")
self.backRight = Motor(forward="BOARD38", backward="BOARD40")
# set speed to zero
self.frontLeft.forward(0)
self.backLeft.forward(0)
self.frontRight.forward(0)
self.backRight.forward(0)
# fix max speed if entered wrong
while abs(maxSpeed) > 1:
maxSpeed = abs(maxSpeed) / 10
print(f"DriveAI started, motors have a max speed of: {str(maxSpeed)}")
self.maxSpeed = maxSpeed
#TODO Change these to pull from a yaml config
self.turnRate = 107.5 # turn 107.5 degrees a second
self.moveRate = 1.1667 # move 14 in a second
# set up RabbitMQ
self.connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
self.channel = self.connection.channel()
self.channel.queue_declare(queue='moveInput')
self.channel.queue_declare(queue='audioInput')
self.channel.basic_consume(queue='moveInput', on_message_callback=self.driveCallback, auto_ack=True)
def startListening(self):
"""
Starts listening to the message queues
"""
print("Beginning RabbitMQ listener")
self.channel.start_consuming()
def driveCallback(self, ch, method, properties, body):
"""
Callback method for drive input queue
Args:
ch (_type_): _description_
method (_type_): _description_
properties (_type_): _description_
body (str): message from the user
"""
message = body.decode()
#print(message)
command = str(message).split(', ')
if command[0].startswith("TURN"):
if len(command) == 2: # correct length
try:
self.turn(float(command[1]))
except ValueError:
print("Error, expected angle value could not turn into a float")
self.channel.basic_publish(exchange='', routing_key='audioInput', body="Error from the navigation AI, expected angle value could not turn into a float")
else:
print("Error, unexpected number of of commands")
self.channel.basic_publish(exchange='', routing_key='audioInput', body="Error from the navigation AI, unexpected number of commands")
elif command[0].startswith("MOVED"):
if len(command) == 3:
try:
self.moveD(float(command[1]), float(command[2]))
except ValueError:
print("Error, expected values could not turn into a float")
self.channel.basic_publish(exchange='', routing_key='audioInput', body="Error from the navigation AI, expected angle value could not turn into a float")
else:
print("Error, unexpected number of of commands")
self.channel.basic_publish(exchange='', routing_key='audioInput', body="Error from the navigation AI, unexpected number of commands")
elif command[0].startswith("MOVET"):
if len(command) == 3:
try:
self.moveT(float(command[1]), float(command[2]))
except ValueError:
print("Error, expected values could not turn into a float")
self.channel.basic_publish(exchange='', routing_key='audioInput', body="Error from the navigation AI, expected angle value could not turn into a float")
else:
print("Error, unexpected number of of commands")
self.channel.basic_publish(exchange='', routing_key='audioInput', body="Error from the navigation AI, unexpected number of commands")
else:
print("Error, unexpected command")
self.channel.basic_publish(exchange='', routing_key='audioInput', body="Error from the navigation AI, unexpected commands")
def turn(self, angle):
"""
Handles turn movements
Args:
angle (float): the angle to turn to in degrees
"""
turnTime = abs(float(angle)) / self.turnRate # calc how long to turn
if angle > 0: # turn right
# Left motors forward, right motors reverse
self.frontLeft.forward(self.maxSpeed)
self.backLeft.forward(self.maxSpeed)
self.frontRight.backward(self.maxSpeed)
self.backRight.backward(self.maxSpeed)
print(f"Turn right for {str(turnTime)} seconds")
time.sleep(turnTime)
# set speed to zero
self.frontLeft.forward(0)
self.backLeft.forward(0)
self.frontRight.forward(0)
self.backRight.forward(0)
else: # turn left
# Left motors reverse, right motors forward
self.frontLeft.backward(self.maxSpeed)
self.backLeft.backward(self.maxSpeed)
self.frontRight.forward(self.maxSpeed)
self.backRight.forward(self.maxSpeed)
print(f"Turn left for {str(turnTime)} seconds")
time.sleep(turnTime)
# set speed to zero
self.frontLeft.forward(0)
self.backLeft.forward(0)
self.frontRight.forward(0)
self.backRight.forward(0)
def moveD(self, angle, distance):
"""
Handles moving a given distance
Args:
angle (float): the angle to move at (0 for forward, 180 for reverse, anywhere in between)
distance (float): the distance to travel in feet
"""
# translate distance into time
moveTime = distance / self.moveRate
if -90 <= float(angle) <= 90:
self.moveForward(float(angle), moveTime)
else:
self.moveReverse(float(angle), moveTime)
def moveT(self, angle, moveTime):
"""
Handles moving a given time
Args:
angle (float): the angle to move at (0 for forward, 180 for reverse, anywhere in between)
moveTime (float): the time to travel in seconds
"""
if -90 <= float(angle) <= 90:
self.moveForward(float(angle), moveTime)
else:
self.moveReverse(float(angle), moveTime)
def moveReverse(self, angle, runtime):
"""
Handles moving the robot backwards at a given angle for a given time
Args:
angle (float): The angle to move at
runtime (float): the time to run
"""
# because of mecanum wheel design all movements are done in movement pairs across the four wheels
primary = self.maxSpeed
secondary = self.maxSpeed
angle = float(angle) # cast to float for math
# check to make sure angle is within valid range:
# check to make sure its not larger than 180 degrees
while angle > 180:
angle = angle - 360
while angle < -180:
angle = angle + 360
if -90 <= angle <= 90: # actually moving forward
self.moveForward(angle, runtime)
if angle > 0: # moving backwards in a right leaning angle
# motors frontLeft and rearRight move with secondary
# motors frontRight and rearLeft move with primary
if angle <= 135: # secondary motors move forward
secondary = secondary - (secondary * ((angle - 90) / 45))
# catch edge cases
if secondary > 1:
secondary = 1
if secondary < 0:
secondary = 0
# Reverse
#frontRight = primary
#rearLeft = primary
self.backLeft.backward(primary)
self.frontRight.backward(primary)
# Forward
#frontLeft = secondary
#rearRight = secondary
self.frontLeft.forward(secondary)
self.backRight.forward(secondary)
time.sleep(float(runtime))
# turn all motors off
self.frontLeft.forward(0)
self.backLeft.forward(0)
self.frontRight.forward(0)
self.backRight.forward(0)
print(f"Move Reverse, 90 to 135: P: {str(primary)} S: {str(secondary)}")
else: # secondary motors move in reverse
secondary = (secondary * ((angle - 135) / 45))
# catch edge cases
if secondary > 1:
secondary = 1
if secondary < 0:
secondary = 0
# Reverse
self.frontLeft.backward(secondary)
self.backLeft.backward(primary)
self.frontRight.backward(primary)
self.backRight.backward(secondary)
#frontRight = primary
#rearLeft = primary
#frontLeft = secondary
#rearRight = secondary
time.sleep(float(runtime))
# turn all motors off
self.frontLeft.forward(0)
self.backLeft.forward(0)
self.frontRight.forward(0)
self.backRight.forward(0)
print(f"Move Reverse, 135 to 180: P: {str(primary)} S: {str(secondary)}")
else: # moving backward in a left leaning direction
# motors frontLeft and rearRight move with primary
# motors frontRight and rearLeft move with secondary
if abs(angle) <= 135: # secondary motors move forward
secondary = secondary - (secondary * ((abs(angle) - 90) / 45))
# catch edge cases
if secondary > 1:
secondary = 1
if secondary < 0:
secondary = 0
# Forward
#frontRight = secondary
#rearLeft = secondary
self.backLeft.forward(secondary)
self.frontRight.forward(secondary)
# Reverse
#frontLeft = primary
#rearRight = primary
self.frontLeft.backward(primary)
self.backRight.backward(primary)
time.sleep(float(runtime))
# turn all motors off
self.frontLeft.forward(0)
self.backLeft.forward(0)
self.frontRight.forward(0)
self.backRight.forward(0)
print(f"Move Reverse, -90 to -135: P: {str(primary)} S: {str(secondary)}")
else: # secondary motors move in reverse
secondary = (secondary * ((abs(angle) - 135) / 45))
# catch edge cases
if secondary > 1:
secondary = 1
if secondary < 0:
secondary = 0
# Reverse
#frontRight = secondary
#rearLeft = secondary
#frontLeft = primary
#rearRight = primary
self.frontLeft.backward(primary)
self.backLeft.backward(secondary)
self.frontRight.backward(secondary)
self.backRight.backward(primary)
time.sleep(float(runtime))
# turn all motors off
self.frontLeft.forward(0)
self.backLeft.forward(0)
self.frontRight.forward(0)
self.backRight.forward(0)
print(f"Move Reverse, -135 to -180: P: {str(primary)} S: {str(secondary)}")
def moveForward(self, angle, runtime):
"""
Handles moving the robot forward at a given angle for a given time
Args:
angle (float): The angle to move at
runtime (float): the time to run
"""
# because of mecanum wheel design all movements are done in movement pairs across the four wheels
primary = self.maxSpeed
secondary = self.maxSpeed
angle = float(angle) # cast to float for math
if angle >= 0: # move forward in a right leaning angle
# motors frontLeft and rearRight move with primary
# motors frontRight and rearLeft move with secondary
if angle <= 45: # secondary motors moving forward
secondary = secondary - (secondary * (angle / 45))
# catch edge cases
if secondary > 1:
secondary = 1
if secondary < 0:
secondary = 0
# all forward
#frontLeft = primary
#rearRight = primary
#frontRight = secondary
#rearLeft = secondary
self.frontLeft.forward(primary)
self.backLeft.forward(secondary)
self.frontRight.forward(secondary)
self.backRight.forward(primary)
time.sleep(float(runtime))
# turn all motors off
self.frontLeft.forward(0)
self.backLeft.forward(0)
self.frontRight.forward(0)
self.backRight.forward(0)
print(f"Move Forward, 0 to 45: P: {str(primary)} S: {str(secondary)}")
elif angle <= 90: # secondary motors moving in reverse
secondary = (secondary * ((angle - 45) / 45))
# catch edge cases
if secondary > 1:
secondary = 1
if secondary < 0:
secondary = 0
# primary forward
#frontLeft = primary
#rearRight = primary
self.frontLeft.forward(primary)
self.backRight.forward(primary)
# secondary reverse
#frontRight = secondary
#rearLeft = secondary
self.backLeft.backward(secondary)
self.frontRight.backward(secondary)
time.sleep(float(runtime))
# turn all motors off
self.frontLeft.forward(0)
self.backLeft.forward(0)
self.frontRight.forward(0)
self.backRight.forward(0)
print(f"Move Forward, 45 to 90: P: {str(primary)} S: {str(secondary)}")
else:
# angle is greater than 90 degrees which means we're actually going in reverse
self.moveReverse(angle, runtime)
else: # move forward in a left leaning direction
# motors frontRight and rearLeft move with primary
# motors frontLeft and rearRight move with secondary
if angle >= -45: # secondary motors moving forward
secondary = secondary - (secondary * (abs(angle) / 45))
# catch edge cases
if secondary > 1:
secondary = 1
if secondary < 0:
secondary = 0
# all forward
#frontRight = primary
#rearLeft = primary
#frontLeft = secondary
#rearRight = secondary
self.frontLeft.forward(secondary)
self.backLeft.forward(primary)
self.frontRight.forward(primary)
self.backRight.forward(secondary)
time.sleep(float(runtime))
# turn all motors off
self.frontLeft.forward(0)
self.backLeft.forward(0)
self.frontRight.forward(0)
self.backRight.forward(0)
#print(f"Move Forward, -0 to -45: P: {str(primary)} S: {str(secondary)}")
elif angle >= -90: # secondary motors moving in reverse
secondary = (secondary * ((abs(angle) - 45) / 45))
# catch edge cases
if secondary > 1:
secondary = 1
if secondary < 0:
secondary = 0
# primary forward
#frontRight = primary
#rearLeft = primary
self.backLeft.forward(primary)
self.frontRight.forward(primary)
# secondary reverse
#frontLeft = secondary
#rearRight = secondary
self.frontLeft.backward(secondary)
self.backRight.backward(secondary)
time.sleep(float(runtime))
# turn all motors off
self.frontLeft.forward(0)
self.backLeft.forward(0)
self.frontRight.forward(0)
self.backRight.forward(0)
print(f"Move Forward, -45 to -90: P: {str(primary)} S: {str(secondary)}")
else:
# angle is greater than 90 degrees which means we're actually going in reverse
self.moveReverse(angle, runtime)
if __name__ == "__main__":
print("Running DriveAI")
driveAI = DriveAI(1)
driveAI.startListening()