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.
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.
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;
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!
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++ also has comparison operators, which let you compare the values of two different variables. Here are the comparison operators included with C++:
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 }
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... "; MMAccessorwalk(walker_id); walk.mc()->setTargetVelocity(0,0,1.4,2); }
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(int i=0; i<100; i++)
{
cout << i << endl;
}
Finally, the while loop repeats a set of statements until the test condition become false. The basic pattern is:
while(condition)
{
statements
}
while(mcid != cmdlist.end())
{
cout << mcid << endl;
mcid=cmdlist.next(mcid);
}
Along with control structures, functions allow you to structure and organize the logic of the code you write.
Arrays are used in C++ to hold a series of values.
(Briefly discuss vectors)
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.
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.
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)
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
(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))
(Make, gcc, compilation process, linking)