forked from RedRobotics/RedBoard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard_control.py
153 lines (119 loc) · 3.69 KB
/
keyboard_control.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
# Control a robot from the terminal using your computers keyboard.
# Author: Neil Lambeth. [email protected] @NeilRedRobotics
from __future__ import print_function # Make print work with python 2 & 3
import curses
import time
import redboard
stdscr = curses.initscr()
curses.noecho()
stdscr.keypad(True)
print("\r")
print("W = Forwards\r")
print("S = Backwards\r")
print("A = Left\r")
print("D = Right\r")
print("Spacebar = Stop\r")
print("T = Turbo\r")
print("R = Reverse Steering\r")
time.sleep(3)
def main(stdscr):
motor1 = 0
motor2 = 0
turbo = False
rSteer = False
keypress = 0
up = 0
stop = 0
while True:
curses.halfdelay(1)
c = stdscr.getch()
#print(c) # Uncomment to show keypresses
#print("\r")
if c == ord('w'):
keypress = 1
print("Forward")
print("\r")
stop = 0
motor1 = 100 # Set motor speed and direction
motor2 = 100 # Set motor speed and direction
elif c == ord('s'):
keypress = 1
stop = 0
print("Backwards")
print("\r")
motor1 = -100
motor2 = -100
elif c == ord('a'):
keypress = 1
stop = 0
print("Left")
print("\r")
motor1 = 100
motor2 = -100
elif c == ord('d'):
keypress = 1
stop = 0
print("Right")
print("\r")
motor1 = -100
motor2 = 100
elif c == 32: # Spacebar
keypress = 1
stop = 0
print("Stop")
print("\r")
motor1 = 0
motor2 = 0
elif c == ord('r') and rSteer == False:
keypress = 1
stop = 0
rSteer = True
print ("Reverse Steering")
print("\r")
elif c == ord('r') and rSteer == True:
keypress = 1
stop = 0
rSteer = False
print ("Normal Steering")
print("\r")
elif c == ord('t') and turbo == False:
keypress = 1
stop = 0
turbo = True
print ("Turbo on")
print("\r")
elif c == ord('t') and turbo == True:
keypress = 1
stop = 0
turbo = False
print ("Turbo off")
print("\r")
# Pressing a key also produces a number of key up events.
# This block of code only stops the robot moving after at least 4 key up events have been detected.
# This makes driving the robot smoother but adds a short delay-
# -from when you release the key until the robot stops.
if c == -1: # Check for key release
stop += 1 # Count the number of key up events
if keypress == 1 and stop > 5: # Min = 4 - If the robot pauses when you hold a key down-
# -increase this number.
keypress = 0
stop = 0
print("Stop----------------------------------------")
print("\r")
motor1 = 0
motor2 = 0
# Half the speed if Turbo is off
if turbo == True:
m1 = motor1
m2 = motor2
elif turbo == False:
m1 = motor1 / 2
m2 = motor2 / 2
# Reverse the steering if 'R' has been pressed
if rSteer == False:
redboard.M1(m1)
redboard.M2(m2)
else:
redboard.M1(m2)
redboard.M2(m1)
curses.wrapper(main)