# State Machines

My team wants me to rewrite our nasty autonomous code from last year and I decided I need to learn state machines, does anyone know where I can learn these

The particular flavor that you will want to â€śstudy upâ€ť on is called the Finite State Machine (FSM). Here are a few good links to get you started (then I recommend you get on Google!):

http://sakharov.net/fsm.html

Basically, what you do is make up a list of all of the different â€śoperational modesâ€ť that your various robot subsystems can support, for example: stopped, started, running slow, running fast, broke (ha! ha!). One way to do this is to get a group in front of a white board and put each operational mode inside a circle or box. Then you draw arrows between each circle to the other circles to show how one â€śstateâ€ť can â€śtransitionâ€ť to the next. Then beside each arrow you write down the â€śtransition rulesâ€ť in terms of the various signals or values of variables in your program.

This is a very iterative process. You have to make several passes at it to get it right! Spend the time writing down your cicles and lines and talk about them with the folks who are building the various robot parts. Also talk to the folks who are designing your strategies for the various parts of the game. Finite State Machines can be nicely hierarchical: you can build up bigger state machines by putting together smaller machines (remember that drive train state machine might include the wheel rate monitoring machine and the PWM output control state machine). Also, one stateâ€™s output can be the trigger to several different other state machines to go into another mode, for example the bits that signal the end of autonomous mode could be used to deploy various robot components that were not allowed to operate in autonomous mode for robot safety reasons.

Anyway, Team 147 went through this process last year on the design of their robot controller software. The code was simply a collection of FSMs, the most simple of which was the FSM that turned the compressor on and off.

If you are interested, send me an email and Iâ€™ll try to locate last yearâ€™s source code so that you can see a few examples.

1 Like

Have a look at the programming white papers, there are several.
One of the sections of â€śIntroduction to C Programming for FIRST
section at www.srvhsrobotics.org is specifically on state machines.
If you want to see a few hundred lines of state machine code that uses
time, measured distance, measured gyro angle, positions an arm
with analog feedback, and looks at switches on a robot to select among
autonomous modes, Iâ€™ll put our control code up from last year for you
to go through. It wonâ€™t take you long at all to master state machine
programming.

Yes, can you both send me your code pieces so I can learn these, that would be very helpful

Thank you

Please provide an email address to which we can send the code examples.

Thanks.

A state machine is really a simple concept. Lets say you wanted to design a state machine to, oh lets just pick some random example, find a tetrahedron and place it on a bigger one. I know this seems like a farfetched example, but itâ€™s all I can think of right now. So in order to create a state machine you break this task up into various parts:

1. Determine location of tetrahedron
2. Place robot within grabbing range of tetrahedron
3. Grab tetrahedron
4. Move within capping range of goal
5. Put arm in proper location for capping
6. Cap!

This seems obvious right. But when you are programming a robot you canâ€™t just write out a long program of actions and run it straight through, you need to do things every cycle. This is where state machines come in. One way you could do this is to have a global variable containing the current state of the robot, and then a big switch statement with different actions for each state. Somewhere in the code for each state we have code that advances the state if a certain condition is met. Example: A sensor in your gripper has detected that the tetrahedron has been grabbed, so you want to move on to the next state. Simple concept right? State machines can do very complex things, and states donâ€™t nessicarily have to go in order. There are whole feilds of mathematics dedicated to state machine analysis.

also, take a look at some of the code this year, robot.c in the code posted at kevin.org/frc is a great example of state machines