forked from nikivanov/watney
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotorcontroller.py
134 lines (118 loc) · 5.01 KB
/
motorcontroller.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from events import Events
from motor import Motor
import pigpio
class MotorController:
validBearings = ["n", "ne", "e", "se", "s", "sw", "w", "nw", "0"]
def __init__(self, config, gpio, audioManager):
driverConfig = config["DRIVER"]
leftMotorConfig = config["LEFTMOTOR"]
rightMotorConfig = config["RIGHTMOTOR"]
self.trim = float(driverConfig["Trim"])
self.pwmFrequency = int(driverConfig["PWMFrequency"])
leftMotor = Motor(
gpio,
self.pwmFrequency,
int(leftMotorConfig["ForwardPin"]),
int(leftMotorConfig["ReversePin"]),
1 if self.trim >= 0 else 1 + self.trim,
'Left Motor'
)
rightMotor = Motor(
gpio,
self.pwmFrequency,
int(rightMotorConfig["ForwardPin"]),
int(rightMotorConfig["ReversePin"]),
1 if self.trim <= 0 else 1 - self.trim,
'Right Motor'
)
self.leftMotor = leftMotor
self.rightMotor = rightMotor
self.gpio = gpio
self.straightaway = float(driverConfig["Straightaway"])
self.straightawaySlow = float(driverConfig["StraightawaySlow"])
self.halfTurnUndersteer = float(driverConfig["HalfTurnUndersteer"])
self.halfTurnSlowFactor = float(driverConfig["HalfTurnSlowFactor"])
self.tankTurnSpeed = float(driverConfig["TankTurnSpeed"])
self.tankTurnSpeedSlow = float(driverConfig["TankTurnSpeedSlow"])
self.enablePin = int(driverConfig["EnablePin"])
self.gpio.set_mode(self.enablePin, pigpio.OUTPUT)
self.gpio.write(self.enablePin, pigpio.LOW)
self.audioManager = audioManager
self.audioToken = 'fe7a1846-a0bb-4a44-aa3e-5b080089d37a'
def getTargetMotorDCs(self, targetBearing, slow):
if targetBearing == "0":
leftDC = 0
rightDC = 0
elif targetBearing == "n":
if not slow:
leftDC = 100 * self.straightaway
rightDC = 100 * self.straightaway
else:
leftDC = 100 * self.straightawaySlow
rightDC = 100 * self.straightawaySlow
elif targetBearing == "ne":
if not slow:
leftDC = 100
rightDC = 100 * self.halfTurnUndersteer
else:
leftDC = 100 * self.halfTurnSlowFactor
rightDC = 100 * self.halfTurnUndersteer * self.halfTurnSlowFactor
elif targetBearing == "e":
if not slow:
leftDC = 100 * self.tankTurnSpeed
rightDC = -100 * self.tankTurnSpeed
else:
leftDC = 100 * self.tankTurnSpeedSlow
rightDC = -100 * self.tankTurnSpeedSlow
elif targetBearing == "se":
if not slow:
leftDC = -100
rightDC = -100 * self.halfTurnUndersteer
else:
leftDC = -100 * self.halfTurnSlowFactor
rightDC = -100 * self.halfTurnUndersteer * self.halfTurnSlowFactor
elif targetBearing == "s":
if not slow:
leftDC = -100 * self.straightaway
rightDC = -100 * self.straightaway
else:
leftDC = -100 * self.straightawaySlow
rightDC = -100 * self.straightawaySlow
elif targetBearing == "sw":
if not slow:
leftDC = -100 * self.halfTurnUndersteer
rightDC = -100
else:
leftDC = -100 * self.halfTurnUndersteer * self.halfTurnSlowFactor
rightDC = -100 * self.halfTurnSlowFactor
elif targetBearing == "w":
if not slow:
leftDC = -100 * self.tankTurnSpeed
rightDC = 100 * self.tankTurnSpeed
else:
leftDC = -100 * self.tankTurnSpeedSlow
rightDC = 100 * self.tankTurnSpeedSlow
elif targetBearing == "nw":
if not slow:
leftDC = 100 * self.halfTurnUndersteer
rightDC = 100
else:
leftDC = 100 * self.halfTurnUndersteer * self.halfTurnSlowFactor
rightDC = 100 * self.halfTurnSlowFactor
else:
raise Exception("Bad bearing: " + targetBearing)
return int(leftDC), int(rightDC)
def setBearing(self, bearing, slow):
if bearing not in self.validBearings:
raise ValueError("Invalid bearing: {}".format(bearing))
leftDC, rightDC = self.getTargetMotorDCs(bearing, slow)
leftActive = self.leftMotor.setMotion(leftDC)
rightActive = self.rightMotor.setMotion(rightDC)
if leftActive or rightActive:
self.gpio.write(self.enablePin, pigpio.HIGH)
self.audioManager.lowerVolume(self.audioToken)
Events.getInstance().fireMotionOn()
else:
self.gpio.write(self.enablePin, pigpio.LOW)
self.audioManager.restoreVolume(self.audioToken)
Events.getInstance().fireMotionOff()