Simple Arduino FSM
DoorCtrl.cpp
Go to the documentation of this file.
1 
14 #include "DoorCtrl.h"
15 
16 //=====PUBLIC====================================================================================
17 DoorCtrl::DoorCtrl() : currentState(State::opened) {
19 }
20 
22  DBFUNCCALLln("DoorCtrl::loop()");
23  process((this->*doActionFPtr)()); //do actions
24 }
25 
26 void DoorCtrl::loop(Event currentEvent) {
27  DBFUNCCALLln("DoorCtrl::loop(Event)");
29  process((this->*doActionFPtr)()); //do actions
30 }
31 
33  return currentState;
34 }
35 //=====PRIVATE====================================================================================
37  DBFUNCCALL("DoorCtrl::process ")
39  switch (currentState) {
40  case State::opened:
41  if (Event::Close == e) {
42  exitAction_opened(); // Exit-action current state
43  entryAction_closing(); // Entry-actions next state
44  } else if (Event::Error == e) {
45  exitAction_opened(); // Exit-action current state
46  entryAction_errorState(); // Entry-actions next state
47  }
48  break;
49 
50  case State::closing:
51  if (Event::Open == e) {
52  exitAction_closing(); // Exit-action current state
53  entryAction_opening(); // Entry-actions next state
54  } else if (Event::Closed == e) {
55  exitAction_closing(); // Exit-action current state
56  entryAction_closed(); // Entry-actions next state
57  } else if (Event::Error == e) {
58  exitAction_closing(); // Exit-action current state
59  entryAction_errorState(); // Entry-actions next state
60  }
61  break;
62 
63  case State::closed:
64  if (Event::Open == e) {
65  exitAction_closed(); // Exit-action current state
66  entryAction_opening(); // Entry-actions next state
67  } else if (Event::Error == e) {
68  exitAction_closed(); // Exit-action current state
69  entryAction_errorState(); // Entry-actions next state
70  }
71  break;
72 
73  case State::opening:
74  if (Event::Close == e) {
75  exitAction_opening(); // Exit-action current state
76  entryAction_closing(); // Entry-actions next state
77  } else if (Event::Opened == e) {
78  exitAction_opening(); // Exit-action current state
79  entryAction_opened(); // Entry-actions next state
80  } else if (Event::Error == e) {
81  exitAction_opening(); // Exit-action current state
82  entryAction_errorState(); // Entry-actions next state
83  }
84  break;
85 
86  case State::errorState:
87  if (Event::Resume == e) {
88  exitAction_errorState(); // Exit-action current state
89  switch (lastStateBevorError) {
90  case State::opened:
91  entryAction_opened(); // Entry-actions next state
92  break;
93  case State::closing:
94  entryAction_closing(); // Entry-actions next state
95  break;
96  case State::closed:
97  entryAction_closed(); // Entry-actions next state
98  break;
99  case State::opening:
100  entryAction_opening(); // Entry-actions next state
101  break;
102  default:
103  break;
104  }
105  }
106  default:
107  break;
108  }
109 }
110 
111 //==1 opened==========================================================
113  DBSTATUSln("Door Entering State: opened");
114  currentState = State::opened; // state transition
116  //Entry-Action
117 }
118 
120  DBINFO1ln("Door State: opened");
121  //Generate the Event
122  return Event::NoEvent;
123 }
124 
126  DBSTATUSln("Door Leaving State: opened");
127 }
128 
129 //==2 closing==========================================================
131  DBSTATUSln("Door Entering State: closing");
132  currentState = State::closing; // state transition
134  //Entry-Action
135 }
136 
138  DBINFO1ln("Door State: closing");
139  //Generate Event
140  if (pDoor.close()) {
141  return Event::Closed;
142  }
143  return Event::NoEvent;
144 }
145 
147  DBSTATUSln("Door Leaving State: closing");
148 }
149 
150 //==3 closed==========================================================
152  DBSTATUSln("Door Entering State: closed");
153  currentState = State::closed; // state transition
155  //Entry-Action
156 }
157 
159  DBINFO1ln("Door State: closed");
160  //Generate the Event
161  return Event::NoEvent;
162 }
163 
165  DBSTATUSln("Door Leaving State: closed");
166 }
167 
168 //==4 opening==========================================================
170  DBSTATUSln("Door Entering State: opening");
171  currentState = State::opening; // state transition
173  //Entry-Action
174 }
175 
177  DBINFO1ln("Door State: opening");
178  //Generate the Event
179  if (pDoor.open()) {
180  return Event::Opened;
181  }
182  return Event::NoEvent;
183 }
184 
186  DBSTATUSln("Door Leaving State: opening");
187 }
188 
189 //==errorState========================================================
191  DBSTATUSln("Door Entering State: errorState");
193  currentState = State::errorState; // state transition
195  //Entry-Action
196 }
197 
199  DBINFO1ln("Door State: errorState");
200  //Generate the Event
201 
202  return Event::NoEvent;
203 }
204 
206  DBSTATUSln("Door Leaving State: errorState");
207 }
208 
209 //============================================================================
210 //==Aux-Function==============================================================
212  switch (state) {
213  case State::opened:
214  return "State::opened";
215  case State::closing:
216  return "State::closing";
217  break;
218  case State::closed:
219  return "State::closed";
220  break;
221  case State::opening:
222  return "State::opening";
223  break;
224  case State::errorState:
225  return "State::errorState";
226  break;
227  default:
228  return "ERROR: No matching state";
229  break;
230  }
231 }
232 
234  switch (event) {
235  case Event::Open:
236  return "Event::Open";
237  break;
238  case Event::Close:
239  return "Event::Close";
240  break;
241  case Event::Opened:
242  return "Event::Opened";
243  break;
244  case Event::Closed:
245  return "Event::Closed";
246  break;
247  case Event::Error:
248  return "Event::Error";
249  break;
250  case Event::Resume:
251  return "Event::Resume";
252  break;
253  case Event::NoEvent:
254  return "Event::NoEvent";
255  break;
256  default:
257  return "ERROR: No matching event";
258  break;
259  }
260 }
#define DBEVENTln(x)
State lastStateBevorError
holds the last state of the FSM so it's possible to resume after error
Definition: DoorCtrl.h:94
Event currentEvent
holds the current event of the FSM
Definition: DoorCtrl.h:96
#define DBINFO1ln(x)
void entryAction_closing()
executes the entry action of the closing state.
Definition: DoorCtrl.cpp:130
void exitAction_opening()
executes the exit action of the opening state.
Definition: DoorCtrl.cpp:185
DoorCtrl::Event doAction_opened()
executes the main action of the opened state.
Definition: DoorCtrl.cpp:119
No event generated.
void entryAction_closed()
executes the entry action of the closed state.
Definition: DoorCtrl.cpp:151
void process(Event e)
Changes the state of the FSM based on the event Here is where the magic happens :)
Definition: DoorCtrl.cpp:36
DoorCtrl()
Construct a new Door Ctrl object and initailize the currentState with low state.
Definition: DoorCtrl.cpp:17
The Door Controll class contains the FSM for the Door.
State currentState
holds the current state of the FSM
Definition: DoorCtrl.h:95
bool close()
Closes the Door one Step per Function-Call.
Definition: Door.cpp:34
DoorCtrl::Event doAction_errorState()
main action of the errorState state.
Definition: DoorCtrl.cpp:198
Ext: Start Opening.
DoorCtrl::Event doAction_opening()
executes the main action of the opening state.
Definition: DoorCtrl.cpp:176
const State getcurrentState()
Get the current State.
Definition: DoorCtrl.cpp:32
void exitAction_closed()
executes the exit action of the closed state.
Definition: DoorCtrl.cpp:164
void entryAction_errorState()
entry action of the errorState state.
Definition: DoorCtrl.cpp:190
DoorCtrl::Event doAction_closing()
executes the main action of the closing state.
Definition: DoorCtrl.cpp:137
void exitAction_opened()
executes the exit action of the opened state.
Definition: DoorCtrl.cpp:125
DoorCtrl::Event doAction_closed()
executes the main action of the closed state.
Definition: DoorCtrl.cpp:158
void entryAction_opening()
executes the entry action of the opening state.
Definition: DoorCtrl.cpp:169
Door pDoor
Door Object with ClosedPostion 0 and OpenPosition 10.
Definition: DoorCtrl.h:105
State
Enum holds all possible states for the Door.
Definition: DoorCtrl.h:47
String decodeState(State state)
Decodes the State-Enum and returns a description.
Definition: DoorCtrl.cpp:211
void exitAction_errorState()
exit action of the errorState state.
Definition: DoorCtrl.cpp:205
void loop()
Calls the do-function of the active state and hence generates Events.
Definition: DoorCtrl.cpp:21
Ext: Start Closeing.
Signal: Close position reached.
bool open()
Opens the Door one Step per Function-Call.
Definition: Door.cpp:22
#define DBFUNCCALLln(x)
#define DBSTATUSln(x)
Signal: Open position reached.
void exitAction_closing()
executes the exit action of the closing state.
Definition: DoorCtrl.cpp:146
#define DBFUNCCALL(x)
Event
Enum holds all possible events.
Definition: DoorCtrl.h:34
Event(DoorCtrl::* doActionFPtr)(void)
Functionpointer to call the current states do-function.
Definition: DoorCtrl.h:103
String decodeEvent(Event event)
Decodes the Event-Enum and returns a description.
Definition: DoorCtrl.cpp:233
Ext: Resume after Error occured.
void entryAction_opened()
executes the entry action of the opened state.
Definition: DoorCtrl.cpp:112