-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrive1.py
More file actions
97 lines (78 loc) · 2.36 KB
/
drive1.py
File metadata and controls
97 lines (78 loc) · 2.36 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
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))
import asyncio
import keyboard
from helper_keyboard_input import KeyboardHelper
from sphero_sdk import SerialAsyncDal
from sphero_sdk import SpheroRvrAsync
# initialize global variables
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))
def key_commands(self, e):
if str(e) == 'KeyboardEvent(w down)':
print("Got key release event: " + str(e))
speed = 65
flag = 0
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()
print("Sphero team sux at python")
while True:
# 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.1)
def run_loop():
global loop
keyboard.hook(self.key_command)
loop.run_until_complete(
asyncio.gather(
main()
)
)
if __name__ == "__main__":
try:
run_loop()
except KeyboardInterrupt:
print("Keyboard Interrupt...")
finally:
print("Press any key to exit.")
exit(1)