forked from simonmonk/raspberrypi_cookbook_ed2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rover.py
67 lines (58 loc) · 1.51 KB
/
rover.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
# 08_manual_robot.py
# Use the arrow keys to direct the robot
from rrb3 import *
import sys
import tty
import termios
rr = RRB3(9.0, 6.0) # battery, motor
UP = 0
DOWN = 1
RIGHT = 2
LEFT = 3
print("Use the arrow keys to move the robot")
print("Press CTRL-c to quit the program")
# These functions allow the program to read your keyboard
def readchar():
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)
if ch == '0x03':
raise KeyboardInterrupt
return ch
def readkey(getchar_fn=None):
getchar = getchar_fn or readchar
c1 = getchar()
if ord(c1) != 0x1b:
return c1
c2 = getchar()
if ord(c2) != 0x5b:
return c1
c3 = getchar()
return ord(c3) - 65 # 0=Up, 1=Down, 2=Right, 3=Left arrows
# This will control the movement of your robot and display on your screen
try:
while True:
keyp = readkey()
if keyp == UP:
rr.forward(1)
print 'forward'
elif keyp == DOWN:
rr.reverse(1)
print 'backward'
elif keyp == RIGHT:
rr.right(1)
print 'clockwise'
elif keyp == LEFT:
rr.left(1)
print 'anti clockwise'
elif keyp == LEFT:
rr.left(1)
print 'anti clockwise'
elif ord(keyp) == 3:
break
except KeyboardInterrupt:
GPIO.cleanup()