-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor_test.py
67 lines (53 loc) · 1.46 KB
/
motor_test.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
import time
import RPi.GPIO as GPIO
mode = GPIO.getmode()
GPIO.cleanup()
left_forward = 26
left_backward = 19
right_forward = 16
right_backward = 13
sleep_time = 1
pwm_frequency = 200
GPIO.setmode(GPIO.BCM)
GPIO.setup(left_forward, GPIO.OUT)
GPIO.setup(left_backward, GPIO.OUT)
GPIO.setup(right_forward, GPIO.OUT)
GPIO.setup(right_backward, GPIO.OUT)
def forward(x):
GPIO.output(left_forward, GPIO.HIGH)
GPIO.output(right_forward, GPIO.HIGH)
print("Moving Forward")
time.sleep(x)
GPIO.output(left_forward, GPIO.LOW)
GPIO.output(right_forward, GPIO.LOW)
def reverse(x):
GPIO.output(left_backward, GPIO.HIGH)
GPIO.output(right_backward, GPIO.HIGH)
print("Moving Backward")
time.sleep(x)
GPIO.output(left_backward, GPIO.LOW)
GPIO.output(right_backward, GPIO.LOW)
def test_pwm():
left = GPIO.PWM(left_forward, pwm_frequency)
right = GPIO.PWM(right_forward, pwm_frequency)
left.start(0)
right.start(0)
try:
for dc in range(0, 101):
left.ChangeDutyCycle(dc)
right.ChangeDutyCycle(dc)
time.sleep(0.1)
for dc in range(100, -1, -1):
right.ChangeDutyCycle(dc)
left.ChangeDutyCycle(dc)
time.sleep(0.1)
except KeyboardInterrupt:
pass
left.stop()
right.stop()
while 1:
# forward(1)
# reverse(1)
test_pwm()
GPIO.cleanup()
break