Go Down

Topic: New library: arduino-fsm (Read 1 time) previous topic - next topic

jonblack

I've created a new arduino library called arduino-fsm for implementing a finite state machine1. It's similar to this library2; however, the user triggers a transition by sending an event, allowing the client to remove the need for complex switch statements in their code. This is really an implementation of the State design pattern, using callbacks instead of inheritence.

The code is available on github: https://github.com/jonblack/arduino-fsm

See the example code for a guide on using it. I'll follow up with a blog post soon. It's worth noting the following callbacks:

* on_enter (when a state is entered)
* on_exit (when a state is exited)
* on_transition (when transitioning from one state to another)

Each callback is optional.

Questions? Post here. Clear bugs? See the github issue tracker.

[1] https://en.wikipedia.org/wiki/Finite-state_machine
[2] http://playground.arduino.cc/Code/FiniteStateMachine

robtillaart

Thanks for sharing, bookmarked!
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Chris72622

May you add some more examples, please (because it's a little bit tricky for beginners like me)?  :~

Greetz Chris
Suche PoE Modul für Ethernet Shield

Chris72622

Nowhere i can find the documentation of timeInCurrentState().

Please help!  :smiley-eek:

Greetz Chris
Suche PoE Modul für Ethernet Shield

Chris72622

Nowhere i can still find the documentation of timeInCurrentState().

Please help!  :smiley-eek:

Greetz Chris
Suche PoE Modul für Ethernet Shield

scottmcphee

#5
Sep 01, 2017, 10:03 pm Last Edit: Sep 01, 2017, 10:13 pm by scottmcphee
Check Jon's page here  for an example:

http://www.jonblack.me/arduino-multitasking-using-finite-state-machines/

and this very basic example illustrates the library

http://www.jonblack.me/arduino-finite-state-machine-library/


scottmcphee

There is an unwanted remnant in the version 2.1 of the library... a serial.print command in the fsm.cpp file.

For my install, this is the directory the fsm library lands into:

C:\Users\ <<me!!>> \Documents\Arduino\libraries\arduino-fsm


Find the fsm.cpp file in your installed library directory and comment the serial.print out, as seen below:

Code: [Select]

void Fsm::check_timer()
{
  for (int i = 0; i < m_num_timed_transitions; ++i)
  {
    TimedTransition* transition = &m_timed_transitions[i];
    if (transition->transition.state_from == m_current_state)
    {
      if (transition->start == 0)
      {
        transition->start = millis();
      }
      else
      {
        unsigned long now = millis();
        // **** commented this out: Serial.println(now);
        if (now - transition->start >= transition->interval)
        {
          m_current_state = transition->transition.make_transition();
          transition->start = 0;
        }
      }
    }
  }
}

Go Up