Skip to content

Commit

Permalink
refactor module status and mode
Browse files Browse the repository at this point in the history
  • Loading branch information
PonomarevDA committed Jun 4, 2024
1 parent b1a2339 commit 853fd34
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 38 deletions.
14 changes: 7 additions & 7 deletions Src/common/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<uint32_t>(1000.0f / frequency) : 1000;
}
37 changes: 19 additions & 18 deletions Src/common/module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,29 @@
#include <cstdint>
#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();

Expand All @@ -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};
Expand Down
10 changes: 5 additions & 5 deletions Src/cyphal_application/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ __attribute__((noreturn)) void application_entry_point() {
FeedbackModule feedback;
CircuitStatus crct;

std::array<BaseModule*, 3> modules = { &setpoint, &feedback, &crct };
std::array<Module*, 3> 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) {
Expand All @@ -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<uint8_t>(health)});
cyphal.setNodeMode(uavcan_node_Mode_1_0{static_cast<uint8_t>(mode)});
LedPeriphery::toggle(led_color);
LedPeriphery::toggle(color);
WatchdogPeriphery::refresh();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions Src/cyphal_application/modules/feedback/feedback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Src/cyphal_application/modules/setpoint/setpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions Src/cyphal_application/modules/setpoint/setpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 853fd34

Please sign in to comment.