-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrivetrain.py
executable file
·74 lines (65 loc) · 2.26 KB
/
drivetrain.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
import serial
# Mixer and serial comms
class DriveTrain:
def __init__(self):
"""Constructor"""
self.enabled = False
self.motor_left = 0
self.motor_right = 0
# self.ser = serial.Serial('/dev/ttyUSB0', 9600)
self.ser = serial.Serial(
"/dev/ttyUSB0",
baudrate=57600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
writeTimeout=0,
timeout=10,
rtscts=False,
dsrdtr=False,
xonxoff=False)
def stop(self):
"""Called when we want to shut down."""
if self.ser:
self.ser.close()
def mix_tank(self, lx, ly, rx, ry):
"""Mix [-1,1] values for Left and Right X,Y axis"""
# Worlds simplest mixer, for tank driving only
# need to send ly and ry direct to motors.
self.motor_left = ly
self.motor_right = ry
def mix_channels_and_send(self, lx, ly, rx, ry):
"""Mix axis and send the motors"""
# Send left and right axis values to appropriate mixer
self.mix_tank(lx, ly, rx, ry)
# Left and Right motor values will
# have been updated by above method
self.send_to_motors()
def send_neutral_to_motors(self):
if self.ser:
# Can send neutral even when motors disabled
command = "[0]\n"
print(command)
self.ser.write(command)
# result = self.ser.readline()
# print(result)
def send_to_motors(self):
"""Send motor Left and Right values to arduino"""
if self.enabled and self.ser:
command = "[1,%f,%f]\n" % (self.motor_left, self.motor_right)
print(command)
self.ser.write(command)
# esult = self.ser.readline()
# print(result)
def set_neutral(self):
"""Set motors to neutral"""
self.motor_left = 0
self.motor_right = 0
self.send_neutral_to_motors()
# self.send_to_motors()
def enable_motors(self, enable):
"""Enable or disable motors"""
self.enabled = enable
# If now disabled, send neutral to motors
if not self.enabled:
self.set_neutral()