-
Notifications
You must be signed in to change notification settings - Fork 47
/
PiMotor.py
301 lines (251 loc) · 10.2 KB
/
PiMotor.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/python
# Library for PiMotor Shield V2
# Developed by: SB Components
# Project: RPi Motor Shield
import RPi.GPIO as GPIO #Import GPIO library
import time
from time import sleep
GPIO.setmode(GPIO.BOARD) #Set GPIO pin numbering
GPIO.setwarnings(False)
class Motor:
''' Class to handle interaction with the motor pins
Supports redefinition of "forward" and "backward" depending on how motors are connected
Use the supplied Motorshieldtest module to test the correct configuration for your project.
Arguments:
motor = string motor pin label (i.e. "MOTOR1","MOTOR2","MOTOR3","MOTOR4") identifying the pins to which
the motor is connected.
config = int defining which pins control "forward" and "backward" movement.
'''
motorpins = {"MOTOR4":{"config":{1:{"e":32,"f":24,"r":26},2:{"e":32,"f":26,"r":24}},"arrow":1},
"MOTOR3":{"config":{1:{"e":19,"f":21,"r":23},2:{"e":19,"f":23,"r":21}}, "arrow":2},
"MOTOR2":{"config":{1:{"e":22,"f":16,"r":18},2:{"e":22,"f":18,"r":16}}, "arrow":3},
"MOTOR1":{"config":{1:{"e":11,"f":15,"r":13},2:{"e":11,"f":13,"r":15}},"arrow":4}}
def __init__(self, motor, config):
self.testMode = False
self.arrow = Arrow(self.motorpins[motor]["arrow"])
self.pins = self.motorpins[motor]["config"][config]
GPIO.setup(self.pins['e'],GPIO.OUT)
GPIO.setup(self.pins['f'],GPIO.OUT)
GPIO.setup(self.pins['r'],GPIO.OUT)
self.PWM = GPIO.PWM(self.pins['e'], 50) # 50Hz frequency
self.PWM.start(0)
GPIO.output(self.pins['e'],GPIO.HIGH)
GPIO.output(self.pins['f'],GPIO.LOW)
GPIO.output(self.pins['r'],GPIO.LOW)
def test(self, state):
''' Puts the motor into test mode
When in test mode the Arrow associated with the motor receives power on "forward"
rather than the motor. Useful when testing your code.
Arguments:
state = boolean
'''
self.testMode = state
def forward(self, speed):
''' Starts the motor turning in its configured "forward" direction.
Arguments:
speed = Duty Cycle Percentage from 0 to 100.
0 - stop and 100 - maximum speed
'''
print("Forward")
if self.testMode:
self.arrow.on()
else:
self.PWM.ChangeDutyCycle(speed)
GPIO.output(self.pins['f'],GPIO.HIGH)
GPIO.output(self.pins['r'],GPIO.LOW)
def reverse(self,speed):
''' Starts the motor turning in its configured "reverse" direction.
Arguments:
speed = Duty Cycle Percentage from 0 to 100.
0 - stop and 100 - maximum speed
'''
print("Reverse")
if self.testMode:
self.arrow.off()
else:
self.PWM.ChangeDutyCycle(speed)
GPIO.output(self.pins['f'],GPIO.LOW)
GPIO.output(self.pins['r'],GPIO.HIGH)
def stop(self):
''' Stops power to the motor,
'''
print("Stop")
self.arrow.off()
self.PWM.ChangeDutyCycle(0)
GPIO.output(self.pins['f'],GPIO.LOW)
GPIO.output(self.pins['r'],GPIO.LOW)
def speed(self):
''' Control Speed of Motor,
'''
class LinkedMotors:
''' Links 2 or more motors together as a set.
This allows a single command to be used to control a linked set of motors
e.g. For a 4x wheel vehicle this allows a single command to make all 4 wheels go forward.
Starts the motor turning in its configured "forward" direction.
Arguments:
*motors = a list of Motor objects
'''
def __init__(self, *motors):
self.motor = []
for i in motors:
print(i.pins)
self.motor.append(i)
def forward(self,speed):
''' Starts the motor turning in its configured "forward" direction.
Arguments:
speed = Duty Cycle Percentage from 0 to 100.
0 - stop and 100 - maximum speed
'''
for i in range(len(self.motor)):
self.motor[i].forward(speed)
def reverse(self,speed):
''' Starts the motor turning in its configured "reverse" direction.
Arguments:
speed = Duty Cycle Percentage from 0 to 100.
0 - stop and 100 - maximum speed
'''
for i in range(len(self.motor)):
self.motor[i].reverse(speed)
def stop(self):
''' Stops power to the motor,
'''
for i in range(len(self.motor)):
self.motor[i].stop()
class Stepper:
''' Defines stepper motor pins on the MotorShield
Arguments:
motor = stepper motor
'''
stepperpins = {"STEPPER1":{"en1":11, "en2":22, "c1":13,"c2":15, "c3":18, "c4":16},
"STEPPER2":{"en1":19, "en2":32, "c1":21,"c2":23, "c3":24, "c4":26}}
def __init__(self, motor):
self.config = self.stepperpins[motor]
GPIO.setup(self.config["en1"],GPIO.OUT)
GPIO.setup(self.config["en2"],GPIO.OUT)
GPIO.setup(self.config["c1"],GPIO.OUT)
GPIO.setup(self.config["c2"],GPIO.OUT)
GPIO.setup(self.config["c3"],GPIO.OUT)
GPIO.setup(self.config["c4"],GPIO.OUT)
GPIO.output(self.config["en1"],GPIO.HIGH)
GPIO.output(self.config["en2"],GPIO.HIGH)
GPIO.output(self.config["c1"],GPIO.LOW)
GPIO.output(self.config["c2"],GPIO.LOW)
GPIO.output(self.config["c3"],GPIO.LOW)
GPIO.output(self.config["c4"],GPIO.LOW)
''' Set steps of Stepper Motor
Arguments:
w1,w2,w3,w4 = Wire of Stepper Motor
'''
def setStep(self, w1, w2, w3, w4):
GPIO.output(self.config["c1"], w1)
GPIO.output(self.config["c2"], w2)
GPIO.output(self.config["c3"], w3)
GPIO.output(self.config["c4"], w4)
''' Rotate Stepper motor in forward direction
Arguments:
delay = time between steps in miliseconds
steps = Number of Steps
'''
def forward(self, delay, steps):
for i in range(0, steps):
self.setStep(1, 0, 1, 0)
time.sleep(delay)
self.setStep(0, 1, 1, 0)
time.sleep(delay)
self.setStep(0, 1, 0, 1)
time.sleep(delay)
self.setStep(1, 0, 0, 1)
time.sleep(delay)
''' Rotate Stepper motor in backward direction
Arguments:
delay = time between steps
steps = Number of Steps
'''
def backward(self, delay, steps):
for i in range(0, steps):
self.setStep(1, 0, 0, 1)
time.sleep(delay)
self.setStep(0, 1, 0, 1)
time.sleep(delay)
self.setStep(0, 1, 1, 0)
time.sleep(delay)
self.setStep(1, 0, 1, 0)
time.sleep(delay)
def stop(self):
''' Stops power to the motor,
'''
print("Stop Stepper Motor")
GPIO.output(self.config['c1'],GPIO.LOW)
GPIO.output(self.config['c2'],GPIO.LOW)
GPIO.output(self.config['c3'],GPIO.LOW)
GPIO.output(self.config['c4'],GPIO.LOW)
class Sensor:
''' Defines a sensor connected to the sensor pins on the MotorShield
Arguments:
sensortype = string identifying which sensor is being configured.
i.e. "IR1", "IR2", "ULTRASONIC"
boundary = an integer specifying the minimum distance at which the sensor
will return a Triggered response of True.
'''
Triggered = False
def iRCheck(self):
input_state = GPIO.input(self.config["echo"])
if input_state == True:
print("Sensor 2: Object Detected")
self.Triggered = True
else:
self.Triggered = False
def sonicCheck(self):
print("SonicCheck has been triggered")
time.sleep(0.333)
GPIO.output(self.config["trigger"], True)
time.sleep(0.00001)
GPIO.output(self.config["trigger"], False)
start = time.time()
while GPIO.input(self.config["echo"])==0:
start = time.time()
while GPIO.input(self.config["echo"])==1:
stop = time.time()
elapsed = stop-start
measure = (elapsed * 34300)/2
self.lastRead = measure
if self.boundary > measure:
print("Boundary breached")
print(self.boundary)
print(measure)
self.Triggered = True
else:
self.Triggered = False
sensorpins = {"IR1":{"echo":7, "check":iRCheck}, "IR2":{"echo":12, "check":iRCheck},
"ULTRASONIC":{"trigger":29, "echo": 31, "check":sonicCheck}}
def trigger(self):
''' Executes the relevant routine that activates and takes a reading from the specified sensor.
If the specified "boundary" has been breached the Sensor's Triggered attribute gets set to True.
'''
self.config["check"](self)
print("Trigger Called")
def __init__(self, sensortype, boundary):
self.config = self.sensorpins[sensortype]
self.boundary = boundary
self.lastRead = 0
if "trigger" in self.config:
print("trigger")
GPIO.setup(self.config["trigger"],GPIO.OUT)
GPIO.setup(self.config["echo"],GPIO.IN)
class Arrow():
''' Defines an object for controlling one of the LED arrows on the Motorshield.
Arguments:
which = integer label for each arrow. The arrow number if arbitrary starting with:
1 = Arrow closest to the Motorshield's power pins and running clockwise round the board
...
4 = Arrow closest to the motor pins.
'''
arrowpins={1:33,2:35,3:37,4:36}
def __init__(self, which):
self.pin = self.arrowpins[which]
GPIO.setup(self.pin,GPIO.OUT)
GPIO.output(self.pin, GPIO.LOW)
def on(self):
GPIO.output(self.pin,GPIO.HIGH)
def off(self):
GPIO.output(self.pin,GPIO.LOW)