... | ... | @@ -2,7 +2,7 @@ A **finite state machine** is a model that can simulate some logic or computer p |
|
|
This abstraction is very useful when designing an algorithm since it can be translated to code almost for free.
|
|
|
|
|
|
## Implementation
|
|
|
In *skyward-boardcore* implementation, the **FSM** class extends the [EventHandler](EventHandler) class. This means that the main communication method use by FSMs are events: state machines can receive events, handle them and react according to the received event. If an [EventBroker](EventBroker) is used, the FSM can subscribe only to the required topics, in order to "filter" the events it will receive.
|
|
|
In _skyward-boardcore_ implementation, the **FSM** class extends the [EventHandler](EventHandler) class. This means that the main communication method use by FSMs are events: state machines can receive events, handle them and react according to the received event. If an [EventBroker](EventBroker) is used, the FSM can subscribe only to the required topics, in order to "filter" the events it will receive.
|
|
|
|
|
|
The machine states are defined by the class methods, for example:
|
|
|
```cpp
|
... | ... | @@ -25,7 +25,7 @@ void state_myState(const Event& ev) |
|
|
```
|
|
|
In the constructor we can specify the initial one.
|
|
|
The FSM maintains the current state by means of a pointer to one of the class methods.
|
|
|
Then, whenever a new event is received, *handleEvent()* dereferences the current state pointer, calling the relative method (and passing the event as parameter):
|
|
|
Then, whenever a new event is received, _handleEvent()_ dereferences the current state pointer, calling the relative method (and passing the event as parameter):
|
|
|
```cpp
|
|
|
void handleEvent(const Event& e) override
|
|
|
{
|
... | ... | @@ -49,9 +49,9 @@ Available public methods are: |
|
|
|
|
|
#### Standard Events
|
|
|
`EV_ENTRY` and `EV_EXIT` are automatically posted when entering or exiting a state.
|
|
|
In fact when the *transition()* method is called, the FSM itself handles `EV_EXIT` according to the current state, performs the state transition and then handles `EV_ENTRY` according to the new state.
|
|
|
By handling these two events, actions to be performed on entry/exit from a state can be specified.
|
|
|
|
|
|
In fact when the _transition()_ method is called, the FSM itself handles `EV_EXIT` according to the current state, performs the state transition and then handles `EV_ENTRY` according to the new state.
|
|
|
By handling these two events, actions to be performed on entry/exit from a state can be specified.
|
|
|
|
|
|
You can find the complete implementation of the FSM class in `src/shared/events/FSM.h`.
|
|
|
|
|
|
## Example 1
|
... | ... | @@ -59,8 +59,8 @@ This example implements the following very simple finite state machine behavior: |
|
|
|
|
|

|
|
|
|
|
|
No action is specified for the involved events (`EV_ENTRY`, `EV_EXIT`, `EV_1`, `EV_2`). They simply trigger the transitions.
|
|
|
|
|
|
No action is specified for the involved events (`EV_ENTRY`, `EV_EXIT`, `EV_1`, `EV_2`). They simply trigger the transitions.
|
|
|
|
|
|
[Here](https://git.skywarder.eu/scs/skyward-boardcore/-/tree/master/src/tests/catch/examples) and [here](https://git.skywarder.eu/r2a/skyward-boardcore/tree/master/src/entrypoints/examples) you can find a more advanced example which also use the [catch testing framework](Testing) to test the implemented FSM.
|
|
|
|
|
|
#### MyFSM.h
|
... | ... | @@ -170,7 +170,6 @@ private: |
|
|
};
|
|
|
```
|
|
|
|
|
|
|
|
|
#### test-fsm.cpp
|
|
|
```cpp
|
|
|
#include <Common.h>
|
... | ... | |