闹起事件处理10秒延迟

时间:2013-08-07 11:12:14

标签: python event-handling delay led nao-robot

我正在使用Windows-XP计算机上的NAO机器人和Python 2.7。

我想在语音中检测标记。整个过程都有效,但遗憾的是我现在必须面对10秒的延迟并且我的事件未被检测到(回调函数未被调用)。

首先,我的主要功能:

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)

这里是我的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)

请问,有人有同样的问题吗? 有人可以给我一个建议吗?

提前致谢!

3 个答案:

答案 0 :(得分:1)

以下是如何使用更新的Naoqi版本:

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.")

答案 1 :(得分:0)

您正在订阅模块外的活动。如果我没有错,你必须使用__init__方法。

class SpeechEventModule(ALModule):

    def __init__(self, name, ext_memory):
        ALModule.__init__(self, name)
        memory = ALProxy("ALMemory")
        leds = ALProxy("ALLeds")

无论如何,检查你的主要功能是否一直在运行(如果你遇到键盘中断会更好),或者你的程序将在他能够捕获任何关键字之前结束。

try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    print
    print "Interrupted by user, shutting down"
    myBroker.shutdown()
    sys.exit(0)

看看this tutorial,它可能会有所帮助。

答案 2 :(得分:0)

也许你应该尝试手动绑定回调,这样做:

def __init__(self, name, ext_memory):
    ALModule.__init__(self, name)
    self.BIND_PYTHON( self.getName(),"onBookmarkDetected" );

这是我在一些Choregraphe盒子中使用回调时所做的。