bug fixes

This commit is contained in:
znetsixe
2025-07-02 16:00:52 +02:00
parent 8d2a3b80e7
commit f24a5fb90b

View File

@@ -63,6 +63,9 @@ class Machine {
this.model = machineConfig.asset.model; // Get the model from the machineConfig
this.curve = this.model ? loadCurve(this.model) : null;
//Always initialize config first
this.config = this.configUtils.initConfig(machineConfig);
if (!this.model || !this.curve) {
this.logger.warning(`${!this.model ? 'Model not specified' : 'Curve not found for model ' + this.model} in machineConfig. Cannot make predictions.`);
// Set prediction objects to null to prevent method calls
@@ -73,13 +76,17 @@ class Machine {
}
else{
this.hasCurve = true;
this.config = this.configUtils.updateConfig(this.config, {
asset: { ...this.config.asset, machineCurve: this.curve }
});
machineConfig = { ...machineConfig, asset: { ...machineConfig.asset, machineCurve: this.curve } }; // Merge curve into machineConfig
this.config = this.configUtils.initConfig(machineConfig);
this.predictFlow = new predict({ curve: this.config.asset.machineCurve.nq }); // load nq (x : ctrl , y : flow relationship)
this.predictPower = new predict({ curve: this.config.asset.machineCurve.np }); // load np (x : ctrl , y : power relationship)
this.predictCtrl = new predict({ curve: this.reverseCurve(this.config.asset.machineCurve.nq) }); // load reversed nq (x: flow, y: ctrl relationship)
}
this.state = new state(stateConfig, this.logger); // Init State manager and pass logger
this.errorMetrics = new nrmse(errorMetricsConfig, this.logger);
@@ -244,6 +251,8 @@ class Machine {
throw new Error("Invalid setpoint: Setpoint must be a non-negative number.");
}
this.logger.info(`Setting setpoint to ${setpoint}. Current position: ${this.state.getCurrentPosition()}`);
// Move to the desired setpoint
await this.state.moveTo(setpoint);
@@ -254,7 +263,7 @@ class Machine {
// Calculate flow based on current pressure and position
calcFlow(x) {
if(!this.hasCurve) {
if(this.hasCurve) {
const state = this.state.getCurrentState();
if (!["operational", "accelerating", "decelerating"].includes(state)) {
@@ -279,7 +288,7 @@ class Machine {
// Calculate power based on current pressure and position
calcPower(x) {
if(!this.hasCurve) {
if(this.hasCurve) {
const state = this.state.getCurrentState();
if (!["operational", "accelerating", "decelerating"].includes(state)) {
this.measurements.type("power").variant("predicted").position('upstream').value(0);
@@ -302,7 +311,7 @@ class Machine {
// calculate the power consumption using only flow and pressure
inputFlowCalcPower(flow) {
if(!this.hasCurve) {
if(this.hasCurve) {
this.predictCtrl.currentX = flow;
const cCtrl = this.predictCtrl.y(flow);
@@ -320,7 +329,7 @@ class Machine {
// Function to predict control value for a desired flow
calcCtrl(x) {
if(!this.hasCurve) {
if(this.hasCurve) {
this.predictCtrl.currentX = x;
const cCtrl = this.predictCtrl.y(x);
this.measurements.type("ctrl").variant("predicted").position('upstream').value(cCtrl);
@@ -407,7 +416,7 @@ class Machine {
}
// get
const upstreamFlow = this.measurements.type('pressure').variant('measured').position('upstream').getCurrentValue();
const upstreamFlow = this.measurements.type('flow').variant('measured').position('upstream').getCurrentValue();
// Only upstream => might still accept it, but warn
if (upstreamFlow != null) {
@@ -416,7 +425,7 @@ class Machine {
}
// get
const downstreamFlow = this.measurements.type('pressure').variant('measured').position('downstream').getCurrentValue();
const downstreamFlow = this.measurements.type('flow').variant('measured').position('downstream').getCurrentValue();
// Only downstream => might still accept it, but warn
if (downstreamFlow != null) {
@@ -450,6 +459,7 @@ class Machine {
if (this.state.getCurrentState() == "operational" || this.state.getCurrentState() == "accelerating" || this.state.getCurrentState() == "decelerating") {
// put value in measurements
this.measurements.type("pressure").variant("measured").position(position).value(value);
this.logger.debug(`Updated measured pressure: ${value} at position: ${position}`);
//when measured pressure gets updated we need some logic to fetch the relevant value which could be downstream or differential pressure
const pressure = this.getMeasuredPressure();
//update the flow power and cog