Skip to content
jdddog edited this page Sep 23, 2014 · 74 revisions

Programming Nao with the Human Robot Interaction API

This page describes how to get started programming your Nao robot with the Human Robot Interaction API (hri_api).


Contents

1. Tutorials

1.1. Starting hri_api

1.2. Making Nao to speak (Python)

1.3. Making Nao gesture (Python)

1.4. Making Nao gaze at people (Python)

1.5. Making Nao speak and gaze at a group of people simultaneously (Python)

1.6. Querying the World for objects Nao can sense (Python)

1.7. Listening to what people say (Python)

1. Tutorials

These tutorials describe how to get started programming human-robot interaction for the Nao humanoid robot. Make sure that Nao is on, connected to the network and in a stable standing pose. All of the examples described in this tutorial are located in the folder nao_hri/scripts.

1.1. Starting hri api

A number of background services need to be started before you can run your human-robot interaction programs on Nao. First, open a terminal terminal window and start the ros master service:

user@pc:~$ roscore
... logging to /home/user/.ros/log/3a034616-42a2-11e4-969d-5
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://pc:54570/
ros_comm version 1.11.9


SUMMARY
========

PARAMETERS
 * /rosdistro: indigo
 * /rosversion: 1.11.9

...

Then, in a new terminal tab, start the hri_api background services:

user@pc:~$ roslaunch nao_hri nao_interaction_sim.launch
... logging to /home/user/.ros/log/d29dcda4-42ae-11e4-b129-b8ca3a81
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

SUMMARY
========

PARAMETERS
 * /armature_name: Armature
 * /blender_target_controllers: /home/user/catki...
 * /launch_blender/blend_file: /home/user/catki...
 * /launch_blender/python_script: /home/user/catki...
 * /launch_blender/use_game_engine: False
 * /nao_gaze_action_server/action_server_name: gaze
 * /nao_gaze_action_server/axes: yz
...

1.2. Making Nao speak (Python)

Now that the background services have been started, lets learn how to make Nao speak.

1.2.1 The Code

This is an example script that makes Nao speak. You can also find it in the nao_hri package under: nao_hri/scripts/hri_say_examples.py

1 #!/usr/bin/env python
2 # license removed for brevity
3 from nao_hri import Nao
4  
5 robot = Nao()
6 robot.say_and_wait("Hello")
7 robot.say_and_wait("I'm a crazy robot")

1.2.2 The Code Explained

Now, let's break the code down.

1 #!/usr/bin/env python

Every hri_api script must have this declaration at the top. The first line makes sure your script is executed as a Python script.

3 from nao_hri import Nao

The nao_hri import is so that we can use the Nao robot class to program Nao.

5 robot = Nao()

This line defines a Nao robot instance.

6 robot.say_and_wait("Hello")
7 robot.say_and_wait("I'm a crazy robot")

This section of code makes Nao say "Hello" and then "I'm a crazy robot". The function robot.say_and_wait(text) instructs the robot to speak the text contained in the parameter text. This function returns when the robot has finished speaking.

1.3. Making Nao gesture (Python)

Now that we've made Nao speak, lets learn to make him gesture.

1.3.1 The Code

This is an example script that makes Nao gesture. You can also find it in the nao_hri package under: nao_hri/scripts/hri_gesture_examples.py

 1 #!/usr/bin/env python
 2 # license removed for brevity
 3 from nao_hri import Nao, Gesture
 4 
 5 robot = Nao()
 6
 7 robot.gesture_and_wait(Gesture.HandsOnHips)
 8 robot.gesture_and_wait(Gesture.MotionLeft)
 9 robot.gesture_and_wait(Gesture.MotionRight)
10 robot.gesture_and_wait(Gesture.WaveLarm, duration=10.0)
11 robot.gesture_and_wait(Gesture.LarmDown)
12 robot.gesture_and_wait(Gesture.RarmDown)

1.3.2 The Code Explained

Now, let's break the code down.

1 #!/usr/bin/env python

As mentioned previously, every hri_api script must have this declaration at the top. The first line makes sure your script is executed as a Python script.

3 from nao_hri import Nao, Gesture

This time, as well as importing a Nao class, we have also imported a Gesture class. This is an enum that contains a list of all of the gestures Nao can perform.

5 robot = Nao()

This line defines a Nao robot instance.

 7 robot.gesture_and_wait(Gesture.HandsOnHips)
 8 robot.gesture_and_wait(Gesture.MotionLeft)
 9 robot.gesture_and_wait(Gesture.MotionRight)

This section makes Nao put his hands on his hips, then motion his left hand to the left and then motion his right hand to the right. The robot.gesture_and_wait(gesture) function enables this, it takes a Gesture enum as a parameter which details the type of gesture to perform.

10 robot.gesture_and_wait(Gesture.WaveLarm, duration=10.0)

