-
Notifications
You must be signed in to change notification settings - Fork 0
/
mimicRight.py
executable file
·90 lines (80 loc) · 2.29 KB
/
mimicRight.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
#!/usr/bin/env python
import alsaaudio
import sys
import time
import audioop
from Maestor import maestor
robot = maestor()
hitTimes = []
oldTime = time.time()
leftHit = 0
leftBase = 0
rightHit = 0
rightBase = 0
def startStopTime():
global oldTime
currTime = time.time()
interval = currTime - oldTime
if interval > .115:
oldTime = currTime
hitTimes.append(interval)
print hitTimes
def loadJointValues():
global leftHit
global rightHit
global leftBase
global rightBase
f = open("hitConfig.txt", "r")
for line in f:
words = line.split(" ")
if words[0] == "Left:":
leftHit = float(words[1])
elif words[0] == "Right:":
rightHit = float(words[1])
leftBase = float(robot.getProperties("LWY", "position"))
rightBase = float(robot.getProperties("RWY", "position"))
print leftBase
print leftHit
f.close()
def hitDrum():
robot.setProperty("RWY", "position", rightHit)
time.sleep(.1)
robot.setProperty("RWY", "position", rightBase)
def printHits():
for i in hitTimes:
time.sleep(i)
hitDrum()
print "Hit!"
def listen():
#Initialize and set the properties of PCM object
card = 'default'
audioInput = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL, card)
audioInput.setchannels(2)
audioInput.setrate(44100)
audioInput.setformat(alsaaudio.PCM_FORMAT_S16_LE)
audioInput.setperiodsize(160)
oldL = 1
oldR = 1
try:
#Start an big loop that gets and analyzes audio data
for i in range(0, 5000):
l, data = audioInput.read()
if l > 0:
spike = 0
lchan = audioop.tomono(data, 2, 1, 0)
rchan = audioop.tomono(data, 2, 0, 1)
lmax = audioop.max(lchan, 2)
rmax = audioop.max(rchan, 2)
if spike < lmax:
spike = lmax
if spike < rmax:
spike = rmax
if spike > 1000:
startStopTime()
time.sleep(.001) #audio refresh rate
except KeyboardInterrupt :
sys.exit() #TODO make it actually exit
if __name__ == '__main__':
loadJointValues()
listen()
printHits()