-
Notifications
You must be signed in to change notification settings - Fork 1
/
motor-drive.py
121 lines (106 loc) · 2.88 KB
/
motor-drive.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
from gpiozero import PWMOutputDevice
from gpiozero import DigitalOutputDevice
from time import sleep
#///////////////// Define Motor Driver GPIO Pins /////////////////
# Motor A, Left Side GPIO CONSTANTS
PWM_DRIVE_LEFT = 21 # ENA - H-Bridge enable pin
FORWARD_LEFT_PIN = 26 # IN1 - Forward Drive
REVERSE_LEFT_PIN = 19 # IN2 - Reverse Drive
# Motor B, Right Side GPIO CONSTANTS
PWM_DRIVE_RIGHT = 5 # ENB - H-Bridge enable pin
FORWARD_RIGHT_PIN = 13 # IN1 - Forward Drive
REVERSE_RIGHT_PIN = 6 # IN2 - Reverse Drive
# Initialise objects for H-Bridge GPIO PWM pins
# Set initial duty cycle to 0 and frequency to 1000
driveLeft = PWMOutputDevice(PWM_DRIVE_LEFT, True, 0, 1000)
driveRight = PWMOutputDevice(PWM_DRIVE_RIGHT, True, 0, 1000)
# Initialise objects for H-Bridge digital GPIO pins
forwardLeft = DigitalOutputDevice(FORWARD_LEFT_PIN)
reverseLeft = DigitalOutputDevice(REVERSE_LEFT_PIN)
forwardRight = DigitalOutputDevice(FORWARD_RIGHT_PIN)
reverseRight = DigitalOutputDevice(REVERSE_RIGHT_PIN)
def allStop():
forwardLeft.value = False
reverseLeft.value = False
forwardRight.value = False
reverseRight.value = False
driveLeft.value = 0
driveRight.value = 0
def forwardDrive():
forwardLeft.value = True
reverseLeft.value = False
forwardRight.value = True
reverseRight.value = False
driveLeft.value = 1.0
driveRight.value = 1.0
def reverseDrive():
forwardLeft.value = False
reverseLeft.value = True
forwardRight.value = False
reverseRight.value = True
driveLeft.value = 1.0
driveRight.value = 1.0
def spinLeft():
forwardLeft.value = False
reverseLeft.value = True
forwardRight.value = True
reverseRight.value = False
driveLeft.value = 1.0
driveRight.value = 1.0
def SpinRight():
forwardLeft.value = True
reverseLeft.value = False
forwardRight.value = False
reverseRight.value = True
driveLeft.value = 1.0
driveRight.value = 1.0
def forwardTurnLeft():
forwardLeft.value = True
reverseLeft.value = False
forwardRight.value = True
reverseRight.value = False
driveLeft.value = 0.2
driveRight.value = 0.8
def forwardTurnRight():
forwardLeft.value = True
reverseLeft.value = False
forwardRight.value = True
reverseRight.value = False
driveLeft.value = 0.8
driveRight.value = 0.2
def reverseTurnLeft():
forwardLeft.value = False
reverseLeft.value = True
forwardRight.value = False
reverseRight.value = True
driveLeft.value = 0.2
driveRight.value = 0.8
def reverseTurnRight():
forwardLeft.value = False
reverseLeft.value = True
forwardRight.value = False
reverseRight.value = True
driveLeft.value = 0.8
driveRight.value = 0.2
def main():
allStop()
forwardDrive()
sleep(5)
reverseDrive()
sleep(5)
spinLeft()
sleep(5)
SpinRight()
sleep(5)
forwardTurnLeft()
sleep(5)
forwardTurnRight()
sleep(5)
reverseTurnLeft()
sleep(5)
reverseTurnRight()
sleep(5)
allStop()
if __name__ == "__main__":
""" This is executed when run from the command line """
main()