-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanual.py
80 lines (65 loc) · 2.19 KB
/
manual.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
"""Manual remote control for the Grasshopper Tamiya."""
import json
import signal
import socket
import sys
from control.driver import Driver
from control.test.dummy_logger import DummyLogger
from control.test.dummy_telemetry import DummyTelemetry
driver = None
def signal_handler(signal_number, frame):
# Don't exit on resizing the terminal
if signal_number == signal.SIGWINCH:
return
# Anything else, quit
global driver
driver.drive(0.0, 0.0)
sys.exit(0)
def main():
"""Main function."""
signal.signal(signal.SIGWINCH, signal_handler)
# First, shut the damn car up
throttle_percentage = 0.0
# And reset the steering
steering_percentage = 0.0
logger = DummyLogger()
telemetry = DummyTelemetry(logger, (40.0182663, -105.2761267))
global driver
driver = Driver(telemetry, logger)
socket_ = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socket_.bind(('', 12345))
socket_.settimeout(2)
try:
data = None
while True:
print(
'Throttle: {}, steering: {}'.format(
throttle_percentage,
steering_percentage
)
)
try:
data, address = socket_.recvfrom(1024) # pylint: disable=unused-variable
command = json.loads(data.decode())
except ValueError as value_error:
print('Unable to parse JSON {}: {}'.format(data, value_error))
continue
except:
print('Timed out')
throttle_percentage = 0.0
steering_percentage = 0.0
command = {}
if 'quit' in command:
break
if 'throttle' in command:
throttle_percentage = float(command['throttle'])
if 'steering' in command:
steering_percentage = float(command['steering'])
driver.drive(throttle_percentage, steering_percentage)
except Exception as exc: # pylint: disable=broad-except
print('Exception: {}'.format(exc))
finally:
driver.drive(0.0, 0.0)
if __name__ == '__main__':
for i in range(10):
main()