Writing Your First MotionCommand

This document will walk you through the creation of your first MotionCommand.  MotionCommands provide reusable motion primitives which are combined and controlled by a Behavior to accomplish a task.  This guide will show you how to write a MotionCommand to make three of the legs to mirror the position of the fourth leg.


  1. You should complete the "Writing your first Behavior" guide before doing this since we will use the SampleBehavior class here.

  2. Read through the Doxygen documentation on the MotionCommand class, we will be inheriting from it.

  3. If you're new to C++, or just rusty, there is a C++ review available.

The Generic MotionCommand

  1. Create a file named SampleMC.h (or whatever you want to call it) in your project directory.

  2. Set up the usual C++ boilerplate for a class which inherits from the MotionCommand class.  The functions are described below.

    #ifndef INCLUDED_SampleMC_h_
    #define INCLUDED_SampleMC_h_

    #include "Motion/MotionCommand.h"

    class SampleMC : public MotionCommand {
    // Constructor
    SampleMC() : MotionCommand() {}

    // Abstract functions:
    // These must be defined by every MotionCommand
    virtual int updateOutputs() {}
    virtual int isDirty() {}
    virtual int isAlive() {}

  3. This isn't quite valid yet - several of the required functions have return codes which we are ignoring.  But first, a brief description of the functions:

    After adding these lines, the MotionCommand should be valid, although it doesn't do anything.

  4. Now lets add SampleMC to the SampleBehavior.h so you can test it out.  Not that it will do anything yet, but you will be able to test incremental changes from now on.

    First, include the header file and create a new MotionPtr<SampleMC> member variable, initialize it, and then activate it in DoStart

    // [...]
    #include "SampleMC.h"

    class SampleBehavior : public BehaviorBase {
    // [...]
    MotionPtr<SampleMC> mirror;

    SampleBehavior() : BehaviorBase("SampleBehavior"),
    // [...]
    mirror() // initializer

    DoStart() {
    // [...]
    addMotion(mirror); // activate as soon as the behavior runs
    // [...]

    And that's it!  From this point we customize what we want SampleMC to do that makes it unique.

    Note that if your MotionCommand has a definite stopping point and uses isAlive() to reflect this status, you may instead wish to use addPrunableMotion(), and skip removeMotion().  If you do this and care to know when the MC has finished, you can listen for the appropriate deactivate event from motmanEGID.

Further Exercises

Part 2: Adding MotionCommand Functionality will show you how to control the joints.
back to top