forked from aryan-02/line_follow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpi_dc_li.py
353 lines (302 loc) · 12.9 KB
/
rpi_dc_li.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env python3
"""
title :rpi_dc_lib.py
description :Part of a RpiMotorLib python 3 library for motors
and servos to connect to a raspberry pi
This file is for DC Motors. direct current.
TEST MATRIX :
DC motor via L298n motor controller = L298NMDc class
DC motor via L9110S and DRV8833 motor controller = DRV8833 class.
DC motor via TB6612FNG motor controller = TB6612FNGDc class.
DC motor via a transistor = TranDc class.
author :Gavin Lyons
web :https://github.com/gavinlyonsrepo/RpiMotorLib
mail :glyons66@hotmail.com
python_version :3.5.3
"""
# ========================== IMPORTS ======================
# Import the system modules needed to run rpiMotorlib.py
import time
import RPi.GPIO as GPIO
# ==================== CLASS SECTION ===============================
class L298NMDc():
""" Class to control DC motor via L298n motor controller
6 methods 1. __init__ 2. forward
3.backward 4.stop 5 .brake 6.cleanup"""
def __init__(self, pin_one, pin_two,
pwm_pin, freq=50, verbose=False, name="DCMotorX"):
""" init method
(1) pin_one, type=int, GPIO pin connected to IN1 or IN3
(2) Pin two type=int, GPIO pin connected to IN2 or IN4
(3) pwm_pin type=int, GPIO pin connected to EnA or ENB
(4) freq in Hz default 50
(5) verbose, type=bool type=bool default=False
help="Write pin actions"
(6) name, type=string, name attribute
"""
self.name = name
self.pin_one = pin_one
self.pin_two = pin_two
self.pwm_pin = pwm_pin
self.freq = freq
self.verbose = verbose
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(self.pin_one, GPIO.OUT)
GPIO.setup(self.pin_two, GPIO.OUT)
GPIO.setup(self.pwm_pin, GPIO.OUT)
self.my_pwm = GPIO.PWM(self.pwm_pin, self.freq)
self.last_pwm = 0
self.my_pwm.start(self.last_pwm)
if self.verbose:
print(" Motor initialized named: {} ".format(self.name))
print(" Pin one In1 or In3: {}".format(self.pin_one))
print(" Pin two In2 or in4: {}".format(self.pin_two))
print(" Pin pwm enA or enB: {}".format(self.pwm_pin))
print(" Frequency: {} ".format(self.freq))
def forward(self, duty_cycle=50):
""" Move motor forwards passed duty cycle for speed control """
GPIO.output(self.pin_one, True)
GPIO.output(self.pin_two, False)
if self.verbose:
print("Moving Motor Forward : Duty Cycle = {}".format(duty_cycle))
if duty_cycle != self.last_pwm:
self.my_pwm.ChangeDutyCycle(duty_cycle)
self.last_pwm = duty_cycle
def backward(self, duty_cycle=50):
""" Move motor backwards passed duty cycle for speed control"""
GPIO.output(self.pin_one, False)
GPIO.output(self.pin_two, True)
if self.verbose:
print("Moving Motor Backward : Duty Cycle = {}".format(duty_cycle))
if duty_cycle != self.last_pwm:
self.my_pwm.ChangeDutyCycle(duty_cycle)
self.last_pwm = duty_cycle
def stop(self, duty_cycle=0):
""" Stop motor"""
GPIO.output(self.pin_one, False)
GPIO.output(self.pin_two, False)
if self.verbose:
print("Stoping Motor : Duty Cycle = {}".format(duty_cycle))
if duty_cycle != self.last_pwm:
self.my_pwm.ChangeDutyCycle(duty_cycle)
self.last_pwm = duty_cycle
def brake(self, duty_cycle=100):
""" brake motor"""
GPIO.output(self.pin_one, True)
GPIO.output(self.pin_two, True)
if self.verbose:
print("Braking Motor : Duty Cycle = {}".format(duty_cycle))
if duty_cycle != self.last_pwm:
self.my_pwm.ChangeDutyCycle(duty_cycle)
self.last_pwm = duty_cycle
def cleanup(self, clean_up=False):
""" cleanup all GPIO connections used in event of error by lib user"""
if self.verbose:
print("rpi_dc_lib.py : Cleaning up")
GPIO.output(self.pin_one, False)
GPIO.output(self.pin_two, False)
self.my_pwm.ChangeDutyCycle(0)
if clean_up:
GPIO.cleanup()
class DRV8833NmDc():
""" Class to control DC motor via L9110S and DRV8833 motor controller
6 methods 1. __init__ 2. forward
3.backward 4.stop 5.brake 6.cleanup"""
def __init__(self, pin_one, pin_two,
freq=50, verbose=False, name="DCMotorY"):
""" init method
(1) pin_one, type=int, GPIO pin direction pin connected to IN1 or IN3
(2) Pin two type=int, GPIO pin PWM speed pin connected to IN2 or IN4
(3) freq in Hz default 50
(4) verbose, type=bool type=bool default=False
help="Write pin actions"
(5) name, type=string, name attribute
"""
self.name = name
self.pin_one = pin_one
self.pin_two = pin_two
self.freq = freq
self.verbose = verbose
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(self.pin_one, GPIO.OUT)
GPIO.setup(self.pin_two, GPIO.OUT)
self.my_pwm = GPIO.PWM(self.pin_two, self.freq)
self.last_pwm = 0
self.my_pwm.start(self.last_pwm)
if self.verbose:
print(" Motor initialized named: {} ".format(self.name))
print(" Direction pin In1 or In3: {}".format(self.pin_one))
print(" PWM speed pin In2 or in4: {}".format(self.pin_two))
print(" Frequency: {} ".format(self.freq))
def forward(self, duty_cycle=50):
""" Move motor forwards passed duty cycle for speed control """
GPIO.output(self.pin_one, True)
if self.verbose:
print("Moving Motor Forward : Duty Cycle = {}".format(duty_cycle))
if duty_cycle != self.last_pwm:
self.my_pwm.ChangeDutyCycle(duty_cycle)
self.last_pwm = duty_cycle
def backward(self, duty_cycle=50):
""" Move motor backwards passed duty cycle for speed control"""
GPIO.output(self.pin_one, False)
if self.verbose:
print("Moving Motor Backward : Duty Cycle = {}".format(duty_cycle))
if duty_cycle != self.last_pwm:
self.my_pwm.ChangeDutyCycle(duty_cycle)
self.last_pwm = duty_cycle
def stop(self, duty_cycle=0):
""" Stop motor"""
GPIO.output(self.pin_one, False)
self.my_pwm.ChangeDutyCycle(duty_cycle)
if self.verbose:
print("Stoping Motor : Duty Cycle = {}".format(duty_cycle))
if duty_cycle != self.last_pwm:
self.my_pwm.ChangeDutyCycle(duty_cycle)
self.last_pwm = duty_cycle
def brake(self, duty_cycle=100):
""" brake motor"""
GPIO.output(self.pin_one, True)
if self.verbose:
print("Braking Motor : Duty Cycle = {}".format(duty_cycle))
if duty_cycle != self.last_pwm:
self.my_pwm.ChangeDutyCycle(duty_cycle)
self.last_pwm = duty_cycle
def cleanup(self, clean_up=False):
""" cleanup all GPIO connections used in event of error by lib user"""
if self.verbose:
print("rpi_dc_lib.py : Cleaning up")
GPIO.output(self.pin_one, False)
self.my_pwm.ChangeDutyCycle(0)
GPIO.output(self.pin_two, False)
if clean_up:
GPIO.cleanup()
class TranDc():
""" Class to control DC motor via a transistor """
def __init__(self, pin, freq=50, verbose=False):
""" init method
(1) pin_one, type=int, GPIO pin connected base of transistor
(2) PWM freq in Hz default 50
(3) verbose, type=bool type=bool default=False
help="Write pin actions"
"""
self.pin = pin
self.freq = freq
self.verbose = verbose
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(self.pin, GPIO.OUT)
self.motor_pwm = GPIO.PWM(self.pin, self.freq)
self.motor_pwm.start(0)
def dc_motor_run(self, speed=10, step_delay=1):
""" controls speed of motor passed speed and step_delay
speed is PWm duty cycle in percentage, delay is in seconds """
self.motor_pwm.ChangeDutyCycle(speed)
time.sleep(step_delay)
if self.verbose:
print("Speed PWM duty cycle percentage {}".format(speed))
def dc_clean_up(self):
""" docstring """
self.motor_pwm.ChangeDutyCycle(0)
self.motor_pwm.stop()
GPIO.output(self.pin, False)
class TB6612FNGDc():
""" Class to control DC motor via TB6612FNGDC motor controller
6 methods 1. __init__ 2. forward
3.backward 4.stop 5 .brake 6.cleanup 7.standby"""
def __init__(self, pin_one, pin_two,
pwm_pin, freq=50, verbose=False, name="DCMotorX"):
""" init method
(1) pin_one, type=int, GPIO pin connected to AI1 or BI1
(2) Pin two type=int, GPIO pin connected to AI2 or BI2
(3) pwm_pin type=int, GPIO pin connected to PWA or PWB
(4) freq in Hz default 50
(5) verbose, type=bool type=bool default=False
help="Write pin actions"
(6) name, type=string, name attribute
"""
self.name = name
self.pin_one = pin_one
self.pin_two = pin_two
self.pwm_pin = pwm_pin
self.freq = freq
self.verbose = verbose
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(self.pin_one, GPIO.OUT)
GPIO.setup(self.pin_two, GPIO.OUT)
GPIO.setup(self.pwm_pin, GPIO.OUT)
self.my_pwm = GPIO.PWM(self.pwm_pin, self.freq)
self.last_pwm = 0
self.my_pwm.start(self.last_pwm)
if self.verbose:
print(" Motor initialized named: {} ".format(self.name))
print(" Pin one AI1 or BI1: {}".format(self.pin_one))
print(" Pin two AI2 or BI2: {}".format(self.pin_two))
print(" Pin pwm PWA or PWB: {}".format(self.pwm_pin))
print(" Frequency: {} ".format(self.freq))
def standby(standby_pin, standby_on=True):
"""Enables/disables the standby mode of TB661FNG controller"""
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
if standby_on:
GPIO.setup(standby_pin, GPIO.OUT)
GPIO.output(standby_pin, True)
else:
GPIO.output(standby_pin, False)
def forward(self, duty_cycle=50):
""" Move motor forwards passed duty cycle for speed control """
GPIO.output(self.pin_one, True)
GPIO.output(self.pin_two, False)
if self.verbose:
print("Moving Motor Forward : Duty Cycle = {}".format(duty_cycle))
if duty_cycle != self.last_pwm:
self.my_pwm.ChangeDutyCycle(duty_cycle)
self.last_pwm = duty_cycle
def backward(self, duty_cycle=50):
""" Move motor backwards passed duty cycle for speed control"""
GPIO.output(self.pin_one, False)
GPIO.output(self.pin_two, True)
if self.verbose:
print("Moving Motor Backward : Duty Cycle = {}".format(duty_cycle))
if duty_cycle != self.last_pwm:
self.my_pwm.ChangeDutyCycle(duty_cycle)
self.last_pwm = duty_cycle
def stop(self, duty_cycle=0):
""" Stop motor"""
GPIO.output(self.pin_one, False)
GPIO.output(self.pin_two, False)
if self.verbose:
print("Stoping Motor : Duty Cycle = {}".format(duty_cycle))
if duty_cycle != self.last_pwm:
self.my_pwm.ChangeDutyCycle(duty_cycle)
self.last_pwm = duty_cycle
def brake(self, duty_cycle=100):
""" brake motor"""
GPIO.output(self.pin_one, True)
GPIO.output(self.pin_two, True)
if self.verbose:
print("Braking Motor : Duty Cycle = {}".format(duty_cycle))
if duty_cycle != self.last_pwm:
self.my_pwm.ChangeDutyCycle(duty_cycle)
self.last_pwm = duty_cycle
def cleanup(self, clean_up=False):
""" cleanup all GPIO connections used in event of error by lib user"""
if self.verbose:
print("rpi_dc_lib.py : Cleaning up : {}".format(self.name))
GPIO.output(self.pin_one, False)
GPIO.output(self.pin_two, False)
self.my_pwm.ChangeDutyCycle(0)
if clean_up:
GPIO.cleanup()
def importtest(text):
"""import print test statement"""
pass
# print(text)
# ===================== MAIN ===============================
if __name__ == '__main__':
importtest("main")
else:
importtest("Imported {}".format(__name__))
# ===================== END ===============================