-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrc.py
200 lines (172 loc) · 4.13 KB
/
rc.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
import curses
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor
import atexit
import ultrasonic as u
import time
mh = Adafruit_MotorHAT(addr=0x60)
ul = []
last = None
U = ord('u')
W = ord('w')
A = ord('a')
S = ord('s')
D = ord('d')
Q = ord('q')
E = ord('e')
Z = ord('z')
X = ord('x')
C = ord('c')
F = ord('f')
G = ord('g')
V = ord('v')
B = ord('b')
ONE = ord('1')
TWO = ord('2')
THREE = ord('3')
FOUR = ord('4')
FIVE = ord('5')
def print(*args):
pass
right = mh.getMotor(1)
left = mh.getMotor(2)
speed = 100
def turnOffMotors():
# pass
left.run(Adafruit_MotorHAT.RELEASE)
right.run(Adafruit_MotorHAT.RELEASE)
def forward(speed):
print('forward')
left.run(Adafruit_MotorHAT.FORWARD)
right.run(Adafruit_MotorHAT.FORWARD)
left.setSpeed(speed)
right.setSpeed(speed)
def backward(speed):
print('backward')
left.run(Adafruit_MotorHAT.BACKWARD)
right.run(Adafruit_MotorHAT.BACKWARD)
left.setSpeed(speed)
right.setSpeed(speed)
def turnleft(speed):
print('left')
speed = 75
left.run(Adafruit_MotorHAT.BACKWARD)
right.run(Adafruit_MotorHAT.FORWARD)
left.setSpeed(speed)
right.setSpeed(speed)
def turnright(speed):
print('right')
speed = 75
left.run(Adafruit_MotorHAT.FORWARD)
right.run(Adafruit_MotorHAT.BACKWARD)
left.setSpeed(speed)
right.setSpeed(speed)
def forright(speed):
s2 = int(0.75 * speed)
left.run(Adafruit_MotorHAT.FORWARD)
right.run(Adafruit_MotorHAT.FORWARD)
left.setSpeed(speed)
right.setSpeed(s2)
def forleft(speed):
s2 = int(0.75 * speed)
left.run(Adafruit_MotorHAT.FORWARD)
right.run(Adafruit_MotorHAT.FORWARD)
left.setSpeed(s2)
right.setSpeed(speed)
def for2right(speed):
s2 = int(0.25 * speed)
left.run(Adafruit_MotorHAT.FORWARD)
right.run(Adafruit_MotorHAT.FORWARD)
left.setSpeed(speed)
right.setSpeed(s2)
def for2left(speed):
s2 = int(0.25 * speed)
left.run(Adafruit_MotorHAT.FORWARD)
right.run(Adafruit_MotorHAT.FORWARD)
left.setSpeed(s2)
right.setSpeed(speed)
def smallLeft():
turnleft(51)
time.sleep(.1)
turnOffMotors()
def smallForward():
forward(51)
time.sleep(.1)
turnOffMotors()
def smallRight():
turnright(51)
time.sleep(.1)
turnOffMotors()
def ultra(scr):
global last
fit = lambda x: '{:1.3f}'.format(x)
x = str(list(map(fit, u.all())))
if '3.000' in x or '0.000' in x:
ul.append(last + ' l')
ul.append(x)
last = x
scr.addstr(4, 10, x)
scr.refresh()
def stop():
print('stop')
left.run(Adafruit_MotorHAT.BACKWARD)
right.run(Adafruit_MotorHAT.FORWARD)
left.setSpeed(0)
right.setSpeed(0)
def main():
atexit.register(curses.endwin)
scr = curses.initscr()
curses.cbreak()
curses.setsyx(-1, -1)
# scr.keypad(1)
scr.addstr(0, 10, 'debug_control.py: the ultimate boxbot controller')
scr.addstr(2, 10, 'Press Q to exit.')
scr.refresh()
speed = 100
while True:
key = scr.getch()
if key == Q:
turnOffMotors()
with open('ultra_out.txt', 'w') as f:
for i in ul:
f.write(i)
f.write('\n')
break
elif key == W:
forward(speed)
elif key == S:
backward(speed)
elif key == V:
turnleft(speed)
elif key == B:
turnright(speed)
elif key == Z:
smallLeft()
elif key == X:
smallForward()
elif key == C:
smallRight()
elif key == F:
forleft(speed)
elif key == G:
forright(speed)
elif key == A:
for2left(speed)
elif key == D:
for2right(speed)
elif key == E:
stop()
elif key == ONE:
speed = 51
elif key == TWO:
speed = 51*2
elif key == THREE:
speed = 51*3
elif key == FOUR:
speed = 51*4
elif key == U:
ultra(scr)
else:
print("not a valid key")
curses.endwin()
if __name__ == '__main__':
main()