-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimatronic_movements.py
515 lines (429 loc) · 17 KB
/
animatronic_movements.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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
import time
import threading
from pydispatch import dispatcher
import random
# Valve1 -> 0x20, GP0 -> Eye right
# Valve2 -> 0x21, GP4 -> Eye left
# Valve3 -> 0x20, GP1 -> Eyelid Up
# Valve4 -> 0x21, GP5 -> Eyelid down
# Valve5 -> 0x20, GP2 -> Mustache
# Valve6 -> NONE
# Valve7 -> 0x20, GP3 -> Neck down
# Valve8 -> 0x21, GP6 -> Neck up
# Valve9 -> 0x20, GP4 -> Left shoulder out
# Valve10 -> 0x21, GP7 -> Left shoulder in
# Valve11 -> 0x20, GP5 -> --unused--
# Valve12 -> 0x23, GP0 -> --unused--
# Valve13 -> 0x20, GP6 -> Left arm down
# Valve14 -> 0x23, GP1 -> Left arm up
# Valve15 -> 0x20, GP7 -> Mouth open
# Valve16 -> 0x23, GP2 -> Mouth closed
# Valve17 -> 0x21, GP0 -> Neck/torso left
# Valve18 -> 0x23, GP3 -> Neck/torso right
# Valve19 -> 0x21, GP1 -> Lean forward
# Valve20 -> 0x23, GP4 -> Lean Backward
# Valve21 -> 0x21, GP2 -> Right arm down
# Valve22 -> 0x23, GP5 -> Right arm up
# Valve23 -> 0x21, GP3 -> Right shoulder in
# Valve24 -> 0x23, GP6 -> Right shoulder out
# Valve25 -> NONE (COM 24v)
# Valve26 -> 0x23, GP7 -> --unused--
class Struct():
key = '' # A keyboard key press assigned to this movement
description = "" # A handy description of this movement
outputPin1 = [] # Index 0 is the I2C address for the GPIO expander, index 1 is the assigned pin on the GPIO expander
outputPin2 = [] # Optional second IO pin array, usually the inverse state of outputPin1 (see comment above)
midiNote = 0 # A MIDI note assigned to this movement to be recorded in a sequencer
outputPin1MaxTime = -1 # How much time is pin 1 allowed to be pulled high? (optional, -1 means infinite)
outputPin2MaxTime = -1 # See above (optional, -1 means infinite)
outputInverted = False # Invert high/low for this movement (optional)
callbackFunc = None # A function to call when the value has changed (optional)
linkedKeys = [] # Instead of a unique movement, this movement can link two or more keys together as a combined movement (optional)
mirroredKey = None # If mirroring is enabled, we call this key instead (i.e. swapping left and right shoulders, optional)
bIsOriginalMovement = False # Whether or not this movement was on the original 1981 animatronic
bEnableOnRetroMode = False # If activating retro mode (original original 1981 movements), invert this movement
class Movement:
all = []
# Callback function for eyes to do a best attempt to re-center the eyeballs when keys are released.
def onEyeMove( self, movement, val ):
if val == 0 and self.leftShoulder.keyIsPressed != True and self.rightShoulder.keyIsPressed != True:
pin = None
moveTime = 0.06
if movement == self.eyesLeft:
pin = self.eyesRight.outputPin1
elif movement == self.eyesRight:
pin = self.eyesLeft.outputPin1
moveTime = 0.04
if pin:
# Move eyes in the opposite direction for a split second.
# This will actuate the springs and hopefully re-center the eyeballs.
self.setPin(pin,1,movement)
time.sleep(moveTime)
# Don't disable output if user has pressed another eye key
if self.eyesRight.keyIsPressed != True and self.eyesLeft.keyIsPressed != True:
self.setPin(pin,0,movement)
# Callback function for eyes to attempt a half-blink... or, as I call it, the "stink eye"
def onEyeBlinkHalf(self, movement, val):
try:
blinked = self.eyesBlinkHalf.bBlinked
except:
self.eyesBlinkHalf.bBlinked = False
self.setPin(self.eyesBlinkFull.outputPin2, 0, self.eyesBlinkFull)
if val == 1 and self.eyesBlinkHalf.bBlinked != True:
self.eyesBlinkHalf.bBlinked = True
time.sleep(0.020)
self.setPin(self.eyesBlinkHalf.outputPin1, 0, self.eyesBlinkFull)
elif val == 1 and self.eyesBlinkHalf.bBlinked == True:
self.eyesBlinkHalf.bBlinked = False
self.setPin(self.eyesBlinkHalf.outputPin1, 0, self.eyesBlinkFull)
self.executeMovement(self.eyesBlinkFull.key,0)
time.sleep(0.025)
self.setPin(self.eyesBlinkFull.outputPin2, 0, self.eyesBlinkFull)
def __init__(self, gpio):
self.bMirrored = False # Swap left/right body movement to mirror animation
self.bRetroModeActive = False # Retro mode disables any movement not part of the original Pasqually
self.gpio = gpio
self.bThreadStarted = False
# Define all of our movements here.
self.rightShoulder = Struct()
self.rightShoulder.description = "Shoulder R"
self.rightShoulder.key = 'o'
self.rightShoulder.outputPin2 = [0x23, 6] # Shoulder out
self.rightShoulder.outputPin1 = [0x21, 3] # Shoulder in
self.rightShoulder.outputPin2MaxTime = 5*60
self.rightShoulder.outputPin1MaxTime = 5*60
self.rightShoulder.midiNote = 50
self.rightShoulder.mirroredKey = 'u'
self.rightShoulder.bIsOriginalMovement = True
self.all.append( self.rightShoulder )
self.leftShoulder = Struct()
self.leftShoulder.description = "Shoulder L"
self.leftShoulder.key = 'u'
self.leftShoulder.outputPin2 = [0x20, 4] # Shoulder out
self.leftShoulder.outputPin1 = [0x21, 7] # Shoulder in
self.leftShoulder.outputPin2MaxTime = 5*60
self.leftShoulder.outputPin1MaxTime = 5*60
self.leftShoulder.midiNote = 51
self.leftShoulder.mirroredKey = 'o'
self.leftShoulder.bIsOriginalMovement = True
self.all.append( self.leftShoulder )
self.leftAndRightElbows = Struct()
self.leftAndRightElbows.description = "Elbows L+R"
self.leftAndRightElbows.key = 'i'
self.leftAndRightElbows.midiNote = 52
self.leftAndRightElbows.linkedKeys = ['u','o']
self.all.append( self.leftAndRightElbows )
self.rightElbow = Struct()
self.rightElbow.key = 'l'
self.rightElbow.description = "Elbow R"
self.rightElbow.outputPin2 = [0x23, 5] # Arm up
self.rightElbow.outputPin1 = [0x21, 2] # Arm down
#self.rightElbow.outputPin2MaxTime = -1
#self.rightElbow.outputPin1MaxTime = 0.75
self.rightElbow.midiNote = 53
self.rightElbow.mirroredKey = 'j'
self.all.append( self.rightElbow )
self.leftElbow = Struct()
self.leftElbow.description = "Elbow L"
self.leftElbow.key = 'j'
self.leftElbow.outputPin2 = [0x23, 1] # Arm up
self.leftElbow.outputPin1 = [0x20, 6] # Arm down
#self.leftElbow.outputPin2MaxTime = -1
#self.leftElbow.outputPin1MaxTime = 0.75
self.leftElbow.midiNote = 54
self.leftElbow.mirroredKey = 'l'
self.all.append( self.leftElbow )
self.leftAndRightElbows = Struct()
self.leftAndRightElbows.description = "Arms L+R"
self.leftAndRightElbows.key = 'k'
self.leftAndRightElbows.midiNote = 55
self.leftAndRightElbows.linkedKeys = ['j','l']
self.all.append( self.leftAndRightElbows )
self.mouth = Struct()
self.mouth.description = "Mouth"
self.mouth.key = 'x'
self.mouth.outputPin1 = [0x20, 7] # Mouth open
self.mouth.outputPin2 = [0x23, 2] # Mouth close
self.mouth.outputPin1MaxTime = 0.75
#self.mouth.outputPin2MaxTime = 0.75
self.mouth.midiNote = 56
self.mouth.bIsOriginalMovement = True
self.all.append( self.mouth )
self.mustache = Struct()
self.mustache.description = "Mustache"
self.mustache.key = 'z'
self.mustache.outputPin1 = [0x20, 2]
self.mustache.outputPin1MaxTime = 60*5
self.mustache.midiNote = 57
self.mustache.bIsOriginalMovement = True
self.all.append( self.mustache )
self.mouthAndMustache = Struct()
self.mouthAndMustache.description = "Mouth + Mustache"
self.mouthAndMustache.key = 'c'
self.mouthAndMustache.midiNote = 51
self.mouthAndMustache.linkedKeys = ['z','x']
self.all.append( self.mouthAndMustache )
self.eyesLeft = Struct()
self.eyesLeft.description = "Eyes L"
self.eyesLeft.key = 'q'
self.eyesLeft.outputPin1 = [0x21, 4]
self.eyesLeft.outputPin1MaxTime = 60*10
self.eyesLeft.midiNote = 58
self.eyesLeft.callbackFunc = self.onEyeMove
self.eyesLeft.mirroredKey = 'e'
self.eyesLeft.bIsOriginalMovement = True
self.all.append( self.eyesLeft )
self.eyesRight = Struct()
self.eyesRight.description = "Eyes R"
self.eyesRight.key = 'e'
self.eyesRight.outputPin1 = [0x20, 0]
self.eyesRight.outputPin1MaxTime = 60*10
self.eyesRight.midiNote = 59
self.eyesRight.callbackFunc = self.onEyeMove
self.eyesRight.mirroredKey = 'q'
self.eyesRight.bIsOriginalMovement = True
self.all.append( self.eyesRight )
self.eyesBlinkFull = Struct()
self.eyesBlinkFull.description = "Eyes Blink"
self.eyesBlinkFull.key = 'w'
self.eyesBlinkFull.outputPin1 = [0x21, 5] # Eyes closeas
self.eyesBlinkFull.outputPin2 = [0x20, 1] # Eyes open
self.eyesBlinkFull.outputPin1MaxTime = 1
self.eyesBlinkFull.outputPin2MaxTime = 1
self.eyesBlinkFull.midiNote = 60
self.eyesBlinkFull.bIsOriginalMovement = True
self.all.append( self.eyesBlinkFull )
self.headLeft = Struct()
self.headLeft.description = "Head L"
self.headLeft.key = 'a'
self.headLeft.outputPin1 = [0x21, 0]
#self.headLeft.outputPin1MaxTime = 0.8
self.headLeft.midiNote = 61
self.headLeft.mirroredKey = 'd'
self.headLeft.bIsOriginalMovement = True
self.all.append( self.headLeft )
self.headRight = Struct()
self.headRight.description = "Head R"
self.headRight.key = 'd'
self.headRight.outputPin1 = [0x23, 3]
#self.headRight.outputPin1MaxTime = 0.8
self.headRight.midiNote = 62
self.headRight.mirroredKey = 'a'
self.headRight.bIsOriginalMovement = True
self.all.append( self.headRight )
self.headUp = Struct()
self.headUp.description = "Head Up"
self.headUp.key = 's'
self.headUp.outputPin1 = [0x21, 6] # Head up
self.headUp.outputPin2 = [0x20, 3] # Head down
self.headUp.midiNote = 63
self.headUp.bEnableOnRetroMode = True
self.all.append( self.headUp )
self.bodyLeanForward = Struct()
self.bodyLeanForward.description = "Lean Forward"
self.bodyLeanForward.key = 'm'
self.bodyLeanForward.outputPin1 = [0x23, 4] # Lean forward
self.bodyLeanForward.outputInverted = True
self.bodyLeanForward.midiNote = 64
self.all.append( self.bodyLeanForward )
#self.bodyLeanDown = Struct()
#self.bodyLeanDown.description = "Lean Back"
#self.bodyLeanDown.key = 'n'
#self.bodyLeanDown.outputPin1 = [0x23, 4] # Lean backward
#self.bodyLeanDown.outputPin1MaxTime = 2
#self.bodyLeanDown.midiNote = 63
#self.all.append( self.bodyLeanDown )
self.animationThreadsActive = False
self.blinkAnimationThread = None # A thread that plays some random blinking
self.eyeLeftRightAnimationThread = None # A thread that moves Pasqually's eyes left/right as he's speaking
self.mustacheAnimationThread = None # A thread that moves Pasqually's mustache briefly
for i in self.all:
i.keyIsPressed = False
val = 0
try:
if i.outputInverted == True:
val = 1
except:
i.outputInverted = False
i.pin1Time = 0
if i.outputPin1:
self.setPin(i.outputPin1, val, i)
if( i.outputPin2 ):
i.pin2Time = 0
self.setPin(i.outputPin2, 1-val, i)
def setMirrored(self, bMirrored):
if self.bMirrored == bMirrored:
return
self.bMirrored = bMirrored
print(f"Setting mirrored mode: {self.bMirrored}")
for movement in self.all:
if movement.mirroredKey:
mirroredKey = movement.mirroredKey
movement.mirroredKey = movement.key
movement.key = mirroredKey
# Fromat MIDI notes into a string to pass to the HTML front end
# This way, javascript key presses can control MIDI events directly
def getMidiNotes( self ):
fullString = ""
midiNote1 = None
midiNote2 = None
for i in self.all:
fullString += i.key
midiNote1 = str(i.midiNote)
if( len(midiNote1 ) < 2 ):
midiNote1 = "0" + midiNote1
fullString+=midiNote1+"00"
fullString+=","
return fullString
def getAllMovementInfo(self):
allMovements = []
for i in self.all:
movementInfo = [i.key, i.midiNote]
allMovements.append(movementInfo)
return allMovements
# Monitor state of IO and disable any that have been left on past the maximum allowed time.
def updatePins(self):
while self:
time.sleep(0.1)
for i in self.all:
if i.outputPin1MaxTime > -1 and i.pin1Time > 0:
i.pin1Time-=0.1
if i.pin1Time <= 0:
i.pin1Time = 0
self.setPin(i.outputPin1, 0, i)
if i.outputPin2MaxTime > -1 and i.pin2Time > 0:
i.pin2Time-=0.1
if i.pin2Time <= 0:
i.pin2Time = 0
self.setPin(i.outputPin2, 0, i)
# Set the GPIO pin state. When using the MCP23008 GPIO expander, index 0 of the pin value is the I2C address of the GPIO bank...
# ...and index 1 is the assigned pin on the MCP23008. For example, "0x20 pin 1"
def setPin( self, pin, val, movement ):
self.gpio.set_pin_from_address(pin[0], pin[1], val)
def executeMovement( self, key, val ):
bDoCallback = False
for i in self.all:
if( i.key == key and key ):
if val == 1 and i.keyIsPressed == False:
i.keyIsPressed = True
bDoCallback = True
elif val == 0 and i.keyIsPressed == True:
i.keyIsPressed = False
bDoCallback = True
if bDoCallback:
if len(i.linkedKeys) > 0:
for linkedKey in i.linkedKeys:
self.executeMovement(linkedKey, val)
return True
# If RetroMode is active and this wasn't part of the original movement, don't set the pin.
if self.bRetroModeActive and not i.bIsOriginalMovement:
if i.bEnableOnRetroMode:
self.setPin(i.outputPin1, 1, i)
self.setPin(i.outputPin2, 0, i)
return True
if i.outputInverted == True:
val = 1 - val
print(f"{i.description}: {val}")
self.setPin(i.outputPin1, val, i)
if( val == 1 ):
i.pin1Time = i.outputPin1MaxTime
else:
i.pin1Time = 0
if( i.outputPin2 ):
self.setPin(i.outputPin2, 1-val, i)
if( val == 1 ):
i.pin2Time = 0
else:
i.pin2Time = i.outputPin2MaxTime
func = i.callbackFunc
try:
# If a callback function has been defined for this movement, execute in a thread.
if bDoCallback:
t = threading.Thread(target=i.callbackFunc, args = (i,val))
t.setDaemon(True)
t.start()
except:
pass
break
if self.bThreadStarted == False:
self.bThreadStarted = True
t = threading.Thread(target=self.updatePins, args = ())
t.setDaemon(True)
t.start()
return bDoCallback
def executeMidiNote(self, midiNote, val):
for movement in self.all:
if movement.midiNote == midiNote:
self.executeMovement(movement.key, val)
break
def setRetroMode(self, bEnable):
self.bRetroModeActive = bEnable
print(f"Set Retro Mode: {bEnable}")
for movement in self.all:
if not movement.bIsOriginalMovement:
self.executeMovement(movement.key, 0)
def stopAllAnimationThreads(self):
self.animationThreadsActive = False
def animShutdown():
dispatcher.send(signal="keyEvent", key=self.headUp.key, val=0)
dispatcher.send(signal="keyEvent", key=self.mustache.key, val=0)
if self.blinkAnimationThread and self.blinkAnimationThread.is_alive():
self.blinkAnimationThread.join()
#if self.eyeLeftRightAnimationThread and self.eyeLeftRightAnimationThread.is_alive():
# self.eyeLeftRightAnimationThread.join()
if not self.animationThreadsActive:
# Reset animated movements back to their default state.
dispatcher.send(signal="keyEvent", key=self.eyesBlinkFull.key, val=0)
dispatcher.send(signal="keyEvent", key=self.eyesLeft.key, val=0)
dispatcher.send(signal="keyEvent", key=self.eyesRight.key, val=0)
animShutdownThread = threading.Thread(target=animShutdown, daemon=True)
animShutdownThread.start()
def playWakewordAcknowledgement(self):
def mustacheShake():
dispatcher.send(signal="keyEvent", key=self.headUp.key, val=1)
dispatcher.send(signal="keyEvent", key=self.mustache.key, val=1)
time.sleep(0.2)
dispatcher.send(signal="keyEvent", key=self.mustache.key, val=0)
time.sleep(0.2)
dispatcher.send(signal="keyEvent", key=self.mustache.key, val=1)
time.sleep(0.2)
dispatcher.send(signal="keyEvent", key=self.mustache.key, val=0)
time.sleep(0.2)
self.mustacheAnimationThread = threading.Thread(target=mustacheShake, daemon=True)
self.mustacheAnimationThread.start()
def playBlinkAnimation(self):
self.animationThreadsActive = True
maxTimeBetweenBlinks = 3 # Seconds
def blink():
while self.animationThreadsActive:
dispatcher.send(signal="keyEvent", key=self.eyesBlinkFull.key, val=1)
time.sleep(random.uniform(0.1, 0.5))
dispatcher.send(signal="keyEvent", key=self.eyesBlinkFull.key, val=0)
time.sleep(random.uniform(0.25, maxTimeBetweenBlinks))
# Use a thread for the blink animation
self.blinkAnimationThread = threading.Thread(target=blink, daemon=True)
self.blinkAnimationThread.start()
def playEyeLeftRightAnimation(self):
if self.eyeLeftRightAnimationThread and self.eyeLeftRightAnimationThread.is_alive():
return
self.animationThreadsActive = True
def eyes():
bMoveLeft = random.choice([True, False])
eyeMovement = None
while self.animationThreadsActive:
if bMoveLeft:
eyeMovement = self.eyesLeft.key
else:
eyeMovement = self.eyesRight.key
dispatcher.send(signal="keyEvent", key=eyeMovement, val=1)
if not self.animationThreadsActive:
return
time.sleep(random.uniform(0.5, 3))
dispatcher.send(signal="keyEvent", key=eyeMovement, val=0)
if not self.animationThreadsActive:
return
time.sleep(random.uniform(0.5, 3))
bMoveLeft = not bMoveLeft
# Use a thread for the blink animation
self.eyeLeftRightAnimationThread = threading.Thread(target=eyes, daemon=True)
self.eyeLeftRightAnimationThread.start()