Introduction to programming
We will introduce the basic programming concepts using the NAO robot to illustrate them.
The concepts are:
- Input/ Output
- Variable and Parameter
By the end of this lesson, you will be able to make your robot say something.
Step 1: The definitions
- In programming, a function is a named section of a program that performs a specific task. In this sense, a function is a type of procedure or routine.
- Based on the definition above, can you think of functions in your life ? Give an example.
- An input is a piece of data message sent to a function. An output is a piece of data returned by the function.
- Can you think of an input and output in your life?
- A variable is a value that can change, depending on conditions or on information passed to the program.
- What's the variable in our example?
Next, we will program the NAO to say “Hello.” The NAO uses a text-to-speech engine to convert text into sound, which is output through the speakers.
- In Choregraphe, look at the “Box List” to the left. Navigate to Audio -> Voice, then drag and drop a “Say” box to the central area. This central area is called the workspace, and contains the commands that the robot executes.
- To execute the “Say” box, we must connect it to the program “flow”. Click on the small arrow to the left of the workspace, and drag a line to the arrow on the left of the “Say” box. The boxes will be executed sequentially in the order that they are connected. The last box should connect to global stop [x] on the right hand side of the work area.
- Finally, click the green “Play” button next to the “Connect” button. The NAO should say “Hello.”
Intermediate Task: Say Anything!
We will learn how to change the words the NAO says to “Hello world.”
1. Beginning with the results of the previous task, double-click on the “Say” box.
2. A “Localized Text", will appear connected to a “Say Text” box. To return to the previous screen, click “root” on the upper box hierarchy “breadcrumb trail”.
3. Replace the “Hello” in the text box with “Hello World!”.
4. Click the play button, and listen to what the NAO says.
5. Try experimenting with different words and phrases.
Intermediate Task: Voice Acting
This module introduces the concept of Variable and Parameters
The NAO can speak faster, slower, lower or higher, depending on two parameters, “Voice Shaping” and “Speed”.
1. Click on the wrench in the bottom left corner of the “Say” box.
2. The window below will appear:
3. Move the two sliders to a different position and click OK. Press the play button for the NAO to speak again.
4. What changed? Try experimenting with different values for each of the sliders. What do the two parameters control?
Advanced Task: Speak with Python
In addition to dragging and dropping boxes in Choregraphe, the NAO can be programmed using several programming languages including C++ and Python. We will be using Python for our exercise. Documentation about programming in C++ is available online.
We will begin with a simple programming exercise in Python. We will have the robot say “Hello World” exactly as we did before with the Choregraphe Say box. We will do this by creating our own box in Choregraphe that executes our python code.
1. First, open an empty workspace in Choregraphe by creating a new project.
2. Right-click on the workspace, and choose “Create a new box” from the menu.
3. A dialog will appear. In the “Name” box, enter “Python Say”, and in the tooltip box, enter a description of what your box will do. Do not change any of the other options.
4. Click the “Edit” button in the “Image” section. A new dialog box will appear.
5. Click the first “Browse” button and select the image “say.png”. This image will be displayed in the newly created box.
6. Click “OK” on both of the dialog windows. Your new box will appear in the workspace with the icon you selected.
7. Double click on the box to open the Script Editor. The Script Editor shows the Python code that is executed by your box.
8. Look for the line that says “def onInput_onStart(self):”. This line is the definition for a function (or method) named onInput_onStart. A function is a procedure which can be called elsewhere in the code. In this particular case, the onInput_onStart method is called when the box begins executing, and the indented code below this line is called. We will modify the code below this line to make the robot speak.
9. Replacepass in the onInput_onStart method with the following two lines of code, as in the picture below:
ttsProxy = ALProxy(“ALTextToSpeech”)
Note that in Python, the amount of whitespace (spaces and tabs) at the beginning of a line matter. Be sure to put the exact same combination of tabs and spaces on both of these lines (double the amount of whitespace of the line beginning with def).
The first line creates an object that gives us access to the robot’s text-to-speech capabilities. This object is assigned to the variable ttsProxy. We can access this object later through the name ttsProxy.
The second line calls a different function, say, belonging to the object we just created. This function takes an argument, “Hello world!”. “Hello world!” is a string, or a sequence of characters, denoted by the double quote marks. The robot will speak the string that was passed to the say method aloud.
10 Close the script editor window, and link your Python box to the start arrow.
11. Connect to the robot and press the play button. The robot should say “Hello world!” aloud.
- Have the NAO introduce itself and greet the class.
- Have the NAO play the parts of multiple characters in a play by using different voices for each character.
- Have the NAO sing the alphabet by varying the voice shaping and pitch that it pronounces each letter with.