-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMoveCarNoPWM.py
133 lines (120 loc) · 4.1 KB
/
MoveCarNoPWM.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
import RPi.GPIO as GPIO
import time
class MoveCar():
def __init__(self):
self.control=''
#right wheels
self.right_enable = 33
self.right_input_1 = 13
self.right_input_2 = 11
#left wheels
self.left_enable = 32
self.left_input_1 = 16
self.left_input_2 = 18
def initialize(self):
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#right wheels
GPIO.setup(self.right_enable, GPIO.OUT)
GPIO.setup(self.right_input_1, GPIO.OUT)
GPIO.setup(self.right_input_2, GPIO.OUT)
#left wheels
GPIO.setup(self.left_enable, GPIO.OUT)
GPIO.setup(self.left_input_1, GPIO.OUT)
GPIO.setup(self.left_input_2 ,GPIO.OUT)
# self.p1 = GPIO.PWM(self.right_enable, 20) # Created a PWM object
# self.p2 = GPIO.PWM(self.left_enable, 20)
# self.p1.start(0)
# self.p2.start(0)
def move_forward(self):
self.control='f'
#right wheels
GPIO.output(self.right_enable , GPIO.HIGH)
#self.p1.ChangeDutyCycle(100)
GPIO.output(self.right_input_1 , GPIO.LOW)
GPIO.output(self.right_input_2 , GPIO.HIGH)
#left wheels
GPIO.output(self.left_enable , GPIO.HIGH)
#self.p2.ChangeDutyCycle(100)
GPIO.output(self.left_input_1 , GPIO.LOW)
GPIO.output(self.left_input_2 , GPIO.HIGH)
def move_backward(self):
self.control='b'
#right wheels
GPIO.output(self.right_enable , GPIO.HIGH)
#self.p1.ChangeDutyCycle(100)
GPIO.output(self.right_input_1 , GPIO.HIGH)
GPIO.output(self.right_input_2 , GPIO.LOW)
#left wheels
GPIO.output(self.left_enable , GPIO.HIGH)
#self.p2.ChangeDutyCycle(100)
GPIO.output(self.left_input_1 , GPIO.HIGH)
GPIO.output(self.left_input_2 , GPIO.LOW)
def move_left(self):
#right wheels
GPIO.output(self.right_enable , GPIO.HIGH)
#self.p1.ChangeDutyCycle(100)
GPIO.output(self.right_input_1 , GPIO.LOW)
GPIO.output(self.right_input_2 , GPIO.HIGH)
#left wheels
GPIO.output(self.left_enable , GPIO.LOW)
#self.p2.ChangeDutyCycle(15)
GPIO.output(self.left_input_1 , GPIO.LOW)
GPIO.output(self.left_input_2 , GPIO.HIGH)
def move_right(self):
#right wheels
GPIO.output(self.right_enable , GPIO.LOW)
#self.p1.ChangeDutyCycle(15)
GPIO.output(self.right_input_1 , GPIO.LOW)
GPIO.output(self.right_input_2 , GPIO.HIGH)
#left wheels
GPIO.output(self.left_enable , GPIO.HIGH)
#self.p2.ChangeDutyCycle(100)
GPIO.output(self.left_input_1 , GPIO.LOW)
GPIO.output(self.left_input_2 , GPIO.HIGH)
def stop_car(self):
self.control=''
#right wheels
#GPIO.output(self.right_enable , GPIO.LOW)
self.p1.ChangeDutyCycle(0)
GPIO.output(self.right_input_1 , GPIO.LOW)
GPIO.output(self.right_input_2 , GPIO.LOW)
#left wheels
#GPIO.output(self.left_enable , GPIO.LOW)
self.p2.ChangeDutyCycle(0)
GPIO.output(self.left_input_1 , GPIO.LOW)
GPIO.output(self.left_input_2 , GPIO.LOW)
def brake(self):
if(self.control==''):
pass
elif(self.control=='f'):
print( "Applying brake.")
self.move_backward()
time.sleep(self.runtime)
self.stop_car()
self.control=''
elif(self.control=='b'):
print ("Applying brake.")
self.move_forward()
time.sleep(self.runtime)
self.stop_car()
self.control=''
else:
pass
def close(self):
GPIO.cleanup()
if __name__ == '__main__':
time.sleep(2)
car = MoveCar()
car.initialize()
car.move_forward()
time.sleep(2)
car.move_backward()
time.sleep(3)
car.move_right()
time.sleep(3)
car.move_left()
time.sleep(3)
#car.brake()
car.stop_car()
car.close()