-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.py
38 lines (33 loc) · 1.11 KB
/
serial.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
import serial
class SerialController():
def __init__(self):
self.conn = serial.Serial(
'/dev/ttyS0',
baudrate=9600
)
self.steering = 0.0
self.throttle = 0.0
self.mode = 'user'
# Called once, in a background thread, when the car has started.
def update(self):
while True:
line = self.conn.readline().strip()
if len(line) == 0:
continue
try:
[key, val] = line.split(b':')
if key == b'0':
self.throttle = float(val)
if key == b'1':
self.steering = float(val)
if key == b'2':
if val == b'1':
self.mode = 'auto'
else:
self.mode = 'user'
print('Switching mode to {0}'.format(self.mode))
except ValueError:
pass
# Called repeated in the main thread to read outputs.
def run_threaded(self):
return self.steering, self.throttle, self.mode