-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7-pwm.py
107 lines (87 loc) · 2.8 KB
/
7-pwm.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
# Get your motors running...
# ... head out on the highway!
import RPi.GPIO as GPIO # import the GPIO library for controlling the Pi's pins
import time
# set GPIO modes
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# set variables for motor pins
pinMotorAForwards = 10
pinMotorABackwards = 9
pinMotorBForwards = 8
pinMotorBBackwards = 7
# how many times to turn the pin on and off per second
Frequency = 20
# how long the pin stays on each cycle, as a percent
DutyCycleA = 30
DutyCycleB = 30
# setting the duty cycle to 0 means the motors will not turn
Stop = 0
# set the GPIO pin mode
GPIO.setup(pinMotorBBackwards, GPIO.OUT) # left motor backwards
GPIO.setup(pinMotorBForwards, GPIO.OUT) # left motor forwards
GPIO.setup(pinMotorABackwards, GPIO.OUT) # right motor backwards
GPIO.setup(pinMotorAForwards, GPIO.OUT) # right motor forwards
# set the GPIO to software PWM at 'Frequency' hertz
pwmMotorAForwards = GPIO.PWM(pinMotorAForwards, Frequency)
pwmMotorABackwards = GPIO.PWM(pinMotorABackwards, Frequency)
pwmMotorBForwards = GPIO.PWM(pinMotorBForwards, Frequency)
pwmMotorBBackwards = GPIO.PWM(pinMotorBBackwards, Frequency)
pwmMotorAForwards.start(Stop)
pwmMotorABackwards.start(Stop)
pwmMotorBForwards.start(Stop)
pwmMotorBBackwards.start(Stop)
# turn all motors off
def StopMotors():
pwmMotorAForwards.ChangeDutyCycle(Stop)
pwmMotorABackwards.ChangeDutyCycle(Stop)
pwmMotorBForwards.ChangeDutyCycle(Stop)
pwmMotorBBackwards.ChangeDutyCycle(Stop)
# full impulse
def Forwards():
pwmMotorAForwards.ChangeDutyCycle(DutyCycleA)
pwmMotorABackwards.ChangeDutyCycle(Stop)
pwmMotorBForwards.ChangeDutyCycle(DutyCycleB)
pwmMotorBBackwards.ChangeDutyCycle(Stop)
# full reverse!
def Backwards():
pwmMotorAForwards.ChangeDutyCycle(Stop)
pwmMotorABackwards.ChangeDutyCycle(DutyCycleA)
pwmMotorBForwards.ChangeDutyCycle(Stop)
pwmMotorBBackwards.ChangeDutyCycle(DutyCycleB)
# turn right
def Right(hard=False):
pwmMotorBBackwards.ChangeDutyCycle(Stop)
pwmMotorBForwards.ChangeDutyCycle(DutyCycleB)
if(hard == True):
pwmMotorABackwards.ChangeDutyCycle(DutyCycleA)
else:
pwmMotorABackwards.ChangeDutyCycle(Stop)
pwmMotorAForwards.ChangeDutyCycle(Stop)
# turn left
def Left(hard=False):
if(hard == True):
pwmMotorBBackwards.ChangeDutyCycle(DutyCycleB)
else:
pwmMotorBBackwards.ChangeDutyCycle(Stop)
pwmMotorBForwards.ChangeDutyCycle(Stop)
pwmMotorABackwards.ChangeDutyCycle(Stop)
pwmMotorAForwards.ChangeDutyCycle(DutyCycleA)
# forwards for 1 second
Forwards()
time.sleep(1)
# hard left for 2 secs
Left(True)
time.sleep(2)
# cruise for 1 sec
Forwards()
time.sleep(1)
# right for 2 secs
Right()
time.sleep(2)
# rewind for 0.5 secs
Backwards()
time.sleep(0.5)
StopMotors()
# reset the GPIO pins (turns off motors too)
GPIO.cleanup()