-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrive3.py
More file actions
121 lines (101 loc) · 3.22 KB
/
drive3.py
File metadata and controls
121 lines (101 loc) · 3.22 KB
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
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))
import asyncio
from helper_keyboard_input import KeyboardHelper
from sphero_sdk import SerialAsyncDal
from sphero_sdk import SpheroRvrAsync
# initialize global variables
key_helper = KeyboardHelper()
current_key_code = -1
driving_keys = [119, 97, 115, 100, 32]
speed = 0
heading = 0
flags = 0
loop = asyncio.get_event_loop()
rvr = SpheroRvrAsync(
dal=SerialAsyncDal(
loop
)
)
def keycode_callback(keycode):
global current_key_code
current_key_code = keycode
print("Key code updated: ", str(current_key_code))
async def main():
"""
Runs the main control loop for this demo. Uses the KeyboardHelper class to read a keypress from the terminal.
W - Go forward. Press multiple times to increase speed.
A - Decrease heading by -10 degrees with each key press.
S - Go reverse. Press multiple times to increase speed.
D - Increase heading by +10 degrees with each key press.
Spacebar - Reset speed and flags to 0. RVR will coast to a stop
"""
global current_key_code
global speed
global heading
global flags
await rvr.wake()
await rvr.reset_yaw()
while True:
if current_key_code == 119: # W
# if previously going reverse, reset speed back to 64
if flags == 1:
speed = 64
else:
# else increase speed
speed += 64
# go forward
flags = 0
elif current_key_code == 97: # A
heading -= 10
elif current_key_code == 115: # S
# if previously going forward, reset speed back to 64
if flags == 0:
speed = 64
else:
# else increase speed
speed += 64
# go reverse
flags = 1
elif current_key_code == 100: # D
heading += 10
elif current_key_code == 32 or current_key_code == -1: # SPACE
# reset speed and flags, but don't modify heading.
speed = 0
flags = 0
# check the speed value, and wrap as necessary.
if speed > 255:
speed = 255
elif speed < -255:
speed = -255
# check the heading value, and wrap as necessary.
if heading > 359:
heading = heading - 359
elif heading < 0:
heading = 359 + heading
# reset the key code every loop
current_key_code = -1
# issue the driving command
await rvr.drive_with_heading(speed, heading, flags)
# sleep the infinite loop for a 10th of a second to avoid flooding the serial port.
await asyncio.sleep(0.05)
def run_loop():
global loop
global key_helper
key_helper.set_callback(keycode_callback)
loop.run_until_complete(
asyncio.gather(
main()
)
)
if __name__ == "__main__":
loop.run_in_executor(None, key_helper.get_key_continuous)
try:
run_loop()
except KeyboardInterrupt:
print("Keyboard Interrupt...")
key_helper.end_get_key_continuous()
finally:
print("Press any key to exit.")
exit(1)