Added changes for potential statemachine rework

This commit is contained in:
dario
2023-11-07 12:37:53 +01:00
committed by carlwachter
parent 2205ff7446
commit 098ef140a6
5 changed files with 82 additions and 118 deletions

View File

@@ -20,6 +20,8 @@
# define STA_TACOS_INITIAL_STATE 0
#endif
#define STA_TACOS_STATEMACHINE_QUEUE_LENGTH 4
// State transition happened because of
#define STA_TACOS_STATE_CHANGE_NORMAL_FLAG ( 0x1U )
#define STA_TACOS_STATE_CHANGE_FORCED_FLAG ( 0x1U << 1)
@@ -29,34 +31,41 @@
#include <sta/tacos/thread.hpp>
#include <sta/rtos/queue.hpp>
#include <sta/rtos/timer.hpp>
#include <sta/rtos/event.hpp>
#include <sta/event.hpp>
#include <sta/debug/assert.hpp>
#include <functional>
#include <tuple>
namespace sta
{
namespace tacos
{
typedef std::function<uint16_t(uint16_t)> state_fn;
typedef std::function<void(uint16_t, uint16_t, uint32_t)> timer_fn;
enum EventFlags
{
NORMAL = 0x1U,
FORCED = 0x1U << 1,
TIMEOUT = 0x1U << 2,
STARTUP = 0x1U << 3,
ALL = NORMAL | FORCED | TIMEOUT | STARTUP
};
struct StateTransition
{
uint16_t from;
uint16_t to;
EventFlags event;
uint32_t lockout;
};
class Statemachine : public TacosThread
{
public:
enum EventFlags
{
NORMAL = 0x1U,
FORCED = 0x1U << 1,
TIMEOUT = 0x1U << 2,
STARTUP = 0x1U << 3,
ALL = NORMAL | FORCED | TIMEOUT | STARTUP
};
/**
* @brief The global event signaling a state change.
*/
@@ -84,29 +93,23 @@ namespace sta
uint16_t getCurrentState() const;
/**
* @brief Registers a new state transition function. This is a function for the user to implement state transition rules.
* @brief Request a state transition from a state to another.
*
* @param from The state which we want to leave. This is used to filter out obsolete transitions.
* @param to The state to transition to.
* @param lockout The minimum number of milliseconds we expect to stay in this state. This is used to block premature transitions.
*/
void setStateFunction(state_fn func);
void requestStateTransition(uint32_t from, uint32_t to, uint32_t lockout = 0);
/**
* @brief Registers a new timer function. This is a function for the user to set the timers after a state change.
* @brief Request a state transition after a given time has passed.
*
* @param from The state which we want to leave. This is used to filter out obsolete transitions.
* @param to The state to transition to.
* @param millis the number of milliseconds to wait before triggering the transition.
* @param lockout The minimum number of milliseconds we expect to stay in this state. This is used to block premature transitions.
*/
void setTimerFunction(timer_fn func);
/**
* @brief Starts the failsafe timer for the desired duration and the desired next state.
*/
void setFailsafeTimer(uint32_t millis, uint16_t nextState);
/**
* @brief Starts the lockoutTimer for the desired duration.
*/
void setLockoutTimer(uint32_t millis);
/**
* @brief this function call allows forced state transitions from external and internal sources.
*/
void forceStateTransition(uint16_t state);
void requestTimedStateTransition(uint32_t from, uint32_t to, uint32_t millis, uint32_t lockout = 0);
void init() override;
void func() override;
@@ -133,15 +136,19 @@ namespace sta
~Statemachine() {}
private:
/**
* @brief Starts the lockoutTimer for the desired duration.
*/
void setLockoutTimer(uint32_t millis);
private:
uint16_t currentState_;
uint16_t failsafeState_;
RtosTimer lockoutTimer_;
RtosTimer failsafeTimer_;
state_fn transitionFunc_;
timer_fn timerFunc_;
RtosQueue<StateTransition> queue_;
};
} // namespace tacos
} // namespace sta