Code Monkey home page Code Monkey logo

easy-finite-state-machine-arduino's Introduction

Easy Finite State Machine for Arduino

Macros to create a finite state machine in Arduino

Create a finite state machine in 8 steps

  1. Include efsm.h
  2. Define states
  3. Define inital state
  4. Define events
  5. Define transitions
  6. Define executions
  7. Define triggers
  8. Enjoy you fsm!

Include efsm.h

efsm define C Macros to make easy create a fsm.

#include <efsm.h>

Define States

STATES {start, step1, step2, finish};

Is translate to:

enum States{start, step1, step2, finish};

Define Initial State

INIT(start);

Is translate to:

enum States state = start;

Define events

EVENTS {next, back, end};

Is translate to:

enum Events{next, back, end};

Define transitions

Transitions define changes in state when an event is launched:

TRANSITION(event,initalState,finalState,function())

if(event && state == initalState){
state = finalState;
call function();
}

START_TRANSITIONS

TRANSITION(next,start,step1,printf("next\n"))//execute a function
TRANSITION(next,step1,step2,)//no execute function

END_TRANSITIONS

Define executions

Executions define funtions to call in each state

EXECUTION(executionState,function())

if(state == executionState){ function(); }

START_EXECUTIONS  

EXECUTION(start,printf("start\n"))  
EXECUTION(step1,printf("step1\n"))  

END_EXECUTIONS  

It is not mandatory to use EXECUTIONS

TRIGGERS

Define conditions to launch events

There are 3 types:

COUNTER(initalState,number,event);

In pseudocode:

if(state == intialState && timesTriggerIsCalledSinLastEvent >= number){
launchEvent(event);
}

TIMER(initalState,milliseconds,event);

In pseudocode:

if(state == intialState && millisecondsSinceLastEvent >= milliseconds){
launchEvent(event);
}

CONDITIONAL(initalState,condition,event);

In pseudocode:

if(state == intialState && condition){
launchEvent(event);
}

START_TRIGGERS

COUNTER(start,5,next); //State start wait 5 iterations and launch event next  
TIMER(step1,2000,next); //State step1 wait 2 sg and launch event next  
CONDITIONAL(start, digitalRead(buttonPin) == HIGH, restart); //State finish wait until push button an launch event restart  

END_TRIGGERS

Enjoy you fsm!

You need add to loop function:

efsmExecute();
efsmTriggers();

Others macros

ANY_EVENT
ANY_STATE

Usefull to define events, transactions and triggers

isState(state);

Is true is machine state is state

changeState(state);

Put machine state to state

resetTimer();

Reset time since last event

resetCounter();

Reset counter since last event

easy-finite-state-machine-arduino's People

Contributors

cubiwan avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.