I will be maintaining my libraries here:
http://bit.ly/pATDBi

I am the lead developer for libraries 
that ship with the Wiring distribution. 
As per version 1.0 -
Wiring will support Arduino boards.
You are welcome to check it out!
http://wiring.org.co/download/

Finite State Machine Library for Arduino
Author:  Alexander Brevig
Contact: alexanderbrevig@gmail.com


Navigation


Current version

1.7 2010-03-08: Fixed a bug, constructor ran update, thanks to René Pressé


History

2.0 2013-02-08: LINK: Versions fixed for Arduino 1.0x Terry King http://arduino-info.wikispaces.com/HAL-LibrariesUpdates

1.8 2010-02-04: Fixed a bug, missing return type, thanks to Rick Howard
1.7 2010-03-08: Fixed a bug, constructor ran update, thanks to René Pressé
1.6 2010-03-08: Added timeInCurrentState() , requested by sendhb
1.5 2009-11-29: Fixed a bug that was introduced by the last fix, thanks to Jon Hylands again
1.4 2009-11-29: The first state now calls it's enter(), thank you Jon Hylands for bringing this bug to my attention.
1.3 2009-11-01: Added getCurrentState and isInState, requested by Henry Herman
1.2 2009-05-18: Enter and exit bug fix
1.1 2009-05-18: Added support for cascaded calls
1.0 2009-04-11: Beta


Description

Finite State Machine Description

A FSM serves as a manager that organizes a set of states, or behaviors. It manages the transition between states, and the state itself.
The transitions might be caused by the user, a wall or something other that is external, and they might be caused by some internal logic. Transitions either happen immediately, or they are deferred to the next update. The latter is the most common. It's used because that way you'll know that all code related to the current state, will have executed on the same state.

FSM Library Description

FSM is a library for the Arduino.

It is created to help organizing and standardizing the way a FSM could be implemented.

This library expects all functionality to be coded in the sketch, and the State class will simply use callbacks to simulate functionality.

All States expects an enter function, an update function and an exit function. These functions are being called according to this scheme:

current.exit();  //exit current state
next.enter();    //enter next state
current = next;
while no transition
  current.update();

//TODO - extend this section, contact if urgent


Download, install and import

Download here: FSM

NOTE: Updated version for Arduino1.0x can be found here: http://arduino-info.wikispaces.com/HAL-LibrariesUpdates 2/13 terry@yourduino.com

Put the FSM folder in "hardware\libraries\".

In the Arduino IDE, create a new sketch (or open one) and

select from the menubar "Sketch->Import Library->FSM".

Once the library is imported, an "#include <FiniteStateMachine.h>" line will appear

at the top of your Sketch.


Creation

State Creation

State( enterFunction , updateFunction , exitFunction )

State ethernetDebugState = State( connectToHost , debug , closeConnectionToHost );

Finite State Machine Creation

FiniteStateMachine(State& current)
FSM(State& current)

FSM ethernetStateMachine = FSM(ethernetDebugState);

Initializes an FSM object with the current state equal to ethernetDebugState


Functions

State Functions

void enter()

This function gets called whenever this state is entered

void update()

This function gets called whenever the state machine updates while in this state

void exit()

This function gets called whenever this state is exited

Finite State Machine Functions

void update()

This function will trigger update on the current State

void transitionTo(State& next)

This function will schedule a state change, the change itself will occur at the beginning of the next update

void immediateTransitionTo(State& next)

This function will instantly change the current state to next state

State& getCurrentState()

Returns the current state

boolean isInState( State &state )

Check if state is equal to the current state of the FSM


Example

LED Finite State Machine

We will implement a state machine for an LED.

From a design point of view we want to make the led go on and off, as well as fade in and out. This translates directly to the states for our example:

  • On
  • Off
  • FadeIn
  • FadeOut

The states describe themselves.

We will use external transitions, caused by a user and their button presses.
Each button will cause the FSM to advance to the next State in the diagram above.

  1. //http://playground.arduino.cc/uploads/Code/FSM.zip
  2. #include <FiniteStateMachine.h>
  3. //http://playground.arduino.cc/uploads/Code/Button.zip
  4. #include <Button.h>
  5. //http://playground.arduino.cc/uploads/Code/LED.zip
  6. #include <LED.h>
  7.  
  8. const byte NUMBER_OF_STATES = 4; //how many states are we cycling through?
  9.  
  10. //initialize states
  11. State On = State(ledOn);
  12. State Off = State(ledOff);
  13. State FadeIn = State(ledFadeIn);
  14. State FadeOut = State(ledFadeOut);
  15.  
  16. FSM ledStateMachine = FSM(On);     //initialize state machine, start in state: On
  17.  
  18. Button button = Button(12,PULLUP); //initialize the button (wire between pin 12 and ground)
  19. LED led = LED(11);                 //initialize the LED
  20. byte buttonPresses = 0;            //counter variable, hols number of button presses
  21.  
  22. void setup(){ /*nothing to setup*/ }
  23.  
  24. //poor example, but then again; it's just an example
  25. void loop(){
  26.   if (button.uniquePress()){
  27.     //increment buttonPresses and constrain it to [ 0, 1, 2, 3 ]
  28.     buttonPresses = ++buttonPresses % NUMBER_OF_STATES;
  29.     switch (buttonPresses){
  30.       case 0: ledStateMachine.transitionTo(On); break;
  31.       case 1: ledStateMachine.transitionTo(Off); break;
  32.       case 2: ledStateMachine.transitionTo(FadeIn); break;
  33.       case 3: ledStateMachine.transitionTo(FadeOut); break;
  34.     }
  35.   }
  36.   ledStateMachine.update();
  37. }
  38.  
  39. //utility functions
  40. void ledOn(){ led.on(); }
  41. void ledOff(){ led.off(); }
  42. void ledFadeIn(){ led.fadeIn(500); }
  43. void ledFadeOut(){ led.fadeOut(500); }
  44. //end utility functions


FAQ

How can I use multiple FSMs? //Under Construction


Links


Information about this page

Part of AlphaBeta Libraries.
Last Modified: January 04, 2014, at 09:04 PM
By: pert

Share