I am working with a NAO-robot on a Windows-XP machine and Python 2.7.
I want to detect markers in speech. The whole thing worked, but unfortunately I have to face now a 10 Secounds delay and my events aren't detected (the callback function isnt invoked).
First, my main-function:
from naoqi import ALProxy, ALBroker
from speechEventModule import SpeechEventModule
myString = "Put that \\mrk=1\\ there."
NAO_IP = "192.168.0.105"
NAO_PORT = 9559
memory = ALProxy("ALMemory", NAO_IP, NAO_PORT)
tts = ALProxy("ALTextToSpeech", NAO_IP, NAO_PORT)
tts.enableNotifications()
myBroker = ALBroker("myBroker",
"0.0.0.0", # listen to anyone
0, # find a free port and use it
NAO_IP, # parent broker IP
NAO_PORT) # parent broker port
global SpeechEventListener
SpeechEventListener = SpeechEventModule("SpeechEventListener", memory)
memory.subscribeToEvent("ALTextToSpeech/CurrentBookMark", "SpeechEventListener", "onBookmarkDetected")
tts.say(initialString)
And here my speechEventModule:
from naoqi import ALModule
from naoqi import ALProxy
NAO_IP = "192.168.0.105"
NAO_PORT = 9559
SpeechEventListener = None
leds = None
memory = None
class SpeechEventModule(ALModule):
def __init__(self, name, ext_memory):
ALModule.__init__(self, name)
global memory
memory = ext_memory
global leds
leds = ALProxy("ALLeds",NAO_IP, NAO_PORT)
def onBookmarkDetected(self, key, value, message):
print "Event detected!"
print "Key: ", key
print "Value: " , value
print "Message: " , message
if(value == 1):
global leds
leds.fadeRGB("FaceLeds", 0x00FF0000, 0.2)
if(value == 2):
global leds
leds.fadeRGB("FaceLeds", 0x000000FF, 0.2)
Please, do anybody have the same problem? Can anybody give me an advice?
Thanks in advance!
Here is how it would be done with a more recent Naoqi version:
import qi
import argparse
class SpeechEventListener(object):
""" A class to react to the ALTextToSpeech/CurrentBookMark event """
def __init__(self, session):
super(SpeechEventListener, self).__init__()
self.memory = session.service("ALMemory")
self.leds = session.service("ALLeds")
self.subscriber = self.memory.subscriber("ALTextToSpeech/CurrentBookMark")
self.subscriber.signal.connect(self.onBookmarkDetected)
# keep this variable in memory, else the callback will be disconnected
def onBookmarkDetected(self, value):
""" callback for event ALTextToSpeech/CurrentBookMark """
print "Event detected!"
print "Value: " , value # key and message are not useful here
if value == 1:
self.leds.fadeRGB("FaceLeds", 0x00FF0000, 0.2)
if value == 2:
self.leds.fadeRGB("FaceLeds", 0x000000FF, 0.2)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--ip", type=str, default="127.0.0.1",
help="Robot IP address. On robot or Local Naoqi: use '127.0.0.1'.")
parser.add_argument("--port", type=int, default=9559,
help="Naoqi port number")
args = parser.parse_args()
# Initialize qi framework
connection_url = "tcp://" + args.ip + ":" + str(args.port)
app = qi.Application(["SpeechEventListener", "--qi-url=" + connection_url])
app.start()
session = app.session
speech_event_listener = SpeechEventListener(session)
tts = session.service("ALTextToSpeech")
# tts.enableNotifications() --> this seems outdated
while True:
raw_input("Say something...")
tts.say("Put that \\mrk=1\\ there.")
You are subscribing to the event outside your module. if I am not wrong you have to do it into the __init__
method.
class SpeechEventModule(ALModule):
def __init__(self, name, ext_memory):
ALModule.__init__(self, name)
memory = ALProxy("ALMemory")
leds = ALProxy("ALLeds")
Anyway, check that your main function keeps running forever (better if you catch a keyboard interruption) or you program will end before he can catch any keyword.
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print
print "Interrupted by user, shutting down"
myBroker.shutdown()
sys.exit(0)
Take a look to this tutorial, it could be helpful.
Perhaps you should try to manually bind the callback, by doing so:
def __init__(self, name, ext_memory):
ALModule.__init__(self, name)
self.BIND_PYTHON( self.getName(),"onBookmarkDetected" );
It's what I do when using callback in some Choregraphe boxes.
User contributions licensed under CC BY-SA 3.0