-
Notifications
You must be signed in to change notification settings - Fork 0
/
FollowTheSound.py
executable file
·90 lines (69 loc) · 2.31 KB
/
FollowTheSound.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
from Maestor import maestor
import alsaaudio
import audioop
import sys
import time
import math
robot = maestor()
audioInput = 0
maxPos = 1.5
minPos = -1.5
def initAudio():
global audioInput
#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)
def localize():
#Returns an error between -1 and 1 that specifies which direction to move the head to
try:
#Start an infite loop that gets and analyzes audio data
alpha = .05 #The width of the sigmoid. Gotta tweak it a little.
#Average difference
avgDiff = 0
for i in range(0, 100):
l, data = audioInput.read()
if l > 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)
#lowpass filter
if lmax < 70 and rmax < 70:
continue
#calculage the difference in intensity. Positive difference means the source
#is further to the left
diff = lmax - rmax
avgDiff = avgDiff + diff
time.sleep(.001) #audio refresh rate
avgDiff = avgDiff / 1000
print avgDiff
#Logistic Sigmoid function!
z = math.exp(-1 * alpha * avgDiff)
error = (1 / (1 + z)) - 0.5
return error
except Exception, e:
print e
def adjust(error):
#adjusts hubo's neck yaw to position himself closer to the source of the sound
pos = float(robot.getProperties("NKY", "position"))
pos = pos + error
if pos > maxPos:
pos = maxPos
elif pos < minPos:
pos = minPos
robot.setProperty("NKY", "position", pos) #will work on it. But for now.
print pos + error
def main():
#The main part of the program
initAudio()
while True:
err = localize()
adjust(err)
time.sleep(.25)
if __name__ == '__main__':
main()