State Machines are Your Friends! Sean Donovan What is a state machine? • A state machine is a way of expressing an algorithm or procedure by breaking it down into “states” and “transitions” • A state machine has one “starting state” What is a State? • States usually consist of 2 components: a name and an associated action. • A name could be: “Goto Dentist” and the action could be making yourself goto the dentist. • The “starting state” is the state where the state machine starts in. What is a Transition? • Transitions are conditions that must be satisfied to go from one state to another. • A transition could be: “Time is greater than 2pm”, or “Wheel rotated 14 times” • There can be more than one transition per State • A transition can start in one state and loop back to that same state An Example State Machine • Your daily plan could be shown as a State Machine. Name:Sleep Time = 8AM Name: Work Action: Do work Action: Sleep Time = 2PM Tired = True Hungry = True Name: Eat Dinner Action: Prepare dinner, eat Name: Dentist Appointment Action: Goto Dentist Explanation • The Bubbles represent States • The Connection Arrows represent Transitions Robot Related Usefulness • In an Autonomous Program, State Machines are usually a good way of planning what to do Robot Related Usefulness (cont.) • Say you wanted to follow a line and you have 3 reflective sensors watching the line. Line Sensor Line Following Example • One question you must ask is how many states can there be? 1 2 5 3 6 4 Line Following Example (cont.) • As we can see, there are six States. It is nice to name these states, so they have been numbered to simplify drawing later on. • Since we’ve found out what the states are, we must say what we want to do when each of these states occurs. • At this point we can determine the starting state. We can assign state 3 to be starting state, as ideally the robot would start positioned over the line. • Try to determine what to do before going on with this slide show Line Following Example (cont.) • • • • • • For State 1: Turn to the left For State 2: Turn a little to the left For State 3: Go Straight For State 4: Turn a little to the right For State 5: Turn to the right For State 6: At the end of the line, stop Line Following Example (cont.) • The next step in creating a state machine is to create the transitions. • In this case, the transitions will be simply moving over the new lines, so you do not have to give a reason. • Try to draw a picture of the state machine before going on. Line Following Example (cont.) Start 3 2 4 1 5 6 Line Following Example (cont.) • As you can see, there is a transition from 1 to 2 and a transition from 2 to 1. This is very possible, and quite common • Also not that State 6 stays looping back to itself at the end all the time. Another Example • Suppose this autonomous mode has been planed out: – Go straight for 3 seconds – Turn left for 1 second – Go strait for 3 seconds – Then spin around for the remaining 8 seconds Another Example (cont.) • Below is code that could do just that: Drive(60,0); //straight Wait(3000); Drive(0,60); //turn Wait(1000); Drive(60,0); //straight Wait(3000); Drive(0,60); //spin Wait(8000); Drive(0,0); Another Example (cont.) • This could be put into a loop that checks to see if the robot is in autonomous mode ever time it goes through the loop. (Soon to be implemented in the WPI Framework) while(IsAutonomous()) { //run the state machine } Another Example (cont.) • First step: Identify the states • Very simple this time: – State 1: Go straight – State 2: Turn – State 3: Go straight – State 4: Spin Another Example (cont.) • Next: Identify the Transitions • This is a bit more difficult. Fortunately you may notice that there are only 2 transitions per state. One being a loop back (to self). • Try to determine the transitions before going on. Another Example (cont.) Start <3000 ms 1 >=3000 ms <4000 ms 2 >=4000 ms <7000 ms 3 >=7000 ms IsAutonomous == True 4 IsAutonomous != True Driver Mode Another Example (cont.) • This can be coded in a variety of ways – switch statements – if-else if statements • Code Examples will be posted later in the season