我如何使用事件' ALTextToSpeech / Status'拿到闹起的价值?

时间:2014-12-01 18:51:56

标签: python text-to-speech nao-robot

我希望获得"价值"的价值?我应该如何在Python中使用该事件?

    a = self.memoryProxy.subscribeToEvent("ALTextToSpeech/Status", "value",str)
    print "value"
    self.tts.setParameter("pitchShift", 1.1)        
    self.tts.post.say(str) 

1 个答案:

答案 0 :(得分:2)

根据您的naoqi版本,您可以使用以下模式之一。

旧的方式是这样的(见Naoqi doc):

from naoqi import ALProxy, ALBroker, ALModule
import argparse

# Global variable to store the TTSEventWatcher module instance
tts_event_watcher = None
memory = None

class TTSEventWatcher(ALModule):
    """ An ALModule to react to the ALTextToSpeech/Status event """

    def __init__(self, ip_robot, port_robot):
        super(TTSEventWatcher, self).__init__("tts_event_watcher")
        global memory
        memory = ALProxy("ALMemory", ip_robot, port_robot)
        self.tts = ALProxy("ALTextToSpeech", ip_robot, port_robot)
        memory.subscribeToEvent("ALTextToSpeech/Status",
                                "tts_event_watcher",  # module instance
                                "on_tts_status")  # callback name

    def on_tts_status(self, key, value, message):
        """ callback for event ALTextToSpeech/Status """
        print "TTS Status value:", value


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()

    event_broker = ALBroker("event_broker", "0.0.0.0", 0,
                            args.ip, args.port)
    global tts_event_watcher
    tts_event_watcher = TTSEventWatcher(args.ip, args.port)
    while True:
        raw_input("Watch events be triggered...")
        tts_event_watcher.tts.say("Hello!")

小心,那里有一些隐藏的技巧,例如强迫你将事件管理代码放在主文件中。

在较新的版本中,您可以使用这种功能更友好的模式:

import qi
import argparse

class HumanTrackedEventWatcher(object):
    """ A class to react to the ALTextToSpeech/Status event """

    def __init__(self, app):
        super(HumanTrackedEventWatcher, self).__init__()
        app.start()
        session = app.session
        self.memory = session.service("ALMemory")
        self.tts = session.service("ALTextToSpeech")
        self.subscriber = self.memory.subscriber("ALTextToSpeech/Status")
        self.subscriber.signal.connect(self.on_tts_status)
        # keep this variable in memory, else the callback will be disconnected

    def on_tts_status(self, value):
        """ callback for event ALTextToSpeech/Status """
        print "TTS Status value:", value


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(["HumanTrackedEventWatcher", "--qi-url=" + connection_url])
    tts_event_watcher = HumanTrackedEventWatcher(app)   

    while True:
        raw_input("Watch TTS events be triggered...")
        tts_event_watcher.tts.say("Hello!")