43
Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith [email protected]

Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith [email protected]

Embed Size (px)

Citation preview

Page 1: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

Mindstorms State Machines

A structured method for handling tasks andevents using RoboLab and NQC

Brian [email protected]

Page 2: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@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

Page 3: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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

Page 4: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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

Page 5: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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

Page 6: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

Step 1 - Define the states

Seek

Locate bright light

Page 7: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

Step 1 - Define the states

Seek Avoid

Avoid an obstacle

Page 8: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

Step 1 - Define the states

Seek Avoid

Follow

Follow bright light

Page 9: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

Step 2 - Define the transition events

Seek Avoid

Follow

LightHigh

Sensor detectincrease in light

Page 10: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

Step 2 - Define the transition events

Seek Avoid

Follow

LightHigh

LightLow

Sensor detectsdecrease in light

Page 11: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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

Page 12: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

Step 3 - Define the transitions

Seek Avoid

Follow

LightHigh

LightLow

Obstacle

Light High from Seek – go to Follow

Page 13: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

Step 3 - Define the transitions

Seek Avoid

Follow

LightHigh

LightLow

Obstacle

Light Low from Follow – go to Seek

Page 14: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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

Page 15: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

Step 3 - Define the transitions

Seek Avoid

Follow

LightHigh

LightLow

Obstacle

Avoid Complete – go to Seek

Page 16: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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

Page 17: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

We'll use an RCX differential drive robot with a touch bumper and a light sensor.

Page 18: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

To write the program in RoboLab we need:

Page 19: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

To write the program in RoboLab we need:● A task to watch the touch sensor

Page 20: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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

Page 21: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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

Page 22: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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

Page 23: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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

Page 24: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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

Page 25: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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

Page 26: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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.

Page 27: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking RobotLight Task - RoboLab

Page 28: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

Obstacle Task - RoboLab

Page 29: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

Follow Subroutine - RoboLab

Page 30: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

Avoid Subroutine - RoboLab

Page 31: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

Seek Subroutine - RoboLab

Page 32: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

State Change Task - RoboLab

Page 33: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

State Machine ExampleA Light Seeking Robot

Complete RoboLab program

Page 34: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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

Page 35: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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;

Page 36: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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; }}

Page 37: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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; }}

Page 38: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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);}

Page 39: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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;}

Page 40: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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; }

Page 41: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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;}

Page 42: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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;

Page 43: Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com

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; } }}