-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_thread.py
40 lines (34 loc) · 1.36 KB
/
arduino_thread.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
import threading
import time
import serial
class ArduinoThread(threading.Thread):
"""Main class for controlling Arduino."""
def __init__(self, xval, radius, colorval):
threading.Thread.__init__(self)
self.arduino = serial.Serial(port="/dev/ttyUSB0", baudrate=115200, timeout=0.1)
self.isRunning = True
self.colorval = colorval
self.xval = xval
self.radius = radius
def run(self):
"""This method instructs boat to follow red/blue ball,
turn lef/right, or keep going straight.
If boat position is between radius 20 to 70 = follow ball.
If boat position is under radius 20 = keep going straight.
If boat position is over 70 radius = turn left/right.
"""
if self.colorval == "Red":
if 20 < self.radius < 70:
self.arduino.write(str(self.xval).encode())
elif self.radius <= 20:
self.arduino.write(str("100").encode())
elif self.radius >= 70:
self.arduino.write("10".encode())
else:
if 20 < self.radius < 70:
self.arduino.write(str(self.xval).encode())
elif self.radius <= 20:
self.arduino.write(str("100").encode())
elif self.radius >= 70:
self.arduino.write("10".encode())
time.sleep(0.1)