more stable implementation of speech stuff

This commit is contained in:
Pavel Lutskov
2019-02-02 13:38:35 +01:00
parent 3217b7f841
commit 655c5418fd
7 changed files with 77 additions and 279 deletions

View File

@@ -51,7 +51,7 @@ def handle_request(r):
elif module == 'speech':
if message == 'recognize':
if STATE in ('idle', 'imitate'):
if STATE in ('idle', 'imitate', 'dead'):
permission = True
elif message == 'imitate':
if STATE == 'idle':
@@ -61,6 +61,13 @@ def handle_request(r):
if STATE == 'imitate':
STATE = 'idle'
permission = True
elif message == 'kill':
STATE = 'dead'
permission = True
elif message == 'revive':
if STATE == 'dead':
STATE = 'idle'
permission = True
rospy.logdebug(
'GOT REQUEST FROM %s TO %s.\nPERMISSION: %s.\nSTATE IS NOW: %s.' % (

View File

@@ -13,9 +13,15 @@ from controller import inform_controller_factory
in_progress = False
state = 'idle'
IMITATE = 'repeat'
KILL = 'kill'
REVIVE = 'go'
STOP = 'stop'
voc_state = {
'idle': 'start',
'imitate': 'stop'
'idle': [IMITATE, KILL],
'imitate': [STOP, KILL],
'killed': [REVIVE]
}
_inform_controller = inform_controller_factory('speech')
@@ -23,10 +29,17 @@ _inform_controller = inform_controller_factory('speech')
def done_cb(_, result):
global in_progress, state
rospy.loginfo(result)
if result.word == 'start' and _inform_controller('imitate'):
rospy.loginfo('SPEECH CLIENT: {}'.format(result))
if result is None:
in_progress = False
return
if result.word == IMITATE and _inform_controller('imitate'):
state = 'imitate'
elif result.word == 'stop' and _inform_controller('stop'):
elif result.word == STOP and _inform_controller('stop'):
state = 'idle'
elif result.word == KILL and _inform_controller('kill'):
state = 'killed'
elif result.word == REVIVE and _inform_controller('revive'):
state = 'idle'
in_progress = False
@@ -46,11 +59,10 @@ if __name__ == '__main__':
client.cancel_goal()
in_progress = False
state = 'idle'
continue
if not in_progress:
in_progress = True
client.send_goal(RequestSpeechGoal([voc_state[state]]),
done_cb)
rospy.Rate(2).sleep()
else:
if not in_progress:
in_progress = True
client.send_goal(RequestSpeechGoal(voc_state[state]),
done_cb)
rospy.Rate(4).sleep()

View File

@@ -10,7 +10,6 @@ from teleoperation.msg import RequestSpeechAction, RequestSpeechResult
speech_broker = None
almem = None
r = False
def request_speech(goal):
@@ -18,7 +17,9 @@ def request_speech(goal):
sas.set_succeeded(RequestSpeechResult(word=''))
return
while not sas.is_preempt_requested() and not speech_detector.have_word():
while (not sas.is_preempt_requested() and
not speech_detector.have_word() and
not rospy.is_shutdown()):
rospy.Rate(10).sleep()
if speech_detector.have_word():
@@ -40,16 +41,24 @@ class SpeechDetectorModule(ALModule):
self.asr = ALProxy('ALSpeechRecognition')
self.tts = ALProxy('ALTextToSpeech')
self.asr.setLanguage('English')
self.running = False
almem.subscribeToEvent("WordRecognized",
"speech_detector",
"on_word_recognized")
self.asr.pause(True)
self._busy = False
def start_speech(self, voc):
if self.running:
def get_status(self):
print(almem.getData('ALSpeechRecognition/Status'))
def start_speech(self, voc, resume=False):
if self._busy != resume:
return False
self.voc = voc
self.asr.setVocabulary(voc, False)
self.asr.subscribe(self.subid)
if not resume:
self.voc = voc
self.asr.setVocabulary(voc, False)
self.asr.subscribe(self.subid)
self.asr.pause(False)
self.running = True
self._busy = True
return True
def have_word(self):
@@ -60,23 +69,26 @@ class SpeechDetectorModule(ALModule):
self.recognized = None
return result
def stop_speech(self):
if not self.running:
def stop_speech(self, pause=False):
if not self._busy:
return
self.asr.unsubscribe(self.subid)
self.asr.pause(True)
self.running = False
if not pause:
self.asr.unsubscribe(self.subid)
self._busy = False
def on_word_recognized(self, *_args):
word, conf = almem.getData('WordRecognized')
print(word, conf)
if conf > 0.4:
self.stop_speech()
self.stop_speech(pause=True)
self.tts.say(word)
self.recognized = word
else:
self.stop_speech()
else:
self.stop_speech(pause=True)
self.tts.say('I didn\'t understand. Please repeat')
self.start_speech(self.voc)
self.start_speech(self.voc, resume=True)
@@ -84,21 +96,16 @@ if __name__ == '__main__':
rospy.init_node('speech_server')
speech_broker = ALBroker('speech_broker', '0.0.0.0', 0,
os.environ['NAO_IP'], 9559)
speech_detector = SpeechDetectorModule('speech_detector')
almem = ALProxy('ALMemory')
almem.subscribeToEvent("WordRecognized",
"speech_detector",
"on_word_recognized")
speech_detector.asr.pause(True)
speech_detector = SpeechDetectorModule('speech_detector')
sas = actionlib.SimpleActionServer('speech_server', RequestSpeechAction,
execute_cb=request_speech,
auto_start=False)
sas.start()
while not rospy.is_shutdown():
rospy.Rate(4).sleep()
if speech_detector.running:
speech_detector.stop_speech()
rospy.Rate(1).sleep()
while sas.is_active():
pass
speech_broker.shutdown()

View File

@@ -21,8 +21,8 @@ VMIN = 0.3
VMAX = 1.0
def thirdway(a, b):
return a + (b - a) / 3
def n_way(a, b, n=3):
return a + (b - a) / n
def global_init():
@@ -32,12 +32,12 @@ def global_init():
x = json.load(f)
cx, cy, cz = x['cr']
FW = thirdway(cx, x['fw']), x['fw']
BK = thirdway(cx, x['bk']), x['bk']
LT = thirdway(cy, x['lt']), x['lt']
RT = thirdway(cy, x['rt']), x['rt']
LR = thirdway(cz, x['lr']), x['lr']
RR = thirdway(cz, x['rr']), x['rr']
FW = n_way(cx, x['fw']), x['fw']
BK = n_way(cx, x['bk']), x['bk']
LT = n_way(cy, x['lt']), x['lt']
RT = n_way(cy, x['rt']), x['rt']
LR = n_way(cz, x['lr'], 2), x['lr']
RR = n_way(cz, x['rr'], 2), x['rr']
_inform_controller = inform_controller_factory('walker')
@@ -96,7 +96,8 @@ if __name__ == '__main__':
if not any(movement):
rospy.logdebug('WALKER: STOP')
_inform_controller('stop')
mp.move(0, 0, 0)
# mp.move(0, 0, 0)
mp.stopMove()
continue
permission = _inform_controller('move')