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!):
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.
Have a look at the programming white papers, there are several.
One of the sections of “Introduction to C Programming for FIRST
robotics,” the most recent version can be obtained from the Technical
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
Yes, can you both send me your code pieces so I can learn these, that would be very helpful
Please provide an email address to which we can send the code examples.
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:
- Determine location of tetrahedron
- Place robot within grabbing range of tetrahedron
- Grab tetrahedron
- Move within capping range of goal
- Put arm in proper location for capping
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