-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwirus.py
executable file
·560 lines (438 loc) · 17.8 KB
/
wirus.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import sys
import signal
import logging
import config
from threading import Thread
import ultrasonic_senor
from Adafruit_PWM_Servo_Driver import PWM
import termios
import tty
import L298NHBridge as HBridge
import os
import speech
class Wirus:
def __init__(self):
self.pwm = PWM(0x40)
self.moving = False
self.reverse = False
self.left_motor_speed = 0
self.right_motor_speed = 0
self.moving = False
self.current_step = 0
self.default_speed_power = config.DEFAULT_SPEED
self.left_motor_speed_handicap = 0
self.right_motor_speed_handicap = 0
self.time_of_last_wheel_check = time.time()
self.time_of_last_turn = time.time()
self.time_of_last_stuck = time.time()
self.speed_left = 0
self.speed_right = 0
self.last_turn_right = True
self.left_motor_rotation = 0
self.right_motor_rotation = 0
self.right_motor_rotation_per_min = 0
self.left_motor_rotation_per_min = 0
self.current_distance = 100
self.wheel_check_thread = Thread(target=self.wheel_check_thread_function, args=())
self.keyboard_thread = Thread(target=self.keyboard_thread_function, args=())
self.autonomous_thread = Thread(target=self.autonomous, args=())
self.servo_pos = config.SERVO_HALF
self.keyboard_check_running = False
self.wheel_check_running = False
self.autonomous_thread_running = False
self.autonomous_thread_pause = False
self.printscreen()
self.setup()
time.sleep(1)
self.start_wheel_check_thread()
self.start_keyboard_thread()
self.start_autonomous_thread()
def setup(self):
signal.signal(signal.SIGINT, self.signal_handler)
logging.basicConfig(filename=config.LOG_PATH, format=config.LOG_FORMAT, level=config.LOG_LEVEL)
GPIO.setmode(GPIO.BCM)
self.pwm.setPWMFreq(config.PWM_FREQUENCY)
self.pwm.setPWM(config.PWM_DISTANCE_SERVO_CHANNEL, 0, config.SERVO_HALF)
GPIO.setup(config.LEFT_MOTOR_ENCODER, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(config.RIGHT_MOTOR_ENCODER, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(config.LEFT_MOTOR_ENCODER, GPIO.RISING, callback=self.left_motor_encoder_callback)
GPIO.add_event_detect(config.RIGHT_MOTOR_ENCODER, GPIO.RISING, callback=self.right_motor_encoder_callback)
self.set_motor_speed(config.DEFAULT_SPEED)
def get_unstuck(self):
# if self.get_time_since_last_turn() < TIME_SINCE_LAST_TURN_THRESHOLD and not self.last_turn_right:
self.backward(config.BACKWARD_TIME * 2)
self.rotate_right(1)
# else:
# self.forward(1)
# self.rotate_left(1)
def signal_handler(self, signal, frame):
logging.info("clean up")
GPIO.cleanup()
self.exit()
def exit(self):
self.wheel_check_running = False
self.keyboard_check_running = False
self.autonomous_thread_running = False
try:
self.autonomous_thread.join()
self.keyboard_thread.join()
self.wheel_check_thread.join()
except RuntimeError:
pass
GPIO.cleanup()
sys.exit()
def start_wheel_check_thread(self):
self.wheel_check_running = True
self.wheel_check_thread.start()
def start_keyboard_thread(self):
self.keyboard_check_running = True
self.keyboard_thread.start()
def keyboard_thread_function(self):
while self.keyboard_check_running:
self.keyboard_check()
def wheel_check_thread_function(self):
while self.wheel_check_running:
if self.autonomous_thread_pause:
time.sleep(.1)
continue
self.wheel_check()
def keyboard_check(self):
while True:
char = self.getch()
if char == "x" or char == "z":
self.exit()
elif char == "q":
self.stop()
elif char == "w":
self.forwards()
elif char == "s":
self.backwards()
elif char == "d":
self.right()
elif char == "a":
self.left()
elif char == "i":
self.sensor_left()
elif char == "o":
self.sensor_center()
elif char == "p":
self.sensor_right()
elif char == "e" and self.autonomous_thread_pause:
self.start_autonomous()
elif char == "e":
self.pause_autonomous()
elif char == "r":
speech.say_something(True)
def forwards(self):
if not self.autonomous_thread_pause:
self.pause_autonomous()
if self.speed_left > self.speed_right:
self.speed_right = self.speed_left
else:
self.speed_left = self.speed_right
self.speed_left += 0.1
self.speed_right += 0.1
if self.speed_left > 1:
self.speed_left = 1
if self.speed_right > 1:
self.speed_right = 1
HBridge.setMotorLeft(self.speed_left)
HBridge.setMotorRight(self.speed_right)
self.printscreen()
def backwards(self):
if not self.autonomous_thread_pause:
self.pause_autonomous()
if self.speed_left < self.speed_right:
self.speed_right = self.speed_left
else:
self.speed_left = self.speed_right
self.speed_left -= 0.1
self.speed_right -= 0.1
if self.speed_left < -1:
self.speed_left = -1
if self.speed_right < -1:
self.speed_right = -1
HBridge.setMotorLeft(self.speed_left)
HBridge.setMotorRight(self.speed_right)
self.printscreen()
def right(self):
if not self.autonomous_thread_pause:
self.pause_autonomous()
self.speed_right -= 0.1
self.speed_left += 0.1
if self.speed_right < -1:
self.speed_right = -1
if self.speed_left > 1:
self.speed_left = 1
HBridge.setMotorLeft(self.speed_left)
HBridge.setMotorRight(self.speed_right)
self.printscreen()
def left(self):
if not self.autonomous_thread_pause:
self.pause_autonomous()
self.speed_left -= 0.1
self.speed_right += 0.1
if self.speed_left < -1:
self.speed_left = -1
if self.speed_right > 1:
self.speed_right = 1
HBridge.setMotorLeft(self.speed_left)
HBridge.setMotorRight(self.speed_right)
self.printscreen()
def stop(self):
if not self.autonomous_thread_pause:
self.pause_autonomous()
self.speed_left = 0
self.speed_right = 0
HBridge.setMotorLeft(0)
HBridge.setMotorRight(0)
self.printscreen()
def sensor_right(self):
if not self.autonomous_thread_pause:
self.pause_autonomous()
self.set_servo(config.STEP_POS[0])
self.current_distance = self.get_top_distance()
self.printscreen()
def sensor_left(self):
if not self.autonomous_thread_pause:
self.pause_autonomous()
self.set_servo(config.STEP_POS[4])
self.current_distance = self.get_top_distance()
self.printscreen()
def sensor_center(self):
if not self.autonomous_thread_pause:
self.pause_autonomous()
self.set_servo(config.STEP_POS[2])
self.current_distance = self.get_top_distance()
self.printscreen()
def start_autonomous(self):
logging.info("Start autonomous mode")
self.stop()
self.start_forward()
self.autonomous_thread_pause = False
self.printscreen()
def pause_autonomous(self):
logging.info("Pause autonomous mode")
self.stop_forward()
self.autonomous_thread_pause = True
self.printscreen()
def start_autonomous_thread(self):
self.autonomous_thread_running = True
self.autonomous_thread.start()
def autonomous(self):
speech.speak("I am Humphrey!")
while self.autonomous_thread_running:
if self.autonomous_thread_pause:
time.sleep(.1)
self.time_of_last_stuck = time.time()
continue
self.start_forward()
if config.SERVO_ON:
servo = self.next_step()
self.set_servo(servo)
self.current_distance = self.get_top_distance()
if self.object_detected(self.current_distance, servo):
self.stop_forward()
self.backward(config.BACKWARD_TIME)
self.auto_turn(servo)
elif self.is_robot_stuck():
self.stop_forward()
self.get_unstuck()
speech.say_something()
def getch(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def wheel_check(self):
logging.debug("running wheel check")
self.left_motor_rotation_per_min = self.left_motor_rotation * 120
self.left_motor_rotation = 0
self.right_motor_rotation_per_min = self.right_motor_rotation * 120
self.right_motor_rotation = 0
logging.debug("left wheel rotations: %d" % self.left_motor_rotation_per_min)
logging.debug("right wheel rotations: %d" % self.right_motor_rotation_per_min)
self.time_of_last_wheel_check = time.time()
self.balance_wheel_speed()
time.sleep(.5)
def left_motor_encoder_callback(self, channel):
self.left_motor_rotation += 1
def right_motor_encoder_callback(self, channel):
self.right_motor_rotation += 1
def get_time_since_last_turn(self):
logging.debug("get time since last turn")
return time.time() - self.time_of_last_turn
def next_step(self):
if self.current_step == 0:
self.reverse = False
elif self.current_step >= len(config.STEP_POS) - 1:
self.reverse = True
if self.reverse:
self.current_step -= 1
else:
self.current_step += 1
return config.STEP_POS[self.current_step]
def rotate_left(self, secs):
logging.debug("rotate left: %d", secs)
old_left_speed = self.left_motor_speed
old_right_speed = self.right_motor_speed
self.set_motor_speed(config.ROTATE_SPEED)
GPIO.setup(config.RIGHT_MOTOR_FORWARD, GPIO.OUT)
GPIO.setup(config.LEFT_MOTOR_BACKWARD, GPIO.OUT)
GPIO.output(config.RIGHT_MOTOR_FORWARD, True)
GPIO.output(config.LEFT_MOTOR_BACKWARD, True)
time.sleep(secs)
GPIO.output(config.RIGHT_MOTOR_FORWARD, False)
GPIO.output(config.LEFT_MOTOR_BACKWARD, False)
self.set_left_motor_speed(old_left_speed)
self.set_right_motor_speed(old_right_speed)
self.time_of_last_turn = time.time()
self.last_turn_right = False
def rotate_right(self, secs):
logging.debug("rotate right: %d", secs)
old_left_speed = self.left_motor_speed
old_right_speed = self.right_motor_speed
self.set_motor_speed(config.ROTATE_SPEED)
GPIO.setup(config.RIGHT_MOTOR_BACKWARD, GPIO.OUT)
GPIO.setup(config.LEFT_MOTOR_FORWARD, GPIO.OUT)
GPIO.output(config.RIGHT_MOTOR_BACKWARD, True)
GPIO.output(config.LEFT_MOTOR_FORWARD, True)
time.sleep(secs)
GPIO.output(config.RIGHT_MOTOR_BACKWARD, False)
GPIO.output(config.LEFT_MOTOR_FORWARD, False)
self.set_left_motor_speed(old_left_speed)
self.set_right_motor_speed(old_right_speed)
self.time_of_last_turn = time.time()
self.last_turn_right = True
def forward(self, secs):
logging.debug("move forwards: %d", secs)
GPIO.setup(config.LEFT_MOTOR_FORWARD, GPIO.OUT)
GPIO.setup(config.RIGHT_MOTOR_FORWARD, GPIO.OUT)
GPIO.output(config.LEFT_MOTOR_FORWARD, True)
GPIO.output(config.RIGHT_MOTOR_FORWARD, True)
if secs > 0:
time.sleep(secs)
GPIO.output(config.LEFT_MOTOR_FORWARD, False)
GPIO.output(config.RIGHT_MOTOR_FORWARD, False)
def backward(self, secs):
logging.debug("move backwards: %d", secs)
GPIO.setup(config.LEFT_MOTOR_BACKWARD, GPIO.OUT)
GPIO.setup(config.RIGHT_MOTOR_BACKWARD, GPIO.OUT)
GPIO.output(config.LEFT_MOTOR_BACKWARD, True)
GPIO.output(config.RIGHT_MOTOR_BACKWARD, True)
time.sleep(secs)
GPIO.output(config.LEFT_MOTOR_BACKWARD, False)
GPIO.output(config.RIGHT_MOTOR_BACKWARD, False)
def object_detected(self, distance, servo_position):
logging.debug("detecting object")
quarter_amount = (config.SERVO_MAX - config.SERVO_MIN) / 4
bottom_limit = config.SERVO_HALF - quarter_amount
top_limit = config.SERVO_HALF + quarter_amount
if servo_position <= top_limit >= bottom_limit:
if distance <= config.DISTANCE_THRESHOLD:
logging.info("detected object in front: %d", distance)
return True
elif distance <= config.SIDE_DISTANCE_THRESHOLD:
logging.info("detected object on side: %d", distance)
return True
return False
def set_motor_speed(self, speed):
self.set_left_motor_speed(speed)
self.set_right_motor_speed(speed)
def set_left_motor_speed(self, speed):
if self.left_motor_speed != speed and (100 >= speed > 0):
logging.debug("set left speed: %d", speed)
self.pwm.setPWM(config.PWM_LEFT_MOTOR_SPEED_CHANNEL, 0, int(speed * 40))
self.left_motor_speed = speed
def set_right_motor_speed(self, speed):
if self.right_motor_speed != speed and (100 >= speed > 0):
logging.debug("set right speed: %d", speed)
self.pwm.setPWM(config.PWM_RIGHT_MOTOR_SPEED_CHANNEL, 0, int(speed * 40))
self.right_motor_speed = speed
def distance_to_speed(self, speed):
logging.debug("distance to speed setting: %d", speed)
if speed < 80:
out_speed = 50
else:
out_speed = 100
return out_speed
def get_top_distance(self):
logging.debug("getting distance")
value = ultrasonic_senor.Measurement(config.TRIG, config.ECHO)
raw_measurement = value.raw_distance(config.SAMPLE_SIZE, config.SAMPLE_WAIT)
distance = int(value.distance_metric(raw_measurement))
logging.debug("TOP sensor distance %d", distance)
return distance
def stop_forward(self):
if self.moving:
logging.debug("stop forward movement")
self.moving = False
GPIO.output(config.LEFT_MOTOR_FORWARD, False)
GPIO.output(config.RIGHT_MOTOR_FORWARD, False)
def start_forward(self):
if not self.moving:
logging.debug("start forward movement")
self.moving = True
GPIO.setup(config.LEFT_MOTOR_FORWARD, GPIO.OUT)
GPIO.setup(config.RIGHT_MOTOR_FORWARD, GPIO.OUT)
GPIO.output(config.LEFT_MOTOR_FORWARD, True)
GPIO.output(config.RIGHT_MOTOR_FORWARD, True)
def is_robot_stuck(self):
if time.time() - self.time_of_last_stuck > config.WHEEL_MOVEMENT_STUCK_TIME:
if self.left_motor_rotation_per_min < config.WHEEL_MOVEMENT_STUCK_THRESHOLD \
or self.right_motor_rotation_per_min < config.WHEEL_MOVEMENT_STUCK_THRESHOLD:
logging.error("robot stuck")
self.time_of_last_stuck = time.time()
return True
return False
def balance_wheel_speed(self):
if self.left_motor_rotation_per_min > self.right_motor_rotation_per_min:
left_speed = self.left_motor_speed - 1
right_speed = self.right_motor_speed + 1
self.set_left_motor_speed(left_speed)
self.set_right_motor_speed(right_speed)
elif self.right_motor_rotation_per_min > self.left_motor_rotation_per_min:
left_speed = self.left_motor_speed + 1
right_speed = self.right_motor_speed - 1
self.set_left_motor_speed(left_speed)
self.set_right_motor_speed(right_speed)
def auto_turn(self, servo):
logging.debug("auto turning")
if servo >= config.SERVO_HALF:
turn_right = True
else:
turn_right = False
if self.get_time_since_last_turn() < config.TIME_SINCE_LAST_TURN_THRESHOLD \
and self.last_turn_right != turn_right:
self.rotate_left(config.ROTATE_TIME)
elif turn_right:
self.rotate_right(config.ROTATE_TIME)
else:
self.rotate_left(config.ROTATE_TIME)
def set_servo(self, servo):
logging.debug("set servo: %d", servo)
self.servo_pos = servo
self.pwm.setPWM(config.PWM_DISTANCE_SERVO_CHANNEL, 0, servo)
def printscreen(self):
# Print the motor speed just for interest
os.system('clear')
if not self.autonomous_thread_pause:
print "******* autonomous mode ********"
print ""
print("i/o/p: distance sensor position: ", self.servo_pos, ", distance: ", self.current_distance)
print("w/s: direction")
print("a/d: steering")
print("q: stops the motors")
print("e: autonomous mode")
print("r: say something")
print("x: exit")
print("========== Speed Control ==========")
print "left motor: ", self.speed_left
print "right motor: ", self.speed_right