mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/TACOS.git
synced 2025-06-12 17:45:58 +00:00
Compilable but untested statemachine implementation including a lot of clean up.
This commit is contained in:
parent
67f3e9aa15
commit
4efc0382f8
@ -10,6 +10,7 @@
|
|||||||
#include <sta/debug/debug.hpp>
|
#include <sta/debug/debug.hpp>
|
||||||
|
|
||||||
#include <sta/tacos/manager.hpp>
|
#include <sta/tacos/manager.hpp>
|
||||||
|
#include <sta/tacos/statemachine.hpp>
|
||||||
#include <tasks/dummy.hpp>
|
#include <tasks/dummy.hpp>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
@ -31,7 +31,6 @@ namespace demo
|
|||||||
STA_DEBUG_PRINTLN(this->getName());
|
STA_DEBUG_PRINTLN(this->getName());
|
||||||
// STA_DEBUG_PRINT("Executing ");
|
// STA_DEBUG_PRINT("Executing ");
|
||||||
// STA_DEBUG_PRINTLN(this->getName());
|
// STA_DEBUG_PRINTLN(this->getName());
|
||||||
osDelay(1000);
|
|
||||||
}
|
}
|
||||||
} // namespace demo
|
} // namespace demo
|
||||||
|
|
||||||
|
@ -20,14 +20,18 @@
|
|||||||
# define STA_TACOS_INITIAL_STATE 0
|
# define STA_TACOS_INITIAL_STATE 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define TACOS_STATE_TRANSITION_FLAG 0x00000001U
|
// State transition happened because of
|
||||||
#define TACOS_STATE_CHANGE_FORCED_FLAG 0x00000002U
|
#define STA_TACOS_STATE_CHANGE_NORMAL_FLAG ( 0x1U )
|
||||||
#define TACOS_STATE_CHANGE_ALL_FLAG 0x00000003U
|
#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/tacos/thread.hpp>
|
||||||
#include <sta/rtos/timer.hpp>
|
#include <sta/rtos/timer.hpp>
|
||||||
#include <sta/rtos/event.hpp>
|
#include <sta/rtos/event.hpp>
|
||||||
|
#include <sta/event.hpp>
|
||||||
#include <sta/debug/assert.hpp>
|
#include <sta/debug/assert.hpp>
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
@ -37,40 +41,76 @@ namespace sta
|
|||||||
{
|
{
|
||||||
namespace tacos
|
namespace tacos
|
||||||
{
|
{
|
||||||
|
typedef std::function<uint16_t(uint16_t)> state_fn;
|
||||||
|
typedef std::function<void(uint16_t, uint16_t, uint16_t)> timer_fn;
|
||||||
|
|
||||||
|
|
||||||
class Statemachine : public TacosThread
|
class Statemachine : public TacosThread
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static RtosEvent* stateChangeEvent;
|
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 Statemachine* instance()
|
||||||
{
|
{
|
||||||
static CGuard g;
|
static CGuard g;
|
||||||
|
|
||||||
if (!_instance)
|
if (!_instance)
|
||||||
{
|
{
|
||||||
// Create a the manager singleton instance.
|
// Create the manager singleton instance.
|
||||||
Statemachine::_instance = new Statemachine();
|
Statemachine::_instance = new Statemachine();
|
||||||
Statemachine::stateChangeEvent = new RtosEvent();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return _instance;
|
return _instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
void init() override;
|
|
||||||
|
|
||||||
void func() override;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns the statemachine's current state.
|
* @brief Returns the statemachine's current state.
|
||||||
*/
|
*/
|
||||||
uint16_t getCurrentState() const;
|
uint16_t getCurrentState() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Registers a new state transition function.
|
* @brief Registers a new state transition function. This is a function for the user to implement state transition rules.
|
||||||
*/
|
*/
|
||||||
void setStateTransitionFunction(std::function<uint16_t(uint16_t)> function);
|
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 forceStateTransition(uint16_t state);
|
||||||
|
|
||||||
|
void init() override;
|
||||||
|
void func() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Statemachine * _instance;
|
static Statemachine * _instance;
|
||||||
|
|
||||||
@ -82,9 +122,7 @@ namespace sta
|
|||||||
if( NULL != Statemachine::_instance )
|
if( NULL != Statemachine::_instance )
|
||||||
{
|
{
|
||||||
delete Statemachine::_instance;
|
delete Statemachine::_instance;
|
||||||
delete Statemachine::stateChangeEvent;
|
|
||||||
Statemachine::_instance = NULL;
|
Statemachine::_instance = NULL;
|
||||||
Statemachine::stateChangeEvent = NULL;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -94,11 +132,15 @@ namespace sta
|
|||||||
Statemachine(const Statemachine&);
|
Statemachine(const Statemachine&);
|
||||||
|
|
||||||
~Statemachine() {}
|
~Statemachine() {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint16_t currentState_;
|
uint16_t currentState_;
|
||||||
|
|
||||||
RtosTimer lockoutTimer_;
|
RtosTimer lockoutTimer_;
|
||||||
RtosTimer failsafeTimer_;
|
RtosTimer failsafeTimer_;
|
||||||
std::function<uint16_t(uint16_t)> transitionFunc_;
|
|
||||||
|
state_fn transitionFunc_;
|
||||||
|
timer_fn timerFunc_;
|
||||||
};
|
};
|
||||||
} // namespace tacos
|
} // namespace tacos
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
@ -78,7 +78,7 @@ namespace sta
|
|||||||
|
|
||||||
void Manager::func()
|
void Manager::func()
|
||||||
{
|
{
|
||||||
Statemachine::stateChangeEvent->wait(TACOS_STATE_CHANGE_ALL_FLAG, osWaitForever);
|
Statemachine::stateChangeEvent.wait(Statemachine::EventFlags::ALL, osWaitForever);
|
||||||
|
|
||||||
STA_DEBUG_PRINTLN("UPDATING THREADS");
|
STA_DEBUG_PRINTLN("UPDATING THREADS");
|
||||||
|
|
||||||
|
@ -19,20 +19,15 @@ namespace sta
|
|||||||
currentState_{STA_TACOS_INITIAL_STATE},
|
currentState_{STA_TACOS_INITIAL_STATE},
|
||||||
lockoutTimer_{[](void *){}, nullptr},
|
lockoutTimer_{[](void *){}, nullptr},
|
||||||
failsafeTimer_{[](void *){}, nullptr},
|
failsafeTimer_{[](void *){}, nullptr},
|
||||||
transitionFunc_{[](uint16_t) -> uint16_t { return Statemachine::instance()->getCurrentState(); }}
|
transitionFunc_{[](uint16_t) -> uint16_t { return Statemachine::instance()->getCurrentState(); }},
|
||||||
|
timerFunc_{[](uint16_t, uint16_t, uint16_t) {}}
|
||||||
{
|
{
|
||||||
STA_ASSERT(STA_TACOS_INITIAL_STATE < STA_TACOS_NUM_STATES);
|
STA_ASSERT(STA_TACOS_INITIAL_STATE < STA_TACOS_NUM_STATES);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Statemachine::init()
|
void Statemachine::init()
|
||||||
{
|
{
|
||||||
lockoutTimer_.setCallback([](void *) { }, nullptr);
|
timerFunc_(0, 0, EventFlags::STARTUP);
|
||||||
lockoutTimer_.start(5000);
|
|
||||||
|
|
||||||
failsafeTimer_.setCallback([](void *) {
|
|
||||||
Statemachine::instance()->forceStateTransition(1);
|
|
||||||
}, nullptr);
|
|
||||||
failsafeTimer_.start(10000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Statemachine::func()
|
void Statemachine::func()
|
||||||
@ -41,14 +36,18 @@ namespace sta
|
|||||||
|
|
||||||
STA_ASSERT(next < STA_TACOS_NUM_STATES);
|
STA_ASSERT(next < STA_TACOS_NUM_STATES);
|
||||||
|
|
||||||
if (next != currentState_)
|
// Check if a state change is desired. Block if the lockoutTimer is still running.
|
||||||
|
if (next != currentState_ && !lockoutTimer_.isRunning())
|
||||||
{
|
{
|
||||||
currentState_ = next;
|
// Call user space code to set the timers again.
|
||||||
|
timerFunc_(next, currentState_, EventFlags::NORMAL);
|
||||||
|
|
||||||
Statemachine::stateChangeEvent->set(TACOS_STATE_TRANSITION_FLAG);
|
// Update the state and trigger the global state changed event.
|
||||||
|
currentState_ = next;
|
||||||
|
Statemachine::stateChangeEvent.set(EventFlags::NORMAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
Statemachine::stateChangeEvent->clear(TACOS_STATE_CHANGE_ALL_FLAG);
|
Statemachine::stateChangeEvent.clear(EventFlags::ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Statemachine::getCurrentState() const
|
uint16_t Statemachine::getCurrentState() const
|
||||||
@ -56,24 +55,51 @@ namespace sta
|
|||||||
return currentState_;
|
return currentState_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Statemachine::setStateTransitionFunction(std::function<uint16_t(uint16_t)> function)
|
void Statemachine::setStateFunction(state_fn func)
|
||||||
{
|
{
|
||||||
STA_ASSERT(function != nullptr);
|
STA_ASSERT(func != nullptr);
|
||||||
|
|
||||||
transitionFunc_ = function;
|
transitionFunc_ = func;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Statemachine::setTimerFunction(timer_fn func)
|
||||||
|
{
|
||||||
|
STA_ASSERT(func != nullptr);
|
||||||
|
|
||||||
|
timerFunc_ = func;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Statemachine::setFailsafeTimer(uint32_t millis, uint16_t nextState)
|
||||||
|
{
|
||||||
|
STA_ASSERT(nextState < STA_TACOS_NUM_STATES);
|
||||||
|
STA_ASSERT(!failsafeTimer_.isRunning());
|
||||||
|
|
||||||
|
failsafeTimer_.setCallback([nextState](void *) {
|
||||||
|
Statemachine::instance()->forceStateTransition(nextState);
|
||||||
|
}, nullptr);
|
||||||
|
|
||||||
|
failsafeTimer_.start(millis);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Statemachine::setLockoutTimer(uint32_t millis)
|
||||||
|
{
|
||||||
|
STA_ASSERT(!failsafeTimer_.isRunning());
|
||||||
|
|
||||||
|
lockoutTimer_.setCallback([](void *) { }, nullptr);
|
||||||
|
lockoutTimer_.start(millis);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Statemachine::forceStateTransition(uint16_t state)
|
void Statemachine::forceStateTransition(uint16_t state)
|
||||||
{
|
{
|
||||||
currentState_ = state;
|
currentState_ = state;
|
||||||
|
|
||||||
Statemachine::stateChangeEvent->set(TACOS_STATE_CHANGE_FORCED_FLAG);
|
Statemachine::stateChangeEvent.set(EventFlags::FORCED);
|
||||||
Statemachine::stateChangeEvent->clear(TACOS_STATE_CHANGE_ALL_FLAG);
|
Statemachine::stateChangeEvent.clear(EventFlags::ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
Statemachine* Statemachine::_instance = nullptr;
|
Statemachine* Statemachine::_instance = nullptr;
|
||||||
RtosEvent* Statemachine::stateChangeEvent = nullptr;
|
|
||||||
|
|
||||||
|
RtosEvent Statemachine::stateChangeEvent;
|
||||||
} // namespace tacos
|
} // namespace tacos
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
|
||||||
|
@ -46,6 +46,8 @@ namespace sta
|
|||||||
instance_ = osThreadNew(entry_point, this, &attribs_);
|
instance_ = osThreadNew(entry_point, this, &attribs_);
|
||||||
|
|
||||||
STA_ASSERT(instance_ != NULL);
|
STA_ASSERT(instance_ != NULL);
|
||||||
|
|
||||||
|
// SetFlag(THREAD_START);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TacosThread::isRunning()
|
bool TacosThread::isRunning()
|
||||||
@ -70,6 +72,8 @@ namespace sta
|
|||||||
// The thread has to wait until the startup has been completed.
|
// The thread has to wait until the startup has been completed.
|
||||||
rtos::waitForStartupEvent();
|
rtos::waitForStartupEvent();
|
||||||
|
|
||||||
|
// wait(THREAD_START)
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
||||||
// Run the thread until the termination flag is set.
|
// Run the thread until the termination flag is set.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user