Simple Arduino FSM
main.cpp
Go to the documentation of this file.
1 
12 /*
13 https://en.wikipedia.org/wiki/Finite-state_machine
14 Moore machine
15  The FSM uses only entry actions, i.e., output depends only on the state.
16  The advantage of the Moore model is a simplification of the behaviour.
17 
18  Consider an elevator door:
19  The state machine recognizes two commands: "command_open" and "command_close",
20  which trigger state changes.
21  The entry action (E:) in state "Opening" starts a motor opening the door,
22  the entry action in state "Closing" starts a motor in the other direction closing the door.
23  States "Opened" and "Closed" stop the motor when fully opened or closed.
24  They signal to the outside world (e.g., to other state machines)
25  the situation: "door is open" or "door is closed".
26 */
27 
28 #include <Arduino.h>
29 
30 #include "LogConfiguration.h" //You can change the DebugLevel in LogConfiguration
31 
32 #include "DoorCtrl.h"
33 
35 
36 void setup() {
37  // put your setup code here, to run once:
38  Serial.begin(9600);
39  while (!Serial) {
40  ; // wait for serial port to connect. Needed for native USB port only
41  }
42 
43  doorctrl = new DoorCtrl();
44 }
45 
46 void loop() {
47  // put your main code here, to run repeatedly:
48 
49  //https://www.arduino.cc/en/Tutorial/SwitchCase2
50  int inByte;
51  DBSTATUSln("==Test Door CTRL==");
52  //read Serial in and generate events
53  Serial.print("Current State: ");
54  Serial.println(doorctrl->decodeState(doorctrl->getcurrentState()));
55  Serial.println("Possible Events are:");
56  Serial.println("O - Open");
57  Serial.println("A - Automatic Open");
58  Serial.println("C - Close");
59  Serial.println("a - Automatic Close");
60  Serial.println("E - Error");
61  Serial.println("R - Resume");
62  Serial.println("N - No Event");
63  Serial.println("o - Signal: Opened");
64  Serial.println("c - Signal: Closed");
65  Serial.print("Choose Event: ");
66  while (Serial.available() <= 0) {
67  }
68  inByte = Serial.read();
69  Serial.print((char)inByte);
70  Serial.println();
71  switch (inByte) {
72  case 'O':
74  break;
75  case 'A':
76  while ((doorctrl->getcurrentState() != DoorCtrl::State::opened) && (Serial.read() != 'E')) {
78  }
79  break;
80  case 'C':
82  break;
83  case 'a':
84  while ((doorctrl->getcurrentState() != DoorCtrl::State::closed) && (Serial.read() != 'E')) {
86  }
87  break;
88  case 'E':
90  break;
91  case 'R':
93  break;
94  case 'N':
96  break;
97  case 'o':
99  break;
100  case 'c':
102  break;
103  default:
104  DBINFO1ln("Error: Unknown value entered");
105  break;
106  }
107 }
#define DBINFO1ln(x)
void loop()
Definition: main.cpp:46
No event generated.
void setup()
Definition: main.cpp:36
The Door Controll class contains the FSM for the Door.
DoorCtrl * doorctrl
Definition: main.cpp:34
Ext: Start Opening.
const State getcurrentState()
Get the current State.
Definition: DoorCtrl.cpp:32
Contains the FSM for the Door.
Definition: DoorCtrl.h:27
Contains Pre-Compiler directives for diffent Serialprints for Datalogin.
String decodeState(State state)
Decodes the State-Enum and returns a description.
Definition: DoorCtrl.cpp:211
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.
#define DBSTATUSln(x)
Signal: Open position reached.
Ext: Resume after Error occured.