refactor: merged manger task into statemachine task

This commit is contained in:
CarlWachter
2024-11-03 13:13:58 +01:00
parent b39a2a9027
commit 7152baca2a
10 changed files with 137 additions and 266 deletions

View File

@@ -3,7 +3,7 @@
#include <sta/tacos/can_bus.hpp>
#include <sta/debug/assert.hpp>
#include <sta/tacos/manager.hpp>
#include <sta/tacos/statemachine.hpp>
#include <sta/tacos.hpp>
extern CAN_HandleTypeDef STA_STM32_CAN_HANDLE;
@@ -64,7 +64,7 @@ namespace sta
{
// Append to the correct thread's queue
for (std::shared_ptr<TacosThread> thread : Manager::instance()->getActiveThreads())
for (std::shared_ptr<TacosThread> thread : Statemachine::instance()->getActiveThreads())
{
if (thread->getCanID() == sysMsg.header.sid)
{

View File

@@ -1,110 +0,0 @@
/*
* manager.cpp
*
* Created on: Sep 19, 2023
* Author: Dario
*/
#include <sta/tacos/manager.hpp>
#include <sta/tacos/statemachine.hpp>
#include <sta/debug/debug.hpp>
#include <algorithm>
#include <FreeRTOS.h>
#include <sta/tacos.hpp>
namespace sta
{
namespace tacos
{
void Manager::registerThread(std::shared_ptr<TacosThread> thread, std::set<uint16_t> states)
{
for (uint16_t state : states)
{
STA_ASSERT(state < STA_TACOS_NUM_STATES);
threads_[state].push_back(thread);
}
}
std::vector<std::shared_ptr<TacosThread>> Manager::getActiveThreads()
{
return threads_[tacos::getState()];
}
void Manager::startThreads(uint16_t state)
{
STA_ASSERT(state < STA_TACOS_NUM_STATES);
for (std::shared_ptr<TacosThread> thread : threads_[state])
{
if (!thread->isRunning())
{
thread->start();
}
}
}
void Manager::stopThreads(uint16_t state)
{
std::set<std::shared_ptr<TacosThread>> terminated;
for (uint16_t other = 0; other < STA_TACOS_NUM_STATES; ++other)
{
if (other == state)
{
continue;
}
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() && terminated.count(thread) == 0 && std::count(threads_[state].begin(), threads_[state].end(), thread) == 0)
{
// ...politely request termination.
thread->requestTermination();
terminated.emplace(thread);
}
}
}
}
void Manager::updateThreads()
{
uint16_t state = Statemachine::instance()->getCurrentState();
stopThreads(state);
startThreads(state);
}
void Manager::init()
{
startThreads(Statemachine::instance()->getCurrentState());
}
void Manager::func()
{
Statemachine::stateChangeEvent.wait(EventFlags::ALL, osWaitForever);
HeapStats_t stats;
vPortGetHeapStats(&stats);
// Start all new tasks and stop all the tasks that aren't supposed to be running.
updateThreads();
}
Manager::Manager()
: TacosThread{"Manager", STA_TACOS_MANAGER_PRIORITY, STA_TACOS_MANAGER_STACK_SIZE},
threads_{}
{
}
Manager* Manager::_instance = nullptr;
} // namespace tacos
} // namespace sta

View File

@@ -33,7 +33,6 @@
// Tacos-specific includes.
#include <sta/tacos/c_api/startup.h>
#include <sta/tacos/manager.hpp>
#include <sta/tacos/statemachine.hpp>
#include <sta/tacos/watchdog.hpp>
#include <sta/tacos/can_bus.hpp>
@@ -101,23 +100,6 @@ namespace sta
Statemachine::instance()->start();
}
/**
* @brief Function that is called before the manager task is started. Override it to adjust
* the manager to your specifications.
*
* @ingroup tacos_startup
*/
STA_WEAK
void onManagerInit()
{}
void initManager()
{
onManagerInit();
Manager::instance()->start();
}
#ifdef STA_TACOS_WATCHDOG_ENABLED
STA_WEAK
void onWatchdogInit()
@@ -158,8 +140,6 @@ namespace sta
tacos::initStatemachine();
tacos::initManager();
#ifdef STA_TACOS_WATCHDOG_ENABLED
tacos::initWatchdog();
#endif // STA_TACOS_WATCHDOG_ENABLED

View File

@@ -9,6 +9,7 @@
#include <sta/tacos/statemachine.hpp>
#include <sta/debug/debug.hpp>
#include <FreeRTOS.h>
namespace sta
@@ -20,14 +21,15 @@ namespace sta
currentState_{STA_TACOS_INITIAL_STATE},
lockoutTimer_{[](void *){}, nullptr},
failsafeTimer_{[](void *){}, nullptr},
queue_{STA_TACOS_STATEMACHINE_QUEUE_LENGTH}
queue_{STA_TACOS_STATEMACHINE_QUEUE_LENGTH},
threads_{}
{
STA_ASSERT(STA_TACOS_INITIAL_STATE < STA_TACOS_NUM_STATES);
}
void Statemachine::init()
{
startThreads(getCurrentState());
}
void Statemachine::func()
@@ -64,6 +66,13 @@ namespace sta
{
setLockoutTimer(transition.lockout);
}
// get heap stats at the end of the state transition
HeapStats_t stats;
vPortGetHeapStats(&stats);
// Start all new tasks and stop all the tasks that aren't supposed to be running.
updateThreads();
}
}
@@ -130,6 +139,66 @@ namespace sta
lockoutTimer_.start(millis);
}
void Statemachine::registerThread(std::shared_ptr<TacosThread> thread, std::set<uint16_t> states)
{
for (uint16_t state : states)
{
STA_ASSERT(state < STA_TACOS_NUM_STATES);
threads_[state].push_back(thread);
}
}
std::vector<std::shared_ptr<TacosThread>> Statemachine::getActiveThreads()
{
return threads_[tacos::getState()];
}
void Statemachine::startThreads(uint16_t state)
{
STA_ASSERT(state < STA_TACOS_NUM_STATES);
for (std::shared_ptr<TacosThread> thread : threads_[state])
{
if (!thread->isRunning())
{
thread->start();
}
}
}
void Statemachine::stopThreads(uint16_t state)
{
std::set<std::shared_ptr<TacosThread>> terminated;
for (uint16_t other = 0; other < STA_TACOS_NUM_STATES; ++other)
{
if (other == state)
{
continue;
}
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() && terminated.count(thread) == 0 && std::count(threads_[state].begin(), threads_[state].end(), thread) == 0)
{
// ...politely request termination.
thread->requestTermination();
terminated.emplace(thread);
}
}
}
}
void Statemachine::updateThreads()
{
uint16_t state = getCurrentState();
stopThreads(state);
startThreads(state);
}
Statemachine* Statemachine::_instance = nullptr;
RtosEvent Statemachine::stateChangeEvent;

View File

@@ -2,7 +2,7 @@
#ifdef STA_TACOS_WATCHDOG_ENABLED
#include <sta/tacos/manager.hpp>
#include <sta/tacos/statemachine.hpp>
namespace sta
{
@@ -10,7 +10,7 @@ namespace sta
{
void Watchdog::func()
{
for (std::shared_ptr<TacosThread> thread : Manager::instance()->getActiveThreads())
for (std::shared_ptr<TacosThread> thread : Statemachine::instance()->getActiveThreads())
{
switch (thread->getStatus())
{