From 1a9a96503e1f90bae34d22b5d960e347833dc8e1 Mon Sep 17 00:00:00 2001 From: dario Date: Fri, 29 Sep 2023 15:12:44 +0200 Subject: [PATCH] Changed registerThread to accept shared_ptrs to fix some issues with polymorphism. --- App/Inc/sta/config.hpp | 4 ++-- App/Src/startup.cpp | 8 +++++--- App/Src/tasks/dummy.cpp | 6 ++++-- Tacos/include/sta/tacos/manager.hpp | 10 +++++++--- Tacos/include/sta/tacos/thread.hpp | 4 ++-- Tacos/src/manager.cpp | 23 +++++++---------------- Tacos/src/statemachine.cpp | 3 --- Tacos/src/thread.cpp | 5 +---- 8 files changed, 28 insertions(+), 35 deletions(-) diff --git a/App/Inc/sta/config.hpp b/App/Inc/sta/config.hpp index c1cacbf..8fba61b 100644 --- a/App/Inc/sta/config.hpp +++ b/App/Inc/sta/config.hpp @@ -24,8 +24,8 @@ // Settings for the rtos-utils #define STA_RTOS_SYSTEM_EVENTS_ENABLE -#define STA_RTOS_SYSTEM_WATCHDOG_ENABLE -#define STA_RTOS_WATCHDOG_ENABLE +// #define STA_RTOS_SYSTEM_WATCHDOG_ENABLE +// #define STA_RTOS_WATCHDOG_ENABLE // Settings for TACOS diff --git a/App/Src/startup.cpp b/App/Src/startup.cpp index a2e0875..a6265f9 100644 --- a/App/Src/startup.cpp +++ b/App/Src/startup.cpp @@ -12,6 +12,8 @@ #include #include +#include + namespace sta { @@ -26,9 +28,9 @@ namespace sta { STA_DEBUG_PRINTLN("Starting manager task!"); - // Manager::instance()->registerThread(demo::DummyTask("A"), {0, 1}); - // Manager::instance()->registerThread(demo::DummyTask("B"), {1, 2}); - // Manager::instance()->registerThread(demo::DummyTask("C"), {2, 3}); + Manager::instance()->registerThread(std::make_unique("A"), {0, 1}); + Manager::instance()->registerThread(std::make_unique("B"), {0, 2}); + Manager::instance()->registerThread(std::make_unique("C"), {0, 3}); } } // namespace tacos } // namespace sta diff --git a/App/Src/tasks/dummy.cpp b/App/Src/tasks/dummy.cpp index c93f329..2e6e131 100644 --- a/App/Src/tasks/dummy.cpp +++ b/App/Src/tasks/dummy.cpp @@ -23,13 +23,15 @@ namespace demo void DummyTask::init() { - STA_DEBUG_PRINTLN("Initialized dummy task!"); + } void DummyTask::func() { - STA_DEBUG_PRINT("Executing "); STA_DEBUG_PRINTLN(this->getName()); + // STA_DEBUG_PRINT("Executing "); + // STA_DEBUG_PRINTLN(this->getName()); + osDelay(1000); } } // namespace demo diff --git a/Tacos/include/sta/tacos/manager.hpp b/Tacos/include/sta/tacos/manager.hpp index 07120d8..c11ab3d 100644 --- a/Tacos/include/sta/tacos/manager.hpp +++ b/Tacos/include/sta/tacos/manager.hpp @@ -15,8 +15,9 @@ # error "Manger task priority not specified in config.hpp" #else -#include #include +#include +#include #include namespace sta @@ -39,7 +40,10 @@ namespace sta return _instance; } - void registerThread(TacosThread thread, std::list states); + /** + * + */ + void registerThread(std::shared_ptr thread, std::list states); void init() override; @@ -73,7 +77,7 @@ namespace sta void stopThreads(uint16_t state); - std::set threads_[STA_TACOS_NUM_STATES]; + std::set> threads_[STA_TACOS_NUM_STATES]; }; } // namespace tacos } // namespace sta diff --git a/Tacos/include/sta/tacos/thread.hpp b/Tacos/include/sta/tacos/thread.hpp index c054f8b..b809fc1 100644 --- a/Tacos/include/sta/tacos/thread.hpp +++ b/Tacos/include/sta/tacos/thread.hpp @@ -61,12 +61,12 @@ namespace sta /** * @brief This function is executed first when this thread is started. */ - virtual void init(); + virtual void init() = 0; /** * @brief The body of the thread's loop. Has to be implemented by the user. */ - virtual void func(); + virtual void func() = 0; private: diff --git a/Tacos/src/manager.cpp b/Tacos/src/manager.cpp index eebdc1f..cf52ed2 100644 --- a/Tacos/src/manager.cpp +++ b/Tacos/src/manager.cpp @@ -14,7 +14,7 @@ namespace sta { namespace tacos { - void Manager::registerThread(TacosThread thread, std::list states) + void Manager::registerThread(std::shared_ptr thread, std::list states) { for (uint16_t state : states) { @@ -28,11 +28,11 @@ namespace sta { STA_ASSERT(state < STA_TACOS_NUM_STATES); - for (TacosThread thread : threads_[state]) + for (std::shared_ptr thread : threads_[state]) { - if (!thread.isRunning()) + if (!thread->isRunning()) { - thread.start(); + thread->start(); } } } @@ -48,13 +48,13 @@ namespace sta continue; } - for (TacosThread thread : threads_[state]) + for (std::shared_ptr thread : threads_[state]) { // 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_[currentState].count(thread) == 0) { // ...politely request termination. - thread.requestTermination(); + thread->requestTermination(); } } } @@ -70,20 +70,11 @@ namespace sta void Manager::init() { - STA_DEBUG_PRINTLN("INITIALIZING MANAGER!"); - startThreads(Statemachine::instance()->getCurrentState()); } void Manager::func() { - if (true) - { - // STA_DEBUG_PRINTLN("LOOPY LOOP IN MANAGER TASK"); - osDelay(1000); - } - - // Wait for either the termination request or the state change flag. uint32_t flags = osEventFlagsWait(getInstance(), STA_RTOS_THREAD_FLAG_TERMINATE, osFlagsWaitAny, osWaitForever); diff --git a/Tacos/src/statemachine.cpp b/Tacos/src/statemachine.cpp index 0972024..244ccb1 100644 --- a/Tacos/src/statemachine.cpp +++ b/Tacos/src/statemachine.cpp @@ -35,10 +35,7 @@ namespace sta void Statemachine::func() { - STA_DEBUG_PRINTLN("LOOPY LOOP IN STATEMACHINE."); - STA_DEBUG_PRINTLN(lockoutTimer_.isRunning() ? "LOCKOUT RUNNING!" : "LOCKOUT STOPPED!"); - osDelay(500); } uint16_t Statemachine::getCurrentState() const diff --git a/Tacos/src/thread.cpp b/Tacos/src/thread.cpp index f821973..e0e3d5f 100644 --- a/Tacos/src/thread.cpp +++ b/Tacos/src/thread.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -87,10 +88,6 @@ namespace sta return std::strcmp(this->getName(), other.getName()) < 0; } - void TacosThread::init(){} - - void TacosThread::func(){} - TacosThread::~TacosThread(){} } // namespace tacos