slide1 l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Mindstorms State Machines PowerPoint Presentation
Download Presentation
Mindstorms State Machines

Loading in 2 Seconds...

play fullscreen
1 / 43

Mindstorms State Machines - PowerPoint PPT Presentation


  • 95 Views
  • Uploaded on

A structured method for handling tasks and events using RoboLab and NQC Brian Smith bbsmith@twmi.rr.com . Mindstorms State Machines. A state machine is a model of the behavior of a system – in this case an autonomous robot – consisting of states , transitions , and actions.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Mindstorms State Machines' - wolfgang


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1
A structured method for handling tasks and

events using RoboLab and NQC

Brian Smith

bbsmith@twmi.rr.com

Mindstorms State Machines
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

What is a State Machine?
slide3
What the robot is doing

Following a line, avoiding an obstacle, turning, or operating an arm are states

Doing nothing is also a state

States
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

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

Actions
step 2 define the transition events
Step 2 - Define the transition eventsState Machine ExampleA Light Seeking Robot

Seek

Avoid

Light

High

Follow

Sensor detect

increase in light

step 2 define the transition events10
Step 2 - Define the transition eventsState Machine ExampleA Light Seeking Robot

Sensor detects

decrease in light

Seek

Avoid

Light

Low

Light

High

Follow

step 2 define the transition events11
Step 2 - Define the transition eventsState Machine ExampleA Light Seeking Robot

Obstacle

Seek

Avoid

Light

Low

Sensor detects an obstacle

in the robot's path

Light

High

Follow

step 3 define the transitions
Step 3 - Define the transitionsState Machine ExampleA Light Seeking Robot

Obstacle

Seek

Avoid

Light

Low

Light

High

Follow

Light High from Seek – go to Follow

step 3 define the transitions13
Step 3 - Define the transitionsState Machine ExampleA Light Seeking Robot

Obstacle

Seek

Avoid

Light

Low

Light

High

Follow

Light Low from Follow – go to Seek

step 3 define the transitions14
Step 3 - Define the transitionsState Machine ExampleA Light Seeking Robot

Obstacle

Seek

Avoid

Light

Low

Light

High

Follow

Obstacle from Follow or Seek – go to Avoid

step 3 define the transitions15
Step 3 - Define the transitionsState Machine ExampleA Light Seeking Robot

Obstacle

Seek

Avoid

Light

Low

Light

High

Follow

Avoid Complete – go to Seek

state machine example a 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
slide20
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
state machine example a light seeking robot21

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
state machine example a light seeking robot22

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
state machine example a light seeking robot23

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
state machine example a light seeking robot24

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
state machine example a light seeking robot25

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 Robot
state machine example a light seeking robot robolab 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 RobotRoboLab Containers
state machine example nqc 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
state machine example nqc implementation35

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 3

int nCurrState;

int nNextState;

State Machine ExampleNQC Implementation
state machine example a 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 RobotLight Task - NQC
state machine example a light seeking robot obstacle 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 RobotObstacle Task - NQC
state machine example a 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 RobotFollow Subroutine - NQC
state machine example a 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 RobotAvoid Subroutine - NQC
state machine example a light seeking robot seek 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 RobotSeek Subroutine - NQC
state machine example a 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 RobotSeek Subroutine – NQC (cont)
state machine example a light seeking robot state change task nqc

// initial setup, handle state changes

task 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 RobotState Change Task - NQC
state machine example a 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;

}

}

}

State Machine ExampleA Light Seeking RobotState Change Task – NQC (cont)