Simple Arduino FSM
DoorCtrl Class Reference

Contains the FSM for the Door. More...

#include <DoorCtrl.h>

Collaboration diagram for DoorCtrl:

Public Types

enum  Event {
  Event::Open, Event::Close, Event::Opened, Event::Closed,
  Event::Error, Event::Resume, Event::NoEvent
}
 Enum holds all possible events. More...
 
enum  State {
  State::opened, State::closing, State::closed, State::opening,
  State::errorState
}
 Enum holds all possible states for the Door. More...
 

Public Member Functions

 DoorCtrl ()
 Construct a new Door Ctrl object and initailize the currentState with low state. More...
 
void loop ()
 Calls the do-function of the active state and hence generates Events. More...
 
void loop (Event currentEvent)
 Procceses the current Event and calls the do-function of the active state. More...
 
const State getcurrentState ()
 Get the current State. More...
 
String decodeState (State state)
 Decodes the State-Enum and returns a description. More...
 

Private Member Functions

void process (Event e)
 Changes the state of the FSM based on the event Here is where the magic happens :) More...
 
void entryAction_opened ()
 executes the entry action of the opened state. More...
 
DoorCtrl::Event doAction_opened ()
 executes the main action of the opened state. More...
 
void exitAction_opened ()
 executes the exit action of the opened state. More...
 
void entryAction_closing ()
 executes the entry action of the closing state. More...
 
DoorCtrl::Event doAction_closing ()
 executes the main action of the closing state. More...
 
void exitAction_closing ()
 executes the exit action of the closing state. More...
 
void entryAction_closed ()
 executes the entry action of the closed state. More...
 
DoorCtrl::Event doAction_closed ()
 executes the main action of the closed state. More...
 
void exitAction_closed ()
 executes the exit action of the closed state. More...
 
void entryAction_opening ()
 executes the entry action of the opening state. More...
 
DoorCtrl::Event doAction_opening ()
 executes the main action of the opening state. More...
 
void exitAction_opening ()
 executes the exit action of the opening state. More...
 
void entryAction_errorState ()
 entry action of the errorState state. More...
 
DoorCtrl::Event doAction_errorState ()
 main action of the errorState state. More...
 
void exitAction_errorState ()
 exit action of the errorState state. More...
 
String decodeEvent (Event event)
 Decodes the Event-Enum and returns a description. More...
 

Private Attributes

State lastStateBevorError
 holds the last state of the FSM so it's possible to resume after error More...
 
State currentState
 holds the current state of the FSM More...
 
Event currentEvent
 holds the current event of the FSM More...
 
Event(DoorCtrl::* doActionFPtr )(void) = nullptr
 Functionpointer to call the current states do-function. More...
 
Door pDoor = Door(0, 10)
 Door Object with ClosedPostion 0 and OpenPosition 10. More...
 

Detailed Description

Contains the FSM for the Door.

DoorCtrl.png

Definition at line 27 of file DoorCtrl.h.

Member Enumeration Documentation

◆ Event

enum DoorCtrl::Event
strong

Enum holds all possible events.

Enumerator
Open 

Ext: Start Opening.

Close 

Ext: Start Closeing.

Opened 

Signal: Open position reached.

Closed 

Signal: Close position reached.

Error 

Error occured.

Resume 

Ext: Resume after Error occured.

NoEvent 

No event generated.

Definition at line 34 of file DoorCtrl.h.

◆ State

enum DoorCtrl::State
strong

Enum holds all possible states for the Door.

Enumerator
opened 

1 opended State

closing 

2 closing State

closed 

3 closed State

opening 

4 opening State

errorState 

error State

Definition at line 47 of file DoorCtrl.h.

Constructor & Destructor Documentation

◆ DoorCtrl()

DoorCtrl::DoorCtrl ( )

Construct a new Door Ctrl object and initailize the currentState with low state.

Todo:
pass the initial state to the CTor

Definition at line 17 of file DoorCtrl.cpp.

Member Function Documentation

◆ loop() [1/2]

void DoorCtrl::loop ( )

Calls the do-function of the active state and hence generates Events.

Definition at line 21 of file DoorCtrl.cpp.

◆ loop() [2/2]

void DoorCtrl::loop ( Event  currentEvent)

Procceses the current Event and calls the do-function of the active state.

Parameters
currentEvent- Event

Definition at line 26 of file DoorCtrl.cpp.

◆ getcurrentState()

const DoorCtrl::State DoorCtrl::getcurrentState ( )

Get the current State.

Returns
State - current State

Definition at line 32 of file DoorCtrl.cpp.

◆ decodeState()

String DoorCtrl::decodeState ( State  state)

Decodes the State-Enum and returns a description.

Move this Func to private if you don't need it in main

Parameters
state- enum State
Returns
String - State as String

