How to build local libraries in Nao robot

After days struggling, finally, I figured out how to build local libraries in Nao robot.

So this post is about building the local libraries. Actually it is not a difficult problem. We could follow the official docs and probably get a good result, with the built libraries registered in the Naoqi framework ( THIS is what we are looking for). But when I did this things did not come as I expected. And one of main problems I encountered is my system, 64bit-based, is not compatible with the 32bit-based naoqi system. And this almost killed me. However, the good point is I learned many things when I was entangled. I wrote this post to share how to build the local libraries for a 64bit system.

At the very beginning, I posted all the codes needed for your reference.

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// main.cpp
#include "mymodule.h"
#include <boost/shared_ptr.hpp>
#include <alcommon/albroker.h>
#include <alcommon/albrokermanager.h>
#ifdef _WIN32
# define ALCALL __declspec(dllexport)
#else
# define ALCALL
#endif
extern "C"
{
ALCALL int _createModule(boost::shared_ptr<AL::ALBroker> broker)
{
AL::ALBrokerManager::setInstance(broker->fBrokerManager.lock());
AL::ALBrokerManager::getInstance()->addBroker(broker);
AL::ALModule::createModule<MyModule>(broker, "MyModule");
return 0;
}
ALCALL int _closeModule( )
{
return 0;
}
} // extern "C"

mymodule.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef MY_MODULE_H
# define MY_MODULE_H
# include <iostream>
# include <alcommon/almodule.h>
namespace AL
{
class ALBroker;
}
class MyModule : public AL::ALModule
{
public:
MyModule(boost::shared_ptr<AL::ALBroker> broker,
const std::string &name);
virtual ~MyModule();
virtual void init();
void printHello();
void printWord(const std::string &word);
bool returnTrue();
};
#endif // MY_MODULE_H

mymodule.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "mymodule.h"
#include <iostream>
#include <alcommon/albroker.h>
MyModule::MyModule(boost::shared_ptr<AL::ALBroker> broker,
const std::string& name)
: AL::ALModule(broker, name)
{
setModuleDescription("My own custom module.");
functionName("printHello", getName(), "Print hello to the world");
BIND_METHOD(MyModule::printHello);
functionName("printWord", getName(), "Print a given word.");
addParam("word", "The word to be print.");
BIND_METHOD(MyModule::printWord);
functionName("returnTrue", getName(), "Just return true");
setReturn("boolean", "return true");
BIND_METHOD(MyModule::returnTrue);
}
MyModule::~MyModule()
{
}
void MyModule::init()
{
std::cout << returnTrue() << std::endl;
}
void MyModule::printHello()
{
std::cout << "Hello!" << std::endl;
}
void MyModule::printWord(const std::string &word)
{
std::cout << word << std::endl;
}
bool MyModule::returnTrue()
{
return true;
}

qiproject.xml

1
2
3
<project version="3">
<qibuild name="mymodule"></qibuild>
</project>

CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
cmake_minimum_required(VERSION 2.8)
project(mymodule)
find_package(qibuild)
set(_srcs
mymodule.cpp
mymodule.h
main.cpp)
qi_create_lib(mymodule SHARED ${_srcs} SUBFOLDER naoqi)
set_target_properties(mymodule PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
qi_use_lib(mymodule ALCOMMON ALPROXIES BOOST)

Before we compile all the files, we are supposed to build the proper toolchain for this project. Since the local libraries are 32bit based, we need to use the 32bit toolchain. And this toolchain is included in the naoqi 32bit sdk and the latter be downloaded from the official site. After extracting the files from the zip, we should create the new toolchain. Let’s name it as naoqi32 like below:

1
2
cd naoqi-sdk-32 // change the unzip folder
qitoolchain create naoqi32 toolchain.xml

Then change to the project folder, we configure our project by command:

1
qibuild config --wizard

Following the instructions like below and choose the naoqi32 toolchain. After that choose N to stop configuring.
[Image: file:///-/blob/CJNAAAZNrNY/oRw5u6FmSpmjE189AJPacQ]Now we can configure and compile our project like this:

1
2
3
qibuild clean -f
qibuild configure
qibuild make

If everything works fine we will find the libmymodule.so in path build-naoqi32/sdk/lib/naoqi .

Then we can scp this file to naoqi system:

1
scp libmymodule.so nao@192.168.1.102:~/yourModuleFolder

Also, we need to configure the autoload.ini file in nao robot to let the naoqi system load the created module:

1
nano ~/naoqi/preferences/autoload.ini

Modify the autoload.ini like this:

1
2
3
4
# autoload.ini
[user] // add the ***absolute*** path for your module
/home/nao/yourModuleFolder/libmymodule.so

The last step is to register this module. First we should stop the naoqi system:

1
2
ssh nao@192.168.1.102 // connect to the naoqi
nao stop // stop naoqi

Then we restart the system:

1
naoqi start

From the log info window, we can find this information, which means the module has been successfully loaded:
[Image: file:///-/blob/CJNAAAZNrNY/hirysEijCqxNfr9k0hSuLw]That is what we want!!!

Extra

The only thing we need to do when we transfer remote module to local is to add this in CMakeLists.txt:

1
set_target_properties(mymodule PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")

And also rewrite the main function to make it suitable for module compilation.

By Mingfei

0%