This line makes Nao wave using his left arm for 10 seconds. In the previous examples, no duration was specified, so the gesture's were performed for their default durations. The default durations are specified in: nao_hri/src/nao_hri/nao.py

11 robot.gesture_and_wait(Gesture.LarmDown)
12 robot.gesture_and_wait(Gesture.RarmDown)

This section makes Nao put both of his arms back down by his side again.

1.4. Making Nao gaze at people (Python)

We now learn how to make Nao gaze at things, in particular parts of a peoples bodies.

1.4.1 The Code

This is an example script that makes Nao gaze at different parts of a peoples bodies. You can also find it in the nao_hri package under: nao_hri/scripts/hri_gaze_examples.py

 1 #!/usr/bin/env python
 2 # license removed for brevity
 3
 4 from hri_api.entities import Person
 5 from nao_hri import Nao
 6
 7 robot = Nao()
 8 
 9 person1 = Person(1)
10 person2 = Person(2)
11 person3 = Person(3)
12
13 robot.gaze_and_wait(person1.head)
14 robot.gaze_and_wait(person2.torso)
15 robot.gaze_and_wait(person3.left_hand, speed=0.8)

1.4.2 The Code Explained

Now, let's break the code down.

 4 from hri_api.entities import Person
 5 from nao_hri import Nao

To make Nao interact with people, we need to import a Person class from the hri_api.entities module.

 7 robot = Nao()

As usual we create a Nao robot instance.

 9 person1 = Person(1)
10 person2 = Person(2)
11 person3 = Person(3)

This snippet creates three instances of the Person class. In section 1.1 when you ran the command roslaunch nao_hri nao_interaction.launch, it started ROS nodes that broadcast simulated coordinates for these three people.

13 robot.gaze_and_wait(person1.head)
14 robot.gaze_and_wait(person2.torso)
15 robot.gaze_and_wait(person3.left_hand, speed=0.8)

We now make Nao gaze at the three people with the robot.gaze_and_wait(target) function. This function returns when the robot has succeeded gazing at the specified target. In this example, Nao first gazes at person1's head, then at person2's torso and lastly at person3's left hand.

As can be seen on line 15, you can also control the speed that the robot gazes at via the speed parameter. This is a normalized value between the range: 0.0 < speed <= 1.0.

1.5. Making Nao speak and gaze at a group of people simultaneously (Python)

In this section we learn how to make Nao speak and gaze at people simultaneously. It is particulary useful if you have long sentences for the robot to speak.

1.5.1 The Code

This is an example script that makes Nao speak and gaze at a group of people simultaneously. You can also find it in the nao_hri package under: nao_hri/scripts/hri_say_to_examples.py

 1 #!/usr/bin/env python
 2 # license removed for brevity
 3
 4 from hri_api.entities import Person
 5 from nao_hri import Nao
 6
 7 robot = Nao()
 8 person1 = Person(1)
 9 people = [person1, Person(2), Person(3)]
10 
11 robot.say_to_and_wait('Hello who are you?', person1)
12 robot.say_to_and_wait('We choose to go to the moon in this decade, ' 
13                       'not because its easy, but because its hard, '
14                       'because that goal will serve to organize and '
15                       'measure the best of our energies and skills, '
16                       'because that challenge is one that we are '
17                       'willing to accept, one we are unwilling to '
18                       'postpone, and one which we intend to win.', people)

1.5.2 The Code Explained

Now, let's break the code down.

 7 robot = Nao()
 8 person1 = Person(1)
 9 people = [person1, Person(2), Person(3)]

As usual we create an instance of the Nao robot class. We then create a Person object and assign it to the variable person, as well as creating a list of people, assining it to the variable people.

11 robot.say_to_and_wait('Hello who are you?', person1)

This line makes the robot say 'Hello who are you?' to person1. It does this via the function robot.say_to_and_wait(text, audience). In this case, the robot first gazes at person1 and then when its gaze action has succeeded, it says 'Hello who are you?' to that person.

12 robot.say_to_and_wait('We choose to go to the moon in this decade, ' 
13                       'not because its easy, but because its hard, '
14                       'because that goal will serve to organize and '
15                       'measure the best of our energies and skills, '
16                       'because that challenge is one that we are '
17                       'willing to accept, one we are unwilling to '
18                       'postpone, and one which we intend to win.', people)

Lines 12 - 18 illustrate a slightly more complex example, which makes the robot say a much longer sentence to a group of people. The audience parameter of robot.say_to_and_wait(text, audience) can be a single Person object, a list of Person objects or a Query that contains Person objects (explained later on). In this case, Nao will initially make eye contact with the person in the people list that is closest to Nao. Once Nao has made eye contact with the person, it will start speaking. Whenever Nao reaches a new sentence (denoted by punctuation: ,.?!), it chooses a random person from the list and changes its gaze to them.

Clone this wiki locally