Writing Your First Behavior

This document will walk you through the creation of your first Behavior.  Behaviors are high level programs which direct the robot to complete some task, comparable to an application on a PC.  Following this guide will give you a behavior which will light up LEDs corresponding to which button is pressed.

Prerequisites

  1. You should read through the Doxygen documentation on the BehaviorBase class.

  2. If you're rusty or new to C++, there is a C++ review available, which goes into more detail regarding the boilerplate given below.

The Generic Behavior

  1. First, use your favorite code editor to create a file named SampleBehavior.h (or whatever you want to call it) in your project directory.

    • By convention, we've been naming our classes <Name>Behavior for behaviors, <Name>MC for MotionCommands, or <Name>Control for Controls

    • There is a template file in <project>/templates/behavior.h which will save you some time of typing the boilerplate each time, but for purposes of this tutorial we're doing everything from scratch.
  2. Set up the usual C++ boilerplate, inheriting from the BehaviorBase class.  This class defines the interface which all Behaviors must follow, and provides some simple housekeeping code.

    SampleBehavior.h
    //-*-c++-*-
    #ifndef INCLUDED_SampleBehavior_h_
    #define INCLUDED_SampleBehavior_h_

    #include "Behaviors/BehaviorBase.h"

    class SampleBehavior : public BehaviorBase {
    public:
    SampleBehavior() : BehaviorBase("SampleBehavior") {}

    virtual void DoStart() { }
    virtual void DoStop() { }
    };

    #endif

    If this looks scary to you, look over the C++ review.

    These functions define the BehaviorBase interface.  You will see -*-c++-*- comments at the top of many of our header files - this is a flag to the Emacs editor that it should use C++ mode instead of C mode for syntax hilighting and formatting.  You can ignore it.

  3. At this point, you have a valid behavior. All that remains is to add a call to it from the framework so you can activate it. The easy way to do that is to edit project/UserBehaviors.h. You will need to add two lines, one to #include your header file, and another to add a menu item.

    UserBehaviors.h
    // Part 1: add #include statements for your behaviors' .h files here:
    #include "Behaviors/Demos/HelloWorldBehavior.h"
    #include "SampleBehavior.h"

    // [...] // Part 2: add MENUITEM entries for your behaviors here:
    MENUITEM(HelloWorldBehavior)
    MENUITEM(SampleBehavior)

    This file is #included by project/StartupBehavior_SetupModeSwitch.cc, which lists all of "Mode Switch" menu entries. You could instead put your behavior in that file, following the example of the demo behaviors already listed there.

  4. Assuming you have already configured Environment.conf, now just go to the root Tekkotsu directory and type make.

    • If you have a multi-core machine, you can pass -jN to make, where N is the number of cores on your machine. This will allow make to compile multiple files in parallel.

    • If you are building for the Aibo, make update will compile and copy to the memory stick. Then just put the memory stick into the Aibo and turn it on!  (Be sure to "unpause" by double-tapping the back button to turn off the emergency stop mode once it boots up.)

    • On other platforms, if you are compiling off-board the robot, you will need to copy the executable tekkotsu-TGT to the robot. See the documentation specific for your robot or the compilation section of the download page.

Further exercises

  • You can put a cout in the DoStart() and DoStop() functions to make "Hello world".  (Remember, on the Aibo telnet to port 59000 on the robot to see the output. On other platforms, the output will appear on the console where you launched Tekkotsu.)

    • If adding cout gives you compilation problems, don't forget that cout is part of the std namespace.

  • Adding sound is really easy.  Take a look at SoundManager to make a dog-like version of "Hello World" (a.k.a. "Woof! Woof!"). There are already sounds included in project/ms/data/sound.

  • Shawn Turner of the University at Albany has written two tutorials, Introduction to Behaviors using Tekkotsu and Writing Finite State Automata in Tekkotsu which may be of interest.  (Local cached PDFs are available from the Media page)

  • Part 2: Adding Behavior Functionality will show you one way to control the LEDs.

developer resources

Last modified: 2010-01-10