闹七事件处理延迟10秒

2024-10-05 13:34:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我在windowsxp和python2.7上使用NAO机器人。在

我想检测言语中的标记。整个过程都成功了,但不幸的是我现在不得不面对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)

这里是我的演讲事件模块:

^{pr2}$

拜托,有人有同样的问题吗? 谁能给我个建议吗?在

提前谢谢!在


Tags: fromimportipport事件ttsmemorynao
3条回答

也许您应该尝试手动绑定回调,方法是:

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

这是我在一些编排框中使用回调时所做的。在

您正在订阅模块外的事件。如果我没有错,你必须在__init__方法中执行它。在

class SpeechEventModule(ALModule):

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

不管怎样,检查一下你的主函数是否一直在运行(如果你捕捉到键盘中断的话会更好),否则你的程序会在他捕捉到任何关键字之前结束。在

^{pr2}$

看看this tutorial,可能会有帮助。在

下面是一个更新的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.")

相关问题 更多 >

    热门问题