07.14.09
Escape Sequence – A generic approach to getting away
The 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.
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.
// *** NEEDS EscapeSequence.h // *** ASSUMES A FRONT BUMPER IS IMPLEMENTED IN THE MAIN ROBOT CODE // BEHAVIOR CODE TO ESCAPE FROM A FRONTAL COLLISION void BEH_BumpEscape() { Behavior(BEHAVIOR_BUMP_ESCAPE); // SET THE BEHAVIOR ID static typEscapeState bumpState; // ESCAPE STATE VALUE bool isCollision; // COLLISION VALUE isCollision = bumper_isBumped(); // DID A COLLISION OCCUR? if (bumpState != STATE_START) { // ALREADY ACTIVE, FOLLOW ESCAPE ROUTINE EscapeSequence(bumpState, isCollision); } else { if (isCollision) EscapeSequence(bumpState, isCollision); else DeclineControl(); // NO COLLISION OCCURED-DO NOTHING } }
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.