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-an-in-robot-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 in-robot, thus a real NAO robot is necessary.

Preparation steps

For this tutorial we will use the following tools:

  • A real NAO robot
  • the rapp-robots-api Github repository

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

RAPP Robots API libraries setup

The first step is to clone the appropriate GitHub repository in your PC:

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

The next step is to transfer the RAPP Python libraries to the NAO robot. This will be done via scp, assuming that the NAO robot's IP is 192.168.0.101 and username and password are nao:

```bash cd ~/rapp_nao/ tar -zcvf rapp_api.tar.gz rapp-robots-api/ scp rapp_api.tar.gz nao@1.nosp@m.92.1.nosp@m.68.0..nosp@m.101:~/rapp_api.tar.gz ```

Now connect in NAO via ssh by ssh nao@192.168.0.101 giving nao as password. Then untar the API:

```bash tar -xvf rapp_api.tar.gz rm rapp_api.tar.gz ```

The next step is to update the PYTHONPATH variable. Since NAO has Gentoo as OS, we will modify the bash_profile file:

```bash echo 'export PYTHONPATH=$PYTHONPATH:~/rapp-robots-api/python/abstract_classes' >> /home/nao/.bash_profile echo 'export PYTHONPATH=$PYTHONPATH:~/rapp-robots-api/python/implementations/nao_v4_naoqi2.1.4' >> /home/nao/.bash_profile source ~/.bash_profile ```

The last step to configure the rapp-robots-api is to declare the NAO IP. Since the robot API is in-robot, the IP must be localhost: 127.0.0.1.

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

``` 127.0.0.1 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:

```bash mkdir ~/rapp_nao cd /home/nao/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:

```bash python /home/nao/rapp_nao/rapps/simple_app.py ```