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.

  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.

Further exercises