-
Notifications
You must be signed in to change notification settings - Fork 66
/
motor.py
60 lines (46 loc) · 1.6 KB
/
motor.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
"""
File: chapter10/motor.py
Using a L293D as a H-Bridge to control a DC Motor.
Dependencies:
pip3 install pigpio
Built and tested with Python 3.7 on Raspberry Pi 4 Model B
"""
import pigpio # (1)
from time import sleep
from motor_class import Motor
# Motor A
CHANNEL_1_ENABLE_GPIO = 18 # (2)
INPUT_1Y_GPIO = 23
INPUT_2Y_GPIO = 24
# Motor B
CHANNEL_2_ENABLE_GPIO = 16 # (3)
INPUT_3Y_GPIO = 20
INPUT_4Y_GPIO = 21
pi = pigpio.pi() # (4)
motor_A = Motor(pi, CHANNEL_1_ENABLE_GPIO, INPUT_1Y_GPIO, INPUT_2Y_GPIO)
motor_B = Motor(pi, CHANNEL_2_ENABLE_GPIO, INPUT_3Y_GPIO, INPUT_4Y_GPIO)
if __name__ == '__main__':
try:
# Make the motors move
print("Motor A and B Speed 50, Right")
motor_A.set_speed(50) # (5)
motor_A.right()
motor_B.set_speed(50)
motor_B.right()
sleep(2)
print("Motor A Speed 100")
motor_A.set_speed(100)
print("Motor B Left")
motor_B.left()
sleep(2)
print("Motor B Speed 100")
motor_B.set_speed(100)
sleep(2)
print("Motor A Classic Brake, Motor B PWM Brake")
motor_A.brake() # (6)
motor_B.brake_pwm()
sleep(2)
finally:
motor_A.set_speed(0)
motor_B.set_speed(0)
pi.stop()