Home > AreaBot, CollectorBot, My Robots, RobotC Code > Escape Sequence – A generic approach to getting away

Escape Sequence – A generic approach to getting away

BumpEscapeStateDiaThe escape behavior is one of the more important behaviors implemented in my robots.  Any time the robot bumps into a wall, there is an opportunity to escape.  In response to a trigger (collision), the robot backs up, spins away, and nudges forward to execute the “escape sequence”.  I have detailed this sequence in articles about creating the AreaBot robot.

The reason I am blogging about it again, is that I believe the escape sequence is a generic action.

Download Sample Code: EscapeSequence.zip

What do I mean?

In my experiments so far, any time the robot needs to escape from a trigger (collision, too much light, etc…) the same sequence of events occurs in order to free the robot from the trigger.

EscapeSequence

We examined BUMP ESCAPE where a bumper collision occurs.  Once triggered, the robot backs up for a time, spins right or left for a number of degrees, and finishes by moving forward a short time to make sure it starts the next behavior in a different location as when escape was triggered.

For the collector robot, a behavior was written that needs to escape when a light source is too bright (LIGHT ESCAPE).  Why not use the same code to escape from a “too bright” light source.  I have a Boolean trigger, and I understand the movements the robot can perform to move away from the light.  At first, I copied the escape code from bump escape, and changed it to work with the bright light.  Then I got an idea…

Maybe I should create a generic escape sequence function and allow other behaviors to utilize the common code.  I spent an evening coping key parts of the code to a new include, and called it escape sequence.  Now a new escape behavior is easy to implement.  I just need to know the current state and trigger condition.

   1: // *** NEEDS EscapeSequence.h

   2: // *** ASSUMES A FRONT BUMPER IS IMPLEMENTED IN THE MAIN ROBOT CODE

   3:  

   4: // BEHAVIOR CODE TO ESCAPE FROM A FRONTAL COLLISION

   5: void BEH_BumpEscape() {

   6:   Behavior(BEHAVIOR_BUMP_ESCAPE);  // SET THE BEHAVIOR ID

   7:   static typEscapeState bumpState; // ESCAPE STATE VALUE

   8:   bool isCollision;                // COLLISION VALUE

   9:  

  10:   isCollision = bumper_isBumped();    // DID A COLLISION OCCUR?

  11:   if (bumpState != STATE_START) {   // ALREADY ACTIVE, FOLLOW ESCAPE ROUTINE

  12:     EscapeSequence(bumpState, isCollision);

  13:   }

  14:   else {

  15:     if (isCollision)

  16:       EscapeSequence(bumpState, isCollision);

  17:       else

  18:         DeclineControl();  // NO COLLISION OCCURED-DO NOTHING

  19:     }

  20: }

Here is the new BUMP ESCAPE behavior.  The behavior checks if the robot is in a collision (trigger condition), and calls EscapeSequence(currentState, currentTriggerCondition).  The Escape Sequence does the rest.  How is that for a few POWERFUL lines of code!

The code is very similar to implement LIGHT ESCAPE.  Notice the trigger condition isTooLight.  The only difference is under the statement that checks “not state = start” where I send “false” as the condition.  If I sent the isTooLight condition here, the robot backs up until there is not too much light.  I just wanted the robot to recognize it was too close to the light source and turn away.

// *** NEEDS EscapeSequence.h
// *** ASSUMES TWO LIGHT SENSORS ARE IMPLEMENTED IN THE MAIN ROBOT CODE

// BEHAVIOR CODE TO ESCAPE FROM A FRONTAL COLLISION
void BEH_LightEscape() {
  Behavior(BEHAVIOR_LIGHT_ESCAPE);  // SET THE BEHAVIOR ID
  static typEscapeState leState; // ESCAPE STATE VALUE
  bool isTooLight;                  // COLLISION VALUE

  isTooLight = DiffLight_IsHome();

  if (leState != STATE_START) { // ACTIVE, EXECUTE ESCAPE ROUTINE
    EscapeSequence(leState, false);
  }
  else {
    if (isTooLight)  // TRIGGER THE ESCAPE ROUTINE TO START
      EscapeSequence(leState, isTooLight);
      else
        DeclineControl();  // NO COLLISION OCCURED-DO NOTHING
    }
}

I will make all code for the collector robot available very soon as one download package.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: