-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
287 lines (244 loc) · 7.18 KB
/
controller.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import RPi.GPIO as GPIO
import time
import sys, termios, tty, os
#-----------------------------------------------------
# Global settings
# set off warnings
GPIO.setwarnings(False)
#set GPIO numbering mode
GPIO.setmode(GPIO.BCM)
degree = 1./150.*(12-2) # max - min servo positiont
increment = degree*3 # increment to add to servo positions
step_sleep = 0.03 # time to sleep between increments
#-----------------------------------------------------
# TILT
# servo MG90D, limited angle type
# tested responsivness from 3.2 to 11.1 duty PWM
# approx 160 degrees angle
# setup
pin = 21
GPIO.setup(pin, GPIO.OUT)
tilt = GPIO.PWM(pin, 50) # 50 Hz
# set min and max values for tilt pwm
tilt_min = 4
tilt_mid = 7.5
tilt_max = 11
# initiate
tilt.start(0)
tilt.ChangeDutyCycle(0)
tilt_current = tilt_min
# startup seqence
def tilt_initiate():
global tilt_current
while tilt_current < tilt_mid:
print('initiating')
tilt_current += increment
tilt.ChangeDutyCycle(tilt_current)
time.sleep(step_sleep)
tilt_current = tilt_mid
tilt.ChangeDutyCycle(tilt_current)
time.sleep(step_sleep)
tilt.ChangeDutyCycle(0)
print('tilt ready')
# shutdown sequence
def tilt_shutdown():
global tilt_current
while tilt_current > tilt_min:
print('shutting down tilt')
tilt_current -= increment
tilt.ChangeDutyCycle(tilt_current)
time.sleep(step_sleep)
tilt_current = tilt_min
tilt.ChangeDutyCycle(tilt_current)
time.sleep(step_sleep)
tilt.ChangeDutyCycle(0)
print('tilt shut down')
# functions to change values of tilt
def tilt_up():
global tilt_current
if tilt_current < tilt_max:
print('tilting up')
tilt_current += increment
tilt.ChangeDutyCycle(tilt_current)
time.sleep(step_sleep)
def tilt_down():
global tilt_current
if tilt_current > tilt_min:
print('tilting down')
tilt_current -= increment
tilt.ChangeDutyCycle(tilt_current)
time.sleep(step_sleep)
#tilt.ChangeDutyCycle(tilt_min)
# function to read keyboard input
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
#-----------------------------------------------------
# ROTOR
# servo MG996R, digi hi torque
# setup
pin = 20
GPIO.setup(pin, GPIO.OUT)
rotor = GPIO.PWM(pin, 50) # 50 Hz
# set min and max values for tilt pwm
rotor_min = 2
rotor_mid = 7
rotor_max = 12
# initiate
rotor.start(0)
rotor.ChangeDutyCycle(0)
rotor_current = rotor_mid
# startup seqence
def rotor_initiate():
global rotor_current
while rotor_current < rotor_mid:
print('initiating rotor')
rotor_current += increment
rotor.ChangeDutyCycle(rotor_current)
time.sleep(step_sleep)
rotor_current = rotor_mid
rotor.ChangeDutyCycle(rotor_current)
time.sleep(step_sleep)
rotor.ChangeDutyCycle(0)
global rotor_current
if rotor_current > rotor_mid:
while rotor_current > rotor_mid:
print('initiating rotor')
rotor_current -= increment
rotor.ChangeDutyCycle(rotor_current)
time.sleep(step_sleep)
rotor_current = rotor_mid
rotor.ChangeDutyCycle(rotor_current)
if rotor_current < rotor_mid:
while rotor_current < rotor_mid:
print('initiating rotor')
rotor_current += increment
rotor.ChangeDutyCycle(rotor_current)
time.sleep(step_sleep)
rotor_current = rotor_mid
rotor.ChangeDutyCycle(rotor_current)
time.sleep(step_sleep)
rotor.ChangeDutyCycle(0)
print('rotor ready')
# shutdown sequence
def rotor_shutdown():
global rotor_current
if rotor_current > rotor_mid:
while rotor_current > rotor_mid:
print('shutting down')
rotor_current -= increment
rotor.ChangeDutyCycle(rotor_current)
time.sleep(step_sleep)
rotor_current = rotor_mid
rotor.ChangeDutyCycle(rotor_current)
if rotor_current < rotor_mid:
while rotor_current < rotor_mid:
print('shutting down')
rotor_current += increment
rotor.ChangeDutyCycle(rotor_current)
time.sleep(step_sleep)
rotor_current = rotor_mid
rotor.ChangeDutyCycle(rotor_current)
time.sleep(step_sleep)
rotor.ChangeDutyCycle(0)
print('rotor shut down')
# functions to change values of rotor
def rotor_left():
global rotor_current
if rotor_current < rotor_max:
print('rotating left')
rotor_current += increment
rotor.ChangeDutyCycle(rotor_current)
time.sleep(step_sleep)
def rotor_right():
global rotor_current
if rotor_current > rotor_min:
print('rotating right')
rotor_current -= increment
rotor.ChangeDutyCycle(rotor_current)
time.sleep(step_sleep)
#tilt.ChangeDutyCycle(tilt_min)
#-------------------------------------------------
# MOTION
#Set variables for the GPIO motor pins
pinMotorAForwards = 8
pinMotorABackwards = 7
pinMotorBForwards = 9
pinMotorBBackwards = 10
# Set the GPIO Pin mode
GPIO.setup(pinMotorAForwards, GPIO.OUT)
GPIO.setup(pinMotorABackwards, GPIO.OUT)
GPIO.setup(pinMotorBForwards, GPIO.OUT)
GPIO.setup(pinMotorBBackwards, GPIO.OUT)
# Turn all motors off
def StopMotors():
GPIO.output(pinMotorAForwards, 0)
GPIO.output(pinMotorABackwards, 0)
GPIO.output(pinMotorBForwards, 0)
GPIO.output(pinMotorBBackwards, 0)
# Turn both motors forwards
def Forwards():
GPIO.output(pinMotorAForwards, 1)
GPIO.output(pinMotorABackwards, 0)
GPIO.output(pinMotorBForwards, 1)
GPIO.output(pinMotorBBackwards, 0)
# Turn both motors backwards
def Backwards():
GPIO.output(pinMotorAForwards, 0)
GPIO.output(pinMotorABackwards, 1)
GPIO.output(pinMotorBForwards, 0)
GPIO.output(pinMotorBBackwards, 1)
def Left():
GPIO.output(pinMotorAForwards, 0)
GPIO.output(pinMotorABackwards, 1)
GPIO.output(pinMotorBForwards, 1)
GPIO.output(pinMotorBBackwards, 0)
def Right():
GPIO.output(pinMotorAForwards, 1)
GPIO.output(pinMotorABackwards, 0)
GPIO.output(pinMotorBForwards, 0)
GPIO.output(pinMotorBBackwards, 1)
StopMotors()
button_delay = 0.2
# START
tilt_initiate()
rotor_initiate()
while True:
char = getch()
if (char == "q"):
tilt_shutdown()
rotor_shutdown()
exit(0)
if (char == "i"):
tilt_up()
if (char == "k"):
tilt_down()
if (char == "j"):
rotor_left()
if (char == "l"):
rotor_right()
if (char == "a"):
print 'moving left'
Left()
time.sleep(button_delay)
if (char == "d"):
print 'moving right'
Right()
time.sleep(button_delay)
elif (char == "w"):
print 'moving forwards'
Forwards()
time.sleep(button_delay)
elif (char == "s"):
print 'moving backwards'
Backwards()
time.sleep(button_delay)
StopMotors()
tilt.ChangeDutyCycle(0)
rotor.ChangeDutyCycle(0)