-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathServoController.py
343 lines (304 loc) · 9.38 KB
/
ServoController.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
from __future__ import division
import time
import Adafruit_PCA9685
from threading import Thread
pwm = Adafruit_PCA9685.PCA9685()
# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)
portMain = 610
starMain = 200
portForarm = 570
starForarm = 200
portHand = 570
starHand = 240
# Center Lift Servo (0) Values
upHeight = 205
neutralHeight = 275
downHeight = 450
# Port Drive Servo (1) Values
forwardPort = 440
neutralPort = 375
backPort = 330
# Starboard Drive Servo (2) Values
forwardStarboard = 292
neutralStarboard = 357
backStarboard = 402
# moves the torso from a neutral position upwards, allowing the torso to pivot forwards or backwards
def height_neutral_to_up():
height = neutralHeight
print('setting center servo (0) Neutral --> Up position')
while (height > upHeight):
height = height - 1
pwm.set_pwm(0, 0, height)
time.sleep(0.001)
print('center servo (0) set to: Up position\n ')
# rotates the torso outwards, enough so that when TARS pivots and lands, the bottom of the torso is
# flush with the ground. Making the torso flush with the ground is an intentional improvement from
# previous programs, where TARS would land and then slide a little on smooth surfaces, which while
# allowing for a simple walking program, inhibited TARS' ability to walk on surfaces with different
# coefficients of friction
def torso_neutral_to_forwards():
port = neutralPort
starboard = neutralStarboard
print('setting port and starboard servos (1)(2) Neutral --> Forward')
while (port < forwardPort):
port = port + 1
starboard = starboard - 1
pwm.set_pwm(1, 1, port)
pwm.set_pwm(2, 2, starboard)
time.sleep(0.0001)
print('port and starboard servos (1)(2) set to: Forward position\n ')
def torso_neutral_to_backwards():
port = neutralPort
starboard = neutralStarboard
print('setting port and starboard servos (1)(2) Neutral --> Forward')
while (port > backPort):
port = port - 1
starboard = starboard + 1
pwm.set_pwm(1, 1, port)
pwm.set_pwm(2, 2, starboard)
time.sleep(0.0001)
print('port and starboard servos (1)(2) set to: Forward position\n ')
# rapidly shifts the torso height from UP --> DOWN and then returns --> UP, which should cause TARS
# to pivot and land on it's torso
def torso_bump():
height = upHeight
print('performing a torso bump\nsetting center servo (0) Up --> Down position FAST')
while (height < downHeight):
height = height + 2
pwm.set_pwm(0, 0, height)
time.sleep(0.000001)
print('setting center servo (0) Down --> Up position FAST')
while (height > upHeight):
height = height - 1
pwm.set_pwm(0, 0, height)
time.sleep(0.0001)
print('center servo (0) returned to Up position\n')
# returns the torso's vertical height and rotation to centered values from up height and forward
# rotation. Activates two external functions so movement in both axes can occur in parallel.
def torso_return():
t1 = Thread(target = torso_return_rotation)
t2 = Thread(target = torso_return_vertical)
t1.start()
t2.start()
# returns torso's rotation to neutral from forward
def torso_return_rotation():
port = forwardPort
starboard = forwardStarboard
print('setting port and starboard servos (1)(2) Forward --> Neutral position')
while (port > neutralPort):
port = port - 1
starboard = starboard + 1
pwm.set_pwm(1, 1, port)
pwm.set_pwm(2, 2, starboard)
time.sleep(0.005)
print('port and starboard servos (1)(2) set to: Neutral position\n ')
# returns torso's vertical to neutral from up
def torso_return_vertical():
height = upHeight
print('setting center servo (0) Up --> Down position')
# moving the torso down to create clearance for the rotation of the legs
while (height < downHeight):
height = height + 1
pwm.set_pwm(0, 0, height)
time.sleep(0.00005)
# moving the torso up from down to neutral
#time.sleep(.2)
while (height > neutralHeight):
height = height - 1
pwm.set_pwm(0, 0, height)
time.sleep(0.00001)
print('center servo (0) set to: Neutral position\n ')
def torso_return2():
t1 = Thread(target = torso_return_rotation2)
t2 = Thread(target = torso_return_vertical2)
t1.start()
t2.start()
# returns torso's rotation to neutral from forward
def torso_return_rotation2():
port = backPort
starboard = backStarboard
print('setting port and starboard servos (1)(2) Forward --> Neutral position')
while (port < neutralPort):
port = port + 1
starboard = starboard - 1
pwm.set_pwm(1, 1, port)
pwm.set_pwm(2, 2, starboard)
time.sleep(0.01)
print('port and starboard servos (1)(2) set to: Neutral position\n ')
# returns torso's vertical to neutral from up
def torso_return_vertical2():
height = upHeight
print('setting center servo (0) Up --> Down position')
# moving the torso down to create clearance for the rotation of the legs
while (height < downHeight):
height = height + 1
pwm.set_pwm(0, 0, height)
time.sleep(0.001)
# moving the torso up from down to neutral
time.sleep(.25)
while (height > neutralHeight):
height = height - 1
pwm.set_pwm(0, 0, height)
time.sleep(0.001)
print('center servo (0) set to: Neutral position\n ')
# moves the torso from neutral position to down
def neutral_to_down():
height = neutralHeight
print('setting center servo (0) Neutral --> Down position')
while (height < downHeight):
height = height + 1
pwm.set_pwm(0, 0, height)
time.sleep(0.001)
def down_to_up():
height = downHeight
print('setting center servo (0) Down --> Neutral position')
while (height > upHeight):
height = height - 1
pwm.set_pwm(0, 0, height)
time.sleep(0.001)
def down_to_neutral():
height = downHeight
print('setting center servo (0) Down --> Neutral position')
while (height > neutralHeight):
height = height - 1
pwm.set_pwm(0, 0, height)
time.sleep(0.001)
def neutral_to_down():
height = neutralHeight
print('setting center servo (0) Down --> Neutral position')
while (height < downHeight):
height = height + 1
pwm.set_pwm(0, 0, height)
time.sleep(0.001)
def turn_right():
port = neutralPort
starboard = neutralStarboard
while (port < forwardPort):
port = port + 1
starboard = starboard + 1
pwm.set_pwm(1, 1, port)
pwm.set_pwm(2, 2, starboard)
time.sleep(0.001)
def turn_left():
port = neutralPort
starboard = neutralStarboard
while (port > backPort):
port = port - 1
starboard = starboard - 1
pwm.set_pwm(1, 1, port)
pwm.set_pwm(2, 2, starboard)
time.sleep(0.001)
def neutral_from_right():
port = forwardPort
starboard = backStarboard
while (port > neutralPort):
port = port - 1
starboard = starboard - 1
pwm.set_pwm(1, 1, port)
pwm.set_pwm(2, 2, starboard)
time.sleep(0.005)
pwm.set_pwm(1, 1, neutralPort)
pwm.set_pwm(2, 2, neutralStarboard)
def neutral_from_left():
port = backPort
starboard = forwardStarboard
while (port < neutralPort):
port = port + 1
starboard = starboard + 1
pwm.set_pwm(1, 1, port)
pwm.set_pwm(2, 2, starboard)
time.sleep(0.005)
pwm.set_pwm(1, 1, neutralPort)
pwm.set_pwm(2, 2, neutralStarboard)
# Arm shenanigans
# port Main Arm
def portMainPlus():
global portMain
portMain = portMain - 10
pwm.set_pwm(3, 3, portMain)
time.sleep(0.0001)
print("increase starMain")
print(portMain)
def portMainMinus():
global portMain
portMain = portMain + 10
pwm.set_pwm(3, 3, portMain)
time.sleep(0.0001)
print("decrease starMain")
print(portMain)
# port Forarm
def portForarmPlus():
global portForarm
portForarm = portForarm - 10
pwm.set_pwm(4, 4, portForarm)
time.sleep(0.0001)
print("increase starForarm")
print(portForarm)
def portForarmMinus():
global portForarm
portForarm = portForarm + 10
pwm.set_pwm(4, 4, portForarm)
time.sleep(0.0001)
print("decrease starForarm")
print(portForarm)
# port Hand
def portHandPlus():
global portHand
portHand = portHand - 10
pwm.set_pwm(5, 5, portHand)
time.sleep(0.0001)
print("increase starHand")
print(portHand)
def portHandMinus():
global portHand
portHand = portHand + 10
pwm.set_pwm(5, 5, portHand)
time.sleep(0.0001)
print("decrease starHand")
print(portHand)
# starboard Main Arm
def starMainPlus():
global starMain
starMain = starMain + 10
pwm.set_pwm(6, 6, starMain)
time.sleep(0.0001)
print("increase starMain")
print(starMain)
def starMainMinus():
global starMain
starMain = starMain - 10
pwm.set_pwm(6, 6, starMain)
time.sleep(0.0001)
print("decrease starMain")
print(starMain)
# port Forarm
def starForarmPlus():
global starForarm
starForarm = starForarm + 10
pwm.set_pwm(7, 7, starForarm)
time.sleep(0.0001)
print("increase starForarm")
print(starForarm)
def starForarmMinus():
global starForarm
starForarm = starForarm - 10
pwm.set_pwm(7, 7, starForarm)
time.sleep(0.0001)
print("decrease starForarm")
print(starForarm)
# port Hand
def starHandPlus():
global starHand
starHand = starHand + 10
pwm.set_pwm(8, 8, starHand)
time.sleep(0.0001)
print("increase starHand")
print(starHand)
def starHandMinus():
global starHand
starHand = starHand - 10
pwm.set_pwm(8, 8, starHand)
time.sleep(0.0001)
print("decrease starHand")
print(starHand)