Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith"— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 State Machine Example A Light Seeking Robot Step 1 - Define the states Seek Locate bright light

7 State Machine Example A Light Seeking Robot Step 1 - Define the states SeekAvoid Avoid an obstacle

8 State Machine Example A Light Seeking Robot Step 1 - Define the states SeekAvoid Follow Follow bright light

9 State Machine Example A Light Seeking Robot Step 2 - Define the transition events SeekAvoid Follow Light High Sensor detect increase in light

10 State Machine Example A Light Seeking Robot Step 2 - Define the transition events SeekAvoid Follow Light High Light Low Sensor detects decrease in light

11 State Machine Example A Light Seeking Robot Step 2 - Define the transition events SeekAvoid Follow Light High Light Low Obstacle Sensor detects an obstacle in the robot's path

12 State Machine Example A Light Seeking Robot Step 3 - Define the transitions SeekAvoid Follow Light High Light Low Obstacle Light High from Seek – go to Follow

13 State Machine Example A Light Seeking Robot Step 3 - Define the transitions SeekAvoid Follow Light High Light Low Obstacle Light Low from Follow – go to Seek

14 State Machine Example A Light Seeking Robot Step 3 - Define the transitions SeekAvoid Follow Light High Light Low Obstacle Obstacle from Follow or Seek – go to Avoid

15 State Machine Example A Light Seeking Robot Step 3 - Define the transitions SeekAvoid Follow Light High Light Low Obstacle Avoid Complete – go to Seek

16 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

17 State Machine Example A Light Seeking Robot We'll use an RCX differential drive robot with a touch bumper and a light sensor.

18 State Machine Example A Light Seeking Robot To write the program in RoboLab we need:

19 State Machine Example A Light Seeking Robot To write the program in RoboLab we need: A task to watch the touch sensor

20 State Machine Example A 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

21 State Machine Example A 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

22 State Machine Example A 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

23 State Machine Example A 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

24 State Machine Example A 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

25 State Machine Example A 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

26 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.

27 State Machine Example A Light Seeking Robot Light Task - RoboLab

28 State Machine Example A Light Seeking Robot Obstacle Task - RoboLab

29 State Machine Example A Light Seeking Robot Follow Subroutine - RoboLab

30 State Machine Example A Light Seeking Robot Avoid Subroutine - RoboLab

31 State Machine Example A Light Seeking Robot Seek Subroutine - RoboLab

32 State Machine Example A Light Seeking Robot State Change Task - RoboLab

33 State Machine Example A Light Seeking Robot Complete RoboLab program

34 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

35 State Machine Example NQC 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 3 int nCurrState; int nNextState;

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

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

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

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

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

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

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

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


Download ppt "Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith"

Similar presentations


Ads by Google