From 853fd344cde76f249e6d99c497c602ea2b756194 Mon Sep 17 00:00:00 2001 From: PonomarevDA Date: Mon, 3 Jun 2024 00:24:37 +0300 Subject: [PATCH] refactor module status and mode --- Src/common/module.cpp | 14 +++---- Src/common/module.hpp | 37 ++++++++++--------- Src/cyphal_application/application.cpp | 10 ++--- .../modules/circuit_status/circuit_status.cpp | 2 +- .../modules/circuit_status/circuit_status.hpp | 4 +- .../modules/feedback/feedback.hpp | 4 +- .../modules/setpoint/setpoint.cpp | 2 +- .../modules/setpoint/setpoint.hpp | 4 +- 8 files changed, 39 insertions(+), 38 deletions(-) diff --git a/Src/common/module.cpp b/Src/common/module.cpp index c30c674..9fd9e0c 100644 --- a/Src/common/module.cpp +++ b/Src/common/module.cpp @@ -6,22 +6,22 @@ #include "module.hpp" -BaseModule::BaseModule(float frequency) : period_ms(period_ms_from_frequency(frequency)) { +Module::Module(float frequency) : period_ms(period_ms_from_frequency(frequency)) { } -void BaseModule::init() { - mode = ModuleMode::OPEARTIONAL; +void Module::init() { + mode = Mode::OPEARTIONAL; } -ModuleStatus BaseModule::get_health() const { +Module::Status Module::get_health() const { return health; } -ModuleMode BaseModule::get_mode() const { +Module::Mode Module::get_mode() const { return mode; } -void BaseModule::process() { +void Module::process() { uint32_t crnt_time_ms = HAL_GetTick(); if (crnt_time_ms < next_spin_time_ms) { return; @@ -32,6 +32,6 @@ void BaseModule::process() { spin_once(); } -uint32_t BaseModule::period_ms_from_frequency(float frequency) { +uint32_t Module::period_ms_from_frequency(float frequency) { return (frequency > 0.001f) ? static_cast(1000.0f / frequency) : 1000; } diff --git a/Src/common/module.hpp b/Src/common/module.hpp index 296b33d..5de89b4 100644 --- a/Src/common/module.hpp +++ b/Src/common/module.hpp @@ -10,28 +10,29 @@ #include #include "main.h" -enum class ModuleStatus: uint8_t { - OK = 0, // ok nominal - MINOR_FAILURE = 1, // warning advisory - MAJOR_FAILURE = 2, // error caution - FATAL_MALFANCTION = 3, // critical warning -}; - -enum class ModuleMode: uint8_t { - OPEARTIONAL = 0, // after successful initialization - INITIALIZATION = 1, // after startup - MAINTENANCE = 2, // calibration, self-test -}; -class BaseModule { +class Module { public: - explicit BaseModule(float frequency); + enum class Status: uint8_t { + OK = 0, // ok nominal + MINOR_FAILURE = 1, // warning advisory + MAJOR_FAILURE = 2, // error caution + FATAL_MALFANCTION = 3, // critical warning + }; + + enum class Mode: uint8_t { + OPEARTIONAL = 0, // after successful initialization + INITIALIZATION = 1, // after startup + MAINTENANCE = 2, // calibration, self-test + }; + + explicit Module(float frequency); virtual void init(); - ModuleStatus get_health() const; + Status get_health() const; - ModuleMode get_mode() const; + Mode get_mode() const; void process(); @@ -41,8 +42,8 @@ class BaseModule { static uint32_t period_ms_from_frequency(float frequency); - ModuleStatus health{ModuleStatus::OK}; - ModuleMode mode{ModuleMode::INITIALIZATION}; + Status health{Status::OK}; + Mode mode{Mode::INITIALIZATION}; uint32_t period_ms; uint32_t next_spin_time_ms{0}; diff --git a/Src/cyphal_application/application.cpp b/Src/cyphal_application/application.cpp index c4dc216..523e981 100644 --- a/Src/cyphal_application/application.cpp +++ b/Src/cyphal_application/application.cpp @@ -35,15 +35,15 @@ __attribute__((noreturn)) void application_entry_point() { FeedbackModule feedback; CircuitStatus crct; - std::array modules = { &setpoint, &feedback, &crct }; + std::array modules = { &setpoint, &feedback, &crct }; for (auto module : modules) { module->init(); } while (true) { - auto health = ModuleStatus::OK; - auto mode = ModuleMode::OPEARTIONAL; + auto health = Module::Status::OK; + auto mode = Module::Mode::OPEARTIONAL; cyphal.process(); for (auto module : modules) { @@ -58,10 +58,10 @@ __attribute__((noreturn)) void application_entry_point() { } } - auto led_color = (health == ModuleStatus::OK) ? LedColor::BLUE_COLOR : LedColor::RED_COLOR; + auto color = (health == Module::Status::OK) ? LedColor::BLUE_COLOR : LedColor::RED_COLOR; cyphal.setNodeHealth(uavcan_node_Health_1_0{static_cast(health)}); cyphal.setNodeMode(uavcan_node_Mode_1_0{static_cast(mode)}); - LedPeriphery::toggle(led_color); + LedPeriphery::toggle(color); WatchdogPeriphery::refresh(); } } diff --git a/Src/cyphal_application/modules/circuit_status/circuit_status.cpp b/Src/cyphal_application/modules/circuit_status/circuit_status.cpp index 6265132..508af20 100644 --- a/Src/cyphal_application/modules/circuit_status/circuit_status.cpp +++ b/Src/cyphal_application/modules/circuit_status/circuit_status.cpp @@ -15,7 +15,7 @@ void CircuitStatus::init() { - health = (CircuitPeriphery::init() < 0) ? ModuleStatus::FATAL_MALFANCTION : ModuleStatus::OK; + health = (CircuitPeriphery::init() < 0) ? Status::FATAL_MALFANCTION : Status::OK; } void CircuitStatus::update_params() { diff --git a/Src/cyphal_application/modules/circuit_status/circuit_status.hpp b/Src/cyphal_application/modules/circuit_status/circuit_status.hpp index e583169..fd6793d 100644 --- a/Src/cyphal_application/modules/circuit_status/circuit_status.hpp +++ b/Src/cyphal_application/modules/circuit_status/circuit_status.hpp @@ -16,9 +16,9 @@ extern "C" { #endif -class CircuitStatus : public BaseModule { +class CircuitStatus : public Module { public: - CircuitStatus() : BaseModule(2) {} + CircuitStatus() : Module(2) {} void init() override; protected: diff --git a/Src/cyphal_application/modules/feedback/feedback.hpp b/Src/cyphal_application/modules/feedback/feedback.hpp index 26bfb83..b880f53 100644 --- a/Src/cyphal_application/modules/feedback/feedback.hpp +++ b/Src/cyphal_application/modules/feedback/feedback.hpp @@ -24,9 +24,9 @@ struct FeedbackPublisher: public cyphal::CyphalPublisher { reg_udral_service_actuator_common_Feedback_0_1 msg; }; -class FeedbackModule : public BaseModule { +class FeedbackModule : public Module { public: - FeedbackModule() : BaseModule(1) {} + FeedbackModule() : Module(1) {} protected: void update_params() override; diff --git a/Src/cyphal_application/modules/setpoint/setpoint.cpp b/Src/cyphal_application/modules/setpoint/setpoint.cpp index 27107ba..6a779a1 100644 --- a/Src/cyphal_application/modules/setpoint/setpoint.cpp +++ b/Src/cyphal_application/modules/setpoint/setpoint.cpp @@ -42,7 +42,7 @@ void SetpointModule::init() { PwmPeriphery::init(PwmPin::PWM_3); PwmPeriphery::init(PwmPin::PWM_4); - health = (sub.init() < 0) ? ModuleStatus::FATAL_MALFANCTION : ModuleStatus::OK; + health = (sub.init() < 0) ? Status::FATAL_MALFANCTION : Status::OK; } void SetpointModule::spin_once() { diff --git a/Src/cyphal_application/modules/setpoint/setpoint.hpp b/Src/cyphal_application/modules/setpoint/setpoint.hpp index 52dbac3..1252e03 100644 --- a/Src/cyphal_application/modules/setpoint/setpoint.hpp +++ b/Src/cyphal_application/modules/setpoint/setpoint.hpp @@ -23,9 +23,9 @@ class SetpointSubscriber: public cyphal::CyphalSubscriber { void callback(const cyphal::CanardRxTransfer& transfer) override; }; -class SetpointModule : public BaseModule { +class SetpointModule : public Module { public: - SetpointModule() : BaseModule(50) {} + SetpointModule() : Module(50) {} void init() override; protected: