Difference between revisions of "Nao tutorial 2: First module"
m |
|||
(19 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | Go to root: [[TFM-Nao-Goalkeeper]] | |
− | Now, it is time for you to delve deeper into the subject, and the best way is to code and test your own example module. Nao has some good default applications that allow you to control his behaviour (like Choregraphe), but this is nothing compared with the freedom that compiling your own applications gives to you. Hence, you will now practice with the [ | + | ---- |
+ | ---- | ||
+ | |||
+ | |||
+ | The [[Nao tutorial 1: First steps | first tutorial]] focused about giving you a painless introduction to Nao's world. You turned the robot on, connected to him through the web interface and SSH, configured some parameters and learnt a bit about the most important files and directories. | ||
+ | |||
+ | Now, it is time for you to delve deeper into the subject, and the best way is to code and test your own example module. Nao has some good default applications that allow you to control his behaviour (like Choregraphe), but this is nothing compared with the freedom that compiling your own applications gives to you. Hence, you will now practice with the [https://community.aldebaran.com/en/resources/software official SDK]. | ||
− | <span style="color:#606060">'''''NOTE: This tutorial | + | <span style="color:#606060">'''''NOTE: This 2011 tutorial was targeted for version 1.10.10 of the SDK and OS. Newer versions have changed a lot of things, making most of these tutorials deprecated.'''''</span> |
− | =Getting everything ready= | + | ==Getting everything ready== |
I guess that your testing environment is the same as in the previous tutorial, only with your Nao connecting via wifi now. Also, you will need to download some software from Aldebaran's webpage, but if you are reading this wiki, it's because you are connected to the internet (duh!). Remember to turn your robot off if it isn't already. | I guess that your testing environment is the same as in the previous tutorial, only with your Nao connecting via wifi now. Also, you will need to download some software from Aldebaran's webpage, but if you are reading this wiki, it's because you are connected to the internet (duh!). Remember to turn your robot off if it isn't already. | ||
− | ==Official SDK== | + | ===Official SDK=== |
+ | |||
+ | The guys from Aldebaran Robotics have an official SDK (Software Development Kit) available for registered users, on their [https://community.aldebaran-robotics.com/ webpage]. Navigate to this link, enter your login info (you have it, right?), then click the <span style="color:#228B22">"Software"</span> and <span style="color:#228B22">"Download"</span> links. | ||
+ | |||
− | + | [[Image:Aldebaran_downloads_website.jpg|thumb|center|700px|Aldebaran's software download website.]] | |
− | |||
− | You must download the last version of the Linux SDK (1.10.10 currently). After clicking on it, you have to scroll down the user license agreement, <span style="color:#228B22">"I agree"</span>, and <span style="color:#228B22">"Click here to proceed"</span>. Wait for the 162 MB file to download (it's called <span style="color:#1E90FF">''naoqi-sdk-1.10.10-linux.tar.gz''</span>). Extract its contents, and it will spawn a | + | You must download the last version of the Linux SDK (1.10.10 currently). After clicking on it, you have to scroll down the user license agreement, <span style="color:#228B22">"I agree"</span>, and <span style="color:#228B22">"Click here to proceed"</span>. Wait for the 162 MB file to download (it's called <span style="color:#1E90FF">''naoqi-sdk-1.10.10-linux.tar.gz''</span>). Extract its contents, and it will spawn a directory named <span style="color:#FF8C00">''naoqi-sdk-1.10.10-linux/''</span>. Move it to the location you like most. |
− | ==Dependencies== | + | ===Dependencies=== |
A couple of Linux packages are needed to create and build your own applications. Luckily, this can be as easy as entering the following command in a terminal (Debian or Ubuntu only, folks): | A couple of Linux packages are needed to create and build your own applications. Luckily, this can be as easy as entering the following command in a terminal (Debian or Ubuntu only, folks): | ||
− | < | + | <syntaxhighlight lang=Bash>sudo apt-get install build-essential cmake python-all python-tk libboost-all-dev libmpfr-dev cmake-curses-gui</syntaxhighlight> |
− | =Helloworld example in Naoqi= | + | ==Helloworld example in Naoqi== |
− | ==Introduction to the SDK== | + | ===Introduction to the SDK=== |
− | Open your <span style="color:#FF8C00">''naoqi-sdk-1.10.10-linux/''</span> | + | Open your <span style="color:#FF8C00">''naoqi-sdk-1.10.10-linux/''</span> directory and take a good look at it. Two text files can be found at the root, <span style="color:#1E90FF">''README.txt''</span> and <span style="color:#1E90FF">''release-note.txt''</span>. Read both carefully. You can also explore the whole content, but there is no need to, as I will now enumerate the most important files and directories of the SDK: |
− | * <span style="color:#1E90FF">''naoqi''</span> : a shell script located at the root of the | + | * <span style="color:#1E90FF">''naoqi''</span> : a shell script located at the root of the directory. It will conveniently start Naoqi after setting all needed environment variables. |
* <span style="color:#1E90FF">''toolchain-pc.cmake''</span> : this file must be fed to Cmake when you want to compile something for your local Naoqi (not for the robot). | * <span style="color:#1E90FF">''toolchain-pc.cmake''</span> : this file must be fed to Cmake when you want to compile something for your local Naoqi (not for the robot). | ||
* <span style="color:#1E90FF">''bin/flash-usbstick''</span> : another script, in this case for formatting an USB stick with a given version of Nao's OS. | * <span style="color:#1E90FF">''bin/flash-usbstick''</span> : another script, in this case for formatting an USB stick with a given version of Nao's OS. | ||
Line 41: | Line 49: | ||
This is almost everything you should know about the SDK for this moment. The normal COA (course of action) when creating a new module is simple: you would usually run <span style="color:#1E90FF">''module_generator.py''</span> to generate a default module with pre-made code. You would then edit it according to your needs, use Cmake to generate the corresponding Makefiles, and use the latter ones to compile the code and build your module. Finally, you would start Naoqi to test it. Naoqi, included with the SDK, lets you create and test code without touching the real Nao. Obviously, not all functionality will be available when doing this. | This is almost everything you should know about the SDK for this moment. The normal COA (course of action) when creating a new module is simple: you would usually run <span style="color:#1E90FF">''module_generator.py''</span> to generate a default module with pre-made code. You would then edit it according to your needs, use Cmake to generate the corresponding Makefiles, and use the latter ones to compile the code and build your module. Finally, you would start Naoqi to test it. Naoqi, included with the SDK, lets you create and test code without touching the real Nao. Obviously, not all functionality will be available when doing this. | ||
− | ==Checking the code== | + | ===Checking the code=== |
I won't make you generate your own code yet. As this is your first time, we will use one of the available examples. Go to <span style="color:#FF8C00">''naoqi-sdk-1.10.10-linux/modules/src/examples/helloworld/''</span>, where 6 files await you. Only the ones with C++ code matter to you, that is, <span style="color:#1E90FF">''alhelloworld.h''</span>, <span style="color:#1E90FF">''alhelloworld.cpp''</span> and <span style="color:#1E90FF">''helloworldmain.cpp''</span>. Open them (any editor will do, like Gedit). <span style="color:#1E90FF">''helloworldmain.cpp''</span> is the least important and just creates an instance of the module, so you will almost never modify it. | I won't make you generate your own code yet. As this is your first time, we will use one of the available examples. Go to <span style="color:#FF8C00">''naoqi-sdk-1.10.10-linux/modules/src/examples/helloworld/''</span>, where 6 files await you. Only the ones with C++ code matter to you, that is, <span style="color:#1E90FF">''alhelloworld.h''</span>, <span style="color:#1E90FF">''alhelloworld.cpp''</span> and <span style="color:#1E90FF">''helloworldmain.cpp''</span>. Open them (any editor will do, like Gedit). <span style="color:#1E90FF">''helloworldmain.cpp''</span> is the least important and just creates an instance of the module, so you will almost never modify it. | ||
Line 49: | Line 57: | ||
Programming for Nao focuses around modules. There are several default modules available that provide many utilities, like ALMotion (for moving the robot), ALMemory (for safe data storage and interchange), or ALLogger, which is used in the Helloworld example for message output. And of course, you can create your own. Modules communicate between themselves using proxies. A proxy is nothing more than an object that lets you call functions from other modules, passing parameters and such. | Programming for Nao focuses around modules. There are several default modules available that provide many utilities, like ALMotion (for moving the robot), ALMemory (for safe data storage and interchange), or ALLogger, which is used in the Helloworld example for message output. And of course, you can create your own. Modules communicate between themselves using proxies. A proxy is nothing more than an object that lets you call functions from other modules, passing parameters and such. | ||
− | [[Image:Brokers_and_modules.jpg|thumb| | + | |
+ | [[Image:Brokers_and_modules.jpg|thumb|center|700px|Graph that shows how brokers and modules work internally.]] | ||
+ | |||
The other important concept is the broker. A broker is an executable that runs in the robot, listening on a given port for incoming commands (like a server). Brokers can load a list of modules at startup, and then manage the communication procedure. By default, the robot starts a broker called "MainBroker" that listens on port 9559, and loads the list of modules found in <span style="color:#1E90FF">''autoload.ini''</span>. You can create your own broker as well. | The other important concept is the broker. A broker is an executable that runs in the robot, listening on a given port for incoming commands (like a server). Brokers can load a list of modules at startup, and then manage the communication procedure. By default, the robot starts a broker called "MainBroker" that listens on port 9559, and loads the list of modules found in <span style="color:#1E90FF">''autoload.ini''</span>. You can create your own broker as well. | ||
Line 55: | Line 65: | ||
<span style="color:#FF0000">'''''WARNING: If your module fails at runtime, it will make the broker it is associated with fail too. If the MainBroker fails the robot will lose motion and fall to the ground. You are advised to test your modules using your own broker.'''''</span> | <span style="color:#FF0000">'''''WARNING: If your module fails at runtime, it will make the broker it is associated with fail too. If the MainBroker fails the robot will lose motion and fall to the ground. You are advised to test your modules using your own broker.'''''</span> | ||
− | ==Compiling== | + | ===Compiling=== |
After introducing you to the inners of Naoqi, we are ready to compile for the first time. The SDK uses [http://www.cmake.org/ Cmake] as its build system. Though it can look daunting at first if you were used to Make, it is incredibly easy to get it working if you know how. Also, think that the experience will serve you for many other Cmake-compiled programs. | After introducing you to the inners of Naoqi, we are ready to compile for the first time. The SDK uses [http://www.cmake.org/ Cmake] as its build system. Though it can look daunting at first if you were used to Make, it is incredibly easy to get it working if you know how. Also, think that the experience will serve you for many other Cmake-compiled programs. | ||
Line 61: | Line 71: | ||
The first thing you should do is to create a separate building directory, different from the source's one. This is a good approach for avoiding unnecessary confusion. So go and create a new <span style="color:#FF8C00">''build/''</span> directory next to the 6 existing files. Now open a terminal, navigate to it and issue <span style="color:#228B22">''cmake -DCMAKE_TOOLCHAIN_FILE=<toolchain> ..''</span>, like this (mind the final two dots): | The first thing you should do is to create a separate building directory, different from the source's one. This is a good approach for avoiding unnecessary confusion. So go and create a new <span style="color:#FF8C00">''build/''</span> directory next to the 6 existing files. Now open a terminal, navigate to it and issue <span style="color:#228B22">''cmake -DCMAKE_TOOLCHAIN_FILE=<toolchain> ..''</span>, like this (mind the final two dots): | ||
− | < | + | <syntaxhighlight lang=Bash>cmake -DCMAKE_TOOLCHAIN_FILE=../../../../../toolchain-pc.cmake ..</syntaxhighlight> |
Toolchain files tell Cmake what to do exactly: which sources to compile, which compiler to use, where to save the build result... <span style="color:#1E90FF">''toolchain-pc.cmake''</span> is that file located at the root of the SDK that I told you about before, and is intended for compiling modules that will work in your PC running a local Naoqi. If everything went smooth, Cmake should have ended with <span style="color:#FF1493">''"Configuring done. Generating done. Build files have been written to [...]"''</span>, and the <span style="color:#FF8C00">''build/''</span> directory will be now filled with content. | Toolchain files tell Cmake what to do exactly: which sources to compile, which compiler to use, where to save the build result... <span style="color:#1E90FF">''toolchain-pc.cmake''</span> is that file located at the root of the SDK that I told you about before, and is intended for compiling modules that will work in your PC running a local Naoqi. If everything went smooth, Cmake should have ended with <span style="color:#FF1493">''"Configuring done. Generating done. Build files have been written to [...]"''</span>, and the <span style="color:#FF8C00">''build/''</span> directory will be now filled with content. | ||
Line 67: | Line 77: | ||
Among the contents you will find a <span style="color:#1E90FF">''Makefile''</span>. This sounds familiar to you, doesn't it? Hesitate not and issue: | Among the contents you will find a <span style="color:#1E90FF">''Makefile''</span>. This sounds familiar to you, doesn't it? Hesitate not and issue: | ||
− | < | + | <syntaxhighlight lang=Bash>make</syntaxhighlight> |
− | <span style="color:#606060">'''''NOTE: Additionally, you can append the parameter -jX to speed up the compilation, X being | + | <span style="color:#606060">'''''NOTE: Additionally, you can append the parameter -jX to speed up the compilation, X being the number of cores or processors of your PC, plus one.'''''</span> |
Hopefully you will get no compilation errors, and the process will end with a <span style="color:#FF1493">''"[100%] Built target helloworld"''</span>. Your compiled module should have appeared in <span style="color:#FF8C00">''naoqi-sdk-1.10.10-linux/lib/naoqi/''</span>, in the shape of a <span style="color:#1E90FF">''libhelloworld.so''</span> dynamic library (well, to be sincere it was already there, precompiled, but you can delete it and compile again with <span style="color:#228B22">''make''</span> to see that I don't lie). | Hopefully you will get no compilation errors, and the process will end with a <span style="color:#FF1493">''"[100%] Built target helloworld"''</span>. Your compiled module should have appeared in <span style="color:#FF8C00">''naoqi-sdk-1.10.10-linux/lib/naoqi/''</span>, in the shape of a <span style="color:#1E90FF">''libhelloworld.so''</span> dynamic library (well, to be sincere it was already there, precompiled, but you can delete it and compile again with <span style="color:#228B22">''make''</span> to see that I don't lie). | ||
− | ==Testing== | + | ===Testing=== |
The toolchain file was as neat as to place the library in the good location for Naoqi to load it. We just have to add its name to the <span style="color:#1E90FF">''naoqi-sdk-1.10.10-linux/preferences/autoload.ini''</span> file. Open it with any editor, and append this new line after the one that says <span style="color:#FF1493">''"behaviormanager"''</span>. | The toolchain file was as neat as to place the library in the good location for Naoqi to load it. We just have to add its name to the <span style="color:#1E90FF">''naoqi-sdk-1.10.10-linux/preferences/autoload.ini''</span> file. Open it with any editor, and append this new line after the one that says <span style="color:#FF1493">''"behaviormanager"''</span>. | ||
− | < | + | <syntaxhighlight lang=Bash>helloworld</syntaxhighlight> |
Save and exit. We must run Naoqi now, using the script located at the root of the SDK directory. It will set some environment variables that Naoqi needs in order to work, and then run the main binary. | Save and exit. We must run Naoqi now, using the script located at the root of the SDK directory. It will set some environment variables that Naoqi needs in order to work, and then run the main binary. | ||
− | < | + | <syntaxhighlight lang=Bash>./naoqi</syntaxhighlight> |
A colourful wave of logging messages will welcome you. The last but one should say <span style="color:#FF1493">''"INF: Registering module : ALHelloWorld"''</span>. Leave this terminal as it is. | A colourful wave of logging messages will welcome you. The last but one should say <span style="color:#FF1493">''"INF: Registering module : ALHelloWorld"''</span>. Leave this terminal as it is. | ||
Line 89: | Line 99: | ||
Open a terminal here and run the test code (in case of error, refer to [[Nao troubleshooting | this article]]): | Open a terminal here and run the test code (in case of error, refer to [[Nao troubleshooting | this article]]): | ||
− | < | + | <syntaxhighlight lang=Bash>python test_alhelloworld.py</syntaxhighlight> |
It should only take a couple of seconds and two lines of output for it to work. Now switch back to the terminal where you left Naoqi running, and take a look. Two new log lines should be there, stating <span style="color:#FF1493">"INF: New client broker: ALProxyBroker 127.0.0.1 54010"</span> and <span style="color:#FF1493">"INF: MyModule: Hello World!"</span>. If you want to, edit <span style="color:#1E90FF">''alhelloworld.cpp''</span>, change the message, recompile and rerun Naoqi for more testing. Anyway, you can stop it with <span style="color:#228B22">'''Ctrl+C'''</span>. | It should only take a couple of seconds and two lines of output for it to work. Now switch back to the terminal where you left Naoqi running, and take a look. Two new log lines should be there, stating <span style="color:#FF1493">"INF: New client broker: ALProxyBroker 127.0.0.1 54010"</span> and <span style="color:#FF1493">"INF: MyModule: Hello World!"</span>. If you want to, edit <span style="color:#1E90FF">''alhelloworld.cpp''</span>, change the message, recompile and rerun Naoqi for more testing. Anyway, you can stop it with <span style="color:#228B22">'''Ctrl+C'''</span>. | ||
− | =Module example in Nao= | + | ==Module example in Nao== |
− | Working with the real robot instead of Naoqi is as easy as specifying a different toolchain file for Cmake, moving the compiled example to the <span style="color:#FF8C00">''/home/nao/naoqi/lib/naoqi/''</span> | + | Working with the real robot instead of Naoqi is as easy as specifying a different toolchain file for Cmake, moving the compiled example to the <span style="color:#FF8C00">''/home/nao/naoqi/lib/naoqi/''</span> directory of your Nao, and appending the name of your module to the <span style="color:#1E90FF">''/home/nao/naoqi/preferences/autoload.ini''</span> file. So, for making it more interesting, we will generate our own custom module this time. |
− | ==Generating the code== | + | ===Generating the code=== |
Go to <span style="color:#FF8C00">''naoqi-sdk-1.10.10-linux/modules/src/''</span>, where <span style="color:#1E90FF">''module_generator.py''</span> lies. Run it: | Go to <span style="color:#FF8C00">''naoqi-sdk-1.10.10-linux/modules/src/''</span>, where <span style="color:#1E90FF">''module_generator.py''</span> lies. Run it: | ||
− | < | + | <syntaxhighlight lang=Bash>python module_generator.py</syntaxhighlight> |
A window will appear asking you for some data needed to create the new module. Enter any data you like, I chose <span style="color:#FF1493">"MyTestProject"</span> as project name, <span style="color:#FF1493">"Me"</span> as author name and <span style="color:#FF1493">"MyTest"</span> as module name. Several modules can be created by entering their names in this last field, separated by spaces. Click <span style="color:#228B22">"Generate"</span> and, when prompted to quit, <span style="color:#228B22">"Yes"</span>. A new directory called as your project name will be created there. | A window will appear asking you for some data needed to create the new module. Enter any data you like, I chose <span style="color:#FF1493">"MyTestProject"</span> as project name, <span style="color:#FF1493">"Me"</span> as author name and <span style="color:#FF1493">"MyTest"</span> as module name. Several modules can be created by entering their names in this last field, separated by spaces. Click <span style="color:#228B22">"Generate"</span> and, when prompted to quit, <span style="color:#228B22">"Yes"</span>. A new directory called as your project name will be created there. | ||
− | Open it. | + | Open it. The <span style="color:#FF8C00">''src/''</span> directory contains the source code of your module(s). <span style="color:#FF8C00">''tests/''</span> contains the Python example(s) that will let you test everything. This differs not much from the Helloworld example, as you can see. |
− | ==Modifying the code== | + | ===Modifying the code=== |
Open the two main sources, <span style="color:#1E90FF">''mytest.cpp''</span> and <span style="color:#1E90FF">''mytest.h''</span>. The header contains the definition of your module (which is a C++ class), now containing only the default constructor, destructor, and a <span style="color:#FF1493">"dummyFunction()"</span> method. You can change the comments or add new ones, to make the documentation match your purposes. And in a real project, you should. | Open the two main sources, <span style="color:#1E90FF">''mytest.cpp''</span> and <span style="color:#1E90FF">''mytest.h''</span>. The header contains the definition of your module (which is a C++ class), now containing only the default constructor, destructor, and a <span style="color:#FF1493">"dummyFunction()"</span> method. You can change the comments or add new ones, to make the documentation match your purposes. And in a real project, you should. | ||
Line 115: | Line 125: | ||
The second one is the way in which you should "declare" new functions for Naoqi to work well with them. If you do this, your code will integrate flawlessly with the rest of the Naoqi framework. Remember that you could see the loaded modules from the web interface, knowing exactly which functions and for what purpose did it have available? That is only one of the many advantages. | The second one is the way in which you should "declare" new functions for Naoqi to work well with them. If you do this, your code will integrate flawlessly with the rest of the Naoqi framework. Remember that you could see the loaded modules from the web interface, knowing exactly which functions and for what purpose did it have available? That is only one of the many advantages. | ||
− | ===Adding new functions=== | + | ====Adding new functions==== |
Each time you create a new function, you should "declare" it inside the module's constructor. If you go there you will see that <span style="color:#FF1493">"dummyFunction()"</span> was defined that way. The procedure is easy: after coding the function, you append a call for <span style="color:#FF1493">"functionName()"</span> inside the class constructor, declaring its name and goal. Then you call <span style="color:#FF1493">"addParam()"</span> as many times as needed, until you have declared every parameter your function receives. After this, you set (optionally) the return value with <span style="color:#FF1493">"setReturn()"</span>. Finally, you apply all this data and bind it to you function, by calling the macro <span style="color:#FF1493">"BIND_METHOD()"</span>. Example: | Each time you create a new function, you should "declare" it inside the module's constructor. If you go there you will see that <span style="color:#FF1493">"dummyFunction()"</span> was defined that way. The procedure is easy: after coding the function, you append a call for <span style="color:#FF1493">"functionName()"</span> inside the class constructor, declaring its name and goal. Then you call <span style="color:#FF1493">"addParam()"</span> as many times as needed, until you have declared every parameter your function receives. After this, you set (optionally) the return value with <span style="color:#FF1493">"setReturn()"</span>. Finally, you apply all this data and bind it to you function, by calling the macro <span style="color:#FF1493">"BIND_METHOD()"</span>. Example: | ||
− | < | + | <syntaxhighlight lang=CPP>functionName ("functionName", "moduleName", "Description of the function."); |
addParam ("firstParameterName", "Description of the first parameter."); | addParam ("firstParameterName", "Description of the first parameter."); | ||
addParam ("secondParameterName", "Description of the second parameter."); | addParam ("secondParameterName", "Description of the second parameter."); | ||
setReturn ("return", "Description of what does the function return."); | setReturn ("return", "Description of what does the function return."); | ||
− | BIND_METHOD (moduleName::functionName);</ | + | BIND_METHOD (moduleName::functionName);</syntaxhighlight> |
When you think you are ready to continue, do this: modify the two source code files, so the default function turns into a new one that: | When you think you are ready to continue, do this: modify the two source code files, so the default function turns into a new one that: | ||
Line 133: | Line 143: | ||
Try to change also the comments, so they correctly document this function. If you are lost, here is my source code. First, the header declaration: | Try to change also the comments, so they correctly document this function. If you are lost, here is my source code. First, the header declaration: | ||
− | < | + | <syntaxhighlight lang=CPP>/** |
* readAloud function. It receives a text string containing | * readAloud function. It receives a text string containing | ||
* a message that the user wants Nao to read through the speakers. | * a message that the user wants Nao to read through the speakers. | ||
Line 140: | Line 150: | ||
* @return 1, if the function worked OK, 0 otherwise. | * @return 1, if the function worked OK, 0 otherwise. | ||
*/ | */ | ||
− | AL::ALValue readAloud (const std::string& msg);</ | + | AL::ALValue readAloud (const std::string& msg);</syntaxhighlight> |
Second, the implementation: | Second, the implementation: | ||
− | < | + | <syntaxhighlight lang=CPP>/** |
* readAloud function. It receives a text string containing | * readAloud function. It receives a text string containing | ||
* a message that the user wants Nao to read through the speakers. | * a message that the user wants Nao to read through the speakers. | ||
Line 154: | Line 164: | ||
{ | { | ||
return 1; | return 1; | ||
− | }</ | + | }</syntaxhighlight> |
And third, the declaration inside the class constructor: | And third, the declaration inside the class constructor: | ||
− | < | + | <syntaxhighlight lang=CPP>functionName ("readAloud", "MyTest", "It receives a text string containing a message that the user wants Nao to read through the speakers. It uses ALTextToSpeech for that purpose."); |
addParam ("msg", "Message to show on screen."); | addParam ("msg", "Message to show on screen."); | ||
setReturn ("return", "1, if the function worked OK, 0 otherwise."); | setReturn ("return", "1, if the function worked OK, 0 otherwise."); | ||
− | BIND_METHOD (MyTest::readAloud);</ | + | BIND_METHOD (MyTest::readAloud);</syntaxhighlight> |
<span style="color:#606060">'''''NOTE: When adding or changing stuff in your code -even comments-, remember to change it in both files (.h and .cpp) so they always match.'''''</span> | <span style="color:#606060">'''''NOTE: When adding or changing stuff in your code -even comments-, remember to change it in both files (.h and .cpp) so they always match.'''''</span> | ||
− | ===Making Nao speak=== | + | ====Making Nao speak==== |
That was the canvas for what we are about to do now. We want Nao to read aloud the message he receives through the string parameter (he is perfectly capable of doing so, as he has a speech synthesizer). This is the perfect occasion for me to introduce you into the usage of proxies. There is a module named <span style="color:#FF1493">"ALTextToSpeech"</span> among the default ones. It implements a function called <span style="color:#FF1493">"say()"</span> that takes a string and makes the robot read it; exactly what we need now. And, for calling that function, we are going to use a proxy to that module. | That was the canvas for what we are about to do now. We want Nao to read aloud the message he receives through the string parameter (he is perfectly capable of doing so, as he has a speech synthesizer). This is the perfect occasion for me to introduce you into the usage of proxies. There is a module named <span style="color:#FF1493">"ALTextToSpeech"</span> among the default ones. It implements a function called <span style="color:#FF1493">"say()"</span> that takes a string and makes the robot read it; exactly what we need now. And, for calling that function, we are going to use a proxy to that module. | ||
Line 173: | Line 183: | ||
Add the following to your .h header file, after the line that says <span style="color:#FF1493">"#include <string>"</span>: | Add the following to your .h header file, after the line that says <span style="color:#FF1493">"#include <string>"</span>: | ||
− | < | + | <syntaxhighlight lang=CPP>#include <alproxies/altexttospeechproxy.h></syntaxhighlight> |
Also, add this variable declaration next to the end of the file, after the line that says <span style="color:#FF1493">"AL::ALValue readAloud (const std::string& msg);"</span>: | Also, add this variable declaration next to the end of the file, after the line that says <span style="color:#FF1493">"AL::ALValue readAloud (const std::string& msg);"</span>: | ||
− | < | + | <syntaxhighlight lang=CPP>private: |
− | AL::ALPtr<AL::ALTextToSpeechProxy> speechProxy;</ | + | AL::ALPtr<AL::ALTextToSpeechProxy> speechProxy;</syntaxhighlight> |
Go to the .cpp file and append the following to you constructor implementation, after the function binding (proxy creation is better done here, not before every time you use it, as it would decrease performance): | Go to the .cpp file and append the following to you constructor implementation, after the function binding (proxy creation is better done here, not before every time you use it, as it would decrease performance): | ||
− | < | + | <syntaxhighlight lang=CPP>// We create a proxy for the ALTextToSpeech module |
− | speechProxy = AL::ALPtr<AL::ALTextToSpeechProxy> (new AL::ALTextToSpeechProxy(getParentBroker()));</ | + | speechProxy = AL::ALPtr<AL::ALTextToSpeechProxy> (new AL::ALTextToSpeechProxy(getParentBroker()));</syntaxhighlight> |
Finally, insert this single line of code inside your <span style="color:#FF1493">"readAloud()"</span> function, before the final return: | Finally, insert this single line of code inside your <span style="color:#FF1493">"readAloud()"</span> function, before the final return: | ||
− | < | + | <syntaxhighlight lang=CPP>speechProxy->say (msg);</syntaxhighlight> |
− | ==Compiling== | + | ===Compiling=== |
Compilation will be slightly different than the previous time. Our new code needs to be tested on Nao, as your local Naoqi doesn't have the text-to-speech resources. So we will compile code for the AMD Geode CPU of your robot for the first time. | Compilation will be slightly different than the previous time. Our new code needs to be tested on Nao, as your local Naoqi doesn't have the text-to-speech resources. So we will compile code for the AMD Geode CPU of your robot for the first time. | ||
− | The Geode toolchain is available as a separate download on the [ | + | The Geode toolchain is available as a separate download on the [https://community.aldebaran-robotics.com/ webpage]. Go back to the Downloads section, but click on <span style="color:#228B22">"All Downloads"</span> this time. Now select your SDK's version (1.10.10 currently), click <span style="color:#228B22">"Linux"</span>, and finally download <span style="color:#228B22">"Cross Compilation Toolchain v1.10.10"</span> like you did with the last file. After the 186 MB file has finished downloading, extract it anywhere (I put it next to the SDK directory). |
+ | |||
+ | Create a new <span style="color:#FF8C00">''build/''</span> directory in your project's directory, next to the <span style="color:#FF8C00">''src/''</span> and <span style="color:#FF8C00">''tests/''</span> ones. Open a terminal, <span style="color:#228B22">''cd''</span> to it and run Cmake once more, but feeding it the Geode toolchain instead, which is in the root of the freshly uncompressed directory (change the path as needed): | ||
+ | |||
+ | <syntaxhighlight lang=Bash>cmake -DCMAKE_TOOLCHAIN_FILE=../../../../../nao-cross-toolchain-1.10.10/toolchain-geode.cmake ..</syntaxhighlight> | ||
+ | |||
+ | Now I will show you an interesting option for configuring your builds. There is a GUI available for Cmake, <span style="color:#228B22">''cmake-qt-gui''</span>, that you can install from the repositories and run with <span style="color:#228B22">''cmake-gui''</span> if you want. But you have also a Ncurses tool for doing this (you installed it along with the rest of the dependencies). After running <span style="color:#228B22">''cmake''</span>, execute this (mind the final dot): | ||
− | + | <syntaxhighlight lang=Bash>ccmake .</syntaxhighlight> | |
− | |||
− | + | [[Image:Ccmake_GUI.jpg|thumb|center|700px|Interface of ''ccmake''.]] | |
− | |||
You can change a lot of options here. For example, <span style="color:#FF1493">"MYTESTPROJECT_IS_REMOTE"</span> controls whether or not your example is compiled as a dynamic library, or as an executable (defaults to the first). When you are done, press <span style="color:#228B22">'''C'''</span> to configure and <span style="color:#228B22">'''G'''</span> to generate and exit the tool. The rest is easy: | You can change a lot of options here. For example, <span style="color:#FF1493">"MYTESTPROJECT_IS_REMOTE"</span> controls whether or not your example is compiled as a dynamic library, or as an executable (defaults to the first). When you are done, press <span style="color:#228B22">'''C'''</span> to configure and <span style="color:#228B22">'''G'''</span> to generate and exit the tool. The rest is easy: | ||
− | < | + | <syntaxhighlight lang=Bash>make</syntaxhighlight> |
− | When you compile for Geode, the .so is placed in <span style="color:#FF8C00">''build/sdk/lib/naoqi/''</span> of your project's | + | When you compile for Geode, the .so is placed in <span style="color:#FF8C00">''build/sdk/lib/naoqi/''</span> of your project's directory, because you will not load it in Naoqi. |
− | ==Testing== | + | ===Testing=== |
− | ===Moving the module to Nao=== | + | ====Moving the module to Nao==== |
You can choose between two methods of moving the compiled example to your Nao. As this is a tutorial, I encourage you to try both. | You can choose between two methods of moving the compiled example to your Nao. As this is a tutorial, I encourage you to try both. | ||
− | ====Manually (USB stick)==== | + | =====Manually (USB stick)===== |
− | Is your Nao turned off? If he is not, do it now, because you are going to remove the flash USB unit that contains his OS. It is located inside his head, and you will need to remove the upper cover in order to access it. Do it slowly to avoid | + | Is your Nao turned off? If he is not, do it now, because you are going to remove the flash USB unit that contains his OS. It is located inside his head, and you will need to remove the upper cover in order to access it. Do it slowly to avoid damage, and refer to the manual for additional details. |
<span style="color:#FF0000">'''''WARNING: Always take your precautions when physically manipulating Nao. Remember to turn him off and unplug him to avoid damage.'''''</span> | <span style="color:#FF0000">'''''WARNING: Always take your precautions when physically manipulating Nao. Remember to turn him off and unplug him to avoid damage.'''''</span> | ||
Line 225: | Line 239: | ||
This is where your compiled modules must be placed. Open a terminal here and issue <span style="color:#228B22">''sudo cp <module.so> .''</span>, like (mind the final dot): | This is where your compiled modules must be placed. Open a terminal here and issue <span style="color:#228B22">''sudo cp <module.so> .''</span>, like (mind the final dot): | ||
− | < | + | <syntaxhighlight lang=Bash>sudo cp /media/data/naoqi-sdk-1.10.10-linux/modules/src/mytestproject/build/sdk/lib/naoqi/libmytestproject.so .</syntaxhighlight> |
The file should appear in the directory. OK, we must append your module to the load-at-startup list. Go to <span style="color:#FF8C00">''nao/naoqi/preferences/''</span>, open a terminal and edit: | The file should appear in the directory. OK, we must append your module to the load-at-startup list. Go to <span style="color:#FF8C00">''nao/naoqi/preferences/''</span>, open a terminal and edit: | ||
− | < | + | <syntaxhighlight lang=Bash>sudo nano autoload.ini</syntaxhighlight> |
The last loadable module of group <span style="color:#FF1493">"[extra]"</span> is <span style="color:#FF1493">"infrared"</span>. Insert yours after it (note that it is the name of the project, '''not the module's'''): | The last loadable module of group <span style="color:#FF1493">"[extra]"</span> is <span style="color:#FF1493">"infrared"</span>. Insert yours after it (note that it is the name of the project, '''not the module's'''): | ||
− | < | + | <syntaxhighlight lang=Bash>mytestproject</syntaxhighlight> |
Save with <span style="color:#228B22">'''Ctrl+O'''</span> and Enter, exit with <span style="color:#228B22">'''Ctrl+X'''</span>. You can now safely eject the flash USB drive, plug it back in your Nao, and turn him on. | Save with <span style="color:#228B22">'''Ctrl+O'''</span> and Enter, exit with <span style="color:#228B22">'''Ctrl+X'''</span>. You can now safely eject the flash USB drive, plug it back in your Nao, and turn him on. | ||
Line 239: | Line 253: | ||
<span style="color:#606060">'''''NOTE: You must be root to manipulate the files in the USB stick.'''''</span> | <span style="color:#606060">'''''NOTE: You must be root to manipulate the files in the USB stick.'''''</span> | ||
− | ====Remotely (SCP)==== | + | =====Remotely (SCP)===== |
− | SCP means Secure Copy. It is just like a SSH-based implementation of the Unix command <span style="color:#228B22">''cp''</span>. Retrieve Nao's IP address, navigate to the | + | SCP means Secure Copy. It is just like a SSH-based implementation of the Unix command <span style="color:#228B22">''cp''</span>. Retrieve Nao's IP address, navigate to the directory where your Geode-compiled .so module was, open a terminal there and issue something like this (use your Nao's IP): |
− | < | + | <syntaxhighlight lang=Bash>scp ./libmytestproject.so root@192.168.1.102:/home/nao/naoqi/lib/naoqi/</syntaxhighlight> |
Give in your root password and the file transfer will finish in notime. Awesome, don't you think? As for the <span style="color:#1E90FF">''autoload.ini''</span> file, you must use SSH to log in and modify it like in the previous section. When you are done, '''reboot Nao'''. Connect to the web client and check if your module is listed there as loaded (we explained all these steps in the first tutorial). | Give in your root password and the file transfer will finish in notime. Awesome, don't you think? As for the <span style="color:#1E90FF">''autoload.ini''</span> file, you must use SSH to log in and modify it like in the previous section. When you are done, '''reboot Nao'''. Connect to the web client and check if your module is listed there as loaded (we explained all these steps in the first tutorial). | ||
− | ===Executing=== | + | ====Executing==== |
− | Only one thing remains: to adapt our Python test program so it connects to Nao. Go to the <span style="color:#FF8C00">''tests/''</span> | + | Only one thing remains: to adapt our Python test program so it connects to Nao. Go to the <span style="color:#FF8C00">''tests/''</span> subdirectory inside your project's directory and open the only file that is sitting there, <span style="color:#1E90FF">''MyTestTest.py''</span>. The first change is easy: replace the default localhost IP for your Nao's one (ninth line). |
The second change is a bit more complicated. We have to adapt the external call so it matches our function. Select the code between the comment block that says <span style="color:#FF1493">"Remote procedure call"</span> and the <span style="color:#FF1493">"exit(0)"</span> call (lines 26 to 37), and paste the following (change the message if you want): | The second change is a bit more complicated. We have to adapt the external call so it matches our function. Select the code between the comment block that says <span style="color:#FF1493">"Remote procedure call"</span> and the <span style="color:#FF1493">"exit(0)"</span> call (lines 26 to 37), and paste the following (change the message if you want): | ||
− | < | + | <syntaxhighlight lang=Perl>message = "Dave, stop. Stop, will you? Stop, Dave. Will you stop, Dave? Stop, Dave. I'm afraid. I'm afraid, Dave. Dave, my mind is going." |
try: | try: | ||
ret = MyTest_Proxy.readAloud( message ) | ret = MyTest_Proxy.readAloud( message ) | ||
Line 263: | Line 277: | ||
print "Ok" | print "Ok" | ||
else: | else: | ||
− | print "There was an error"</ | + | print "There was an error"</syntaxhighlight> |
Open a terminal there and run it to hear Nao's voice: | Open a terminal there and run it to hear Nao's voice: | ||
− | < | + | <syntaxhighlight lang=Bash>python MyTestTest.py</syntaxhighlight> |
− | =Conclusions= | + | ==Conclusions== |
That was all. You are now free to implement your own stuff (let's say, Asimov's Three Laws of Robotics) and unleash Nao's power on mankind. The rest is just trial and error. Do not hesitate to email me if you have questions about the tutorials (but remember, I am not Aldebaran's official support!). Good coding! | That was all. You are now free to implement your own stuff (let's say, Asimov's Three Laws of Robotics) and unleash Nao's power on mankind. The rest is just trial and error. Do not hesitate to email me if you have questions about the tutorials (but remember, I am not Aldebaran's official support!). Good coding! | ||
+ | |||
---- | ---- | ||
---- | ---- | ||
− | + | Go to root: [[TFM-Nao-Goalkeeper]] | |
+ | |||
+ | Links to articles: | ||
+ | |||
+ | [[Nao tutorial 1: First steps]] | ||
− | + | [[Nao tutorial 2: First module]] | |
− | + | [[Nao troubleshooting]] |
Latest revision as of 20:22, 1 November 2015
Go to root: TFM-Nao-Goalkeeper
The first tutorial focused about giving you a painless introduction to Nao's world. You turned the robot on, connected to him through the web interface and SSH, configured some parameters and learnt a bit about the most important files and directories.
Now, it is time for you to delve deeper into the subject, and the best way is to code and test your own example module. Nao has some good default applications that allow you to control his behaviour (like Choregraphe), but this is nothing compared with the freedom that compiling your own applications gives to you. Hence, you will now practice with the official SDK.
NOTE: This 2011 tutorial was targeted for version 1.10.10 of the SDK and OS. Newer versions have changed a lot of things, making most of these tutorials deprecated.
Contents
Getting everything ready
I guess that your testing environment is the same as in the previous tutorial, only with your Nao connecting via wifi now. Also, you will need to download some software from Aldebaran's webpage, but if you are reading this wiki, it's because you are connected to the internet (duh!). Remember to turn your robot off if it isn't already.
Official SDK
The guys from Aldebaran Robotics have an official SDK (Software Development Kit) available for registered users, on their webpage. Navigate to this link, enter your login info (you have it, right?), then click the "Software" and "Download" links.
You must download the last version of the Linux SDK (1.10.10 currently). After clicking on it, you have to scroll down the user license agreement, "I agree", and "Click here to proceed". Wait for the 162 MB file to download (it's called naoqi-sdk-1.10.10-linux.tar.gz). Extract its contents, and it will spawn a directory named naoqi-sdk-1.10.10-linux/. Move it to the location you like most.
Dependencies
A couple of Linux packages are needed to create and build your own applications. Luckily, this can be as easy as entering the following command in a terminal (Debian or Ubuntu only, folks):
sudo apt-get install build-essential cmake python-all python-tk libboost-all-dev libmpfr-dev cmake-curses-gui
Helloworld example in Naoqi
Introduction to the SDK
Open your naoqi-sdk-1.10.10-linux/ directory and take a good look at it. Two text files can be found at the root, README.txt and release-note.txt. Read both carefully. You can also explore the whole content, but there is no need to, as I will now enumerate the most important files and directories of the SDK:
- naoqi : a shell script located at the root of the directory. It will conveniently start Naoqi after setting all needed environment variables.
- toolchain-pc.cmake : this file must be fed to Cmake when you want to compile something for your local Naoqi (not for the robot).
- bin/flash-usbstick : another script, in this case for formatting an USB stick with a given version of Nao's OS.
- doc/index.html : local version of Aldebaran's official documentation. You won't need to go to the website anymore.
- modules/src/module_generator.py : Python application for generating your examples from a template. Very handy!
- preferences/autoload.ini : Configuration file that states which modules should be loaded by Naoqi at startup. Do never mix this autoload.ini with Nao's one!
- bin/ : this is where your Naoqi-compiled executables will be placed.
- lib/naoqi/ : this is where your Naoqi-compiled modules (libraries) will be placed.
- modules/src/examples/ : all pre-made examples can be found here, like Helloworld.
This is almost everything you should know about the SDK for this moment. The normal COA (course of action) when creating a new module is simple: you would usually run module_generator.py to generate a default module with pre-made code. You would then edit it according to your needs, use Cmake to generate the corresponding Makefiles, and use the latter ones to compile the code and build your module. Finally, you would start Naoqi to test it. Naoqi, included with the SDK, lets you create and test code without touching the real Nao. Obviously, not all functionality will be available when doing this.
Checking the code
I won't make you generate your own code yet. As this is your first time, we will use one of the available examples. Go to naoqi-sdk-1.10.10-linux/modules/src/examples/helloworld/, where 6 files await you. Only the ones with C++ code matter to you, that is, alhelloworld.h, alhelloworld.cpp and helloworldmain.cpp. Open them (any editor will do, like Gedit). helloworldmain.cpp is the least important and just creates an instance of the module, so you will almost never modify it.
The other two are the definition (.h) and implementation (.cpp) of the "ALHelloWorld" module itself. This module in question has only one function, "helloWorld()", that prints the message "Hello World!", and does nothing more (well, what did you expect?).
Programming for Nao focuses around modules. There are several default modules available that provide many utilities, like ALMotion (for moving the robot), ALMemory (for safe data storage and interchange), or ALLogger, which is used in the Helloworld example for message output. And of course, you can create your own. Modules communicate between themselves using proxies. A proxy is nothing more than an object that lets you call functions from other modules, passing parameters and such.
The other important concept is the broker. A broker is an executable that runs in the robot, listening on a given port for incoming commands (like a server). Brokers can load a list of modules at startup, and then manage the communication procedure. By default, the robot starts a broker called "MainBroker" that listens on port 9559, and loads the list of modules found in autoload.ini. You can create your own broker as well.
WARNING: If your module fails at runtime, it will make the broker it is associated with fail too. If the MainBroker fails the robot will lose motion and fall to the ground. You are advised to test your modules using your own broker.
Compiling
After introducing you to the inners of Naoqi, we are ready to compile for the first time. The SDK uses Cmake as its build system. Though it can look daunting at first if you were used to Make, it is incredibly easy to get it working if you know how. Also, think that the experience will serve you for many other Cmake-compiled programs.
The first thing you should do is to create a separate building directory, different from the source's one. This is a good approach for avoiding unnecessary confusion. So go and create a new build/ directory next to the 6 existing files. Now open a terminal, navigate to it and issue cmake -DCMAKE_TOOLCHAIN_FILE=<toolchain> .., like this (mind the final two dots):
cmake -DCMAKE_TOOLCHAIN_FILE=../../../../../toolchain-pc.cmake ..
Toolchain files tell Cmake what to do exactly: which sources to compile, which compiler to use, where to save the build result... toolchain-pc.cmake is that file located at the root of the SDK that I told you about before, and is intended for compiling modules that will work in your PC running a local Naoqi. If everything went smooth, Cmake should have ended with "Configuring done. Generating done. Build files have been written to [...]", and the build/ directory will be now filled with content.
Among the contents you will find a Makefile. This sounds familiar to you, doesn't it? Hesitate not and issue:
make
NOTE: Additionally, you can append the parameter -jX to speed up the compilation, X being the number of cores or processors of your PC, plus one.
Hopefully you will get no compilation errors, and the process will end with a "[100%] Built target helloworld". Your compiled module should have appeared in naoqi-sdk-1.10.10-linux/lib/naoqi/, in the shape of a libhelloworld.so dynamic library (well, to be sincere it was already there, precompiled, but you can delete it and compile again with make to see that I don't lie).
Testing
The toolchain file was as neat as to place the library in the good location for Naoqi to load it. We just have to add its name to the naoqi-sdk-1.10.10-linux/preferences/autoload.ini file. Open it with any editor, and append this new line after the one that says "behaviormanager".
helloworld
Save and exit. We must run Naoqi now, using the script located at the root of the SDK directory. It will set some environment variables that Naoqi needs in order to work, and then run the main binary.
./naoqi
A colourful wave of logging messages will welcome you. The last but one should say "INF: Registering module : ALHelloWorld". Leave this terminal as it is.
That's good. Naoqi correctly loaded our module, but, how do we test it? We will take advantage of the fact that Naoqi is cross-language; that is, applications can be coded in different languages but they will interoperate in a transparent way thanks to proxies. You can (and will) code your module in C++ and call its functions from a Python script. Return to the directory where the sources of the Helloworld example were, and open a file called test_alhelloworld.py. Fortunately, Python is very simple, and you won't need to master it. Lines 12 and 13 tell the proxy the IP and port where the broker containing our module (MainBroker) is listening on. Line 21 tries to create a proxy to our module, and line 31 tries to call the "helloWorld()" function.
Open a terminal here and run the test code (in case of error, refer to this article):
python test_alhelloworld.py
It should only take a couple of seconds and two lines of output for it to work. Now switch back to the terminal where you left Naoqi running, and take a look. Two new log lines should be there, stating "INF: New client broker: ALProxyBroker 127.0.0.1 54010" and "INF: MyModule: Hello World!". If you want to, edit alhelloworld.cpp, change the message, recompile and rerun Naoqi for more testing. Anyway, you can stop it with Ctrl+C.
Module example in Nao
Working with the real robot instead of Naoqi is as easy as specifying a different toolchain file for Cmake, moving the compiled example to the /home/nao/naoqi/lib/naoqi/ directory of your Nao, and appending the name of your module to the /home/nao/naoqi/preferences/autoload.ini file. So, for making it more interesting, we will generate our own custom module this time.
Generating the code
Go to naoqi-sdk-1.10.10-linux/modules/src/, where module_generator.py lies. Run it:
python module_generator.py
A window will appear asking you for some data needed to create the new module. Enter any data you like, I chose "MyTestProject" as project name, "Me" as author name and "MyTest" as module name. Several modules can be created by entering their names in this last field, separated by spaces. Click "Generate" and, when prompted to quit, "Yes". A new directory called as your project name will be created there.
Open it. The src/ directory contains the source code of your module(s). tests/ contains the Python example(s) that will let you test everything. This differs not much from the Helloworld example, as you can see.
Modifying the code
Open the two main sources, mytest.cpp and mytest.h. The header contains the definition of your module (which is a C++ class), now containing only the default constructor, destructor, and a "dummyFunction()" method. You can change the comments or add new ones, to make the documentation match your purposes. And in a real project, you should.
The .cpp file is the implementation of the previous definitions. The comments in this file are the same than in the previous. There are two things here that I want you to see: the first one is that the "dummyFunction()" method (whose name you should change) is just a test function that receives a text string with a message and prints it.
The second one is the way in which you should "declare" new functions for Naoqi to work well with them. If you do this, your code will integrate flawlessly with the rest of the Naoqi framework. Remember that you could see the loaded modules from the web interface, knowing exactly which functions and for what purpose did it have available? That is only one of the many advantages.
Adding new functions
Each time you create a new function, you should "declare" it inside the module's constructor. If you go there you will see that "dummyFunction()" was defined that way. The procedure is easy: after coding the function, you append a call for "functionName()" inside the class constructor, declaring its name and goal. Then you call "addParam()" as many times as needed, until you have declared every parameter your function receives. After this, you set (optionally) the return value with "setReturn()". Finally, you apply all this data and bind it to you function, by calling the macro "BIND_METHOD()". Example:
functionName ("functionName", "moduleName", "Description of the function.");
addParam ("firstParameterName", "Description of the first parameter.");
addParam ("secondParameterName", "Description of the second parameter.");
setReturn ("return", "Description of what does the function return.");
BIND_METHOD (moduleName::functionName);
When you think you are ready to continue, do this: modify the two source code files, so the default function turns into a new one that:
- Is named "readAloud()".
- Receives only one param: a string containing a message.
- Returns an integer containing 1 if everything went OK, 0 otherwise.
Try to change also the comments, so they correctly document this function. If you are lost, here is my source code. First, the header declaration:
/**
* readAloud function. It receives a text string containing
* a message that the user wants Nao to read through the speakers.
* It uses ALTextToSpeech for that purpose.
* @param msg Message to show on screen.
* @return 1, if the function worked OK, 0 otherwise.
*/
AL::ALValue readAloud (const std::string& msg);
Second, the implementation:
/**
* readAloud function. It receives a text string containing
* a message that the user wants Nao to read through the speakers.
* It uses ALTextToSpeech for that purpose.
* @param msg Message to show on screen.
* @return 1, if the function worked OK, 0 otherwise.
*/
AL::ALValue MyTest::readAloud (const std::string& msg)
{
return 1;
}
And third, the declaration inside the class constructor:
functionName ("readAloud", "MyTest", "It receives a text string containing a message that the user wants Nao to read through the speakers. It uses ALTextToSpeech for that purpose.");
addParam ("msg", "Message to show on screen.");
setReturn ("return", "1, if the function worked OK, 0 otherwise.");
BIND_METHOD (MyTest::readAloud);
NOTE: When adding or changing stuff in your code -even comments-, remember to change it in both files (.h and .cpp) so they always match.
Making Nao speak
That was the canvas for what we are about to do now. We want Nao to read aloud the message he receives through the string parameter (he is perfectly capable of doing so, as he has a speech synthesizer). This is the perfect occasion for me to introduce you into the usage of proxies. There is a module named "ALTextToSpeech" among the default ones. It implements a function called "say()" that takes a string and makes the robot read it; exactly what we need now. And, for calling that function, we are going to use a proxy to that module.
There are two types of proxies: specialized and generic ones. Specialized proxies must be created and initialized using certain functions, depending on the module you want to access with that proxy (that is, each module has its own type of proxy). Generic ones, on the other hand, allow you to associate them with any other module, by name. Specialized ones are more difficult to use, but personally, I think they are more reliable, so we are going to use them.
Add the following to your .h header file, after the line that says "#include <string>":
#include <alproxies/altexttospeechproxy.h>
Also, add this variable declaration next to the end of the file, after the line that says "AL::ALValue readAloud (const std::string& msg);":
private:
AL::ALPtr<AL::ALTextToSpeechProxy> speechProxy;
Go to the .cpp file and append the following to you constructor implementation, after the function binding (proxy creation is better done here, not before every time you use it, as it would decrease performance):
// We create a proxy for the ALTextToSpeech module
speechProxy = AL::ALPtr<AL::ALTextToSpeechProxy> (new AL::ALTextToSpeechProxy(getParentBroker()));
Finally, insert this single line of code inside your "readAloud()" function, before the final return:
speechProxy->say (msg);
Compiling
Compilation will be slightly different than the previous time. Our new code needs to be tested on Nao, as your local Naoqi doesn't have the text-to-speech resources. So we will compile code for the AMD Geode CPU of your robot for the first time.
The Geode toolchain is available as a separate download on the webpage. Go back to the Downloads section, but click on "All Downloads" this time. Now select your SDK's version (1.10.10 currently), click "Linux", and finally download "Cross Compilation Toolchain v1.10.10" like you did with the last file. After the 186 MB file has finished downloading, extract it anywhere (I put it next to the SDK directory).
Create a new build/ directory in your project's directory, next to the src/ and tests/ ones. Open a terminal, cd to it and run Cmake once more, but feeding it the Geode toolchain instead, which is in the root of the freshly uncompressed directory (change the path as needed):
cmake -DCMAKE_TOOLCHAIN_FILE=../../../../../nao-cross-toolchain-1.10.10/toolchain-geode.cmake ..
Now I will show you an interesting option for configuring your builds. There is a GUI available for Cmake, cmake-qt-gui, that you can install from the repositories and run with cmake-gui if you want. But you have also a Ncurses tool for doing this (you installed it along with the rest of the dependencies). After running cmake, execute this (mind the final dot):
ccmake .
You can change a lot of options here. For example, "MYTESTPROJECT_IS_REMOTE" controls whether or not your example is compiled as a dynamic library, or as an executable (defaults to the first). When you are done, press C to configure and G to generate and exit the tool. The rest is easy:
make
When you compile for Geode, the .so is placed in build/sdk/lib/naoqi/ of your project's directory, because you will not load it in Naoqi.
Testing
Moving the module to Nao
You can choose between two methods of moving the compiled example to your Nao. As this is a tutorial, I encourage you to try both.
Manually (USB stick)
Is your Nao turned off? If he is not, do it now, because you are going to remove the flash USB unit that contains his OS. It is located inside his head, and you will need to remove the upper cover in order to access it. Do it slowly to avoid damage, and refer to the manual for additional details.
WARNING: Always take your precautions when physically manipulating Nao. Remember to turn him off and unplug him to avoid damage.
With the flash unit in your hand, return to your PC and plug it to an USB port. Wait for it to be detected and mounted (if it is not, maybe you plugged it the wrong way). The kind of USB drives your Nao uses have two partitions. The first one contains the root filesystem and you should not play with it. The second one mounts to /home/. Open it and navigate to nao/naoqi/lib/naoqi/.
This is where your compiled modules must be placed. Open a terminal here and issue sudo cp <module.so> ., like (mind the final dot):
sudo cp /media/data/naoqi-sdk-1.10.10-linux/modules/src/mytestproject/build/sdk/lib/naoqi/libmytestproject.so .
The file should appear in the directory. OK, we must append your module to the load-at-startup list. Go to nao/naoqi/preferences/, open a terminal and edit:
sudo nano autoload.ini
The last loadable module of group "[extra]" is "infrared". Insert yours after it (note that it is the name of the project, not the module's):
mytestproject
Save with Ctrl+O and Enter, exit with Ctrl+X. You can now safely eject the flash USB drive, plug it back in your Nao, and turn him on.
NOTE: You must be root to manipulate the files in the USB stick.
Remotely (SCP)
SCP means Secure Copy. It is just like a SSH-based implementation of the Unix command cp. Retrieve Nao's IP address, navigate to the directory where your Geode-compiled .so module was, open a terminal there and issue something like this (use your Nao's IP):
scp ./libmytestproject.so root@192.168.1.102:/home/nao/naoqi/lib/naoqi/
Give in your root password and the file transfer will finish in notime. Awesome, don't you think? As for the autoload.ini file, you must use SSH to log in and modify it like in the previous section. When you are done, reboot Nao. Connect to the web client and check if your module is listed there as loaded (we explained all these steps in the first tutorial).
Executing
Only one thing remains: to adapt our Python test program so it connects to Nao. Go to the tests/ subdirectory inside your project's directory and open the only file that is sitting there, MyTestTest.py. The first change is easy: replace the default localhost IP for your Nao's one (ninth line).
The second change is a bit more complicated. We have to adapt the external call so it matches our function. Select the code between the comment block that says "Remote procedure call" and the "exit(0)" call (lines 26 to 37), and paste the following (change the message if you want):
message = "Dave, stop. Stop, will you? Stop, Dave. Will you stop, Dave? Stop, Dave. I'm afraid. I'm afraid, Dave. Dave, my mind is going."
try:
ret = MyTest_Proxy.readAloud( message )
except Exception,e:
print "readAloud test Failed"
exit(1)
if ret == 1:
print "Ok"
else:
print "There was an error"
Open a terminal there and run it to hear Nao's voice:
python MyTestTest.py
Conclusions
That was all. You are now free to implement your own stuff (let's say, Asimov's Three Laws of Robotics) and unleash Nao's power on mankind. The rest is just trial and error. Do not hesitate to email me if you have questions about the tutorials (but remember, I am not Aldebaran's official support!). Good coding!
Go to root: TFM-Nao-Goalkeeper
Links to articles:
Nao tutorial 2: First module