main.cpp¶
This generated file is a standard main.cpp, this one only print an “Hello, world” on standard output.
We now extend a little this file to communicate with NAOqi and call bind function from NAOqi’s module.
First of all you need to do a small command line option parser with at least --pip and --pport option.
You need create a proxy to the module you want use. To do that you must include <alcommon/alproxy.h>, then create a proxy to the module and finally you can call a method from this module.
#include <iostream>
#include <stdlib.h>
#include <alcommon/alproxy.h>
#include <alcommon/albroker.h>
int main(int argc, char* argv[])
{
// We will try to connect our broker to a running NAOqi
int pport = 9559;
std::string pip = "127.0.0.1";
// command line parse option
// check the number of arguments
if (argc != 1 && argc != 3 && argc != 5)
{
std::cerr << "Wrong number of arguments!" << std::endl;
std::cerr << "Usage: mymodule [--pip robot_ip] [--pport port]" << std::endl;
exit(2);
}
// if there is only one argument it should be IP or PORT
if (argc == 3)
{
if (std::string(argv[1]) == "--pip")
pip = argv[2];
else if (std::string(argv[1]) == "--pport")
pport = atoi(argv[2]);
else
{
std::cerr << "Wrong number of arguments!" << std::endl;
std::cerr << "Usage: mymodule [--pip robot_ip] [--pport port]" << std::endl;
exit(2);
}
}
// Sepcified IP or PORT for the connection
if (argc == 5)
{
if (std::string(argv[1]) == "--pport"
&& std::string(argv[3]) == "--pip")
{
pport = atoi(argv[2]);
pip = argv[4];
}
else if (std::string(argv[3]) == "--pport"
&& std::string(argv[1]) == "--pip")
{
pport = atoi(argv[4]);
pip = argv[2];
}
else
{
std::cerr << "Wrong number of arguments!" << std::endl;
std::cerr << "Usage: mymodule [--pip robot_ip] [--pport port]" << std::endl;
exit(2);
}
}
// A broker needs a name, an IP and a port to listen:
const std::string brokerName = "mybroker";
// Create your own broker
boost::shared_ptr<AL::ALBroker> broker =
AL::ALBroker::createBroker(brokerName, "0.0.0.0", 54000, pip, pport);
/**
* Create a proxy to a module in NAOqi process so that we can call
* the bind method of this module
* AL::ALProxy proxy(<broker>, <module_name>);
*/
// Create a proxy to ALTextToSpeechProxy
AL::ALProxy proxy(broker, "ALTextToSpeech");
/**
* If the bind methode is a void return
* you can call bind methode using callVoid
* proxy.callVoid(<bind_method>, <parameter>, ...)
*/
// Call say methode
proxy.callVoid("say", std::string("Sentence to say!"));
/**
* Otherwise you can use template call methode
* type res = proxy.call<type>(<bind_methode>, <parameter>, ...);
*/
// Call ping function that return a boolean
bool res = proxy.call<bool>("ping");
return 0;
}
Now you know how to communicate with NAOqi and a method from a module, we want to add some functionality to NAOqi. We will now create a module which it extends, adds some new features.