-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminimum_robot.py
More file actions
82 lines (55 loc) · 1.93 KB
/
minimum_robot.py
File metadata and controls
82 lines (55 loc) · 1.93 KB
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
#!/usr/bin/python
#encoding:utf-8
#Tutorial: http://www.knight-of-pi.org/build-a-basic-raspberry-pi-robot-in-no-time-with-the-raspirobotboard-v3-hat/
#Licence: http://creativecommons.org/licenses/by-nc-sa/3.0/
# Author: Johannes Bergs
import time
from random import choice, random
import subprocess
from rrb3 import *
# the robotboard needs to be created as global for using the on/off button
# the parameters are 9V input voltage and 6V for the motors
robotboard = RRB3(9, 6)
class RaspiRobot:
def run(self):
""" Life cycle of the RaspiRobot. """
try:
while True:
if robotboard.sw1_closed() == True:
turn_off()
print("Running...")
distance = robotboard.get_distance()
print("Distance: ", distance)
if distance <= 10.0:
self.avert_collision()
robotboard.forward()
time.sleep(0.1)
except:
print('Quitting...')
GPIO.cleanup()
def avert_collision(self):
""" Avert collision by moving backward for a second and then
turning either left or right for maximally 1 second. """
print('Averting collision...')
robotboard.reverse(1)
turn_time = random()
if choice(['left', 'right']) == 'left':
robotboard.left(turn_time)
else:
robotboard.right(turn_time)
robotboard.forward()
def turn_off():
""" Shutdown the Raspberry Pi """
print("Shutting down...")
GPIO.cleanup()
subprocess.call(['echo posys | sudo -S poweroff'], shell=True)
if __name__ == '__main__':
while True:
print "Waiting for button press to start..."
if robotboard.sw1_closed() == True:
break
time.sleep(0.02)
# let the pushbutton become unpressed
time.sleep(0.5)
raspirobot = RaspiRobot()
raspirobot.run()