-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfurby.py
More file actions
475 lines (388 loc) · 12.7 KB
/
Copy pathfurby.py
File metadata and controls
475 lines (388 loc) · 12.7 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#!/usr/bin/env python
# Import required modules
# from abc import update_abstractmethods
import time
import RPi.GPIO as GPIO
import random
import math
def prepareBoard():
GPIO.cleanup()
# Declare the GPIO settings
GPIO.setmode(GPIO.BOARD)
## equivalent to BCM pin 25
sensor = 22
GPIO.setmode(GPIO.BOARD)
# GPIO.setup(sensor, GPIO.IN)
# Set the filename and path for the sound card in use (See: https://howchoo.com/g/mmnhmti2zjz/how-to-detect-that-audio-is-currently-being-output-in-linux-and-use-it-to-call-a-program#create-an-audio-output-monitor-script)
soundcard_status_file = '/proc/asound/card2/pcm0p/sub0/status'
# Turn off GPIO warnings caused by us declaring our pins outside of the start_furby and stop_furby functions
GPIO.setwarnings(True)
clockwise = False
AIN1 = 29 # PIN 5
AIN2 = 31 # PIN 6
PWMA = 7 # PIN 4
STDBY = 13 # PIN 27
IR = 18 # 24
CAL = 36 # 16
PWMA_TEST = 32 # PWM0 BCM12
TONGUE = 33
ir_ticks = 0
def ir_callback(channel):
print("IR Tick")
cal_ticks = 0
def cal_callback(channel):
# cal_ticks+=1
print("CAL Tick: " + str(channel))
def tongue_callback(channel):
print("TONGUE PRESSED")
pwm.ChangeDutyCycle(0)
def start_furby():
if clockwise:
# Drive the motor clockwise
GPIO.output(AIN1, GPIO.HIGH) # Set AIN1
GPIO.output(AIN2, GPIO.LOW) # Set AIN2
else:
GPIO.output(AIN1, GPIO.LOW) # Set AIN1
GPIO.output(AIN2, GPIO.HIGH) # Set AIN2
# GPIO.output(29, GPIO.HIGH) # Set pin 5 to test functionality
# Set the motor speed
#GPIO.output(PWMA, GPIO.HIGH) # Set PWMA
# pwm.ChangeDutyCycle(50)
pwm.start(40)
#GPIO.output(PWMA_TEST, GPIO.HIGH) # Set AIN2
GPIO.add_event_detect(CAL,GPIO.RISING,callback=cal_callback, bouncetime=200)
GPIO.add_event_detect(TONGUE,GPIO.RISING,callback=tongue_callback, bouncetime=200)
GPIO.add_event_detect(IR,GPIO.RISING,callback=ir_callback, bouncetime=20)
# Disable STBY (standby)
GPIO.output(STDBY, GPIO.HIGH)
print("Running")
time.sleep(1000)
def stop_furby():
# Reset all the GPIO pins by setting them to LOW
GPIO.output(AIN1, GPIO.LOW) # Set AIN1
GPIO.output(AIN2, GPIO.LOW) # Set AIN2
GPIO.output(PWMA, GPIO.LOW) # Set PWMA
GPIO.output(STDBY, GPIO.LOW) # Set STBY
def main():
print "starting"
# Set up GPIO pins
GPIO.setup(PWMA, GPIO.OUT) # Connected to PWMA
GPIO.setup(AIN2, GPIO.OUT) # Connected to AIN2
GPIO.setup(AIN1, GPIO.OUT) # Connected to AIN1
GPIO.setup(STDBY, GPIO.OUT) # Connected to STBY
## AIN2 / AIN1 seem broken. Testing if other IO works.
# GPIO.setup(29, GPIO.OUT) # Connected to AIN2 possibly, testing
count = 0
start_furby()
def foo():
if GPIO.input(sensor):
print "object detected"
while GPIO.input(sensor) is True:
time.sleep(0.001)
print "end of ir gap"
count+=1
print count
stop_furby()
time.sleep(0.01)
else:
print "no object detected"
time.sleep(0.0005)
stop_furby()
time.sleep(0.03)
if count > 3:
time.sleep(3)
clockwise != clockwise
count = 0
stop_furby()
return
# Open file and check contents
with open(soundcard_status_file, 'r') as fh:
value = fh.read()
if value == 'RUNNING':
start_furby()
else:
stop_furby()
class Furby:
def __init__(self):
self.pos = 1
self.maxPos = 0
self.maxError = 30
self.speed = 70
self.clockwise = True
self.calibrated = False
self.calibrating = False
self.des = 0
self.desPerc = 0
GPIO.cleanup()
# Declare the GPIO settings
GPIO.setmode(GPIO.BOARD)
GPIO.setmode(GPIO.BOARD)
# GPIO.setup(IR, GPIO.IN)
GPIO.setup(IR,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(CAL,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(TONGUE,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(PWMA_TEST, GPIO.OUT)
self.pwm = GPIO.PWM(PWMA_TEST, 300)
GPIO.setup(PWMA, GPIO.OUT) # Connected to PWMA
GPIO.setup(AIN2, GPIO.OUT) # Connected to AIN2
GPIO.setup(AIN1, GPIO.OUT) # Connected to AIN1
GPIO.setup(STDBY, GPIO.OUT) # Connected to STBY
GPIO.add_event_detect(CAL,GPIO.RISING,callback=self.calCallback, bouncetime=200)
GPIO.add_event_detect(TONGUE,GPIO.RISING,callback=self.tongueCallback, bouncetime=200)
GPIO.add_event_detect(IR,GPIO.RISING,callback=self.irCallback)
self.setDirection(True)
def start(self):
print("starting")
GPIO.output(STDBY, GPIO.HIGH)
self.pwm.start(self.speed)
def stop(self):
print("stopping at ", self.pos)
print("-------------reached des ", self.des, self.desPerc, " by error of ", self.des - self.pos)
self.pwm.stop()
GPIO.output(STDBY, GPIO.LOW)
print("maxpos", self.maxPos)
def setDirection(self, clockwise):
if clockwise:
print("Setting direction clockwise")
GPIO.output(AIN1, GPIO.HIGH) # Set AIN1
GPIO.output(AIN2, GPIO.LOW) # Set AIN2
else:
print("Setting direction counter clockwise")
GPIO.output(AIN1, GPIO.LOW) # Set AIN1
GPIO.output(AIN2, GPIO.HIGH) # Set AIN2
self.clockwise = clockwise
def setSpeed(self, speed): # 0 < speed < 100
self.speed = speed
self.pwm.ChangeDutyCycle(speed)
def calCallback(self, pin):
print('calibration pin pressed')
print("cal callback " + str(self.pos))
self.calibrated = True
measuredPosition = self.pos
self.pos = 0
print("-----POSITION-RESET----", measuredPosition)
def irCallback(self, pin):
print "[" + str(self.pos)+"]",
if self.maxPos < self.pos:
self.maxPos = self.pos
if self.clockwise:
self.pos += 1
"""
if self.pos >= self.des:
print("reached destination of "+str(self.des))
self.stop()
"""
else:
self.pos -= 1
"""
if self.pos <= self.des:
print("reached destination of "+str(self.des))
self.stop()
"""
def tongueCallback(self, pin):
print("tongue callback")
def moveDown(self, des):
print("moving down, "+str(des))
self.setDirection(False)
self.des = des
self.start()
while self.pos > des:
time.sleep(0.0001)
self.stop()
def moveUp(self, des):
print("moving up, "+str(des))
self.setDirection(True)
print("moving up")
self.des = des
self.start()
while self.pos < des:
time.sleep(0.0001)
self.stop()
print("destination reached")
def moveTo(self, angle):
# Calculate the destination based on the angle (percentile)
# and avoid exceeding the maximum position-error range
des = min(
math.floor((self.maxPos/100)*angle),
(self.maxPos - self.maxError)
) # TODO set maxError smarter based on better maxPos averaging
# With cal = cycle can move over the hinge that sets the position to 0
# Without cal = move will happen without going over the hinge
print('--------------moveto', angle, self.pos, des)
deltaWithoutCal = abs(self.pos - des)
deltaWithCal = min(
abs(((des + self.maxPos) - self.pos)), # angle80 -> angle10 = (100 + 10) - 80 = 30
abs(((self.maxPos + self.pos) - des)) # angle10 -> angle80 = (100 + 10) - 80 = 30
)
print('delta with cal', deltaWithCal)
print('delta without cal', deltaWithoutCal)
self.desPerc = angle
if deltaWithoutCal < deltaWithCal:
print("moving without cal")
if des > self.pos:
self.moveUp(des)
else:
self.moveDown(des)
else:
print("moving with cal, ")
if des < self.pos:
# make current pos negative based on maxpos
self.pos = self.pos - self.maxPos
# pos will reset to 0
# move up instead of down
self.moveUp(des)
print('finished moving up', self.pos)
else:
# make des negative based on maxpos
nDes = des - self.maxPos
# pos will reset to 0
# move down instead of up
self.moveDown(nDes)
print('finished moving down', self.pos)
print('finished move to angle ', angle)
def printMaxPos(self):
print(self.maxPos)
def calibrate(self):
print("calibrating")
# self.pos = 0
self.des = 0
self.calibrating = True
self.setDirection(True)
self.start()
time.sleep(5)
self.stop()
time.sleep(1)
print("has max pos", self.maxPos)
self.moveTo(10)
self.moveTo(50)
self.moveTo(80)
self.moveTo(90)
self.moveTo(10)
self.calibrating = False
print('MaxPos =', self.maxPos)
print("==========CALIBRATED===========")
"""
self.setDirection(False)
self.calibrated = False
time.sleep(0.01)
self.start()
while self.calibrated == False:
print("calibrating..")
time.sleep(0.01)
print("cal reached")
self.stop()
self.calibrated = False
"""
if __name__ == '__main__':
try:
prepareBoard()
f = Furby()
#f.start()
#time.sleep(10)
#f.stop()
f.calibrate()
print("calibrate command finished")
print("wait..")
print(f.pos, f.maxPos, f.des)
"""
f.moveTo(30)
f.moveTo(1)
f.moveTo(60)
time.sleep(1)
"""
print('---------------------------------------ready')
raw_input("Press Enter to continue...")
f.moveTo(90)
raw_input("Press Enter to continue...")
f.moveTo(10)
raw_input("Press Enter to continue...")
f.moveTo(90)
raw_input("Press Enter to continue...")
f.moveTo(10)
raw_input("Press Enter to continue...")
f.moveTo(90)
raw_input("Press Enter to continue...")
f.moveTo(10)
f.moveTo(90)
for x in range(0, 100, 5):
raw_input("Press Enter to continue...")
f.moveTo(x)
"""
raw_input("Press Enter to continue...")
f.moveTo(30)
raw_input("Press Enter to continue...")
" Move from 30 to 10 didn't go as smoothly.
" it went beyond the destination, while it was breaking, and pressed the reset button
f.moveTo(10)
raw_input("Press Enter to continue...")
f.moveTo(20)
raw_input("Press Enter to continue...")
f.moveTo(30)
raw_input("Press Enter to continue...")
f.moveTo(40)
raw_input("Press Enter to continue...")
f.moveTo(50)
raw_input("Press Enter to continue...")
f.moveTo(60)
raw_input("Press Enter to continue...")
f.moveTo(70)
raw_input("Press Enter to continue...")
f.moveTo(80)
raw_input("Press Enter to continue...")
f.moveTo(90)
raw_input("Press Enter to continue...")
f.moveTo(100)
"""
print("testing talk mode")
raw_input("Press Enter to continue...")
for x in range(6):
f.moveTo(80)
f.moveTo(90)
f.moveTo(10)
f.moveTo(90)
raw_input("Press Enter to continue...")
f.moveTo(10)
f.printMaxPos()
"""
f.moveTo(1)
#time.sleep(1)
f.moveTo(60)
#time.sleep(1)
f.moveTo(90)
f.setSpeed(100)
for i in range(3):
f.moveTo(70)
f.moveTo(75)
f.moveTo(90)
f.setSpeed(30)
for i in range(3):
f.moveTo(20)
f.moveTo(15)
f.moveTo(100)
"""
"""
#r = random.randint(0,f.maxPos)
f.moveTo(r)
time.sleep(1)
f.calibrate()
f.moveTo(0)
time.sleep(1)
f.moveTo(100)
time.sleep(1)
f.moveTo(200)
"""
#time.sleep(1)
#f.moveTo(10)
#time.sleep(1)
#f.moveTo(60)
# f.moveTo(30)
# f.moveTo(60)
except KeyboardInterrupt:
print("Cleanup by keyboard interrupt")
GPIO.cleanup()
except Exception as e:
print("uncaught error")
print(e)
finally:
print("Cleanup")
GPIO.cleanup()