mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/TACOS.git
synced 2025-06-12 01:25:59 +00:00
152 lines
3.2 KiB
C++
152 lines
3.2 KiB
C++
/*
|
|
* statemachine.hpp
|
|
*
|
|
* Created on: Sep 14, 2023
|
|
* Author: Dario
|
|
*/
|
|
|
|
#ifndef INCLUDE_TACOS_STATEMACHINE_HPP_
|
|
#define INCLUDE_TACOS_STATEMACHINE_HPP_
|
|
|
|
#include <sta/config.hpp>
|
|
|
|
|
|
#ifndef STA_TACOS_NUM_STATES
|
|
# error "Number of states wasn't defined in config.hpp"
|
|
#else
|
|
|
|
|
|
#ifndef STA_TACOS_INITIAL_STATE
|
|
# define STA_TACOS_INITIAL_STATE 0
|
|
#endif
|
|
|
|
// State transition happened because of
|
|
#define STA_TACOS_STATE_CHANGE_NORMAL_FLAG ( 0x1U )
|
|
#define STA_TACOS_STATE_CHANGE_FORCED_FLAG ( 0x1U << 1)
|
|
#define STA_TACOS_STATE_CHANGE_TIMEOUT ( 0x1U << 2)
|
|
#define STA_TACOS_STATE_CHANGE_STARTUP_FLAG ( 0x1U << 3)
|
|
#define STA_TACOS_STATE_CHANGE_ALL_FLAG ( 0x15U )
|
|
|
|
|
|
#include <sta/tacos/thread.hpp>
|
|
#include <sta/rtos/timer.hpp>
|
|
#include <sta/rtos/event.hpp>
|
|
#include <sta/event.hpp>
|
|
#include <sta/debug/assert.hpp>
|
|
|
|
#include <functional>
|
|
|
|
|
|
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;
|
|
|
|
|
|
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.
|
|
*/
|
|
static RtosEvent stateChangeEvent;
|
|
|
|
/**
|
|
* @brief Getter function for the singleton instance.
|
|
*/
|
|
static Statemachine* instance()
|
|
{
|
|
static CGuard g;
|
|
|
|
if (!_instance)
|
|
{
|
|
// Create the manager singleton instance.
|
|
Statemachine::_instance = new Statemachine();
|
|
}
|
|
|
|
return _instance;
|
|
}
|
|
|
|
/**
|
|
* @brief Returns the statemachine's current state.
|
|
*/
|
|
uint16_t getCurrentState() const;
|
|
|
|
/**
|
|
* @brief Registers a new state transition function. This is a function for the user to implement state transition rules.
|
|
*/
|
|
void setStateFunction(state_fn func);
|
|
|
|
/**
|
|
* @brief Registers a new timer function. This is a function for the user to set the timers after a state change.
|
|
*/
|
|
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 init() override;
|
|
void func() override;
|
|
|
|
private:
|
|
static Statemachine * _instance;
|
|
|
|
class CGuard
|
|
{
|
|
public:
|
|
~CGuard()
|
|
{
|
|
if( NULL != Statemachine::_instance )
|
|
{
|
|
delete Statemachine::_instance;
|
|
Statemachine::_instance = NULL;
|
|
}
|
|
}
|
|
};
|
|
|
|
Statemachine();
|
|
|
|
Statemachine(const Statemachine&);
|
|
|
|
~Statemachine() {}
|
|
|
|
private:
|
|
uint16_t currentState_;
|
|
uint16_t failsafeState_;
|
|
|
|
RtosTimer lockoutTimer_;
|
|
RtosTimer failsafeTimer_;
|
|
|
|
state_fn transitionFunc_;
|
|
timer_fn timerFunc_;
|
|
};
|
|
} // namespace tacos
|
|
} // namespace sta
|
|
|
|
#endif // STA_TACOS_NUM_STATES
|
|
|
|
#endif /* INCLUDE_TACOS_STATEMACHINE_HPP_ */
|