This guide will teach you how to write a qimessaging client in Python, to interact with the various services offered by the robot.
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
Let us make the robot speak by using the say method of the ALTextToSpeech service.
tts = session.service("ALTextToSpeech")
tts.say("Hello Word")
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.
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 _async
argument that performs a call in an asynchronous manner, and notifies you when
the call finishes using a qi.Future:
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 qi.Future
for more complete documentation, but here is what you
most definitely need to know:
qi.Future
, and can be
accessed using qi.Future.error()
.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.value()
and qi.Future.error()
to get the stored value or error.qi.Future.add_callback()
.