-
Notifications
You must be signed in to change notification settings - Fork 0
/
MotorDriver.py
executable file
·193 lines (158 loc) · 4.71 KB
/
MotorDriver.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python
import threading
import time
from Globals import Globals
from Observable import Observable
try:
import wiringpi
except:
print "wiringpi not found, switching to simulation"
Globals.globSimulate = True
# Don't touch these
LEFT_L = 0
LEFT_R = 1
RIGHT_L = 2
RIGHT_R = 3
VERT_L = 4
VERT_R = 5
GPIO_INPUT = 0
GPIO_OUTPUT = 1
GPIO_PWM = 2
UPDATEINTERVAL = 0.01 # seconds
MAXIDLE = 0.3 # seconds
class MotorDriver(threading.Thread, Observable):
keepRunning = True
pwmValue = 700
lastHorizontalChange = 0
lastVerticalChange = 0
debug = False
singleSteerMode = False
# left, right, vert motors
motorPins = []
pinValues = [False] * 6
def __init__(self, pins):
threading.Thread.__init__(self)
Observable.__init__(self)
if len(pins) != 6:
raise "MotorDriver needs 6 pins"
self.motorPins = pins
if not Globals.globSimulate:
for pn in range(0, 6):
pin = self.motorPins[pn]
wiringpi.pinMode(pin, GPIO_OUTPUT)
wiringpi.pinMode(18, GPIO_PWM)
wiringpi.pwmWrite(18, self.pwmValue)
def sendToRPi(self):
for pn in range(0, 6):
pin = self.motorPins[pn]
val = self.pinValues[pn]
if not Globals.globSimulate:
wiringpi.digitalWrite(pin, 1 if val else 0)
if not Globals.globSimulate:
wiringpi.pwmWrite(18, self.pwmValue)
if self.debug:
print "Sent to RPi: pinValues(%s) pwmValue(%s)" % (self.pinValues, self.pwmValue)
def setPin(self, pinName, val):
self.pinValues[pinName] = val
now = time.time()
if pinName == VERT_L or pinName == VERT_R:
self.lastVerticalChange = now
else:
self.lastHorizontalChange = now
self.emit("MotorDriver", self)
def setSpeed(self, val):
self.pwmValue = max(min(val, 1023), 0)
self.emit("MotorDriver", self)
def setSingleSteerMode(self, val):
self.singleSteerMode = val
self.emit("MotorDriver", self)
def getSingleSteerMode(self):
return self.singleSteerMode
def statusByPin(self, l, r):
if self.pinValues[l] == self.pinValues[r]:
return "stop"
if self.pinValues[l]:
return "backward"
return "forward"
def statusLeft(self):
return self.statusByPin(LEFT_L, LEFT_R)
def statusRight(self):
return self.statusByPin(RIGHT_L, RIGHT_R)
def statusVert(self):
return self.statusByPin(VERT_L, VERT_R)
def getPWM(self):
return self.pwmValue
def _leftForward(self):
self.setPin(LEFT_L, False) ; self.setPin(LEFT_R, True)
def _leftBackward(self):
self.setPin(LEFT_R, False) ; self.setPin(LEFT_L, True)
def _leftStop(self):
self.setPin(LEFT_R, False) ; self.setPin(LEFT_L, False)
def _rightForward(self):
self.setPin(RIGHT_L, False) ; self.setPin(RIGHT_R, True)
def _rightBackward(self):
self.setPin(RIGHT_R, False) ; self.setPin(RIGHT_L, True)
def _rightStop(self):
self.setPin(RIGHT_R, False) ; self.setPin(RIGHT_L, False)
def _vertForward(self):
self.setPin(VERT_L, False) ; self.setPin(VERT_R, True)
def _vertBackward(self):
self.setPin(VERT_R, False) ; self.setPin(VERT_L, True)
def _vertStop(self):
self.setPin(VERT_R, False) ; self.setPin(VERT_L, False)
def turnLeft(self):
self._rightForward()
if not self.singleSteerMode:
self._leftBackward()
def turnRight(self):
self._leftForward()
if not self.singleSteerMode:
self._rightBackward()
def forward(self):
self._leftForward() ; self._rightForward()
def backward(self):
self._leftBackward() ; self._rightBackward()
def up(self):
self._vertForward()
def down(self):
self._vertBackward()
def increasePWM(self, d = 20):
self.setSpeed(self.pwmValue + d)
def decreasePWM(self, d = 20):
self.setSpeed(self.pwmValue - d)
def stop(self):
self.stopVertical()
self.stopHorizontal()
def stopVertical(self):
self._vertStop()
def stopHorizontal(self):
self._leftStop()
self._rightStop()
def shutdown(self):
self.keepRunning = False
self.stop()
self.sendToRPi()
def run(self):
while self.keepRunning:
now = time.time()
if self.lastVerticalChange + MAXIDLE < now:
self.stopVertical()
if self.lastHorizontalChange + MAXIDLE < now:
self.stopHorizontal()
self.sendToRPi()
time.sleep(UPDATEINTERVAL)
if __name__ == '__main__':
Globals.globSimulate = True
delay = 0.1
md = MotorDriver([1,2,3,4,5,6]) #Fake values
md.debug = True
md.start()
md.turnLeft() ; time.sleep(delay)
md.turnRight() ; time.sleep(delay)
md.forward() ; time.sleep(delay)
md.backward() ; time.sleep(delay)
md.up() ; time.sleep(delay)
md.down() ; time.sleep(delay)
md.stop() ; time.sleep(delay)
md.shutdown()
md.join()