diff --git a/doc/graph.puml b/doc/graph.puml new file mode 100644 index 0000000..9056964 --- /dev/null +++ b/doc/graph.puml @@ -0,0 +1,98 @@ +@startuml ITCPressureOptimizer + +class OdbWrited{ + +} + +class OdbReader{ + +} + +class InputHandler { + equipmentName : string + spindex : int + tempindex : int + powIndex : int + + setEquipmentName(name : string) + setSPIndex(index : int) + setTemperatureIndex(index : int) + setPowerIndex(index : int) +} + +class SettingsHandler { + +} + +class PressureCalculator { + # From InputHandler + cachedSP : float + cachedTemperature : float + cachedPower : float + + # From DemandHandler + cachedMinimalPressure : float + cachedMaximalPressure : float + cachedConstante1 : float + cachedConstante2 : float + + trigger() : void + + updateSP(value : float) + updateTemperature(value : float) + updatePower(value : float) + updateMinimalPressure(value : float) + updateMaximalPressure(value : float) + updateConstante1(value : float) + updateConstante2(value : float) +} + +class Average { + addValue(float : value) + getAverage() : float +} + +class DemandHandler { + +} + +class FeedbackHandler { + bool isPressureControlModeEnable + enablePressureControlMode(enable : bool) + + setSP(value : float) + setTemperature(value : float) + setPower(value : float) + setAveragePower(value : float) + setPressure(value : float) + setConstante1(value : float) + setConstante2(value : float) +} + +class OutputHandler { + bool isSetPressureEnable + float pressure + enableSetPressure(enable : bool) + setPressure(value : float) +} + +circle ODB_Settings +circle ODB_Equipment_Input +circle ODB_Demand + +ODB_Settings ..> SettingsHandler : Hotlink +ODB_Equipment_Input ..> InputHandler : Hotlink +ODB_Demand ..> DemandHandler : Hotlink + + +SettingsHandler --> InputHandler +InputHandler --> PressureCalculator +DemandHandler --> PressureCalculator + +PressureCalculator --> Average +PressureCalculator --> OutputHandler + +InputHandler --> FeedbackHandler +DemandHandler --> FeedbackHandler + +@enduml \ No newline at end of file diff --git a/out/doc/graph/ITCPressureOptimizer.png b/out/doc/graph/ITCPressureOptimizer.png new file mode 100644 index 0000000..35a06e2 Binary files /dev/null and b/out/doc/graph/ITCPressureOptimizer.png differ diff --git a/src/device/Average.cpp b/src/device/Average.cpp new file mode 100644 index 0000000..9617bca --- /dev/null +++ b/src/device/Average.cpp @@ -0,0 +1,71 @@ +#include "Average.h" +#include +#include + +void Average::setWindowSize(double seconds) { + windowSize = Duration(seconds); + cleanupAndUpdate(Clock::now()); +} + +void Average::addValue(double value) { + TimePoint now = Clock::now(); + cleanupAndUpdate(now); + + currentValue = value; + history.push_back({now, currentValue}); +} + +double Average::getAverage() { + TimePoint now = Clock::now(); + cleanupAndUpdate(now); + + if (history.empty()) { + return currentValue; + } + + // If only one element, it's all the window + if (history.size() == 1) { + return history.front().value; + } + + double totalWeightedValue = 0.0; + double totalDurationSeconds = 0.0; + + // Go throught the full historic + for (size_t i = 0; i < history.size() - 1; ++i) { + // How much time the value was present + Duration duration = history[i + 1].timestamp - history[i].timestamp; + + totalWeightedValue += history[i].value * duration.count(); + totalDurationSeconds += duration.count(); + } + + // add last segment : duration = now - timestamp + Duration lastDuration = now - history.back().timestamp; + totalWeightedValue += history.back().value * lastDuration.count(); + totalDurationSeconds += lastDuration.count(); + + /* + Safety to prevent dividing by 0 + This can occure if only one point is set and we compute the average too + fast + */ + if (totalDurationSeconds <= 0.0) { + return history.back().value; + } + + return totalWeightedValue / totalDurationSeconds; +} + +void Average::cleanupAndUpdate(TimePoint now) { + // Clean all element completly ouside the temporal window + while (history.size() > 1 && (now - history[1].timestamp) >= windowSize) { + history.pop_front(); + } + + // If last element is partialy outside, get his timestamp back + if (!history.empty() && (now - history.front().timestamp) > windowSize) { + history.front().timestamp = + now - std::chrono::duration_cast(windowSize); + } +} \ No newline at end of file diff --git a/src/device/Average.h b/src/device/Average.h new file mode 100644 index 0000000..3e76bbf --- /dev/null +++ b/src/device/Average.h @@ -0,0 +1,28 @@ +#include +#include + +class Average { + private: + using Clock = std::chrono::steady_clock; + using TimePoint = Clock::time_point; + using Duration = std::chrono::duration; + + struct TimeValue { + TimePoint timestamp; + double value; + }; + + std::deque history; + Duration windowSize; + double currentValue = 0.0; + + void cleanupAndUpdate(TimePoint now); + + public: + Average(double windowSizeSeconds) + : windowSize(Duration(windowSizeSeconds)) {}; + + void setWindowSize(double seconds); + void addValue(double value); + double getAverage(); +}; diff --git a/src/device/DemandHandler.cpp b/src/device/DemandHandler.cpp new file mode 100644 index 0000000..96e6b7a --- /dev/null +++ b/src/device/DemandHandler.cpp @@ -0,0 +1,106 @@ +#include "DemandHandler.h" +#include "DemandHandlerConfig.h" +#include +#include + +bool DemandHandler::init() { + midas::odb o(this->path); + + if (!o.exists()) { + // TODO : ERROR MESSAGE + return; + } + + bool indexValid = true; + + if (o.size() < ParameterIndex::MINIMAL_PRESSURE) { + indexValid = false; + } + if (o.size() < ParameterIndex::MAXIMAL_PRESSURE) { + indexValid = false; + } + if (o.size() < ParameterIndex::CONSTANTE_1) { + indexValid = false; + } + if (o.size() < ParameterIndex::CONSTANTE_2) { + indexValid = false; + } + + if (indexValid) + return; + + pullMinimalPressure(); + pullMaximalPressure(); + pullConstante1(); + pullConstante2(); + pullPressureControlMode(); +} + +bool DemandHandler::setHotlink() { + midas::odb to_watch(this->path); + to_watch([&](midas::odb &arg) { + switch (arg.get_last_index()) { + case ParameterIndex::MINIMAL_PRESSURE: + pullMinimalPressure(); + break; + case ParameterIndex::MAXIMAL_PRESSURE: + pullMaximalPressure(); + break; + case ParameterIndex::MINIMAL_PRESSURE: + pullConstante1(); + break; + case ParameterIndex::MINIMAL_PRESSURE: + pullConstante2(); + break; + case ParameterIndex::PRESSURE_CONTROL_MODE: + pullPressureControlMode(); + break; + default: + // Update out of scope, discarded + break; + } + }); +} + +void DemandHandler::pullMinimalPressure() { + midas::odb o(this->path); + float minimalPressure = o[ParameterIndex::MINIMAL_PRESSURE]; + pressureCalculator.get().updateMinimalPressure(minimalPressure); +} + +void DemandHandler::pullMaximalPressure() { + midas::odb o(this->path); + float maximalPressure = o[ParameterIndex::MAXIMAL_PRESSURE]; + pressureCalculator.get().updateMinimalPressure(maximalPressure); +} + +void DemandHandler::pullConstante1() { + midas::odb o(this->path); + float constante1 = o[ParameterIndex::CONSTANTE_1]; + pressureCalculator.get().updateMinimalPressure(constante1); + feedbackHandler.get().setConstante1(constante1); +} + +void DemandHandler::pullConstante2() { + midas::odb o(this->path); + float constance2 = o[ParameterIndex::CONSTANTE_2]; + pressureCalculator.get().updateMinimalPressure(constance2); + feedbackHandler.get().setConstante2(constante2); +} + +void DemandHandler::pullPressureControlMode() { + midas::odb o(this->path); + float pressureControlMode = o[ParameterIndex::PRESSURE_CONTROL_MODE]; + feedbackHandler.get().enablePressureControlMode(pressureControlMode); +} + +DemandHandler::DemandHandler( + std::string equipmentName, + std::reference_wrapper feedbackHandler, + std::reference_wrapper pressureCalculator) { + + this->path = DemanHandlerConfig::PATH_PREFIX + equipmentName + + DemanHandlerConfig::PATH_SUFFIX; + this->feedbackHandler = feedbackHandler; + this->pressureCalculator = pressureCalculator; +} diff --git a/src/device/DemandHandler.h b/src/device/DemandHandler.h new file mode 100644 index 0000000..9b8ca25 --- /dev/null +++ b/src/device/DemandHandler.h @@ -0,0 +1,37 @@ +#ifndef DEMAND_HANDLER_H +#define DEMAND_HANDLER_H + +#include "FeedbackHandler.h" +#include "PressureCalculator.h" +#include +#include + +class DemandHandler { + private: + std::string path; + std::reference_wrapper feedbackHandler; + std::reference_wrapper pressureCalculator; + + void pullMinimalPressure(); + void pullMaximalPressure(); + void pullConstante1(); + void pullConstante2(); + void pullPressureControlMode(); + + public: + DemandHandler( + std::string equipmentName, + std::reference_wrapper feedbackHandler, + std::reference_wrapper pressureCalculator); + /* + @brief Manualy get all the value, and update owned objects + */ + void init(); + /* + @brief Set all the hotlink, waiting for any update + @return true if succes, false if any error encountered + */ + bool setHotlink(); +}; + +#endif \ No newline at end of file diff --git a/src/device/DemandHandlerConfig.h b/src/device/DemandHandlerConfig.h new file mode 100644 index 0000000..1bdf8ff --- /dev/null +++ b/src/device/DemandHandlerConfig.h @@ -0,0 +1,20 @@ +#ifndef DEMAND_HANDLER_CONFIG_H +#define DEMAND_HANDLER_CONFIG_H + +#include + +namespace DemanHandlerConfig { +const std::string PATH_PREFIX = "/Equipment/"; +const std::string PATH_SUFFIX = "/Demand"; + +class enum ParameterIndex { + PRESSURE_CONTROL_MODE = 6, + MINIMAL_PRESSURE = 8, + MAXIMAL_PRESSURE = 9, + CONSTANTE_1 = 10, + CONSTANTE_2 = 11 +} + +} // namespace DemanHandlerConfig + +#endif diff --git a/src/device/FeedbackHandler.cpp b/src/device/FeedbackHandler.cpp new file mode 100644 index 0000000..c6b1865 --- /dev/null +++ b/src/device/FeedbackHandler.cpp @@ -0,0 +1,31 @@ +#include "FeedbackHandler.h" +#include + +FeedbackHandler::setValueToOdb(int index, float value) { + // TODO Check validity + midas::odb o(this->odbPath); + o[outputKeyName.c_str()][index] = value; +} + +FeedbackHandler::FeedbackHandler(std::string equipmentName) { + this->equipmentName = equipmentName; + this->odbPath = "/Equipment/" + equipmentName + "/Variables"; +} + +void FeedbackHandler::enablePressureControlMode(bool enable) { + this->isPressureControlModeEnable = enable; +} + +void FeedbackHandler::setSP(float value) {} + +void FeedbackHandler::setTemperature(float value) {} + +void FeedbackHandler::setPower(float value) {} + +void FeedbackHandler::setAveragePower(float value) {} + +void FeedbackHandler::setPressure(float value) {} + +void FeedbackHandler::setConstante1(float value) {} + +void FeedbackHandler::setConstante2(float value) {} diff --git a/src/device/FeedbackHandler.h b/src/device/FeedbackHandler.h new file mode 100644 index 0000000..1e11f22 --- /dev/null +++ b/src/device/FeedbackHandler.h @@ -0,0 +1,34 @@ +#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 + }; + + 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); +}; diff --git a/src/device/InputHandler.cpp b/src/device/InputHandler.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/device/InputHandler.h b/src/device/InputHandler.h new file mode 100644 index 0000000..fe2b064 --- /dev/null +++ b/src/device/InputHandler.h @@ -0,0 +1,15 @@ +#include + +class InputHandler { + private: + std::string equipmentName; + float cachedSP; + float cachedTemperature; + float cachedPower; + + public: + void updateEquipmentName(std::string name); + void updateSPIndex(int index); + void updateTemperatureIndex(int index); + void updatePowerIndex(int index); +}; diff --git a/src/device/OutputHandler.cpp b/src/device/OutputHandler.cpp new file mode 100644 index 0000000..4efe0b0 --- /dev/null +++ b/src/device/OutputHandler.cpp @@ -0,0 +1,7 @@ +#include "OutputHandler.h" + +void OutputHandler::enableSetPressure(bool enable) { + isSetPressureEnable = enable; +} + +void OutputHandler::setPressure(float value) {} diff --git a/src/device/OutputHandler.h b/src/device/OutputHandler.h new file mode 100644 index 0000000..6474b45 --- /dev/null +++ b/src/device/OutputHandler.h @@ -0,0 +1,11 @@ + + +class OutputHandler { + private: + bool isSetPressureEnable; + float pressure; + + public: + void enableSetPressure(bool enable); + void setPressure(float value); +}; diff --git a/src/device/PressureCalculator.cpp b/src/device/PressureCalculator.cpp new file mode 100644 index 0000000..7c171ab --- /dev/null +++ b/src/device/PressureCalculator.cpp @@ -0,0 +1,46 @@ +#include "PressureCalculator.h" +#include + +void PressureCalculator::update() { + double c2 = average.getAverage() * cachedConstante2; + double c1 = cachedConstante1 * (cachedTemperature - cachedSP - c2); + double uncapedPressure = cachedMinimalPressure + c1; + + if (c2 != 0) { + } +} + +void PressureCalculator::updateSP(float value) { + cachedSP = value; + update(); +} + +void PressureCalculator::updateTemperature(float value) { + cachedTemperature = value; + update(); +} + +void PressureCalculator::updatePower(float value) { + cachedPower = value; + update(); +} + +void PressureCalculator::updateMinimalPressure(float value) { + cachedMinimalPressure = value; + update(); +} + +void PressureCalculator::updateMaximalPressure(float value) { + cachedMaximalPressure = value; + update(); +} + +void PressureCalculator::updateConstante1(float value) { + cachedConstante1 = value; + update(); +} + +void PressureCalculator::updateConstante2(float value) { + cachedConstante2 = value; + update(); +} diff --git a/src/device/PressureCalculator.h b/src/device/PressureCalculator.h new file mode 100644 index 0000000..f53b128 --- /dev/null +++ b/src/device/PressureCalculator.h @@ -0,0 +1,27 @@ +#include "Average.h" + +class PressureCalculator { + private: + Average average; + OutputHandler outputHandler; + float cachedSP; + float cachedTemperature; + float cachedPower; + + float cachedMinimalPressure; + float cachedMaximalPressure; + float cachedConstante1; + float cachedConstante2; + + public: + void update(); + + void updateSP(float value); + void updateTemperature(float value); + void updatePower(float value); + + void updateMinimalPressure(float value); + void updateMaximalPressure(float value); + void updateConstante1(float value); + void updateConstante2(float value); +}; diff --git a/src/device/SettingsHandler.cpp b/src/device/SettingsHandler.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/device/SettingsHandler.h b/src/device/SettingsHandler.h new file mode 100644 index 0000000..8976ee6 --- /dev/null +++ b/src/device/SettingsHandler.h @@ -0,0 +1,6 @@ + + +class SettingsHandler { + private: + public: +}; diff --git a/src/device/itc_pressure_optimizer.cpp b/src/device/itc_pressure_optimizer.cpp deleted file mode 100644 index ed0df3a..0000000 --- a/src/device/itc_pressure_optimizer.cpp +++ /dev/null @@ -1,833 +0,0 @@ -#include "itc_pressure_optimizer.h" -#include "itc_pressure_optimizer_config.h" -#include "itc_pressure_optimizer_info.h" - -#include "midas.h" -#include "odbxx.h" -#include "tmfe.h" - -#include -#include -#include - -itcPressureOptimizer::itcPressureOptimizer(std::string equipmentName, - const char *equipmentFilename, - int channel) - : TMFeEquipment(equipmentName.c_str(), equipmentFilename) { - fEqConfReadOnlyWhenRunning = false; - fEqConfPeriodMilliSec = 100; - this->equipmentPath = std::string("/Equipment/") + equipmentName; - info.num_channels = channel; -} - -TMFeResult -itcPressureOptimizer::HandleInit(const std::vector &args) { - this->mitc_pressc_init(); - return TMFeResult(); -} - -void itcPressureOptimizer::HandlePeriodic() { - midas::odb o(this->equipmentPath + "/" + Configuration::VARIABLE_DIR); - - for (int i = 0; i < info.num_channels; i++) { - info.last_measured[i] = o[Configuration::MEASURED_VARNAME.c_str()][i]; - info.last_demand[i] = o[Configuration::DEMAND_VARNAME.c_str()][i]; - } - - this->mitc_pressc_update_mitc_input(); - this->mitc_pressc_update_pressure_demanded(); - this->mitc_pressc_recalculate(); - - for (int i = 0; i < info.num_channels; i++) { - o[Configuration::MEASURED_VARNAME.c_str()][i] = info.last_measured[i]; - o[Configuration::DEMAND_VARNAME.c_str()][i] = info.last_demand[i]; - } -} - -INT itcPressureOptimizer::mitc_pressc_init() { - int status = SUCCESS; - bool isDefaultNameExists; - bool isDefaultThresholdExists; - bool isDemandExists; - bool isMeasuredExists; - isDefaultNameExists = midas::odb::exists(this->equipmentPath + "/" + - Configuration::SETTINGS_DIR + "/" + - Configuration::NAMES_VARNAME); - - isDefaultThresholdExists = midas::odb::exists( - this->equipmentPath + "/" + Configuration::SETTINGS_DIR + "/" + - Configuration::UPDATE_THRESHOLD_MEASURED_VARNAME); - - isDemandExists = midas::odb::exists(this->equipmentPath + "/" + - Configuration::DEMANDE_PATH); - - isMeasuredExists = midas::odb::exists(this->equipmentPath + "/" + - Configuration::MEASURED_PATH); - - if (!isDefaultNameExists || !isDefaultThresholdExists) { - midas::odb o = { - {Configuration::VARIABLE_DIR.c_str(), - {{Configuration::DEMAND_VARNAME.c_str(), {0.0}}, - {Configuration::MEASURED_VARNAME.c_str(), {0.0}}}}, - {Configuration::SETTINGS_DIR.c_str(), - {{Configuration::NAMES_VARNAME.c_str(), {""}}, - {Configuration::UPDATE_THRESHOLD_MEASURED_VARNAME.c_str(), {0.0}}, - {Configuration::DEVICES_DIR.c_str(), - {{Configuration::MIT_DIR.c_str(), - {{Configuration::ENABLE_VARNAME.c_str(), {true}}, - {Configuration::DD_DIR.c_str(), - {{Configuration::MITC_EQUIPMENT_VARNAME.c_str(), {""}}, - {Configuration::MITC_OUTPUT_PRESS_SP_INDEX_VARNAME.c_str(), - {0}}, - {Configuration::MITC_INPUT_VARIOX_SP_INDEX_VARNAME.c_str(), - {0}}, - {Configuration::MITC_INPUT_VARIOX_TEMP_INDEX_VARNAME - .c_str(), - {0}}, - {Configuration::MITC_INPUT_VARIOX_POW_INDEX_VARNAME.c_str(), - {0}}, - {Configuration::RECALC_INTERVAL_VARNAME.c_str(), {0}}, - {Configuration::MITC_READOUT_INTERVAL_VARNAME.c_str(), {0}}, - {Configuration::PRESS_READOUT_INTERVAL_VARNAME.c_str(), - {0}}}}}}}}}}}; - - o.connect(this->equipmentPath.c_str()); - } - - if (!isDefaultNameExists) { - midas::odb o(this->equipmentPath + "/" + Configuration::SETTINGS_DIR); - std::vector nameLists = - o[Configuration::NAMES_VARNAME.c_str()]; - nameLists.resize(info.num_channels); - for (int i = 0; i < info.num_channels; i++) { - std::string res = this->getDefaultName(i); - nameLists.at(i) = res; - printf("%s\n", res.c_str()); - } - o[Configuration::NAMES_VARNAME.c_str()] = nameLists; - } - - if (!isDefaultThresholdExists) { - midas::odb o(this->equipmentPath + "/" + Configuration::SETTINGS_DIR); - o[Configuration::UPDATE_THRESHOLD_MEASURED_VARNAME.c_str()].resize( - info.num_channels); - for (int i = 0; i < info.num_channels; i++) { - o[Configuration::UPDATE_THRESHOLD_MEASURED_VARNAME.c_str()][i] = - this->getDefaultThreshold(i); - } - } - - if (!isDemandExists) { - midas::odb o(this->equipmentPath + "/" + Configuration::VARIABLE_DIR); - std::vector demandLists = - o[Configuration::DEMAND_VARNAME.c_str()]; - demandLists.resize(info.num_channels); - o[Configuration::DEMAND_VARNAME.c_str()] = demandLists; - } - - if (!isMeasuredExists) { - midas::odb o(this->equipmentPath + "/" + Configuration::VARIABLE_DIR); - std::vector measuredLists = - o[Configuration::MEASURED_VARNAME.c_str()]; - measuredLists.resize(info.num_channels); - o[Configuration::MEASURED_VARNAME.c_str()] = measuredLists; - } - - /* - Caching all the value of /DD folder - */ - { - midas::odb o(this->equipmentPath.c_str()); - info.mitcpressc_settings.mitcequipment = - o[Configuration::MITC_EQUIPMENT_PATH.c_str()]; - info.mitcpressc_settings.setpressindex = - o[Configuration::MITC_OUTPUT_PRESS_SP_INDEX_PATH.c_str()]; - info.mitcpressc_settings.getvarioxspindex = - o[Configuration::MITC_INPUT_VARIOX_SP_INDEX_PATH.c_str()]; - info.mitcpressc_settings.getvarioxtempindex = - o[Configuration::MITC_INPUT_VARIOX_TEMP_INDEX_PATH.c_str()]; - info.mitcpressc_settings.getvarioxpowindex = - o[Configuration::MITC_INPUT_VARIOX_POW_INDEX_PATH.c_str()]; - info.mitcpressc_settings.recalcinterval = - o[Configuration::RECALC_INTERVAL_PATH.c_str()]; - info.mitcpressc_settings.mitcinterval = - o[Configuration::MITC_READOUT_INTERVAL_PATH.c_str()]; - info.mitcpressc_settings.pressinterval = - o[Configuration::PRESS_READOUT_INTERVAL_PATH.c_str()]; - } - - info.mitcPath = Configuration::SLASH + Configuration::EQUIPMENT_DIR + - Configuration::SLASH + - info.mitcpressc_settings.mitcequipment + - Configuration::SLASH + Configuration::VARIABLE_DIR; - - info.last_demand.resize(info.num_channels); - info.last_demand_set.resize(info.num_channels); - info.last_measured.resize(info.num_channels); - - // set invalid values to avoid calculation before init - for (int i = 0; i < info.num_channels; i++) { - info.last_measured.at(i) = -2.f; - } - - info.lastlog = 0; - info.last_press_mitc_output = 0; - info.recalculate = TRUE; - info.pending = TRUE; - info.press_mitc_output = -1.0f; - info.nvals_mitc_input = 0; - info.nvals_mitc_output = 0; - - info.ihis = 0; - - for (int i = 0; i < MAX_HIS; i++) { - info.history[i] = -1.f; // invalidate history - info.history_time[i] = 0; // no value archived yet - } - - // Check equipment Variables Input & Index - if (!(info.mitcpressc_settings.mitcequipment == "NONE") && - !info.mitcpressc_settings.mitcequipment.empty()) { - std::string mitc_input_path = std::string("/Equipment/") + - info.mitcpressc_settings.mitcequipment + - "/" + Configuration::VARIABLE_DIR; - midas::odb o(mitc_input_path.c_str()); - - // INPUTS - info.nvals_mitc_input = o[Configuration::INPUT_VARNAME.c_str()].size(); - - if (info.nvals_mitc_input > 0) { - info.mitc_input.resize(info.nvals_mitc_input); - cm_msg( - MLOG, "", - "Reading Mercury ITC Variables/Input values for Equipment %s", - info.mitcpressc_settings.mitcequipment.c_str()); - // check if index < nvals - if ((info.mitcpressc_settings.getvarioxspindex < 0) || - (info.mitcpressc_settings.getvarioxspindex >= - info.nvals_mitc_input)) { - status = CM_SET_ERROR; - cm_msg(MERROR, "mitc_pressc_init", - "ERROR array index %d to read temperature" - " setpoint is invalid", - info.mitcpressc_settings.getvarioxspindex); - } else { - cm_msg( - MLOG, "", - "Input Index to read Temperature SP of Equipment %s is %d", - info.mitcpressc_settings.mitcequipment.c_str(), - info.mitcpressc_settings.getvarioxspindex); - } - // check if index < nvals - if ((info.mitcpressc_settings.getvarioxtempindex < 0) || - (info.mitcpressc_settings.getvarioxtempindex >= - info.nvals_mitc_input)) { - status = CM_SET_ERROR; - cm_msg(MERROR, "mitc_pressc_init", - "ERROR array index %d to read temperature" - " is invalid", - info.mitcpressc_settings.getvarioxtempindex); - } else { - cm_msg(MLOG, "", - "Input Index to read Temperature of Equipment %s is %d", - info.mitcpressc_settings.mitcequipment.c_str(), - info.mitcpressc_settings.getvarioxtempindex); - } - // check if index < nvals - if ((info.mitcpressc_settings.getvarioxpowindex < 0) || - (info.mitcpressc_settings.getvarioxpowindex >= - info.nvals_mitc_input)) { - status = CM_SET_ERROR; - cm_msg(MERROR, "mitc_pressc_init", - "ERROR array index %d to read heater pow" - "er is invalid", - info.mitcpressc_settings.getvarioxpowindex); - } else { - cm_msg(MLOG, "", - "Input Index to read heater power of Equipment %s is %d", - info.mitcpressc_settings.mitcequipment.c_str(), - info.mitcpressc_settings.getvarioxpowindex); - } - } else { - cm_msg(MERROR, "mitc_pressc_init", - "ERROR Invalid number of channels %d for " - "Mercury ITC Variables/Input!", - info.nvals_mitc_input); - } - - // OUTPUTS - info.nvals_mitc_output = - o[Configuration::OUTPUT_VARNAME.c_str()].size(); - if (info.nvals_mitc_output > 0) { - // check if index < nvals - if ((info.mitcpressc_settings.setpressindex < 0) || - (info.mitcpressc_settings.setpressindex >= - info.nvals_mitc_output)) { - status = CM_SET_ERROR; - cm_msg(MERROR, "mitc_pressc_init", - "ERROR array index %d to read/set pres" - "sure setpoint is invalid", - info.mitcpressc_settings.setpressindex); - } else { - cm_msg(MLOG, "", - "Output Index to read/set Pressure SP of Equipment %s " - "is %d", - info.mitcpressc_settings.mitcequipment.c_str(), - info.mitcpressc_settings.setpressindex); - } - } else { - cm_msg(MERROR, "mitc_pressc_init", - "ERROR Invalid number of channels %d for " - "measured pressure!", - info.nvals_mitc_output); - status = DB_NO_KEY; - } - - } else { - cm_msg(MLOG, "", - "Equipment Name to Set/Get Mercury ITC Variables is not set-up " - "in MITCPRESSC/DD"); - } - - // FAILED PATH - - if (status != SUCCESS) { - cm_msg(MLOG, "", "mitc_pressc_init : ERROR initialising device"); - exit(EXIT_FAILURE); - } - printf("hey \n"); - - // SUCCESS PATH - // Init value - for (int i = 0; i < info.num_channels; i++) { - info.last_demand[i] = -1.f; - info.last_measured[i] = -2.f; - } - // NOTE: calculation will not be performed until info.initialized is TRUE - // this is done in mitc_pressc_set() when the last parameter is - // initialized - - /* - Hot link - */ - - midas::odb to_watch(this->equipmentPath + "/" + - Configuration::VARIABLE_DIR + "/" + - Configuration::DEMAND_VARNAME); - - to_watch.watch([&](midas::odb &arg) { - midas::odb o(this->equipmentPath + "/" + Configuration::VARIABLE_DIR); - int index = arg.get_last_index(); - float value = o[Configuration::DEMAND_VARNAME][arg.get_last_index()]; - this->mitc_pressc_set(index, value); - }); - - return FE_SUCCESS; -} - -INT itcPressureOptimizer::mitc_pressc_exit() { - return DB_SUCCESS; - // Nothing to do, since we do not manage memory with vector. All is drop - // when out of scope -} - -INT itcPressureOptimizer::mitc_pressc_update_mitc_input() { - if (info.last_mitc_input > ss_time()) // system time reset? - info.last_mitc_input = - ss_time() - info.mitcpressc_settings.mitcinterval + 1; - - // Time to read ? - // TODO : (info.hkey_mitc_input != 0) && (info.hkey_mitc_output != 0) - // condition is not checked, May need to add another check if necessary - - bool timeoutThreshold = - static_cast((ss_time() - info.last_mitc_input)) > - info.mitcpressc_settings.mitcinterval; - if (!timeoutThreshold) { - return FE_SUCCESS; - } - - float tpow; - { - std::string mitc_input_path = std::string("/Equipment/") + - info.mitcpressc_settings.mitcequipment + - "/" + Configuration::VARIABLE_DIR; - midas::odb o(mitc_input_path.c_str()); - - info.mitc_input = o[Configuration::INPUT_VARNAME.c_str()]; - } - - // TODO : Understand what this piece of code is used, and does the float - // condition correct ... "!=" is suspect - - float sum, avg; - int i, nvals; - - DWORD curtime; - - // measured MITCPRESSC_MercuryITC Loop0 Temperature Setpoint 1 - printf("update temps\n"); - if ((info.mitc_input[info.mitcpressc_settings.getvarioxspindex]) != -1) { - printf("last measured\n"); - printf("index %d value %f\n", info.mitcpressc_settings.getvarioxspindex, - info.mitc_input[info.mitcpressc_settings.getvarioxspindex]); - if (info.last_measured[1] != - info.mitc_input[info.mitcpressc_settings.getvarioxspindex]) { - - info.last_measured[1] = - info.mitc_input[info.mitcpressc_settings.getvarioxspindex]; - info.recalculate = TRUE; - } - } - - // measured MITCPRESSC_Mercury ITCTemperat(ure) 2 - if (info.mitcpressc_settings.getvarioxtempindex != -1) { - if (info.last_measured[2] != - (info.mitc_input[info.mitcpressc_settings.getvarioxtempindex])) { - info.last_measured[2] = - info.mitc_input[info.mitcpressc_settings.getvarioxtempindex]; - info.recalculate = TRUE; - } - } - - // measured MITCPRESSC_MercuryITC Loop0 Power 3 - // here in Watt - if (info.mitcpressc_settings.getvarioxpowindex != -1) { - tpow = info.mitc_input[info.mitcpressc_settings.getvarioxpowindex]; - } else { - tpow = -1.f; // invalid - should not occur - } - if (tpow < 0.f) { - tpow = -1.f; // invalid - should not occur - } else { - // archive current value - info.history[info.ihis] = tpow; - info.history_time[info.ihis] = ss_time(); - info.ihis += 1; - if (info.ihis >= MAX_HIS) - info.ihis = 0; // ring buffer - - // calculate average power - curtime = ss_time(); - } - - info.last_measured[3] = tpow; - - // sum valid power readings read in the last 30 secs - for (sum = 0.0, nvals = 0, i = 0; i < MAX_HIS; i++) { - // valid power? - if (info.history[i] >= 0.f) { - // measured in the last 30 secs - if (info.history_time[i] + 30 > curtime) { - sum += info.history[i]; - nvals++; - } - } - } - // calculate average - if (nvals > 0) - avg = sum / (float)nvals; - else - avg = -1.f; - - if (info.last_measured[4] != avg) { - // measured MITCPRESSC_AveragedPower 4 - info.last_measured[4] = avg; - info.recalculate = TRUE; - } - - // TODO : in case of error accessing the ODB, this should be executed - - // if (*(info.last_measured + 1) != -1.f) { - // cm_msg(MERROR, "mitc_pressc_update_mitc_input", - // "Error %d returned by " - // "db_get_data() reading Mercury ITC Measured from ODB!", - // status); - // *(info.last_measured + 1) = -1.f; - // *(info.last_measured + 2) = -1.f; - // *(info.last_measured + 3) = -1.f; - // *(info.last_measured + 4) = -1.f; - // } - - info.last_mitc_input = ss_time(); - - return FE_SUCCESS; -} - -INT itcPressureOptimizer::mitc_pressc_update_pressure_demanded() { - if (info.last_press_mitc_output > ss_time()) // time reset? - info.last_press_mitc_output = - ss_time() - info.mitcpressc_settings.pressinterval + 1; - - // time to read? - if (static_cast(ss_time() - info.last_press_mitc_output) > - info.mitcpressc_settings.pressinterval) { - - // read current demanded pressure from odb - - midas::odb o(info.mitcPath); - // TODO : Check if the variable is present to prevent crash - - info.press_mitc_output = o[Configuration::OUTPUT_VARNAME.c_str()] - [info.mitcpressc_settings.setpressindex]; - info.last_press_mitc_output = ss_time(); - } - return FE_SUCCESS; -} - -INT itcPressureOptimizer::mitc_pressc_recalculate() { - if (!info.initialised) { - printf("not initialised\n"); - return FE_SUCCESS; - } - - if (info.pending) { - info.recalculate = TRUE; - info.pending = FALSE; - } - - if (info.last_recalculated > ss_time()) - info.last_recalculated = - ss_time() - info.mitcpressc_settings.recalcinterval + 1; - - // recalculate flagged? or time to recalculate? - if (info.recalculate || - (static_cast(ss_time() - info.last_recalculated) > - info.mitcpressc_settings.recalcinterval)) { - - /* --- do pressure calculation --- */ - - if (info.last_measured[4] >= 0.f) { - // Calculate presure - printf("calculate pressure \n"); - - info.pending = FALSE; - - // Mercury ITC Temperature setpoint invalid ? - if (info.last_measured[1] < 0.f) - info.pending = TRUE; - // Mercury ITC Temperature measured invalid ? - if (info.last_measured[2] < .0f) - info.pending = TRUE; - - // COMMENT FROM ANDREAS - // NIY maybe check also validity of minpress, maxpress, c1, c2 - // in info.last_demand - - if (!info.pending) { - float press, c1contrib, c2contrib; - - // pressure calculation: - // p = pmin+c1*(Tmeas-Tset-avPower*c2) - // if (p < pmin) p = pmin - // if (p > pmax) p = pmax - - c2contrib = info.last_measured[4] * info.last_demand[11]; - c1contrib = - info.last_demand[10] * - (info.last_measured[2] - info.last_measured[1] - c2contrib); - press = info.last_demand[8] + c1contrib; - - info.last_measured[10] = c1contrib; - - if (c2contrib != 0.0f) - info.last_measured[11] = -c2contrib * info.last_demand[10]; - else - info.last_measured[11] = 0.0f; - - info.last_measured[8] = press; - info.last_measured[9] = press; - - // Pressure smaller than min? -> take min - if (press < info.last_demand[8]) - press = info.last_demand[8]; - - // Presure larger than max? -> take max - if (press > info.last_demand[9]) - press = info.last_demand[9]; - - // update measured of MITCPRESSC_CalcPressure 5 - info.last_measured[5] = press; - - // Pressure control mode is set to calculated? - if (info.last_demand[6] == 1.0f) { - float presst = std::roundf(press * 10.0f + 0.5f) / - 10.0f; // Round one decimal - - // only necessary to update when rounded value and demand - // value differ - // Check of hkey_demand is missing - if (fabsf(info.last_demand[0] - presst) >= 0.05f) { - midas::odb o(this->equipmentPath); - printf("%f is a new value\n", presst); - o[Configuration::DEMANDE_PATH.c_str()][0] = presst; - } - info.last_demand[0] = presst; - } - } - - /* NOT able to calculate pressure and calculated pressure expected - * as input */ - } else if (info.last_demand[6] == 1.0f) { - info.pending = TRUE; - info.last_measured[5] = -1.0f; - /* pressure calculation is not necessary as manual mode is set */ - } else if (info.last_demand[6] != 1.0f) { - info.pending = FALSE; - info.last_measured[5] = -1.0f; - } - - /* all conditions met to update setpoint of Mercury ITC in ODB? */ - if (!info.pending) { - info.last_recalculated = ss_time(); - - if (info.last_demand[7] == 1.0f) { - info.last_demand[0] = - std::roundf(info.last_demand[0] * 10.0f + 0.5f) / 10.0f; - info.last_measured[0] = info.last_demand[0]; - - /* is Mercury ITC already set? -> check readout*/ - if (info.press_mitc_output != info.last_demand[0]) { - // update Mercury ITC Demanded value - // NIY maybe check if info.hkey_mitc_output != 0 - - midas::odb o(info.mitcPath); - o[Configuration::OUTPUT_PATH.c_str()] - [info.mitcpressc_settings.setpressindex] = - info.last_demand[0]; - - info.last_mitc_input = ss_time(); - } - } - ss_sleep(100); - } - if (!info.recalculate) - ss_sleep(100); - - info.recalculate = FALSE; - } - return FE_SUCCESS; -} - -/* - _Pressure 0 D - _MercuryITCSetpoint 1 - _MercuryITCTemperat(ure) 2 - _MercuryITCPower 3 - _AveragedPower 4 - _CalcPressure 5 - _PressureControlMode 6 D - _SetRCPressure 7 D - _PressureCalc_minPressure 8 D - _PressureCalc_maxPressure 9 D - _PressureCalc_const1 10 D - _PressureCalc_const2 11 D - */ - -INT itcPressureOptimizer::mitc_pressc_set(INT channel, float value) { - switch (channel) { - case 0: // PRESSURE - only set pressure if not in calculated mode - if (info.last_demand[6] != 1.0f) { - if (info.last_demand[channel] != value) { - info.last_demand[channel] = value; - info.last_demand_set[channel] = ss_time(); - info.recalculate = TRUE; - } - } else { - if (std::abs(info.last_demand[channel] - value) > 0.2f) { - cm_msg(MLOG, "", - "PressureControlMode is set to calculated! " - "- Not setting pressure!"); - } - } - break; - case 6: // Pressure Control Mode : 1 = Calc(automatic), 0 = Man - if ((info.last_demand[channel] != 1.0f) && (value == 1.0f)) { - info.last_demand[channel] = 1.0f; - info.last_demand_set[channel] = ss_time(); - cm_msg(MLOG, "", "Pressure Control Mode is set to Calculated"); - - info.recalculate = TRUE; - } else if ((info.last_demand[channel] != 0.0f) && (value != 1.0f)) { - info.last_demand[channel] = 0.0f; - info.last_demand_set[channel] = ss_time(); - - cm_msg(MLOG, "", "Pressure Control Mode is set to Manual"); - - info.recalculate = TRUE; - } - break; - case 7: // set Pressure for Mercury ITC: 1 = Update Mercury ITC Pressure - // Setpoint - // 0 = do not - if ((info.last_demand[channel] != 1.0f) && (value == 1.0f)) { - info.last_demand[channel] = 1.0f; - info.last_demand_set[channel] = ss_time(); - - cm_msg(MLOG, "", - "MPC_SetPressure is enabled - Setting Mercury ITC " - "Pressure Setpoint in ODB"); - } else if ((info.last_demand[channel] != 0.0f) && (value != 1.0f)) { - info.last_demand[channel] = 0.0f; - info.last_demand_set[channel] = ss_time(); - cm_msg(MLOG, "", - "MPC_SetPressure is disabled - Not Setting Mercury ITC " - "Pressure Setpoint in ODB"); - - info.recalculate = TRUE; - } - break; - case 8: // min pressure - case 9: // max pressure - case 10: // pressure c1 - case 11: // pressure c2 - if (info.last_demand[channel] != value) { - - std::string name, name1; // TODO : change to std::string - - name = this->getDefaultName(channel); - if (info.initialised) - cm_msg(MLOG, "", "Channel %d (%s): output was %f now set to %f", - channel, name.c_str(), info.last_demand[channel], value); - else - cm_msg(MLOG, "", "%s (Channel %d): Output is set to %f", - name.c_str(), channel, value); - - if (channel == 9) { - if (info.last_demand[channel - 1] >= value) { - name1 = this->getDefaultName(channel); - - cm_msg(MERROR, "mitc_pressc_set", - "%s should be larger than %s (%f)", name.c_str(), - name1.c_str(), info.last_demand.at(channel - 1)); - } - } - info.last_demand[channel] = value; - info.last_demand_set[channel] = ss_time(); - info.recalculate = TRUE; - } - break; - } - - // all demand values set from ODB during init? - if ((channel == info.num_channels - 1) && !info.initialised) { - info.initialised = TRUE; // flag init done - info.recalculate = TRUE; // flag to recalculate - } - - mitc_pressc_recalculate(); - return FE_SUCCESS; -} - -float itcPressureOptimizer::getDemand(int channel) { - float pvalue; - if ((channel >= 0) && (channel < info.num_channels)) { - pvalue = info.last_demand.at(channel); - } else { - pvalue = -1.0f; - } - return pvalue; -} - -float itcPressureOptimizer::get(int channel) { - // update variox readout - mitc_pressc_update_mitc_input(); - - // update pressure demand readout - mitc_pressc_update_pressure_demanded(); - - // recalculate pressure and nv% - mitc_pressc_recalculate(); - - if ((channel == info.num_channels - 1) && !info.initialised) - ss_sleep(1000); - float pvalue; - if ((channel >= 0) && (channel < info.num_channels)) { - pvalue = info.last_measured[channel]; - - } else { - pvalue = -1.0f; - } - - return pvalue; -} - -INT itcPressureOptimizer::mitc_pressc_get(INT channel, float *pvalue) { - // update variox readout - mitc_pressc_update_mitc_input(); - - // update pressure demand readout - mitc_pressc_update_pressure_demanded(); - - // recalculate pressure and nv% - mitc_pressc_recalculate(); - - if ((channel == info.num_channels - 1) && !info.initialised) - ss_sleep(1000); - - if (pvalue) { - if ((channel >= 0) && (channel < info.num_channels)) { - if (*pvalue != info.last_measured[channel]) { - *pvalue = info.last_measured[channel]; - } - } else { - *pvalue = -1.0f; - } - } - return FE_SUCCESS; -} - -/* - Default threshold for measured values index threshold - ---------------------------------------------------------------------- - _Pressure 0 0.01 - _MercuryITCSetpoint 1 0.001 - _MercuryITCTemperat(ure) 2 0.0001 - _MercuryITCPower 3 0.1 - _AveragedPower 4 0.01 - _CalcPressure 5 0.01 - _PressureControlMode 6 - - _SetRCPressure 7 - - _PressureCalc_minPressure 8 0.01 - _PressureCalc_maxPressure 9 0.01 - _PressureCalc_const1 10 0.01 - _PressureCalc_const2 11 0.01 - */ - -float itcPressureOptimizer::getDefaultThreshold(int channel) { - switch (channel) { - case 0: - case 4: - case 5: - case 8: - case 9: - case 10: - case 11: - return 0.01f; - break; - case 3: - return 0.1f; - break; - case 1: - return 0.001f; - break; - case 2: - return 0.0001f; - break; - default: - return 1.0f; - } - return FE_SUCCESS; -} - -std::string itcPressureOptimizer::getDefaultName(int channel) { - - int maxSize = Configuration::defaultLabels.size() - 1; - if (channel > Configuration::defaultLabels.size() - 1 || channel < 0) { - channel = Configuration::defaultLabels.size() - 1; - } - - std::string res = Configuration::defaultLabels.at(channel); - printf("%s\n", res.c_str()); - return res; -} \ No newline at end of file diff --git a/src/device/itc_pressure_optimizer.h b/src/device/itc_pressure_optimizer.h deleted file mode 100644 index b7e6050..0000000 --- a/src/device/itc_pressure_optimizer.h +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef ITC_PRESSURE_OPTIMIZER_H -#define ITC_PRESSURE_OPTIMIZER_H - -#include "itc_pressure_optimizer_info.h" -#include "tmfe.h" -class itcPressureOptimizer : public TMFeEquipment { - public: - itcPressureOptimizer(std::string equipmentName, - const char *equipmentFilename, int channel); - - TMFeResult HandleInit(const std::vector &args); - - void HandlePeriodic(); - - private: - std::string equipmentPath; - itcPressureOptimizerInfo info; - /* - Initialize ODB record for settings and initialized its variables and - bus driver - */ - INT mitc_pressc_init(); - /* - Exit routine - */ - INT mitc_pressc_exit(); - - /* - read temperature setpoint, measured temperature and measured power - from Mercury ITC device in ODB and calculate average power of the last - 30 sec - */ - INT mitc_pressc_update_mitc_input(); - - /* - read demanded pressure (setpoint) from Mercury ITC (ODB Demand) - */ - INT mitc_pressc_update_pressure_demanded(); - /* - Calculate pressure - */ - INT mitc_pressc_recalculate(); - - /* - Set pressure to Mercury ITC - */ - INT mitc_pressc_set(INT channel, float value); - /* - ???? - */ - float getDemand(int channel); - - /* - - */ - INT mitc_pressc_get(INT channel, float *pvalue); - - std::string getDefaultName(int channel); - - float getDefaultThreshold(int channel); - - float get(int channel); -}; - -#endif \ No newline at end of file diff --git a/src/device/itc_pressure_optimizer_config.h b/src/device/itc_pressure_optimizer_config.h deleted file mode 100644 index 1721bb6..0000000 --- a/src/device/itc_pressure_optimizer_config.h +++ /dev/null @@ -1,85 +0,0 @@ -#ifndef ITC_PRESSURE_OPTIMIZER_CONFIG_H -#define ITC_PRESSURE_OPTIMIZER_CONFIG_H - -#include - -namespace Configuration { -const std::string SLASH = "/"; -const std::string EQUIPMENT_DIR = "Equipment"; - -const std::string OUTPUT_VARNAME = "Output"; -const std::string INPUT_VARNAME = "Input"; - -const std::string SETTINGS_DIR = "Settings"; -const std::string VARIABLE_DIR = "Variables"; - -const std::string DEVICES_DIR = "Devices"; -const std::string MIT_DIR = "MITCPRESSC"; -const std::string DD_DIR = "DD"; - -const std::string MITC_EQUIPMENT_VARNAME = "MITC_Equipment"; -const std::string MITC_OUTPUT_PRESS_SP_INDEX_VARNAME = - "MITC_OutputPressSPIndex"; -const std::string MITC_INPUT_VARIOX_SP_INDEX_VARNAME = - "MITC_InputVarioxSPIndex"; -const std::string MITC_INPUT_VARIOX_TEMP_INDEX_VARNAME = - "MITC_InputVarioxTempIndex"; -const std::string MITC_INPUT_VARIOX_POW_INDEX_VARNAME = - "MITC_InputVarioxPowIndex"; -const std::string RECALC_INTERVAL_VARNAME = "RecalcInterval"; -const std::string MITC_READOUT_INTERVAL_VARNAME = "MitcReadoutInterval"; -const std::string PRESS_READOUT_INTERVAL_VARNAME = "PressReadoutInterval"; - -const std::string ENABLE_VARNAME = "Enable"; -const std::string NAMES_VARNAME = "Names"; -const std::string UPDATE_THRESHOLD_MEASURED_VARNAME = - "Update Threshold Measured"; - -const std::string DEMAND_VARNAME = "Demand"; -const std::string MEASURED_VARNAME = "Measured"; - -const std::string MITC_EQUIPMENT_PATH = SETTINGS_DIR + SLASH + DEVICES_DIR + - SLASH + MIT_DIR + SLASH + DD_DIR + - SLASH + MITC_EQUIPMENT_VARNAME; -const std::string MITC_OUTPUT_PRESS_SP_INDEX_PATH = - SETTINGS_DIR + SLASH + DEVICES_DIR + SLASH + MIT_DIR + SLASH + DD_DIR + - SLASH + MITC_OUTPUT_PRESS_SP_INDEX_VARNAME; -const std::string MITC_INPUT_VARIOX_SP_INDEX_PATH = - SETTINGS_DIR + SLASH + DEVICES_DIR + SLASH + MIT_DIR + SLASH + DD_DIR + - SLASH + MITC_INPUT_VARIOX_SP_INDEX_VARNAME; -const std::string MITC_INPUT_VARIOX_TEMP_INDEX_PATH = - SETTINGS_DIR + SLASH + DEVICES_DIR + SLASH + MIT_DIR + SLASH + DD_DIR + - SLASH + MITC_INPUT_VARIOX_TEMP_INDEX_VARNAME; -const std::string MITC_INPUT_VARIOX_POW_INDEX_PATH = - SETTINGS_DIR + SLASH + DEVICES_DIR + SLASH + MIT_DIR + SLASH + DD_DIR + - SLASH + MITC_INPUT_VARIOX_POW_INDEX_VARNAME; -const std::string RECALC_INTERVAL_PATH = SETTINGS_DIR + SLASH + DEVICES_DIR + - SLASH + MIT_DIR + SLASH + DD_DIR + - SLASH + RECALC_INTERVAL_VARNAME; -const std::string MITC_READOUT_INTERVAL_PATH = - SETTINGS_DIR + SLASH + DEVICES_DIR + SLASH + MIT_DIR + SLASH + DD_DIR + - SLASH + MITC_READOUT_INTERVAL_VARNAME; -const std::string PRESS_READOUT_INTERVAL_PATH = - SETTINGS_DIR + SLASH + DEVICES_DIR + SLASH + MIT_DIR + SLASH + DD_DIR + - SLASH + PRESS_READOUT_INTERVAL_VARNAME; - -const std::string OUTPUT_PATH = VARIABLE_DIR + SLASH + OUTPUT_VARNAME; -const std::string INPUT_PATH = VARIABLE_DIR + SLASH + INPUT_VARNAME; -const std::string DEMANDE_PATH = VARIABLE_DIR + SLASH + DEMAND_VARNAME; -const std::string MEASURED_PATH = VARIABLE_DIR + SLASH + MEASURED_VARNAME; - -const std::vector defaultLabels = {"Pressure Setpoint", - "MPC_ITC_VarioxSP", - "MPC_ITC_VarioxTemp", - "MPC_ITC_VarioxPower", - "MPC_AveragedPower", - "MPC_CalcPressure", - "Pressure Control Mode", - "MPC_SetPressure", - "MPC_PressCalcminPress", - "MPC_PressCalcmaxPress", - "MPC_PressCalc_const1", - "MPC_PressCalc_const2", - "NONE"}; -} // namespace Configuration -#endif \ No newline at end of file diff --git a/src/device/itc_pressure_optimizer_info.h b/src/device/itc_pressure_optimizer_info.h deleted file mode 100644 index 424f8fa..0000000 --- a/src/device/itc_pressure_optimizer_info.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef ITC_PRESSURE_OPTIMIZER_INFO_H -#define ITC_PRESSURE_OPTIMIZER_INFO_H - -#include "midas.h" -#include "tmfe.h" - -#include -#include - -#define MAX_HIS 100 - -class itcPressureOptimizerSettings { - public: - std::string mitcequipment; // equipment name of Mercury ITC - INT setpressindex; // index of demanded/output pressure - INT getvarioxspindex; // index of input setpoint (readout) - INT getvarioxtempindex; // index of input temperature (readout) - INT getvarioxpowindex; // index of input power (readout) - INT recalcinterval; // interval [sec] to recalculate - INT mitcinterval; // interval [sec] to update Mercury ITC readout - INT pressinterval; // interval [sec] to update demanded pressure -}; - -class itcPressureOptimizerInfo { - public: - itcPressureOptimizerSettings mitcpressc_settings; - INT num_channels; // number of channels of this fe - HNDLE hkey_demand; // ODB key to demand values of this equipment - std::vector - last_demand; // last demand values (used to calculate pressure) - std::vector last_demand_set; // last time a demand value was set - std::vector last_measured; // last "measured" values *** - INT nvals_mitc_input; // number of variables in Mercury ITC input - HNDLE hkey_mitc_input; // ODB key to Mercury ITC input - std::vector mitc_input; // readout of Mercury ITC equipment input - DWORD last_mitc_input; // last time Mercury ITC input readout - INT nvals_mitc_output; // number of variables in Mercury ITC output - HNDLE hkey_mitc_output; // ODB key to Mercury ITC output - float press_mitc_output; // readout of pressure setpoint from Mercury ITC - DWORD last_press_mitc_output; // last time readout of pressure setpoint - DWORD lastlog; - std::string name; // equipment name to access this odb variables - // directly - BOOL initialised; // to flag everything is initialised - BOOL recalculate; // to flag recalculate necessary after param change - BOOL pending; // recalculation is pending due to insufficient info - DWORD last_recalculated; // ss_time() when last recalculated - INT ihis; // position of next value in history array - float history[MAX_HIS]; // history[i] of Mercury ITC power output - DWORD history_time[MAX_HIS]; // time when Mercury ITC power was written to - // history[i] - - std::string mitcPath; -}; - -#endif \ No newline at end of file diff --git a/src/device/odb/OdbReader.tpp b/src/device/odb/OdbReader.tpp new file mode 100644 index 0000000..134ced1 --- /dev/null +++ b/src/device/odb/OdbReader.tpp @@ -0,0 +1,34 @@ +#include +#include + +template class OdbReader { + private: + std::string path; + + public: + OdbReader(std::string path); + std::optional readValue(); + std::optional readValueAtIndex(int index); +}; + +template inline OdbReader::OdbReader(std::string path) { + this->path = path; +} + +template inline std::optional OdbReader::readValue() { + midas::odb o(path); + if (o.exists()) { + return std::optional(o); + } + return std::optional(); +}; + +template +inline std::optional OdbReader::readValueAtIndex(int index) { + midas::odb o(path); + if (o.exists() && o.size() > index) { + return std::optional(o[index]); + } + + return std::optional(); +}; diff --git a/src/device/odb/OdbWriter.tpp b/src/device/odb/OdbWriter.tpp new file mode 100644 index 0000000..0b43e97 --- /dev/null +++ b/src/device/odb/OdbWriter.tpp @@ -0,0 +1,35 @@ +#include +#include + +template class OdbWriter { + private: + std::string path; + + public: + OdbWriter(std::string path); + bool writeValue(T &value); + bool writeValueAtIndex(T &value, int index); +}; + +template inline OdbWriter::OdbWriter(std::string path) { + this->path = path; +} + +template inline bool OdbWriter::writeValue(const T &value) { + midas::odb o(path); + if (o.exists()) { + o = value; + return true; + } + return false; +} + +template +inline bool OdbWriter::writeValueAtIndex(const T &value, int index) { + midas::odb o(path); + if (o.exists() && o.size() > index) { + o[index] = value; + return true; + } + return false; +}