mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/TACOS.git
synced 2025-09-29 02:57:33 +00:00
Multiple bugfixes; working manager task
This commit is contained in:
@@ -9,6 +9,8 @@
|
||||
#include <sta/tacos/statemachine.hpp>
|
||||
#include <sta/debug/debug.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
@@ -39,22 +41,23 @@ namespace sta
|
||||
|
||||
void Manager::stopThreads(uint16_t state)
|
||||
{
|
||||
uint16_t currentState = Statemachine::instance()->getCurrentState();
|
||||
|
||||
for (uint16_t state = 0; state < STA_TACOS_NUM_STATES; ++state)
|
||||
for (uint16_t other = 0; other < STA_TACOS_NUM_STATES; ++other)
|
||||
{
|
||||
if (state == currentState)
|
||||
if (other == state)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
for (std::shared_ptr<TacosThread> thread : threads_[state])
|
||||
for (std::shared_ptr<TacosThread> thread : threads_[other])
|
||||
{
|
||||
// If the thread is currently running but not part of the set of threads that should be running...
|
||||
if (thread->isRunning() && threads_[currentState].count(thread) == 0)
|
||||
if (thread->isRunning() && threads_[state].count(thread) == 0)
|
||||
{
|
||||
// ...politely request termination.
|
||||
thread->requestTermination();
|
||||
|
||||
STA_DEBUG_PRINT("KILLING ");
|
||||
STA_DEBUG_PRINTLN(thread->getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,14 +78,9 @@ namespace sta
|
||||
|
||||
void Manager::func()
|
||||
{
|
||||
// Wait for either the termination request or the state change flag.
|
||||
uint32_t flags = osEventFlagsWait(getInstance(), STA_RTOS_THREAD_FLAG_TERMINATE, osFlagsWaitAny, osWaitForever);
|
||||
Statemachine::stateChangeEvent->wait(TACOS_STATE_CHANGE_ALL_FLAG, osWaitForever);
|
||||
|
||||
if ((flags & STA_RTOS_THREAD_FLAG_TERMINATE) != 0)
|
||||
{
|
||||
// The loop implemented by the TacosThread class should handle termination.
|
||||
return;
|
||||
}
|
||||
STA_DEBUG_PRINTLN("UPDATING THREADS");
|
||||
|
||||
// Start all new tasks and stop all the tasks that aren't supposed to be running.
|
||||
updateThreads();
|
||||
|
@@ -18,7 +18,8 @@ namespace sta
|
||||
: TacosThread{"Statemachine", STA_TACOS_STATEMACHINE_PRIORITY},
|
||||
currentState_{STA_TACOS_INITIAL_STATE},
|
||||
lockoutTimer_{[](void *){}, nullptr},
|
||||
failsafeTimer_{[](void *){}, nullptr}
|
||||
failsafeTimer_{[](void *){}, nullptr},
|
||||
transitionFunc_{[](uint16_t) -> uint16_t { return Statemachine::instance()->getCurrentState(); }}
|
||||
{
|
||||
STA_ASSERT(STA_TACOS_INITIAL_STATE < STA_TACOS_NUM_STATES);
|
||||
}
|
||||
@@ -28,15 +29,14 @@ namespace sta
|
||||
lockoutTimer_.setCallback([](void *) { }, nullptr);
|
||||
lockoutTimer_.start(5000);
|
||||
|
||||
STA_DEBUG_PRINTLN("INITIALIZING STATEMACHINE");
|
||||
|
||||
failsafeTimer_.setCallback([](void * arg) { }, nullptr);
|
||||
failsafeTimer_.setCallback([](void *) {
|
||||
Statemachine::instance()->forceStateTransition(1);
|
||||
}, nullptr);
|
||||
failsafeTimer_.start(10000);
|
||||
}
|
||||
|
||||
void Statemachine::func()
|
||||
{
|
||||
/*
|
||||
uint16_t next = transitionFunc_(currentState_);
|
||||
|
||||
STA_ASSERT(next < STA_TACOS_NUM_STATES);
|
||||
@@ -45,10 +45,10 @@ namespace sta
|
||||
{
|
||||
currentState_ = next;
|
||||
|
||||
// TODO: Emit state transition event.
|
||||
}*/
|
||||
STA_DEBUG_PRINTLN(lockoutTimer_.isRunning() ? "[RUNNING]" : "[STOPPED]");
|
||||
osDelay(1000);
|
||||
Statemachine::stateChangeEvent->set(TACOS_STATE_TRANSITION_FLAG);
|
||||
}
|
||||
|
||||
Statemachine::stateChangeEvent->clear(TACOS_STATE_CHANGE_ALL_FLAG);
|
||||
}
|
||||
|
||||
uint16_t Statemachine::getCurrentState() const
|
||||
@@ -56,14 +56,23 @@ namespace sta
|
||||
return currentState_;
|
||||
}
|
||||
|
||||
void Statemachine::setStateTransitionFunction(uint16_t (*function)(uint16_t))
|
||||
void Statemachine::setStateTransitionFunction(std::function<uint16_t(uint16_t)> function)
|
||||
{
|
||||
STA_ASSERT(function != nullptr);
|
||||
|
||||
transitionFunc_ = function;
|
||||
}
|
||||
|
||||
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* Statemachine::_instance = nullptr;
|
||||
RtosEvent* Statemachine::stateChangeEvent = nullptr;
|
||||
|
||||
} // namespace tacos
|
||||
} // namespace sta
|
||||
|
@@ -9,6 +9,7 @@
|
||||
#include <sta/tacos/thread.hpp>
|
||||
#include <sta/debug/assert.hpp>
|
||||
#include <sta/debug/debug.hpp>
|
||||
#include <sta/rtos/system/events.hpp>
|
||||
|
||||
#include <functional>
|
||||
#include <cstring>
|
||||
@@ -66,9 +67,13 @@ namespace sta
|
||||
|
||||
void TacosThread::loop()
|
||||
{
|
||||
// The thread has to wait until the startup has been completed.
|
||||
rtos::waitForStartupEvent();
|
||||
|
||||
init();
|
||||
|
||||
while ((osEventFlagsGet(instance_) & STA_RTOS_THREAD_FLAG_TERMINATE) == 0)
|
||||
// Run the thread until the termination flag is set.
|
||||
while ((getFlags() & STA_RTOS_THREAD_FLAG_TERMINATE) == 0)
|
||||
{
|
||||
func();
|
||||
}
|
||||
|
Reference in New Issue
Block a user