C++ - How to write a qimessaging client¶
Introduction¶
This guide will teach you how to write a qimessaging client in C++, to interact with the various services offered by the robot.
Prerequisites¶
- An installed NAOqi SDK for your operating system.
- A C++ project setup in your favorite C++ development environment, ready to use the headers and libraries provided by the NAOqi SDK.
Write your function¶
First you need to write the function that does the work you want.
You can use the qi::Session::service
method to get access to a service (local
or remote). This method will return a qi::AnyObject
. In this example, the
danceManager
object works as any other qi::AnyObject
, please refer to its
documentation for more details.
#include <qi/session.hpp>
void dance(qi::SessionPtr session)
{
qi::AnyObject danceManager = session->service("DanceManager");
danceManager.call<void>("dance");
// this function blocks here, it should return a future
// we keep the function like this for the sake of the simplicity of this example
}
Then you must register your function in the module. If you don’t want to use modules, you can skip directly to the Manual way section.
void registerDance(qi::ModuleBuilder* mb)
{
mb->advertiseMethod("dance", &dance);
}
QI_REGISTER_MODULE("dancemodule", ®isterDance);
Finally, create your module through CMake.
cmake_minimum_required(VERSION 2.8)
project(dancedancerotation)
find_package(qibuild)
find_package(qimodule)
qi_create_module(dancemodule
SRC
src/dance.cpp
DEPENDS
QI
)
Warning
The name you give to QI_REGISTER_MODULE
in the C++ file must be the same
as the one you give to qi_create_module
in the CMake file.
Running your client¶
Fully automated way¶
qilaunch can be used to call a function in a module. So, with the module you just created, qilaunch can take care of all the boiler plate of parsing arguments, connecting a session, forwarding logs, etc, and just run that function.
To run your function, just do:
qilaunch -n dancedanceman --function dancemodule.dance
Semi-automated way¶
If you don’t want to use qilaunch, you can write your own main function.
The first step to interact with qi services is to connect a
qi::Session
to the Service Directory
of the robot. The Service Directory address is represented by a standard URL.
One simple way to achieve this is to use the helper class ApplicationSession, which will fetch the URL from the command line (using the –qi-url option), or use a default value of localhost.
Finally, you call qi::Session::asyncCallModule
to call the function and give
the session as the first parameter automatically.
#include <qi/applicationsession.hpp>
int main(int argc, char** argv)
{
qi::ApplicationSession app(argc, argv);
app.startSession(); // connect the session
qi::SessionPtr session = app.session();
qi::Future<void> future = session->asyncCallModule("dancedance.dance");
future.value(); // wait for the end
return 0;
}
Manual way¶
If you don’t want to use module, you can of course call your function directly. The code is very similar to the semi-automated way.
#include <qi/applicationsession.hpp>
#include "dance.hpp"
int main(int argc, char** argv)
{
qi::ApplicationSession app(argc, argv);
app.startSession(); // connect the session
qi::SessionPtr session = app.session();
dance(session);
return 0;
}