Post on 26-Mar-2015
transcript
Mindstorms State Machines
A structured method for handling tasks andevents using RoboLab and NQC
Brian Smithbbsmith@twmi.rr.com
What is a State Machine?
A state machine is a model of the behavior of a system – in this case an autonomous robot – consisting of states, transitions, and
actions
States
What the robot is doing
Following a line, avoiding an obstacle, turning, or operating an arm are states
Doing nothing is also a state
Transitions
A change in condition that causes the robot to change states
A bumper touch, an increase or decrease in a light sensor value, or receiving a message from another robot are transitions
Actions
How the robot changes states, or what the robot does in a state
Stopping all motors to change the robot's state from moving to stopped or following a line in the line following state are actions
State Machine ExampleA Light Seeking Robot
Step 1 - Define the states
Seek
Locate bright light
State Machine ExampleA Light Seeking Robot
Step 1 - Define the states
Seek Avoid
Avoid an obstacle
State Machine ExampleA Light Seeking Robot
Step 1 - Define the states
Seek Avoid
Follow
Follow bright light
State Machine ExampleA Light Seeking Robot
Step 2 - Define the transition events
Seek Avoid
Follow
LightHigh
Sensor detectincrease in light
State Machine ExampleA Light Seeking Robot
Step 2 - Define the transition events
Seek Avoid
Follow
LightHigh
LightLow
Sensor detectsdecrease in light
State Machine ExampleA Light Seeking Robot
Step 2 - Define the transition events
Seek Avoid
Follow
LightHigh
LightLow
Obstacle
Sensor detects an obstaclein the robot's path
State Machine ExampleA Light Seeking Robot
Step 3 - Define the transitions
Seek Avoid
Follow
LightHigh
LightLow
Obstacle
Light High from Seek – go to Follow
State Machine ExampleA Light Seeking Robot
Step 3 - Define the transitions
Seek Avoid
Follow
LightHigh
LightLow
Obstacle
Light Low from Follow – go to Seek
State Machine ExampleA Light Seeking Robot
Step 3 - Define the transitions
Seek Avoid
Follow
LightHigh
LightLow
Obstacle
Obstacle from Follow or Seek – go to Avoid
State Machine ExampleA Light Seeking Robot
Step 3 - Define the transitions
Seek Avoid
Follow
LightHigh
LightLow
Obstacle
Avoid Complete – go to Seek
State Machine ExampleA Light Seeking Robot
We now have a model of the behavior of a light seeking robot
This model doesn't require any particular robot design, sensors, or programming language
All the robot needs is a way to move, a way to detect a change in light, and a way to detect an obstacle in it's path
State Machine ExampleA Light Seeking Robot
We'll use an RCX differential drive robot with a touch bumper and a light sensor.
State Machine ExampleA Light Seeking Robot
To write the program in RoboLab we need:
State Machine ExampleA Light Seeking Robot
To write the program in RoboLab we need:● A task to watch the touch sensor
State Machine ExampleA Light Seeking Robot
To write the program in RoboLab we need:● A task to watch the touch sensor●A task to watch the light sensor
State Machine ExampleA Light Seeking Robot
To write the program in RoboLab we need:● A task to watch the touch sensor●A task to watch the light sensor
●A subroutine to do the Seek actions
State Machine ExampleA Light Seeking Robot
To write the program in RoboLab we need:● A task to watch the touch sensor●A task to watch the light sensor
●A subroutine to do the Seek actions●A subroutine to do the Follow actions
State Machine ExampleA Light Seeking Robot
To write the program in RoboLab we need:● A task to watch the touch sensor●A task to watch the light sensor
●A subroutine to do the Seek actions●A subroutine to do the Follow actions
●A subroutine to do the Avoid actions
State Machine ExampleA Light Seeking Robot
To write the program in RoboLab we need:● A task to watch the touch sensor●A task to watch the light sensor
●A subroutine to do the Seek actions●A subroutine to do the Follow actions●A subroutine to do the Avoid actions
●A task to handle switching between states
State Machine ExampleA Light Seeking Robot
To write the program in RoboLab we need:● A task to watch the touch sensor●A task to watch the light sensor
●A subroutine to do the Seek actions●A subroutine to do the Follow actions●A subroutine to do the Avoid actions
●A task to handle switching between states●Containers for current and next states
State Machine ExampleA Light Seeking RobotRoboLab Containers
RoboLab provides 23 containers for programmers to use – red (0), blue(1), yellow(2) and user containers 3 to 22.
User container 21 will hold a value representing the current state. Container 22 will hold the value representing the next state.
The values assigned will be 1 for Seek, 2 for Avoid, and 3 for Follow.
State Machine ExampleA Light Seeking RobotLight Task - RoboLab
State Machine ExampleA Light Seeking Robot
Obstacle Task - RoboLab
State Machine ExampleA Light Seeking Robot
Follow Subroutine - RoboLab
State Machine ExampleA Light Seeking Robot
Avoid Subroutine - RoboLab
State Machine ExampleA Light Seeking Robot
Seek Subroutine - RoboLab
State Machine ExampleA Light Seeking Robot
State Change Task - RoboLab
State Machine ExampleA Light Seeking Robot
Complete RoboLab program
State Machine ExampleNQC Implementation
The robot behavior described by our state machine model doesn't require a specific language. We could write the program in NQC, Java, or any other
language that has support for our controller
The following slides show how it could be implemented in NQC
State Machine ExampleNQC Implementation
Instead of containers, NQC uses integer (int) variables to hold values.
'#define' allows you to substitute words for values to make your program easier to read – the value ‘1’ can be replaced by the word ‘stateSeek’ in your program
#define stateUndefined -1#define stateInitialization 0#define stateSeek 1#define stateAvoid 2#define stateFollow 3int nCurrState;int nNextState;
State Machine ExampleA Light Seeking Robot
Light Task - NQC//// task to watch the light sensor// wait for high light, set state to 'stateFollow'// --- then ---// wait for low light, set state to 'stateSeek'// NOTE:// this assumes that robot is not facing the bright light when this task starts//task taskLight(){ // loop forever while(true) { // loop until a bright light is found while(lightSensor < 45) { } // stop motors SetOutput(motorLeft + motorRight, OUT_OFF); // turn A and C off // set state to follow nNextState = stateFollow; // wait 1 second Wait(100); // loop until bright light is lost while(lightSensor >= 45) { } nNextState = stateSeek; }}
State Machine ExampleA Light Seeking RobotObstacle Task - NQC
//// task to watch the bumper sensor// on bump, change next state to 'stateAvoid'//task taskObstacle(){ // monitor forever while(true) { // loop until an obstacle is found while(obstacleSensor == 0) { } // set state to avoid nNextState = stateAvoid; }}
State Machine ExampleA Light Seeking Robot
Follow Subroutine - NQC
//// subroutine for follow state action// no need to monitor for state changes - this just sets the// motors forward at full power and exits//sub subFollow(){ // set and display current state nCurrState = stateFollow; SetUserDisplay(nCurrState, 0); PlaySound(SOUND_DOUBLE_BEEP);
// set both motors forward and exit // they will stay forward until something else changes their direction SetDirection(motorLeft + motorRight, OUT_FWD); SetOutput(motorLeft + motorRight, OUT_FULL); On(motorLeft + motorRight);}
State Machine ExampleA Light Seeking Robot
Avoid Subroutine - NQC//// subroutine for avoid state action// back up, turn right or left, then set next state to seek// this subroutine doesn't look for state changes - it runs to completion//sub subAvoid(){ // set and display current state nCurrState = stateAvoid; SetUserDisplay(nCurrState, 0); PlaySound(SOUND_DOUBLE_BEEP);
// reverse for 1 second SetDirection(motorLeft + motorRight, OUT_REV); SetOutput(motorLeft + motorRight, OUT_FULL); On(motorLeft + motorRight); Wait(100); // turn right or left for 1 second if(Random(1) == 0) { SetDirection(motorLeft, OUT_FWD); } else { SetDirection(motorRight, OUT_FWD); } Wait(100); // stop both motors Off(motorLeft + motorRight); nNextState = stateSeek;}
State Machine ExampleA Light Seeking RobotSeek Subroutine - NQC
// subroutine for seek state action// drive forward for a random time <= 5 seconds, then spin for a random time <= 2 seconds// this subroutine needs to allow itself to be interrupted by state changes//sub subSeek(){ // set and display current state nCurrState = stateSeek; SetUserDisplay(nCurrState, 0); PlaySound(SOUND_DOUBLE_BEEP);
int mSecs = 0; // loop until next state changes while(true) { // forward <= 5 seconds mSecs = Random(500); ClearTimer(T1); // forward <= 5 seconds SetDirection(motorLeft + motorRight, OUT_FWD); SetOutput(motorLeft + motorRight, OUT_FULL); On(motorLeft + motorRight); // this loops until the timer reached the desired value or the state changes while((Timer(T1) <= mSecs) && (nNextState == nCurrState)) { } // break while(true) loop if state has changed if(nNextState != nCurrState) { break; }
State Machine ExampleA Light Seeking Robot
Seek Subroutine – NQC (cont) // turn <= 2 seconds mSecs = Random(200); ClearTimer(T1); // turn right or left <= 2 seconds if(Random(1) == 0) { SetDirection(motorLeft, OUT_REV); // reverse left motor } else { SetDirection(motorRight, OUT_REV); // reverse right } On(motorLeft + motorRight); // this loops until the timer reached the desired value or the state changes while((Timer(T1) <= mSecs) && (nNextState == nCurrState)) { } // break while(true) loop if state has changed if(nNextState != nCurrState) { break; } } // gets here if state has changed (break statements) // turn right and left motors off Off(motorLeft + motorRight); return;}
State Machine ExampleA Light Seeking Robot
State Change Task - NQC// initial setup, handle state changestask main(){ // One-time startup code SetSensor(obstacleSensor, SENSOR_TOUCH); SetSensor(lightSensor, SENSOR_LIGHT); nCurrState = stateUndefined; nNextState = stateInitialization; // Continuous loop for state machine while (true) { // don't change state unless needed if(nCurrState == nNextState) { continue; } // change to next state switch (nNextState) { // initialization to start tasks and set first running state case stateInitialization: start taskLight; start taskObstacle; nNextState = stateSeek; break;
State Machine ExampleA Light Seeking Robot
State Change Task – NQC (cont) // go into seek state (from avoid or follow) case stateSeek: subSeek(); break; // go into avoid state (from seek or follow) case stateAvoid: subAvoid(); break; // go into follow state (from seek only) case stateFollow: if(nCurrState == stateSeek) { subFollow(); } else { nNextState = nCurrState; } break; // Should never get here. Play warning message and restart default: PlaySound(SOUND_DOUBLE_BEEP); SetUserDisplay(9999, 0); Wait(100); stop taskLight; stop taskObstacle; nCurrState = stateUndefined; nNextState = stateInitialization; break; } }}