RAPP Platform Wiki  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Files Pages
(novice) Create-a-remote-robotic-application-for-NAO-in-Python-(novice)

This tutorial has a goal to introduce a novice programmer (who is not necessarily a roboticist) into the RApps concept (where RApps stands for Robotic Applications). Here, a simple application will be created for the NAO robot using the Python programming language. This application will be executed remotely in a PC (and not in the actual robot), in order to avoid any advanced in-robot modifications.

As always, the preparation steps are much more than the actual robotic application programming, so lets begin with this.

Preparation steps

For this tutorial we will use the following tools:

  • the NaoQi Python libraries v2.1.4
  • the rapp-robots-api Github repository

Of course the standard prerequisites are a functional installation of Ubuntu 14.04, an editor and a terminal.

NaoQi Python libraries setup

Since the application is going to be remotely deployed (not directly in the robot), the NaoQi Python libraries are needed in order to achieve communication from the PC to the NAO robot. If you are the owner of a NAO robot, you can download this package following these instructions. For this tutorial we will download the Python 2.7 SDK 2.1.4 Linux 64 from Aldebaran's software resources webpage, after logging in with our credentials.

The downloaded file is pynaoqi-python2.7-2.1.4.13-linux64.tar.gz and lets assume that it was downloaded in $HOME. Next, we create a dedicated folder for our project, move the file there and untar it:

``` cd ~ mkdir rapp_nao mv ~/pynaoqi-python2.7-2.1.4.13-linux64.tar.gz ~/rapp_nao cd ~/rapp_nao tar -xvf pynaoqi-python2.7-2.1.4.13-linux64.tar.gz ```

Next, the PYTHONPATH environmental variable must be updated with the location of the NaoQi libraries:

``` echo 'export PYTHONPATH=$PYTHONPATH:~/rapp_nao/pynaoqi-python2.7-2.1.4.13-linux64' >> ~/.bashrc source ~/.bashrc ```

Now, the NaoQi is ready for utilization. The next step is to setup the rapp-robots-api Python libraries.

RAPP Robots API libraries setup

The first step is to clone the appropriate GitHub repository:

``` cd ~/rapp_nao git clone https://github.com/rapp-project/rapp-robots-api.git ```

Similarly to the NaoQi Python libraries, the PYTHONPATH variable has to be updated:

``` echo 'export PYTHONPATH=$PYTHONPATH:~/rapp_nao/rapp-robots-api/python/abstract_classes' >> ~/.bashrc echo 'export PYTHONPATH=$PYTHONPATH:~/rapp_nao/rapp-robots-api/python/implementations/nao_v4_naoqi2.1.4' >> ~/.bashrc source ~/.bashrc ```

The last step to configure the rapp-robots-api is to declare the NAO IP. It should be stated that the PC and NAO must be in the same LAN. In order to find the NAO IP just press once its chest button while in operation and the robot will dictate it.

The IP must be declared in the first line of this file, thus if the IP is 192.168.0.101 the nao_connectivity file located under ~/rapp_nao/rapp-robots-api/python/implementations/nao_v4_naoqi2.1.4/nao_connectivity should contain:

``` 192.168.0.101 9559 ```

Now all tools are in place to write our simple NAO Python application.

Writing a simple application

Let's create a Python file for our application and give it execution rights:

``` cd ~/rapp_nao mkdir rapps && cd rapps touch simple_app.py chmod +x simple_app.py ```

The first step is to check if everything is in place. Write the following in the simple_app.py file:

```python #!/usr/bin/env python from rapp_robot_api import RappRobot rh = RappRobot() rh.audio.speak("Hello there!") ```

If everything was set-up correctly the NAO robot should talk and say "Hello there!" to you. If not, one of the aforementioned instructions was not performed correctly (or if it they all were, please submit a bug to correct this tutorial!).

Now for the real application, you can use any of the documented API calls that exist here. Insert the following in the simple_app.py file:

```python #!/usr/bin/env python

Import the RAPP Robot API

from rapp_robot_api import RappRobot

Create an object in order to call the desired functions

rh = RappRobot()

Adjust the NAO master volume and ask for instructions. The valid commands are 'stand' and 'sit' and NAO waits for 5 seconds

rh.audio.setVolume(50) rh.audio.speak("Hello there! What do you want me to do? I can sit or get up.") res = rh.audio.speechDetection(['sit', 'get up'], 5) print res word = '' inner_word = '' if res['error'] == None: word = res['word']

Check which command was dictated by the human

if word == 'sit':

The motors must be enabled for NAO to move

rh.motion.enableMotors()

NAO sits with 75% of its maximum speed

rh.humanoid_motion.goToPosture('Sit', 0.75) elif word == 'get up':

The motors must be enabled for NAO to move

rh.motion.enableMotors()

NAO stands with 75% of its maximum speed

rh.humanoid_motion.goToPosture('Stand', 0.75) else:

No command was dictated or the command was not understood

pass

Ask the human what movement to do: move the hands or the head?

rh.audio.speak("Do you want me to move my arms or my head?") res = rh.audio.speechDetection(['arms', 'head'], 5) print res if res['error'] == None: word = res['word']

rh.motion.enableMotors() if word == 'arms': rh.audio.speak("Do you want me to open the left or right hand?") res = rh.audio.speechDetection(['left', 'right'], 5) print res if res['error'] == None: inner_word = res['word'] if inner_word == 'left': rh.humanoid_motion.openHand('Left') elif inner_word == 'right': rh.humanoid_motion.openHand('Right') else: pass

rh.audio.speak("I will close my hands now") rh.humanoid_motion.closeHand('Right') rh.humanoid_motion.closeHand('Left') elif word == 'head': rh.audio.speak("Do you want me to turn my head left or right?") res = rh.audio.speechDetection(['left', 'right'], 5) print res if res['error'] == None: inner_word = res['word']

The head moves by 0.4 rads left or right with 50% of its maximum speed

if inner_word == 'left': rh.humanoid_motion.setJointAngles(['HeadYaw'], [0.4], 0.5) elif inner_word == 'right': rh.humanoid_motion.setJointAngles(['HeadYaw'], [-0.4], 0.5) else: pass

rh.audio.speak("I will look straight now") rh.humanoid_motion.setJointAngles(['HeadYaw'], [0], 0.5) else: pass

rh.audio.speak("And now I will sit down and sleep!") rh.humanoid_motion.goToPosture('Sit', 0.7) rh.motion.disableMotors() ```

As you may have noticed the API calls are robot-agnostic, meaning that the developer (you) can create applications without having to specify on which robot they will be executed. If a specific function is not implemented in a robot that will execute this application, the specific command will simply have no effect.

Finally, the last step is to execute the RApp:

``` python ~/rapp_nao/rapps/simple_app.py ```

This application can be found here.