-
Notifications
You must be signed in to change notification settings - Fork 0
/
car_test2.py
77 lines (67 loc) · 1.75 KB
/
car_test2.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
from gpiozero import Motor
import curses
#flmotor = Motor(forward=16, backward=17)
#frmotor = Motor(forward=18, backward=13)
#blmotor = Motor(forward=9, backward=11)
#brmotor = Motor(forward=10, backward=12)
flmotor = Motor(forward=9, backward=10)
frmotor = Motor(forward=11, backward=12)
blmotor = Motor(forward=16, backward=17)
brmotor = Motor(forward=13, backward=18)
def left():
# print('Left ...')
flmotor.forward()
frmotor.backward()
blmotor.backward()
brmotor.forward()
def right():
# print('Right ...')
flmotor.backward()
frmotor.forward()
blmotor.forward()
brmotor.backward()
def forward():
# print('Forwarding ...')
flmotor.forward()
frmotor.forward()
blmotor.forward()
brmotor.forward()
def reverse():
# print('Reversing ...')
flmotor.backward()
frmotor.backward()
blmotor.backward()
brmotor.backward()
def stop():
# print('Stopping ...')
flmotor.stop()
frmotor.stop()
blmotor.stop()
brmotor.stop()
actions = {
curses.KEY_UP: forward,
curses.KEY_DOWN: reverse,
curses.KEY_LEFT: left,
curses.KEY_RIGHT: right,
}
def main(window):
next_key = None
while True:
curses.halfdelay(1)
if next_key is None:
key = window.getch()
else:
key = next_key
next_key = None
if key != -1:
# KEY PRESSED
curses.halfdelay(3)
action = actions.get(key)
if action is not None:
action()
next_key = key
while next_key == key:
next_key = window.getch()
# KEY RELEASED
stop()
curses.wrapper(main)