Experimental: qimessaging API

Warning

This is still a work in progress, and API may change in the future.

What is qimessaging

qimessaging is a new architecture, allowing you to use NAOqi APIs with a new and easier syntax.

Calling methods

  • Instead of creating a proxy to a module, create a session and ask for a service. You will get a Python object that has the same methods as the remote module, just like before:

    import qi
    
    session = qi.Session()
    session.connect("tcp://nao.local:9559")
    tts = session.service("ALTextToSpeech")
    tts.say("Hello, World")
    
  • You can also call methods asynchronously by using the _async keyword:

    import qi
    
    HEY_ANIMATION_1 = "animations/Stand/Gestures/Hey_1";
    
    app = qi.Application()
    session = qi.Session()
    session.connect("tcp://nao.local:9559")
    memory = session.service("ALMemory")
    tts = session.service("ALTextToSpeech")
    bhm = session.service("ALBehaviorManager")
    motion = session.service("ALMotion")
    motion.wakeUp()
    animationDone = bhm.runBehavior(HEY_ANIMATION_1, _async=True)
    tts.say("Hello")
    # block until the animation is over
    # runBehavior returns nothing so the return value is None
    animationDone.value()
    motion.rest()
    

Subscribing to events

  • You do not need a broker, and should create a qi.Application instead, that will take care of creating an event loop, among other things.

  • You no longer need a module to subscribe to an event. Instead, use the subscriber method on ALMemory. It returns an object with a signal property on which you can connect callbacks:

    import qi
    
    app = qi.Application()
    session = qi.Session()
    session.connect("tcp://nao.local:9559")
    memory = session.service("ALMemory")
    tts = session.service("ALTextToSpeech")
    
    def on_touched(event):
        # value is 1 when pressed, 0 when released
        if event > 0:
            tts.say("ouch")
    
    subscriber = memory.subscriber("FrontTactilTouched")
    subscriber.signal.connect(on_touched)
    
    app.run()
    

Writing services

  • Call session.registerService with any Python class you want. All public methods of the object will be available for other services by default.

  • You need to be connected to a special service called ServiceDirectory in order to register a new service. Such a service is provided by the naoqi process.

    import qi
    
    class HelloService:
        def __init__(self, session):
            self.tts = session.service("ALTextToSpeech")
    
        def greet(self):
            self.tts.say("Hello, world")
    
    
    app = qi.Application()
    session = qi.Session()
    session.connect("tcp://nao.local:9559")
    hello = HelloService(session)
    session.registerService("Hello", hello)
    app.run()