Aibo C++ Tutorial

Introduction
Basic C++ Features
Advanced Features Further References

Introdution

This book will focus on using the C++ language to develop for the Aibo platform. Although many languages exist, C++ provides a combination of efficiency, features, and integration with the Aibo platform that makes it a natural choice for programming. If you have experience programming with C++ or other languages already (e.g. Java) you should have no trouble learning how to program for the Aibo. If you are a beginning programmer or if you are a little rusty we provide this basic tutorial on using C++ with a focus on the features that Aibo and Tekkotsu use most. Even though this section should cover most of the major features, it is not a replacement for real instruction in the C++ language. If you need more help please consult some of the references found at the end of the section.


Basic C++ Features

First, we'll start with an overview of the language's main features and how programming in the language works. C++ is a compiled, procedural language which is a descendant of the C language. C++ introduced many new features at the time of its release, such as its class system and support for Object Oriented Programming (OOP). It's become very popular due to its power and efficiency, and is used in many major commercial projects.

C and C++ have especially become useful in the field of robotics and embedded devices, where code must be written without much overhead. This is true of the Aibo platform; processing power is limited and many sensors, motors, etc. must be controlled quickly and efficiently. C++'s features

Now let's start learning some of the basic features you will encounter and use while programming on the Aibo.

Variables

There are mainly two types of statements in the C++ language: expressions and declarations. Expressions typically *do* something with the data that is in memory. However, when we want to start using some data for the first time we must first make a declaration to make it available for use. When we declare a variable we are telling the compiler that we want some piece of memory with a specific type The type of the variable determines what you can do with that variable (say adding 2 variables together, or passing the variable to a certain function). In addition to C++'s built-in types (int, float, etc.) the class system allows you to define your own types.

The basic format of a variable declaration is:

variable_type variable_name = initial_value;
You will be dealing with many different types of data while you are programming, and the different number of types reflects that. Here are a few of the common types built-in to C++: In addition to the built-in types, Open-R and Tekkotsu have defined a wide range types specific to robotics programming on the Aibo platform. Here are some example variable declarations:

int i;
bool savedball = false;
float x=0, y=0, area=0;
MotionManager::MC_ID walker_id = motman->addPersistentMotion(SharedObject<WalkMC>());

Although all 4 of these example declarations are performing the same action (creating some area in memory for a value of a specific type) they look a little different from one another. As you can see in the first line, the assignment part of the declaration may be left off if the variable is assigned in some other place. Also, in the third line, multiple variables can be declared on a single line. Finally, in the last line, a Tekkotsu specific type (MC_ID, a MotionController ID) is being created and assigned to a value obtained from the addPersistenMotion function. Don't worry too much about the last example, it will look natural to you soon enough!

Arithmetic

Once you have some variables declared, you typically want to do something with them, such as basic mathematical operations. C++ has support for all of the operations you would expect, and then some. Also, when you are defining your own types using the class system you can define your own operations using the same symbols (+, /, etc.) Here are the common arithmetic operators that C++ lets you use:

C++ has a feature known as weak typing, which means that when some operation is performed on two variables, some conversion maybe applied to allow the operation to happen. By default, C++ does not want to perform conversions such as these, and in some cases you must perform them manually. For example, if you divide 2 (an int) by 3.0 (a float), 2 will be automatically converted to 2.0 to allow the operation to happen. On the other hand, if you divide 2 by 3, both are the same type so no conversion happens, and integer division is carried out, giving an answer of 0. Be careful when mixing data types; you may not be getting the value you expect when an integers and floats are mixed.

C++ also has comparison operators, which let you compare the values of two different variables. Here are the comparison operators included with C++:

C++ also includes the following logical operators to use with conditional expressions and boolean (true/false) values These comparison operators are most commonly used in the program logic or control structures which guide the flow of the program based on certain conditions.

Control Structures

To make your code respond to a variety of conditions you must be able to control how the code is executed. This is done through the use of control structures such as if...else, for, and while.

The if-then-else control structure works by testing a condition, then executing one set of code if it is true, or another set of code if it is false. Multiple if statements can be linked together using the form:

if(condition)
{
   statements
}
else if(condition)
{
   statements
}
//multiple else ifs can follow here
else(condition)
{
   statements
}
Here is an example of this behavior:
if(saw_ball)
{
   //we see the ball, stop searching and start moving forward
   cout << "SearchStateNode: Ball in view, go track (post search->track event)" << endl;
   erouter->postEvent( EventBase::soccerEGID, SoccerNS::GoTrack, EventBase::statusETID, 0);
}
else if(!saw_ball)
{
   //don't see the ball, keep searching
   cout << "SearchStateNode: Searching... ";
   MMAccessor walk(walker_id);
   walk.mc()->setTargetVelocity(0,0,1.4,2);
}
In this code, a boolean variable, saw_ball keeps track of whether the Aibo has seen the ball or not. If the dog sees the ball (the first block) he will signal that he has seen the ball. If he does not see the ball he will keep moving forward. Don't worry about exactly how this is implemented, this is just an example of what conditional logic looks like on the Aibo.

The for loop control structure repeats some block of code while some condition is true. This is most convenient when you want to repeat some part of your code a finite number of times. The basic pattern is:

for(initialization; test condition; increment)
{
   statements
}
For example:
for(int i=0; i<100; i++)
{
   cout << i << endl;
}
This code will print the numbers 0 up to (but not including) 100. For loops are also useful for enumerating an array of values or iterating over a vector.

Finally, the while loop repeats a set of statements until the test condition become false. The basic pattern is:

while(condition)
{
   statements
}
For example:
while(mcid != cmdlist.end())
{
   cout << mcid << endl;
   mcid=cmdlist.next(mcid);
}
Will iterate over the vector cmdlist (using the next method) until it reaches the end. While loops are most useful in this type of situation where the exact number of iterations is not known.

Functions

Along with control structures, functions allow you to structure and organize the logic of the code you write.

Arrays

Arrays are used in C++ to hold a series of values.

(Briefly discuss vectors)

Output

On the Aibo platform the robot can only be monitored remotely over the wireless network connection. Because of this limit to the amount of communication it becomes important to use C++'s features for output effectively. Getting output of your program in C++ is performed mainly by using the output stream cout.


Advanced Features

Although the basic features are usually present in every program, the most important work is done using C++'s more advanced features, particularly the class system.

Program Layout

In C++ it is good form to seperate your code into header files and implementation files. Header files (file extension .h) contain the prototypes or definitions that represent the structure of your program. The implementation file (file extension .cc) contains the actual code that makes the prototypes work. Seperating the definitions from the implementation is one of the key features of making well structured code and is used heavily throughout the Aibo platform.

In Tekkotsu, a header file typically contains a single definition of a Behavior class. The header file also includes all of the necessary libraries that your program will use. The implementation file

Simple example of header here
Simple example of matching implementation here

(Discussion about including of header files / how code is built on top of Open-R/Tekkotsu code)

Namespaces

Along with seperating code into header and implementation, C++ makes use of namespaces to seperate the different levels of code that you are writing. When a file is included at the top of your header or implentation it is possible that different files make the same definitions. To remedy this conflict a namespace is defined at the top of every file, which is used to identify which object you are

Class System

(What is a class)

(How to define a class, public/protected/private)

(What is inheritance, how to inherit)

(Specific examples of Tekkotsu's Behavior metaclass (DoStart, DoStop, ProcessEvent))

Compiling

(Make, gcc, compilation process, linking)


Further Resources