A state machine is a design mechanism that places the software system into one (or more) states. Triggers are used to move from one state to another state.
What can we use this concept for in Robotics? We can use a state machine any time we want to perform a sequential set of steps or any time we want to perform a set of steps driven by an event or behavior. For example, a sequential set of steps may be following a path. An event may be the robot reaching a wall or seeing a line on the floor.
State machines are often drawn as circles with lines joining the circles together. This is called the software design. Design is very useful for understanding the big picture of how the software works. Complex software statements are simplified down into pseudo code (English statements).The lines are events that move the state machine from one state to the next. In a statemachine, the behaviors of interest can be run whenever the state is active, or on transition from one state to the next. Here is an example of a very simple state machine that is used to turn a light on and off:
The state machine begins in the LIGHT_OFF state. (See the black dot with the arrow pointing into the LIGHT_OFF state). In this state, the word "Off is displayed on the NXT screen. When button 1 (the x button) is pressed on the joystick and a rising edge is detected (the symbol before joy1Btn), the state machine transitions into LIGHT_ON.
In the LIGHT_ON state, the word "ON" is displayed on the NXT screen. When button 1 (the x button) is pressed on the joystick and a rising edge is detected (the symbol before joy1Btn), the state machine transitions into LIGHT_OFF.
Why use a rising edge detector instead of looking from a true value on joy1Btn(1)? If a TRUE value from joy1Btn(1) caused the transition, the state machine would jump back and forth between LIGHT_ON and LIGHT_OFF until the joystick button is de-selected. Try this out in the code to see what it does. This is called a race condition. The race condition can be prevented in a number of ways. For example, I could have used a different button to transition back.
Now that the design is complete, it is time to write software. The statemachine needs to be called continually. To do this, the periodic task program, described in a previous blog post, or a while(true) is used. See the following software:
// // As a home owner, I want to turn a light on and off with my joystick so I can see where I am going when it is dark. // #include "JoystickDriver.c" #include "i_riseEdge.c" bool btn_z1=false; // Used with FallEdge // My states #define LIGHT_ON 1 #define LIGHT_OFF 2 int sm_light = LIGHT_OFF; //The default state task main(){ while(true){ getJoystickSettings(joystick); // update buttons and joysticks eraseDisplay(); //-------------------------- Start of the State Machine ----------------// switch (sm_light) { case LIGHT_OFF: //State // Do this when the light is off nxtDisplayString(2, "Light Off"); if (RiseEdge(joy1Btn(1),btn_z1)){ //Transition // Do this on a transition sm_light=LIGHT_ON; } break; case LIGHT_ON: //State nxtDisplayString(2, "Light On"); if (RiseEdge(joy1Btn(1),btn_z1)){ //Transition // Do this on a transition sm_light=LIGHT_OFF; }
break; } // ------------------------- End of the State Machine ------------------// wait1Msec(50); }// While Loop }// Foreground Task
The states are defined using a "#define STATE_NAME 1" statement. If you want an additional state, just add a new #define statement. Make sure each # define has a unique integer number assigned to it. The current active state is defined as an integer in the line "int sm_light = LIGHT_OFF;" This line says the "LIGHT_OFF" state is the initial state the machine "sm_light" will enter when called.
The work of the state machine is done with a switch/case structure. Each case is a state for the state machine. Transitions are implemented with an if (){ sm_light=LIGHT_OFF} statement. Any code placed within the case statement will execute every iteration. This is where I placed the nxtDisplayString statement.
The function called RiseEdge looks for a rising edge of a boolean signal. RiseEdge returns a true value only when the input signal is true and the past frame signal is false. Note: the & in front of z1 in the function argument passes the address of z1 into the function call. This allows z1 to act as both an input and an output from the function call. See the following code:
// Story: As a software designer I want to find the rising edge of a signal so that I can trigger an event // Usage: Make sure globalBoolEqF is initialized equal to false // bool globalBoolEqF = false; //global // outBool = RiseEdge( inBool, globalBoolEqF ); bool RiseEdge(bool in, bool &z1) { bool out; out=false; if ((in==true) && (z1==false)) out=true; z1=in; return(out); }
This describes the basics behind creating a state machine design and implementing it in software. Here is an example of a state machine that drives a robot in on a path in the shape of a square box 5 inches in size:
Try to develop a state machine that does this. Note: the /num=0 is software that executes on a transition. This means the "num=0" is done when the variable is defined and the /num++ incriments this variable on the transition from Drive_Straight to Turn_Right. The transition "5 inches" and "90 degrees" can be done using the distance of the encoders. Let me know how you do.
No comments:
Post a Comment