-
Notifications
You must be signed in to change notification settings - Fork 1
/
voiceNode.py
111 lines (96 loc) · 3.09 KB
/
voiceNode.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
import speech_recognition as sr
import time
import socket
import sys
import threading
import struct
from random import randint
from packet import Packet, CMD
botIp = {} #used as a dictionary for botIps
botIp['donatello'] = ("137.143.52.234", 13676)
botIp['raphael'] = ("137.143.63.255", 13698)
cmd_buffer = list()
LIN_V = 0.2
ANG_V = 1
KA_PKT = Packet()
KA_PKT.write_ubyte(CMD.KEEPALIVE)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# obtain audio from the microphone
r = sr.Recognizer()
m = sr.Microphone()
def keepAlive():
for key in botIp:
sock.sendto(str(KA_PKT), botIp[key])
threading.Timer(3, keepAlive).start()
def controlLoop(ip):
global LIN_V, ANG_V
word = None
while('end' != word and 'exit' != word):
if len(cmd_buffer)> 0 :
word = cmd_buffer.pop(0)
print word
lv = 0
av = 0
if('forward' == word or 'charge' == word):
lv = LIN_V * 1
elif('backward' == word or 'reverse' == word):
lv = LIN_V * -1
elif('right' == word):
av = ANG_V * -1
elif('left' == word):
av = ANG_V * 1
elif('faster' == word):
LIN_V += 0.2
ANG_V += 0.2
print LIN_V, ANG_V
elif('slower' == word):
LIN_V = LIN_V - 0.2
ANG_V = ANG_V - 0.2
print LIN_V, ANG_V
elif('stop' == word):
lv = 0
av = 0
elif('sing' == word):
pkt = Packet()
pkt.write_ubyte(CMD.SOUND)
val = randint(0, 6)
pkt.write_ubyte(val)
sock.sendto(str(pkt), ip)
else:
pass
pkt = Packet()
pkt.write_ubyte(CMD.MOTION)
pkt.write_double(lv)
pkt.write_double(av)
sock.sendto(str(pkt), ip)
print('leaving control loop')
def callback(recognizer, audio):
global cmd_buffer
try:
words = recognizer.recognize_google(audio).split()
cmd_buffer.extend([str(item.lower()) for item in words])
except sr.UnknownValueError:
print ("Didn't catch that")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
keepAlive()
try:
print("Quiet please!")
with m as source: r.adjust_for_ambient_noise(source)
print("Ambient levels detected.")
stop_listening = r.listen_in_background(m, callback)
while True:
try:
if len(cmd_buffer)> 0 :
word = cmd_buffer.pop(0)
if(word == 'donatello' or word == 'raphael'):
print('dropping into {0}\'s control'.format(word))
controlLoop(botIp[word])
time.sleep(0.1)
except sr.UnknownValueError:
print ("Didn't catch that")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
except KeyboardInterrupt:
stop_listening()
pass