Definition at line 211 of file DoorCtrl.cpp.

◆ process()

void DoorCtrl::process ( Event  e)
private

Changes the state of the FSM based on the event Here is where the magic happens :)

Parameters
e- Event

Definition at line 36 of file DoorCtrl.cpp.

◆ entryAction_opened()

void DoorCtrl::entryAction_opened ( )
private

executes the entry action of the opened state.

Definition at line 112 of file DoorCtrl.cpp.

◆ doAction_opened()

DoorCtrl::Event DoorCtrl::doAction_opened ( )
private

executes the main action of the opened state.

This is an idle-State so it returns always NoEvent when called

Returns
DoorCtrl::Event - generated Event

Definition at line 119 of file DoorCtrl.cpp.

◆ exitAction_opened()

void DoorCtrl::exitAction_opened ( )
private

executes the exit action of the opened state.

Definition at line 125 of file DoorCtrl.cpp.

◆ entryAction_closing()

void DoorCtrl::entryAction_closing ( )
private

executes the entry action of the closing state.

Definition at line 130 of file DoorCtrl.cpp.

◆ doAction_closing()

DoorCtrl::Event DoorCtrl::doAction_closing ( )
private

executes the main action of the closing state.

Calls close() and returns Event::Closed when the Door is closed. else returns NoEvent

Returns
DoorCtrl::Event - generated Event

Definition at line 137 of file DoorCtrl.cpp.

◆ exitAction_closing()

void DoorCtrl::exitAction_closing ( )
private

executes the exit action of the closing state.

Definition at line 146 of file DoorCtrl.cpp.

◆ entryAction_closed()

void DoorCtrl::entryAction_closed ( )
private

executes the entry action of the closed state.

Definition at line 151 of file DoorCtrl.cpp.

◆ doAction_closed()

DoorCtrl::Event DoorCtrl::doAction_closed ( )
private

executes the main action of the closed state.

This is an idle-State so it returns always NoEvent when called

Returns
DoorCtrl::Event - generated Event

Definition at line 158 of file DoorCtrl.cpp.

◆ exitAction_closed()

void DoorCtrl::exitAction_closed ( )
private

executes the exit action of the closed state.

Definition at line 164 of file DoorCtrl.cpp.

◆ entryAction_opening()

void DoorCtrl::entryAction_opening ( )
private

executes the entry action of the opening state.

Definition at line 169 of file DoorCtrl.cpp.

◆ doAction_opening()

DoorCtrl::Event DoorCtrl::doAction_opening ( )
private

executes the main action of the opening state.

Calls open() and returns Event::Closed when the Door is opend. else returns NoEvent.

Returns
DoorCtrl::Event - generated Event

Definition at line 176 of file DoorCtrl.cpp.

◆ exitAction_opening()

void DoorCtrl::exitAction_opening ( )
private

executes the exit action of the opening state.

Definition at line 185 of file DoorCtrl.cpp.

◆ entryAction_errorState()

void DoorCtrl::entryAction_errorState ( )
private

entry action of the errorState state.

Definition at line 190 of file DoorCtrl.cpp.

◆ doAction_errorState()

DoorCtrl::Event DoorCtrl::doAction_errorState ( )
private

main action of the errorState state.

Returns
DoorCtrl::Event - generated Event

Definition at line 198 of file DoorCtrl.cpp.

◆ exitAction_errorState()

void DoorCtrl::exitAction_errorState ( )
private

exit action of the errorState state.

Definition at line 205 of file DoorCtrl.cpp.

◆ decodeEvent()

String DoorCtrl::decodeEvent ( Event  event)
private

Decodes the Event-Enum and returns a description.

Parameters
event- enum Event
Returns
String - Event as String

Definition at line 233 of file DoorCtrl.cpp.

Member Data Documentation

◆ lastStateBevorError

State DoorCtrl::lastStateBevorError
private

holds the last state of the FSM so it's possible to resume after error

Definition at line 94 of file DoorCtrl.h.

◆ currentState

State DoorCtrl::currentState
private

holds the current state of the FSM

Definition at line 95 of file DoorCtrl.h.

◆ currentEvent

Event DoorCtrl::currentEvent
private

holds the current event of the FSM

Definition at line 96 of file DoorCtrl.h.

◆ doActionFPtr

Event(DoorCtrl::* DoorCtrl::doActionFPtr) (void) = nullptr
private

Functionpointer to call the current states do-function.

https://stackoverflow.com/questions/1485983/calling-c-class-methods-via-a-function-pointer

Definition at line 103 of file DoorCtrl.h.

◆ pDoor

Door DoorCtrl::pDoor = Door(0, 10)
private

Door Object with ClosedPostion 0 and OpenPosition 10.

Definition at line 105 of file DoorCtrl.h.


The documentation for this class was generated from the following files: