-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robotkeyboardcontrols.py
92 lines (89 loc) · 2.54 KB
/
robotkeyboardcontrols.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
#a program to control a raspberry pi robot using a keyboard through vnc
import RPi.GPIO as GPIO # Import the GPIO Library
import time # Import the Time library
# Set the GPIO modes
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Set variables for the GPIO motor pins
pinMotorAForwards = 10
pinMotorABackwards = 9
pinMotorBForwards = 8
pinMotorBBackwards = 7
# Set the GPIO Pin mode
GPIO.setup(pinMotorAForwards, GPIO.OUT)
GPIO.setup(pinMotorABackwards, GPIO.OUT)
GPIO.setup(pinMotorBForwards, GPIO.OUT)
GPIO.setup(pinMotorBBackwards, GPIO.OUT)
# Turn all motors off
def StopMotors():
GPIO.output(pinMotorAForwards, 0)
GPIO.output(pinMotorABackwards, 0)
GPIO.output(pinMotorBForwards, 0)
GPIO.output(pinMotorBBackwards, 0)
# Turn both motors forwards
def Forwards():
GPIO.output(pinMotorAForwards, 1)
GPIO.output(pinMotorABackwards, 0)
GPIO.output(pinMotorBForwards, 1)
GPIO.output(pinMotorBBackwards, 0)
# Turn both motors backwards
def Backwards():
GPIO.output(pinMotorAForwards, 0)
GPIO.output(pinMotorABackwards, 1)
GPIO.output(pinMotorBForwards, 0)
GPIO.output(pinMotorBBackwards, 1)
def Left():
GPIO.output(pinMotorAForwards, 1)
GPIO.output(pinMotorABackwards, 0)
GPIO.output(pinMotorBForwards, 0)
GPIO.output(pinMotorBBackwards, 1)
def Right():
GPIO.output(pinMotorAForwards, 0)
GPIO.output(pinMotorABackwards, 1)
GPIO.output(pinMotorBForwards, 1)
GPIO.output(pinMotorBBackwards, 0)
import sys, termios, tty, os
def getch():
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
PIN_LED = 25
GPIO.setup(PIN_LED, GPIO.OUT)
GPIO.output(PIN_LED, 0)
button_delay = 0.2
for x in range(0,3):
GPIO.output(PIN_LED, 1)
time.sleep(0.25)
GPIO.output(PIN_LED, 0)
time.sleep(0.25)
while True:
char = getch()
if (char == "q"):
StopMotors()
exit(0)
if (char == "a"):
print 'Left pressed'
Left()
time.sleep(button_delay)
StopMotors()
if (char == "d"):
print 'Right pressed'
Right()
time.sleep(button_delay)
StopMotors()
elif (char == "w"):
print 'Up pressed'
Forwards()
time.sleep(button_delay)
StopMotors()
elif (char == "s"):
print 'Down pressed'
Backwards()
time.sleep(button_delay)
StopMotors()
StopMotors()