mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/TACOS.git
synced 2025-06-10 16:45:59 +00:00
refactor: merged manger task into statemachine task
This commit is contained in:
parent
f0c5faf29e
commit
53ad885099
@ -9,7 +9,6 @@
|
|||||||
#define STA_TACOS_HPP
|
#define STA_TACOS_HPP
|
||||||
|
|
||||||
#include <sta/tacos/thread.hpp>
|
#include <sta/tacos/thread.hpp>
|
||||||
#include <sta/tacos/manager.hpp>
|
|
||||||
#include <sta/tacos/statemachine.hpp>
|
#include <sta/tacos/statemachine.hpp>
|
||||||
#include <sta/tacos/can_bus.hpp>
|
#include <sta/tacos/can_bus.hpp>
|
||||||
|
|
||||||
@ -74,7 +73,7 @@ namespace sta
|
|||||||
{
|
{
|
||||||
std::shared_ptr<T> thread_ptr = std::make_shared<T>(args...);
|
std::shared_ptr<T> thread_ptr = std::make_shared<T>(args...);
|
||||||
|
|
||||||
Manager::instance()->registerThread(thread_ptr, states);
|
Statemachine::instance()->registerThread(thread_ptr, states);
|
||||||
|
|
||||||
return thread_ptr;
|
return thread_ptr;
|
||||||
}
|
}
|
||||||
|
@ -2,13 +2,11 @@
|
|||||||
#define STA_TACOS_CONFIGS_DEFAULT_HPP
|
#define STA_TACOS_CONFIGS_DEFAULT_HPP
|
||||||
|
|
||||||
// Generally, we assume the TACOS threads to have the highest priorties.
|
// Generally, we assume the TACOS threads to have the highest priorties.
|
||||||
#define STA_TACOS_MANAGER_PRIORITY osPriorityHigh
|
|
||||||
#define STA_TACOS_STATEMACHINE_PRIORITY osPriorityHigh
|
#define STA_TACOS_STATEMACHINE_PRIORITY osPriorityHigh
|
||||||
#define STA_TACOS_WATCHDOG_PRIORITY osPriorityHigh
|
#define STA_TACOS_WATCHDOG_PRIORITY osPriorityHigh
|
||||||
#define STA_TACOS_CAN_BUS_PRIORITY osPriorityHigh
|
#define STA_TACOS_CAN_BUS_PRIORITY osPriorityHigh
|
||||||
|
|
||||||
// Set the Stack size for the TACOS threads, to 0 to use the default stack size, set in the ioc
|
// Set the Stack size for the TACOS threads, to 0 to use the default stack size, set in the ioc
|
||||||
#define STA_TACOS_MANAGER_STACK_SIZE 0
|
|
||||||
#define STA_TACOS_STATEMACHINE_STACK_SIZE 0
|
#define STA_TACOS_STATEMACHINE_STACK_SIZE 0
|
||||||
|
|
||||||
// Per default, we assume state 0 to be the initial state.
|
// Per default, we assume state 0 to be the initial state.
|
||||||
|
@ -1,124 +0,0 @@
|
|||||||
/*
|
|
||||||
* manager.hpp
|
|
||||||
*
|
|
||||||
* Created on: Sep 19, 2023
|
|
||||||
* Author: Dario
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef INCLUDE_STA_TACOS_MANAGER_HPP_
|
|
||||||
#define INCLUDE_STA_TACOS_MANAGER_HPP_
|
|
||||||
|
|
||||||
|
|
||||||
#include <sta/config.hpp>
|
|
||||||
|
|
||||||
#if !defined(STA_TACOS_MANAGER_PRIORITY) && !defined(DOXYGEN)
|
|
||||||
# error "Manger task priority not specified in config.hpp"
|
|
||||||
#else
|
|
||||||
|
|
||||||
#include <set>
|
|
||||||
#include <list>
|
|
||||||
#include <vector>
|
|
||||||
#include <memory>
|
|
||||||
#include <sta/tacos/thread.hpp>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @defgroup tacos_manager Manager Task
|
|
||||||
* @ingroup tacos
|
|
||||||
* @brief Manager task for TACOS.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace sta
|
|
||||||
{
|
|
||||||
namespace tacos
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @brief Manager class for Tacos.
|
|
||||||
*
|
|
||||||
* @ingroup tacos_manager
|
|
||||||
*/
|
|
||||||
class Manager : public TacosThread
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* @brief Get the singleton instance of the manager.
|
|
||||||
*
|
|
||||||
* @ingroup tacos_manager
|
|
||||||
*/
|
|
||||||
static Manager* instance()
|
|
||||||
{
|
|
||||||
static CGuard g;
|
|
||||||
|
|
||||||
if (!_instance)
|
|
||||||
{
|
|
||||||
// Create a the manager singleton instance.
|
|
||||||
Manager::_instance = new Manager();
|
|
||||||
}
|
|
||||||
|
|
||||||
return _instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Register a thread to be managed by the manager.
|
|
||||||
*/
|
|
||||||
void registerThread(std::shared_ptr<TacosThread> thread, std::set<uint16_t> states);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get the Active Threads object
|
|
||||||
*
|
|
||||||
* @return std::vector<std::shared_ptr<TacosThread>>
|
|
||||||
*/
|
|
||||||
std::vector<std::shared_ptr<TacosThread>> getActiveThreads();
|
|
||||||
|
|
||||||
void init() override;
|
|
||||||
|
|
||||||
void func() override;
|
|
||||||
private:
|
|
||||||
static Manager* _instance;
|
|
||||||
|
|
||||||
class CGuard
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
~CGuard()
|
|
||||||
{
|
|
||||||
if( NULL != Manager::_instance )
|
|
||||||
{
|
|
||||||
delete Manager::_instance;
|
|
||||||
Manager::_instance = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Manager();
|
|
||||||
|
|
||||||
Manager(const Manager&);
|
|
||||||
|
|
||||||
//~Manager();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Forces only threads of current state to run.
|
|
||||||
*/
|
|
||||||
void updateThreads();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Starts all threads which should be running in the given state. Does nothing if the state is already running.
|
|
||||||
*/
|
|
||||||
void startThreads(uint16_t state);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Stops all threads which should not be running in the given state.
|
|
||||||
*/
|
|
||||||
void stopThreads(uint16_t state);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Pointers to all threads which are managed by the manager.
|
|
||||||
*
|
|
||||||
* @ingroup tacos_manager
|
|
||||||
*/
|
|
||||||
std::vector<std::shared_ptr<TacosThread>> threads_[STA_TACOS_NUM_STATES];
|
|
||||||
};
|
|
||||||
} // namespace tacos
|
|
||||||
} // namespace sta
|
|
||||||
|
|
||||||
#endif // STA_TACOS_MANAGER_PRIORITY
|
|
||||||
|
|
||||||
#endif /* INCLUDE_STA_TACOS_MANAGER_HPP_ */
|
|
@ -69,6 +69,9 @@
|
|||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
#include <list>
|
||||||
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
@ -134,11 +137,15 @@ namespace sta
|
|||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief The global event signaling a state change.
|
* @brief The global event signaling a state change.
|
||||||
|
*
|
||||||
|
* @ingroup tacos_statemachine
|
||||||
*/
|
*/
|
||||||
static RtosEvent stateChangeEvent;
|
static RtosEvent stateChangeEvent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Getter function for the singleton instance.
|
* @brief Getter function for the singleton instance.
|
||||||
|
*
|
||||||
|
* @ingroup tacos_statemachine
|
||||||
*/
|
*/
|
||||||
static Statemachine* instance()
|
static Statemachine* instance()
|
||||||
{
|
{
|
||||||
@ -146,7 +153,7 @@ namespace sta
|
|||||||
|
|
||||||
if (!_instance)
|
if (!_instance)
|
||||||
{
|
{
|
||||||
// Create the manager singleton instance.
|
// Create the statemachine singleton instance.
|
||||||
Statemachine::_instance = new Statemachine();
|
Statemachine::_instance = new Statemachine();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,6 +162,8 @@ namespace sta
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns the statemachine's current state.
|
* @brief Returns the statemachine's current state.
|
||||||
|
*
|
||||||
|
* @ingroup tacos_statemachine
|
||||||
*/
|
*/
|
||||||
uint16_t getCurrentState() const;
|
uint16_t getCurrentState() const;
|
||||||
|
|
||||||
@ -165,6 +174,8 @@ namespace sta
|
|||||||
* @param to The state to transition to.
|
* @param to The state to transition to.
|
||||||
* @param lockout The minimum number of milliseconds we expect to stay in this state. This is used to block premature transitions.
|
* @param lockout The minimum number of milliseconds we expect to stay in this state. This is used to block premature transitions.
|
||||||
* @param force If true, the state transition will be executed regardless of the current state.
|
* @param force If true, the state transition will be executed regardless of the current state.
|
||||||
|
*
|
||||||
|
* @ingroup tacos_statemachine
|
||||||
*/
|
*/
|
||||||
void requestStateTransition(uint32_t from, uint32_t to, uint32_t lockout = 0, bool force = false, bool publish = true);
|
void requestStateTransition(uint32_t from, uint32_t to, uint32_t lockout = 0, bool force = false, bool publish = true);
|
||||||
|
|
||||||
@ -175,9 +186,27 @@ namespace sta
|
|||||||
* @param to The state to transition to.
|
* @param to The state to transition to.
|
||||||
* @param millis the number of milliseconds to wait before triggering the transition.
|
* @param millis the number of milliseconds to wait before triggering the transition.
|
||||||
* @param lockout The minimum number of milliseconds we expect to stay in this state. This is used to block premature transitions.
|
* @param lockout The minimum number of milliseconds we expect to stay in this state. This is used to block premature transitions.
|
||||||
|
*
|
||||||
|
* @ingroup tacos_statemachine
|
||||||
*/
|
*/
|
||||||
void requestTimedStateTransition(uint32_t from, uint32_t to, uint32_t millis, uint32_t lockout = 0, bool publish = true);
|
void requestTimedStateTransition(uint32_t from, uint32_t to, uint32_t millis, uint32_t lockout = 0, bool publish = true);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Register a thread to be managed by the statemachine.
|
||||||
|
*
|
||||||
|
* @ingroup tacos_statemachine
|
||||||
|
*/
|
||||||
|
void registerThread(std::shared_ptr<TacosThread> thread, std::set<uint16_t> states);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the Active Threads object
|
||||||
|
*
|
||||||
|
* @return std::vector<std::shared_ptr<TacosThread>>
|
||||||
|
*
|
||||||
|
* @ingroup tacos_statemachine
|
||||||
|
*/
|
||||||
|
std::vector<std::shared_ptr<TacosThread>> getActiveThreads();
|
||||||
|
|
||||||
void init() override;
|
void init() override;
|
||||||
void func() override;
|
void func() override;
|
||||||
|
|
||||||
@ -208,9 +237,32 @@ namespace sta
|
|||||||
* @brief Starts the lockoutTimer for the desired duration.
|
* @brief Starts the lockoutTimer for the desired duration.
|
||||||
*
|
*
|
||||||
* @param millis The duration of the timer in milliseconds.
|
* @param millis The duration of the timer in milliseconds.
|
||||||
|
*
|
||||||
|
* @ingroup tacos_statemachine
|
||||||
*/
|
*/
|
||||||
void setLockoutTimer(uint32_t millis);
|
void setLockoutTimer(uint32_t millis);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Forces only threads of current state to run.
|
||||||
|
*
|
||||||
|
* @ingroup tacos_statemachine
|
||||||
|
*/
|
||||||
|
void updateThreads();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Starts all threads which should be running in the given state. Does nothing if the state is already running.
|
||||||
|
*
|
||||||
|
* @ingroup tacos_statemachine
|
||||||
|
*/
|
||||||
|
void startThreads(uint16_t state);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Stops all threads which should not be running in the given state.
|
||||||
|
*
|
||||||
|
* @ingroup tacos_statemachine
|
||||||
|
*/
|
||||||
|
void stopThreads(uint16_t state);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint16_t currentState_;
|
uint16_t currentState_;
|
||||||
|
|
||||||
@ -218,6 +270,13 @@ namespace sta
|
|||||||
RtosTimer failsafeTimer_;
|
RtosTimer failsafeTimer_;
|
||||||
|
|
||||||
RtosQueue<StateTransition> queue_;
|
RtosQueue<StateTransition> queue_;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Pointers to all threads which are managed by the statemachine.
|
||||||
|
*
|
||||||
|
* @ingroup tacos_statemachine
|
||||||
|
*/
|
||||||
|
std::vector<std::shared_ptr<TacosThread>> threads_[STA_TACOS_NUM_STATES];
|
||||||
};
|
};
|
||||||
} // namespace tacos
|
} // namespace tacos
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
@ -79,7 +79,7 @@ namespace sta
|
|||||||
* @brief Create a new thread with the given name and priority.
|
* @brief Create a new thread with the given name and priority.
|
||||||
*
|
*
|
||||||
* @param name The thread's name. This is used for debugging.
|
* @param name The thread's name. This is used for debugging.
|
||||||
* @param prio The thread's priority. Generally, this should be lower than the manager and statemachine priority.
|
* @param prio The thread's priority. Generally, this should be lower than the statemachine priority.
|
||||||
* @param stack_size The stack size for the task. The default is 0, i.e. the stack size specified in the FreeRTOS settings.
|
* @param stack_size The stack size for the task. The default is 0, i.e. the stack size specified in the FreeRTOS settings.
|
||||||
* @param cb_size The control block size for the task. The default is 0, i.e. the size specified in the FreeRTOS settings.
|
* @param cb_size The control block size for the task. The default is 0, i.e. the size specified in the FreeRTOS settings.
|
||||||
*/
|
*/
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include <sta/tacos/can_bus.hpp>
|
#include <sta/tacos/can_bus.hpp>
|
||||||
#include <sta/debug/assert.hpp>
|
#include <sta/debug/assert.hpp>
|
||||||
#include <sta/tacos/manager.hpp>
|
#include <sta/tacos/statemachine.hpp>
|
||||||
#include <sta/tacos.hpp>
|
#include <sta/tacos.hpp>
|
||||||
|
|
||||||
extern CAN_HandleTypeDef STA_STM32_CAN_HANDLE;
|
extern CAN_HandleTypeDef STA_STM32_CAN_HANDLE;
|
||||||
@ -64,7 +64,7 @@ namespace sta
|
|||||||
{
|
{
|
||||||
|
|
||||||
// Append to the correct thread's queue
|
// 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)
|
if (thread->getCanID() == sysMsg.header.sid)
|
||||||
{
|
{
|
||||||
|
110
src/manager.cpp
110
src/manager.cpp
@ -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
|
|
||||||
|
|
||||||
|
|
@ -33,7 +33,6 @@
|
|||||||
|
|
||||||
// Tacos-specific includes.
|
// Tacos-specific includes.
|
||||||
#include <sta/tacos/c_api/startup.h>
|
#include <sta/tacos/c_api/startup.h>
|
||||||
#include <sta/tacos/manager.hpp>
|
|
||||||
#include <sta/tacos/statemachine.hpp>
|
#include <sta/tacos/statemachine.hpp>
|
||||||
#include <sta/tacos/watchdog.hpp>
|
#include <sta/tacos/watchdog.hpp>
|
||||||
#include <sta/tacos/can_bus.hpp>
|
#include <sta/tacos/can_bus.hpp>
|
||||||
@ -101,23 +100,6 @@ namespace sta
|
|||||||
Statemachine::instance()->start();
|
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
|
#ifdef STA_TACOS_WATCHDOG_ENABLED
|
||||||
STA_WEAK
|
STA_WEAK
|
||||||
void onWatchdogInit()
|
void onWatchdogInit()
|
||||||
@ -158,8 +140,6 @@ namespace sta
|
|||||||
|
|
||||||
tacos::initStatemachine();
|
tacos::initStatemachine();
|
||||||
|
|
||||||
tacos::initManager();
|
|
||||||
|
|
||||||
#ifdef STA_TACOS_WATCHDOG_ENABLED
|
#ifdef STA_TACOS_WATCHDOG_ENABLED
|
||||||
tacos::initWatchdog();
|
tacos::initWatchdog();
|
||||||
#endif // STA_TACOS_WATCHDOG_ENABLED
|
#endif // STA_TACOS_WATCHDOG_ENABLED
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
#include <sta/tacos/statemachine.hpp>
|
#include <sta/tacos/statemachine.hpp>
|
||||||
|
|
||||||
#include <sta/debug/debug.hpp>
|
#include <sta/debug/debug.hpp>
|
||||||
|
#include <FreeRTOS.h>
|
||||||
|
|
||||||
|
|
||||||
namespace sta
|
namespace sta
|
||||||
@ -20,14 +21,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},
|
||||||
queue_{STA_TACOS_STATEMACHINE_QUEUE_LENGTH}
|
queue_{STA_TACOS_STATEMACHINE_QUEUE_LENGTH},
|
||||||
|
threads_{}
|
||||||
{
|
{
|
||||||
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()
|
||||||
{
|
{
|
||||||
|
startThreads(getCurrentState());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Statemachine::func()
|
void Statemachine::func()
|
||||||
@ -64,6 +66,13 @@ namespace sta
|
|||||||
{
|
{
|
||||||
setLockoutTimer(transition.lockout);
|
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);
|
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;
|
Statemachine* Statemachine::_instance = nullptr;
|
||||||
|
|
||||||
RtosEvent Statemachine::stateChangeEvent;
|
RtosEvent Statemachine::stateChangeEvent;
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#ifdef STA_TACOS_WATCHDOG_ENABLED
|
#ifdef STA_TACOS_WATCHDOG_ENABLED
|
||||||
|
|
||||||
#include <sta/tacos/manager.hpp>
|
#include <sta/tacos/statemachine.hpp>
|
||||||
|
|
||||||
namespace sta
|
namespace sta
|
||||||
{
|
{
|
||||||
@ -10,7 +10,7 @@ namespace sta
|
|||||||
{
|
{
|
||||||
void Watchdog::func()
|
void Watchdog::func()
|
||||||
{
|
{
|
||||||
for (std::shared_ptr<TacosThread> thread : Manager::instance()->getActiveThreads())
|
for (std::shared_ptr<TacosThread> thread : Statemachine::instance()->getActiveThreads())
|
||||||
{
|
{
|
||||||
switch (thread->getStatus())
|
switch (thread->getStatus())
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user