Compilable but untested statemachine implementation including a lot of clean up.

This commit is contained in:
dario
2023-10-23 23:50:08 +02:00
parent 67f3e9aa15
commit 4efc0382f8
6 changed files with 107 additions and 35 deletions

View File

@@ -78,7 +78,7 @@ namespace sta
void Manager::func()
{
Statemachine::stateChangeEvent->wait(TACOS_STATE_CHANGE_ALL_FLAG, osWaitForever);
Statemachine::stateChangeEvent.wait(Statemachine::EventFlags::ALL, osWaitForever);
STA_DEBUG_PRINTLN("UPDATING THREADS");

View File

@@ -19,20 +19,15 @@ namespace sta
currentState_{STA_TACOS_INITIAL_STATE},
lockoutTimer_{[](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);
}
void Statemachine::init()
{
lockoutTimer_.setCallback([](void *) { }, nullptr);
lockoutTimer_.start(5000);
failsafeTimer_.setCallback([](void *) {
Statemachine::instance()->forceStateTransition(1);
}, nullptr);
failsafeTimer_.start(10000);
timerFunc_(0, 0, EventFlags::STARTUP);
}
void Statemachine::func()
@@ -41,14 +36,18 @@ namespace sta
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
@@ -56,24 +55,51 @@ namespace sta
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)
{
currentState_ = state;
Statemachine::stateChangeEvent->set(TACOS_STATE_CHANGE_FORCED_FLAG);
Statemachine::stateChangeEvent->clear(TACOS_STATE_CHANGE_ALL_FLAG);
Statemachine::stateChangeEvent.set(EventFlags::FORCED);
Statemachine::stateChangeEvent.clear(EventFlags::ALL);
}
Statemachine* Statemachine::_instance = nullptr;
RtosEvent* Statemachine::stateChangeEvent = nullptr;
RtosEvent Statemachine::stateChangeEvent;
} // namespace tacos
} // namespace sta

View File

@@ -46,6 +46,8 @@ namespace sta
instance_ = osThreadNew(entry_point, this, &attribs_);
STA_ASSERT(instance_ != NULL);
// SetFlag(THREAD_START);
}
bool TacosThread::isRunning()
@@ -70,6 +72,8 @@ namespace sta
// The thread has to wait until the startup has been completed.
rtos::waitForStartupEvent();
// wait(THREAD_START)
init();
// Run the thread until the termination flag is set.