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

@@ -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.

View File

@@ -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_ */

View File

@@ -69,6 +69,9 @@
#include <functional>
#include <tuple>
#include <list>
#include <vector>
#include <memory>
#include <set>
#include <algorithm>
@@ -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<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 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<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 sta

View File

@@ -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.
*/