From f2cdd29e0f98ebbb43b984e7596c73302f64ad54 Mon Sep 17 00:00:00 2001 From: Hugo Jean Ponsin Date: Mon, 20 Jul 2026 09:09:18 +0200 Subject: [PATCH] update --- CMakeLists.txt | 2 + src/device/DemandHandler.cpp | 15 +++-- src/device/FeedbackHandlerConfig.h | 39 ++++++++++++ src/device/InputHandler.cpp | 85 +++++++++++++++++++++++++++ src/device/InputHandler.h | 42 +++++++++++-- src/device/InputHandlerConfig.h | 14 +++++ src/device/PressureCalculator.cpp | 32 ++++++++-- src/device/PressureCalculator.h | 16 +++-- src/device/SettingsHandler.cpp | 67 ++++++++++++++------- src/device/SettingsHandler.h | 32 +++++++--- src/device/SettingsHandlerConfig.h | 10 ++-- src/device/itc_pressure_optimizer.cpp | 5 +- src/device/itc_pressure_optimizer.h | 8 +-- 13 files changed, 307 insertions(+), 60 deletions(-) create mode 100644 src/device/FeedbackHandlerConfig.h create mode 100644 src/device/InputHandlerConfig.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 50a2740..fb7a546 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,8 @@ add_library( itcPressureOptimizer src/device/itc_pressure_optimizer.cpp src/device/DemandHandler.cpp + src/device/InputHandler.cpp + src/device/SettingsHandler.cpp ) set_property( diff --git a/src/device/DemandHandler.cpp b/src/device/DemandHandler.cpp index 4e9f7d9..cee662d 100644 --- a/src/device/DemandHandler.cpp +++ b/src/device/DemandHandler.cpp @@ -19,6 +19,8 @@ bool DemandHandler::init() { bool indexValid = true; + // This is checked every start, in case of someone change the index in the + // config header if (o.size() < static_cast(ParameterIndex::MINIMAL_PRESSURE)) { indexValid = false; } @@ -35,11 +37,11 @@ bool DemandHandler::init() { if (indexValid) return false; - pullMinimalPressure(); - pullMaximalPressure(); - pullConstante1(); - pullConstante2(); - pullPressureControlMode(); + this->pullMinimalPressure(); + this->pullMaximalPressure(); + this->pullConstante1(); + this->pullConstante2(); + this->pullPressureControlMode(); return true; } @@ -116,6 +118,7 @@ DemandHandler::DemandHandler(EventBus &eventBusReference, eventBus.subscribe( [this](const itcPressureOptimizer::EquipmentInitEvent &e) { - printf("[EquipmentInitEvent] : init recived\n"); + this->init(); + this->setHotlink(); }); } diff --git a/src/device/FeedbackHandlerConfig.h b/src/device/FeedbackHandlerConfig.h new file mode 100644 index 0000000..4b761b4 --- /dev/null +++ b/src/device/FeedbackHandlerConfig.h @@ -0,0 +1,39 @@ +#ifndef FEEDBACK_HANDLER_CONFIG_H +#define FEEDBACK_HANDLER_CONFIG_H + +#include + +class FeedbackHandler { + private: + bool isPressureControlModeEnable; + std::string equipmentName; + std::string odbPath; + std::string outputKeyName = "Measured" + + enum class RegisterIndex : uint16_t { + SetPoint = 1, + Temperature = 2, + Power = 3, + AveragePower = 4, + Pressure = 5, + Constante1 = 10, + Constante2 = 11 + }; + + void setValueToOdb(int index, float value); + + public: + FeedbackHandler(std::string equipmentName); + + void enablePressureControlMode(bool enable); + + void setSP(float value); + void setTemperature(float value); + void setPower(float value); + void setAveragePower(float value); + void setPressure(float value); + void setConstante1(float value); + void setConstante2(float value); +}; + +#endif \ No newline at end of file diff --git a/src/device/InputHandler.cpp b/src/device/InputHandler.cpp index e69de29..21d91d9 100644 --- a/src/device/InputHandler.cpp +++ b/src/device/InputHandler.cpp @@ -0,0 +1,85 @@ +#include "InputHandler.h" +#include "EventBus.h" +#include "InputHandlerConfig.h" +#include "SettingsHandler.h" + +#include +#include + +void InputHandler::updatePath(std::string name) { + this->path = InputHandlerConfig::PATH_PREFIX + name + + InputHandlerConfig::PATH_SUFFIX; +} + +void InputHandler::updateSPIndex(int index) { this->SPIndex = index; } + +void InputHandler::updateTemperatureIndex(int index) { + this->temperatureIndex = index; +} + +void InputHandler::updatePowerIndex(int index) { this->powerIndex = index; } + +void InputHandler::pullSPValue() { + midas::odb o(this->path); + float SPValue = o[SPIndex]; + Event event = {EventType::SP_VALUE, SPValue}; + eventBus.publish(event); +} +void InputHandler::pullTemperatureValue() { + midas::odb o(this->path); + float temperatureValue = o[temperatureIndex]; + Event event = {EventType::TEMPERATURE_VALUE, temperatureValue}; + eventBus.publish(event); +} +void InputHandler::pullPowerValue() { + midas::odb o(this->path); + float powerValue = o[powerIndex]; + Event event = {EventType::POWER_VALUE, powerValue}; + eventBus.publish(event); +} + +void InputHandler::updateHotlink() { + // TODO : Verified this code work + // This should change the target in case of a new name given + if (hotlink) { + hotlink->unwatch(); + hotlink.reset(); + } + + hotlink.emplace(path); + + hotlink->watch([&](midas::odb &arg) { + int hotlinkIndex = arg.get_last_index(); + if (hotlinkIndex == SPIndex) { + pullSPValue(); + } else if (hotlinkIndex == temperatureIndex) { + pullTemperatureValue(); + } else if (hotlinkIndex == powerIndex) { + pullPowerValue(); + } + }); +} + +InputHandler::InputHandler(EventBus &eventBusReference) + : eventBus(eventBusReference) { + eventBus.subscribe( + [this](const SettingsHandler::Event &e) { + switch (e.type) { + case SettingsHandler::EventType::EQUIPMENT_NAME: + updatePath(std::get(e.value)); + updateHotlink(); + break; + case SettingsHandler::EventType::INPUT_SP_INDEX: + updateSPIndex(std::get(e.value)); + break; + case SettingsHandler::EventType::INPUT_TEMPERATURE_INDEX: + updateTemperatureIndex(std::get(e.value)); + break; + case SettingsHandler::EventType::INPUT_POWER_INDEX: + updatePowerIndex(std::get(e.value)); + break; + default: + break; + } + }); +} diff --git a/src/device/InputHandler.h b/src/device/InputHandler.h index fe2b064..478c90f 100644 --- a/src/device/InputHandler.h +++ b/src/device/InputHandler.h @@ -1,15 +1,45 @@ +#ifndef INPUT_HANDLER_H +#define INPUT_HANDLER_H + +#include "EventBus.h" +#include "odbxx.h" +#include #include class InputHandler { private: - std::string equipmentName; - float cachedSP; - float cachedTemperature; - float cachedPower; + EventBus &eventBus; + std::string path; + int SPIndex; + int temperatureIndex; + int powerIndex; - public: - void updateEquipmentName(std::string name); + std::optional hotlink; + + enum class EventType { + SP_VALUE, + TEMPERATURE_VALUE, + POWER_VALUE, + }; + + struct Event { + EventType type; + float value; + }; + + void updatePath(std::string name); void updateSPIndex(int index); void updateTemperatureIndex(int index); void updatePowerIndex(int index); + + void updateHotlink(); + + void pullSPValue(); + void pullTemperatureValue(); + void pullPowerValue(); + + public: + InputHandler(EventBus &eventBusReference); }; + +#endif \ No newline at end of file diff --git a/src/device/InputHandlerConfig.h b/src/device/InputHandlerConfig.h new file mode 100644 index 0000000..258905e --- /dev/null +++ b/src/device/InputHandlerConfig.h @@ -0,0 +1,14 @@ +#ifndef INPUT_HANDLER_CONFIG_H +#define INPUT_HANDLER_CONFIG_H + +#include + +namespace InputHandlerConfig { + +const std::string PATH_PREFIX = "/Equipment/"; +const std::string PATH_SUFFIX = "/Variables"; + +const std::string INPUT_VARIABLE = "Input"; + +} // namespace InputHandlerConfig +#endif \ No newline at end of file diff --git a/src/device/PressureCalculator.cpp b/src/device/PressureCalculator.cpp index 7c171ab..c2dec03 100644 --- a/src/device/PressureCalculator.cpp +++ b/src/device/PressureCalculator.cpp @@ -2,12 +2,36 @@ #include void PressureCalculator::update() { - double c2 = average.getAverage() * cachedConstante2; - double c1 = cachedConstante1 * (cachedTemperature - cachedSP - c2); - double uncapedPressure = cachedMinimalPressure + c1; - if (c2 != 0) { + double c2contrib = average.getAverage() * cachedConstante2; + double c1contrib = + cachedConstante1 * (cachedTemperature - cachedSP - c2contrib); + double uncapedPressure = cachedMinimalPressure + c1contrib; + + double cappedPressure = uncapedPressure; + + if (c2contrib != 0) { + c2contrib = -c2contrib * cachedConstante1; + } else { + c2contrib = 0.0f; } + + if (cappedPressure < cachedMinimalPressure) + cappedPressure = cachedMinimalPressure; + + if (cappedPressure > cachedMaximalPressure) + cappedPressure = cachedMaximalPressure; + + Event event = {}; + + if (c2contrib != 0) { + event.c1contrib = c1contrib; + event.c2contrib = c2contrib; + event.uncapedPressure = uncapedPressure; + event.cappedPressure = cappedPressure; + } + + eventBus.publish(event); } void PressureCalculator::updateSP(float value) { diff --git a/src/device/PressureCalculator.h b/src/device/PressureCalculator.h index f53b128..d808acf 100644 --- a/src/device/PressureCalculator.h +++ b/src/device/PressureCalculator.h @@ -3,7 +3,15 @@ class PressureCalculator { private: Average average; - OutputHandler outputHandler; + EventBus &eventBus; + + struct Event { + float c1contrib; + float c2contrib; + float uncapedPressure; + float cappedPressure; + }; + float cachedSP; float cachedTemperature; float cachedPower; @@ -13,9 +21,6 @@ class PressureCalculator { float cachedConstante1; float cachedConstante2; - public: - void update(); - void updateSP(float value); void updateTemperature(float value); void updatePower(float value); @@ -24,4 +29,7 @@ class PressureCalculator { void updateMaximalPressure(float value); void updateConstante1(float value); void updateConstante2(float value); + + public: + void update(); }; diff --git a/src/device/SettingsHandler.cpp b/src/device/SettingsHandler.cpp index 19a170f..aae5a54 100644 --- a/src/device/SettingsHandler.cpp +++ b/src/device/SettingsHandler.cpp @@ -1,35 +1,55 @@ #include "SettingsHandler.h" #include "SettingsHandlerConfig.h" +#include "itc_pressure_optimizer.h" +#include + +using namespace SettingsHandlerConfig; void SettingsHandler::pullEquipmentName() { midas::odb o(this->path); - std::string equipmentName = o[ParameterIndex::EQUIPMENT_NAME]; - inputHandler.get().updateEquipmentName(equipmentName); + std::string equipmentName = o(SettingsHandlerConfig::EQUIPMENT_NAME); + Event event = {EventType::EQUIPMENT_NAME, equipmentName}; + eventBus.publish(event); } void SettingsHandler::pullOutputPressureSPIndex() { midas::odb o(this->path); - int outputPressureSPIndex = o[ParameterIndex::OUTPUT_PRESSURE_SP_INDEX]; - inputHandler.get().updateEquipmentName(equipmentName); + int outputPressureSPIndex = + o(SettingsHandlerConfig::OUTPUT_PRESSURE_SP_INDEX); + Event event = {EventType::OUTPUT_PRESSURE_SP_INDEX, outputPressureSPIndex}; + eventBus.publish(event); } -void SettingsHandler::pullInputVarioxSPIndex() { +void SettingsHandler::pullInputSPIndex() { midas::odb o(this->path); - std::string equipmentName = o[ParameterIndex::INPUT_VARIOX_SP_INDEX]; - inputHandler.get().updateEquipmentName(equipmentName); + int inputSPIndex = o(SettingsHandlerConfig::INPUT_SP_INDEX); + Event event = {EventType::INPUT_SP_INDEX, inputSPIndex}; + eventBus.publish(event); } -void SettingsHandler::pullInputVarioxTemperatureIndex() { +void SettingsHandler::pullInputTemperatureIndex() { midas::odb o(this->path); - std::string equipmentName = - o[ParameterIndex::INPUT_VARIOX_TEMPERATURE_INDEX]; - inputHandler.get().updateEquipmentName(equipmentName); + int temperatureIndex = o(SettingsHandlerConfig::INPUT_TEMPERATURE_INDEX); + Event event = {EventType::INPUT_TEMPERATURE_INDEX, temperatureIndex}; + eventBus.publish(event); } -void SettingsHandler::pullInputVarioxPowerIndex() { +void SettingsHandler::pullInputPowerIndex() { midas::odb o(this->path); - std::string equipmentName = o[ParameterIndex::INPUT_VARIOX_POWER_INDEX]; - inputHandler.get().updateEquipmentName(equipmentName); + int powerIndex = o(SettingsHandlerConfig::INPUT_POWER_INDEX); + Event event = {EventType::INPUT_POWER_INDEX, powerIndex}; + eventBus.publish(event); +} + +SettingsHandler::SettingsHandler(EventBus &eventBusReference, + std::string equipmentName) + : path(SettingsHandlerConfig::PATH_PREFIX + equipmentName + + SettingsHandlerConfig::PATH_SUFFIX), + eventBus(eventBusReference) { + eventBus.subscribe( + [this](const itcPressureOptimizer::EquipmentInitEvent &e) { + this->init(); + }); } bool SettingsHandler::init() { @@ -37,21 +57,26 @@ bool SettingsHandler::init() { bool areKeysValids = true; - if (!o.exists(Configuration::EQUIPMENT_NAME)) { + if (!o.exists(SettingsHandlerConfig::EQUIPMENT_NAME)) { areKeysValids = false; } - if (!o.exists(Configuration::OUTPUT_PRESSURE_SP_INDEX)) { + if (!o.exists(SettingsHandlerConfig::OUTPUT_PRESSURE_SP_INDEX)) { areKeysValids = false; } - if (!o.exists(Configuration::EQUIPMINPUT_VARIOX_SP_INDEXENT_NAME)) { + if (!o.exists(SettingsHandlerConfig::INPUT_SP_INDEX)) { areKeysValids = false; } - if (!o.exists(Configuration::INPUT_VARIOX_TEMPERATURE_INDEX)) { + if (!o.exists(SettingsHandlerConfig::INPUT_TEMPERATURE_INDEX)) { areKeysValids = false; } - if (!o.exists(Configuration::INPUT_VARIOX_POWER_INDEX)) { + if (!o.exists(SettingsHandlerConfig::INPUT_POWER_INDEX)) { areKeysValids = false; } -} -bool SettingsHandler::setHotlink() { return false; } + this->pullEquipmentName(); + this->pullOutputPressureSPIndex(); + this->pullInputSPIndex(); + this->pullInputTemperatureIndex(); + this->pullInputPowerIndex(); + return true; +} diff --git a/src/device/SettingsHandler.h b/src/device/SettingsHandler.h index 78b0e4e..cc51847 100644 --- a/src/device/SettingsHandler.h +++ b/src/device/SettingsHandler.h @@ -4,21 +4,37 @@ #include "InputHandler.h" #include #include +#include class SettingsHandler { + + public: + SettingsHandler(EventBus &eventBusReference, std::string equipmentName); + bool init(); + bool setHotlink(); + + enum class EventType { + EQUIPMENT_NAME, + OUTPUT_PRESSURE_SP_INDEX, + INPUT_SP_INDEX, + INPUT_TEMPERATURE_INDEX, + INPUT_POWER_INDEX + }; + + struct Event { + EventType type; + std::variant value; + }; + private: std::string path; - std::reference_wrapper inputHandler; + EventBus &eventBus; void pullEquipmentName(); void pullOutputPressureSPIndex(); - void pullInputVarioxSPIndex(); - void pullInputVarioxTemperatureIndex(); - void pullInputVarioxPowerIndex(); - - public: - bool init(); - bool setHotlink(); + void pullInputSPIndex(); + void pullInputTemperatureIndex(); + void pullInputPowerIndex(); }; #endif \ No newline at end of file diff --git a/src/device/SettingsHandlerConfig.h b/src/device/SettingsHandlerConfig.h index 2960b14..dc2cb8a 100644 --- a/src/device/SettingsHandlerConfig.h +++ b/src/device/SettingsHandlerConfig.h @@ -3,17 +3,17 @@ #include -namespace Configuration { +namespace SettingsHandlerConfig { const std::string PATH_PREFIX = "/Equipment/"; const std::string PATH_SUFFIX = "/Devices/MITCPRESSC/DD/"; const std::string EQUIPMENT_NAME = "MITC_equipment"; const std::string OUTPUT_PRESSURE_SP_INDEX = "MITC_OutputPressSPIndex"; -const std::string INPUT_VARIOX_SP_INDEX = "MITC_InputVarioxSPIndex"; -const std::string INPUT_VARIOX_TEMPERATURE_INDEX = "MITC_InputVarioxTempIndex"; -const std::string INPUT_VARIOX_POWER_INDEX = "MITC_InputVarioxPowIndex"; +const std::string INPUT_SP_INDEX = "MITC_InputVarioxSPIndex"; +const std::string INPUT_TEMPERATURE_INDEX = "MITC_InputVarioxTempIndex"; +const std::string INPUT_POWER_INDEX = "MITC_InputVarioxPowIndex"; -} // namespace Configuration +} // namespace SettingsHandlerConfig #endif \ No newline at end of file diff --git a/src/device/itc_pressure_optimizer.cpp b/src/device/itc_pressure_optimizer.cpp index eac4626..40a7304 100644 --- a/src/device/itc_pressure_optimizer.cpp +++ b/src/device/itc_pressure_optimizer.cpp @@ -13,14 +13,15 @@ itcPressureOptimizer::itcPressureOptimizer(std::string equipmentName, const char *equipmentFilename) : TMFeEquipment(equipmentName.c_str(), equipmentFilename), eventBus(), - demandHandler(eventBus, equipmentName) { + demandHandler(eventBus, equipmentName), + settingsHandler(eventBus, equipmentName), inputHandler(eventBus) { fEqConfPeriodMilliSec = 1000; // refresh every seconds fEqConfLogHistory = 1; fEqConfReadOnlyWhenRunning = false; fEqConfWriteEventsToOdb = true; } -/*, feedbackHandler(), inputHandler(), +/*, feedbackHandler(), , outputHandler(), pressureCalculator(), settingsHandler()*/ diff --git a/src/device/itc_pressure_optimizer.h b/src/device/itc_pressure_optimizer.h index 41ae383..cc86a88 100644 --- a/src/device/itc_pressure_optimizer.h +++ b/src/device/itc_pressure_optimizer.h @@ -3,11 +3,11 @@ #include "DemandHandler.h" #include "EventBus.h" +#include "InputHandler.h" +#include "SettingsHandler.h" // #include "FeedbackHandler.h" -// #include "InputHandler.h" // #include "OutputHandler.h" // #include "PressureCalculator.h" -// #include "SettingsHandler.h" #include "tmfe.h" #include @@ -38,11 +38,11 @@ class itcPressureOptimizer : public TMFeEquipment { private: EventBus eventBus; DemandHandler demandHandler; + SettingsHandler settingsHandler; + InputHandler inputHandler; // FeedbackHandler feedbackHandler; - // InputHandler inputHandler; // OutputHandler outputHandler; // PressureCalculator pressureCalculator; - // SettingsHandler settingsHandler; }; #endif \ No newline at end of file