DemandHandler first try
This commit is contained in:
@@ -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 <cmath>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
|
||||
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<std::string> &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<std::string> 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<float> 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<float> 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<INT>((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<INT>(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<INT>(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;
|
||||
}
|
||||
Reference in New Issue
Block a user