diff --git a/include/sta/tacos.hpp b/include/sta/tacos.hpp index 7f9ef4a..fa88c28 100644 --- a/include/sta/tacos.hpp +++ b/include/sta/tacos.hpp @@ -9,7 +9,6 @@ #define STA_TACOS_HPP #include -#include #include #include @@ -74,7 +73,7 @@ namespace sta { std::shared_ptr thread_ptr = std::make_shared(args...); - Manager::instance()->registerThread(thread_ptr, states); + Statemachine::instance()->registerThread(thread_ptr, states); return thread_ptr; } diff --git a/include/sta/tacos/configs/default.hpp b/include/sta/tacos/configs/default.hpp index c8a698f..bc791bb 100644 --- a/include/sta/tacos/configs/default.hpp +++ b/include/sta/tacos/configs/default.hpp @@ -2,13 +2,11 @@ #define STA_TACOS_CONFIGS_DEFAULT_HPP // 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_WATCHDOG_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 -#define STA_TACOS_MANAGER_STACK_SIZE 0 #define STA_TACOS_STATEMACHINE_STACK_SIZE 0 // Per default, we assume state 0 to be the initial state. diff --git a/include/sta/tacos/manager.hpp b/include/sta/tacos/manager.hpp deleted file mode 100644 index 65f51c5..0000000 --- a/include/sta/tacos/manager.hpp +++ /dev/null @@ -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 - -#if !defined(STA_TACOS_MANAGER_PRIORITY) && !defined(DOXYGEN) -# error "Manger task priority not specified in config.hpp" -#else - -#include -#include -#include -#include -#include - -/** - * @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 thread, std::set states); - - /** - * @brief Get the Active Threads object - * - * @return std::vector> - */ - std::vector> 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> threads_[STA_TACOS_NUM_STATES]; - }; - } // namespace tacos -} // namespace sta - -#endif // STA_TACOS_MANAGER_PRIORITY - -#endif /* INCLUDE_STA_TACOS_MANAGER_HPP_ */ diff --git a/include/sta/tacos/statemachine.hpp b/include/sta/tacos/statemachine.hpp index 452809b..9217c1c 100644 --- a/include/sta/tacos/statemachine.hpp +++ b/include/sta/tacos/statemachine.hpp @@ -69,6 +69,9 @@ #include #include +#include +#include +#include #include #include @@ -134,11 +137,15 @@ namespace sta public: /** * @brief The global event signaling a state change. + * + * @ingroup tacos_statemachine */ static RtosEvent stateChangeEvent; /** * @brief Getter function for the singleton instance. + * + * @ingroup tacos_statemachine */ static Statemachine* instance() { @@ -146,7 +153,7 @@ namespace sta if (!_instance) { - // Create the manager singleton instance. + // Create the statemachine singleton instance. Statemachine::_instance = new Statemachine(); } @@ -155,6 +162,8 @@ namespace sta /** * @brief Returns the statemachine's current state. + * + * @ingroup tacos_statemachine */ uint16_t getCurrentState() const; @@ -165,6 +174,8 @@ namespace sta * @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 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); @@ -175,9 +186,27 @@ namespace sta * @param to The state to transition to. * @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. + * + * @ingroup tacos_statemachine */ 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 thread, std::set states); + + /** + * @brief Get the Active Threads object + * + * @return std::vector> + * + * @ingroup tacos_statemachine + */ + std::vector> getActiveThreads(); + void init() override; void func() override; @@ -208,9 +237,32 @@ namespace sta * @brief Starts the lockoutTimer for the desired duration. * * @param millis The duration of the timer in milliseconds. + * + * @ingroup tacos_statemachine */ 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: uint16_t currentState_; @@ -218,6 +270,13 @@ namespace sta RtosTimer failsafeTimer_; RtosQueue queue_; + + /** + * @brief Pointers to all threads which are managed by the statemachine. + * + * @ingroup tacos_statemachine + */ + std::vector> threads_[STA_TACOS_NUM_STATES]; }; } // namespace tacos } // namespace sta diff --git a/include/sta/tacos/thread.hpp b/include/sta/tacos/thread.hpp index a60064d..32d582e 100644 --- a/include/sta/tacos/thread.hpp +++ b/include/sta/tacos/thread.hpp @@ -79,7 +79,7 @@ namespace sta * @brief Create a new thread with the given name and priority. * * @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 cb_size The control block size for the task. The default is 0, i.e. the size specified in the FreeRTOS settings. */ diff --git a/src/can_bus.cpp b/src/can_bus.cpp index 78686d3..06ad880 100644 --- a/src/can_bus.cpp +++ b/src/can_bus.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include extern CAN_HandleTypeDef STA_STM32_CAN_HANDLE; @@ -64,7 +64,7 @@ namespace sta { // Append to the correct thread's queue - for (std::shared_ptr thread : Manager::instance()->getActiveThreads()) + for (std::shared_ptr thread : Statemachine::instance()->getActiveThreads()) { if (thread->getCanID() == sysMsg.header.sid) { diff --git a/src/manager.cpp b/src/manager.cpp deleted file mode 100644 index 1d59de1..0000000 --- a/src/manager.cpp +++ /dev/null @@ -1,110 +0,0 @@ -/* - * manager.cpp - * - * Created on: Sep 19, 2023 - * Author: Dario - */ - -#include -#include -#include - -#include - -#include -#include - - -namespace sta -{ - namespace tacos - { - void Manager::registerThread(std::shared_ptr thread, std::set states) - { - for (uint16_t state : states) - { - STA_ASSERT(state < STA_TACOS_NUM_STATES); - - threads_[state].push_back(thread); - } - } - - std::vector> Manager::getActiveThreads() - { - return threads_[tacos::getState()]; - } - - void Manager::startThreads(uint16_t state) - { - STA_ASSERT(state < STA_TACOS_NUM_STATES); - - for (std::shared_ptr thread : threads_[state]) - { - if (!thread->isRunning()) - { - thread->start(); - } - } - } - - void Manager::stopThreads(uint16_t state) - { - std::set> terminated; - - for (uint16_t other = 0; other < STA_TACOS_NUM_STATES; ++other) - { - if (other == state) - { - continue; - } - - for (std::shared_ptr 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 - - diff --git a/src/startup.cpp b/src/startup.cpp index 0f450f5..175cf54 100644 --- a/src/startup.cpp +++ b/src/startup.cpp @@ -33,7 +33,6 @@ // Tacos-specific includes. #include -#include #include #include #include @@ -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 diff --git a/src/statemachine.cpp b/src/statemachine.cpp index d970a57..762e48b 100644 --- a/src/statemachine.cpp +++ b/src/statemachine.cpp @@ -9,6 +9,7 @@ #include #include +#include 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 thread, std::set states) + { + for (uint16_t state : states) + { + STA_ASSERT(state < STA_TACOS_NUM_STATES); + + threads_[state].push_back(thread); + } + } + + std::vector> Statemachine::getActiveThreads() + { + return threads_[tacos::getState()]; + } + + void Statemachine::startThreads(uint16_t state) + { + STA_ASSERT(state < STA_TACOS_NUM_STATES); + + for (std::shared_ptr thread : threads_[state]) + { + if (!thread->isRunning()) + { + thread->start(); + } + } + } + + void Statemachine::stopThreads(uint16_t state) + { + std::set> terminated; + + for (uint16_t other = 0; other < STA_TACOS_NUM_STATES; ++other) + { + if (other == state) + { + continue; + } + + for (std::shared_ptr 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; diff --git a/src/watchdog.cpp b/src/watchdog.cpp index 2550893..cf91b5a 100644 --- a/src/watchdog.cpp +++ b/src/watchdog.cpp @@ -2,7 +2,7 @@ #ifdef STA_TACOS_WATCHDOG_ENABLED -#include +#include namespace sta { @@ -10,7 +10,7 @@ namespace sta { void Watchdog::func() { - for (std::shared_ptr thread : Manager::instance()->getActiveThreads()) + for (std::shared_ptr thread : Statemachine::instance()->getActiveThreads()) { switch (thread->getStatus()) {