Python - How to write a qimessaging client
This guide will teach you how to write a qimessaging client in Python, to interact with the various services offered by the robot.
- An installed python NAOqi SDK for your operating system.
Creating an application ¶
The first step to interact with qimessaging services is to create an Application, it will handle the connection to the robot.
import qi app = qi.Application() app.start() session = app.session
Calling a method on a service ¶
Let us make the robot speak by using the say method of the ALTextToSpeech service.
tts = session.service("ALTextToSpeech") tts.say("Hello Word")
Starting your application ¶
You can then run your application. It will connect to localhost by default, but you can override that.
python MyApp.py --qi-url tcp://nao.local:9559
For more information on the arguments your application can take, see: qi application arguments .
Making asynchronous calls using qi::async ¶
Most of the API are designed to block until the operation started by the call completes. For example with this code inside our main:
tts = session.service("ALTextToSpeech") motion = session.service("ALMotion") tts.say("This is a very very very very long sentence.") motion.moveTo(1, 0, 0); // go forward one meter
The robot will only start moving when he finishes speaking.
To perform both actions simultaneously, the API provides a special
argument that performs a call in an asynchronous manner, and notifies you when
the call finishes using a
tts = session.service("ALTextToSpeech") motion = session.service("ALMotion") sayOp = tts.say("This is a very very very very long sentence.", _async=True) moveOp = motion.moveTo(1, 0, 0, _async=True) # Wait for both operations to terminate. sayOp.wait() moveOp.wait()
Look at the
for more complete documentation, but here is what you
most definitely need to know:
If the method throws an exception, it is stored in the
qi.Future, and can be accessed using
qi.Future.wait()to wait for the future to complete. It can accept a timeout duration as argument, and will return the state of the future.
qi.Future.error()to get the stored value or error.
You can register a callback to be notified when the future finishes with