first commit
This commit is contained in:
290
dependencies/valveClass.js
vendored
Normal file
290
dependencies/valveClass.js
vendored
Normal file
@@ -0,0 +1,290 @@
|
||||
/**
|
||||
* @file valveClass.js
|
||||
*
|
||||
* Permission is hereby granted to any person obtaining a copy of this software
|
||||
* and associated documentation files (the "Software"), to use it for personal
|
||||
....
|
||||
*/
|
||||
|
||||
|
||||
//load local dependencies #NOTE: Vul hier nog de juiste dependencies in als meer nodig of sommige niet nodig
|
||||
const EventEmitter = require('events');
|
||||
const Logger = require('../../generalFunctions/helper/logger');
|
||||
const State = require('../../generalFunctions/helper/state/state');
|
||||
const Predict = require('../../predict/dependencies/predict/predict_class');
|
||||
const { MeasurementContainer } = require('../../generalFunctions/helper/measurements/index');
|
||||
|
||||
//load all config modules #NOTE: Vul hier nog de juiste dependencies in als meer nodig of sommige niet nodig
|
||||
const defaultConfig = require('./valveConfig.json');
|
||||
const ConfigUtils = require('../../generalFunctions/helper/configUtils');
|
||||
|
||||
//load registration utility #NOTE: Vul hier nog de juiste dependencies in als meer nodig of sommige niet nodig
|
||||
const ChildRegistrationUtils = require('../../generalFunctions/helper/childRegistrationUtils');
|
||||
|
||||
class Valve {
|
||||
constructor(valveConfig = {}, stateConfig = {}) {
|
||||
this.emitter = new EventEmitter(); // nodig voor ontvangen en uitvoeren van events emit() en on() --> Zien als internet berichten (niet bedraad in node-red)
|
||||
this.configUtils = new ConfigUtils(defaultConfig); // nodig voor het ophalen van de default configuaratie
|
||||
this.config = this.configUtils.initConfig(valveConfig); //valve configurations die bij invoer in node-red worden gegeven
|
||||
|
||||
// Initialize measurements
|
||||
this.measurements = new MeasurementContainer();
|
||||
this.child = {}; // object to hold child information so we know on what to subscribe
|
||||
|
||||
// Init after config is set
|
||||
this.logger = new Logger(this.config.general.logging.enabled, this.config.general.logging.logLevel, this.config.general.name);
|
||||
this.state = new State(stateConfig, this.logger); // Init State manager and pass logger
|
||||
this.state.stateManager.currentState = "operational"; // Set default state to operational
|
||||
|
||||
this.kv = 0; //default
|
||||
this.rho = 1,225 //dichtheid van lucht standaard
|
||||
this.T = 293; // temperatuur in K standaard
|
||||
this.downstreamP = 0.54 //hardcodes for now --> assumed to be constant watercolumn and deltaP diffuser
|
||||
this.currentMode = this.config.mode.current;
|
||||
|
||||
// wanneer hij deze ontvangt is de positie van de klep verandererd en gaat hij de updateposition functie aanroepen wat dan alle metingen en standen gaat updaten
|
||||
this.state.emitter.on("positionChange", (data) => {
|
||||
this.logger.debug(`Position change detected: ${data}`);
|
||||
this.updatePosition()}); //To update deltaP
|
||||
|
||||
|
||||
this.childRegistrationUtils = new ChildRegistrationUtils(this); // Child registration utility
|
||||
|
||||
//replace v_curve loadspecs with config file afterwards !!!!!!!!!!
|
||||
this.vCurve = this.loadSpecs().v_curve
|
||||
this.predictKv = new Predict({curve:this.vCurve}); // load valve size (x : ctrl , y : kv relationship)
|
||||
}
|
||||
|
||||
// -------- Config -------- //
|
||||
updateConfig(newConfig) {
|
||||
this.config = this.configUtils.updateConfig(this.config, newConfig);
|
||||
}
|
||||
|
||||
isValidSourceForMode(source, mode) {
|
||||
const allowedSourcesSet = this.config.mode.allowedSources[mode] || [];
|
||||
return allowedSourcesSet.has(source);
|
||||
}
|
||||
|
||||
async handleInput(source, action, parameter) {
|
||||
if (!this.isValidSourceForMode(source, this.currentMode)) {
|
||||
let warningTxt = `Source '${source}' is not valid for mode '${this.currentMode}'.`;
|
||||
this.logger.warn(warningTxt);
|
||||
return {status : false , feedback: warningTxt};
|
||||
}
|
||||
|
||||
this.logger.info(`Handling input from source '${source}' with action '${action}' in mode '${this.currentMode}'.`);
|
||||
try {
|
||||
switch (action) {
|
||||
case "execSequence":
|
||||
await this.executeSequence(parameter);
|
||||
break;
|
||||
case "execMovement": // past het setpoint aan - movement van klep stand
|
||||
await this.setpoint(parameter);
|
||||
break;
|
||||
case "emergencyStop":
|
||||
this.logger.warn(`Emergency stop activated by '${source}'.`);
|
||||
await this.executeSequence("emergencyStop");
|
||||
break;
|
||||
case "statusCheck":
|
||||
this.logger.info(`Status Check: Mode = '${this.currentMode}', Source = '${source }'.`);
|
||||
break;
|
||||
default:
|
||||
this.logger.warn(`Action '${action}' is not implemented.`);
|
||||
break;
|
||||
}
|
||||
this.logger.debug(`Action '${action}' successfully executed`);
|
||||
return {status : true , feedback: `Action '${action}' successfully executed.`};
|
||||
} catch (error) {
|
||||
this.logger.error(`Error handling input: ${error}`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
setMode(newMode) {
|
||||
const availableModes = defaultConfig.mode.current.rules.values.map(v => v.value);
|
||||
if (!availableModes.includes(newMode)) {
|
||||
this.logger.warn(`Invalid mode '${newMode}'. Allowed modes are: ${availableModes.join(', ')}`);
|
||||
return;
|
||||
}
|
||||
|
||||
this.currentMode = newMode;
|
||||
this.logger.info(`Mode successfully changed to '${newMode}'.`);
|
||||
}
|
||||
|
||||
loadSpecs(){ //static betekend dat die in andere classes kan worden aangeroepen met const specs = Valve.loadSpecs()
|
||||
//lateron based on valve caracteristics --> then it searches for right valve
|
||||
let specs = {
|
||||
supplier : "Binder",
|
||||
type : "HDCV",
|
||||
units:{
|
||||
Nm3: { "temp": 20, "pressure" : 1.01325 , "RH" : 0 }, // according to DIN
|
||||
v_curve : { x : "% stroke", y : "Kv value"} ,
|
||||
},
|
||||
v_curve: {
|
||||
125: // valve size
|
||||
{
|
||||
x:[0,10,20,30,40,50,60,70,80,90,100], //stroke in %
|
||||
y:[0,18,50,95,150,216,337,564,882,1398,1870], //Kv value expressed in m3/h
|
||||
},
|
||||
150: // valve size
|
||||
{
|
||||
x:[0,10,20,30,40,50,60,70,80,90,100], //stroke in %
|
||||
y:[0,25,73,138,217,314,490,818,1281,2029,2715], //oxygen transfer rate expressed in gram o2 / normal m3/h / per m
|
||||
},
|
||||
400: // valve size
|
||||
{
|
||||
x:[0,10,20,30,40,50,60,70,80,90,100], //stroke in %
|
||||
y:[0,155,443,839,1322,1911,2982,4980,7795,12349,16524], //oxygen transfer rate expressed in gram o2 / normal m3/h / per m
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return specs;
|
||||
}
|
||||
|
||||
// -------- Sequence Handlers -------- //
|
||||
async executeSequence(sequenceName) {
|
||||
|
||||
const sequence = this.config.sequences[sequenceName];
|
||||
|
||||
if (!sequence || sequence.size === 0) {
|
||||
this.logger.warn(`Sequence '${sequenceName}' not defined.`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.state.getCurrentState() == "operational" && sequenceName == "shutdown") {
|
||||
this.logger.info(`Machine will ramp down to position 0 before performing ${sequenceName} sequence`);
|
||||
await this.setpoint(0);
|
||||
}
|
||||
|
||||
this.logger.info(` --------- Executing sequence: ${sequenceName} -------------`);
|
||||
|
||||
for (const state of sequence) {
|
||||
try {
|
||||
await this.state.transitionToState(state);
|
||||
// Update measurements after state change
|
||||
|
||||
} catch (error) {
|
||||
this.logger.error(`Error during sequence '${sequenceName}': ${error}`);
|
||||
break; // Exit sequence execution on error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async setpoint(setpoint) {
|
||||
|
||||
try {
|
||||
// Validate setpoint
|
||||
if (typeof setpoint !== 'number' || setpoint < 0) {
|
||||
throw new Error("Invalid setpoint: Setpoint must be a non-negative number.");
|
||||
}
|
||||
|
||||
// Move to the desired setpoint
|
||||
await this.state.moveTo(setpoint);
|
||||
|
||||
} catch (error) {
|
||||
console.error(`Error setting setpoint: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// NOTE: Omdat met zeer kleine getallen wordt gewerkt en er kwadraten in de formule zitten kan het zijn dat we alles *1000 moeten doen
|
||||
updateDeltaPKlep(q,kv,downstreamP,rho,temp){
|
||||
//q must be in Nm3/h
|
||||
//temp must be in K
|
||||
//q must be in m3/h
|
||||
|
||||
//downstreamP must be in bar so transfer from mbar to bar
|
||||
downstreamP = downstreamP / 1000;
|
||||
//convert downstreamP to absolute bar
|
||||
downstreamP += 1.01325;
|
||||
|
||||
//calculate deltaP
|
||||
let deltaP = ( q**2 * rho * temp ) / ( 514**2 * kv**2 * downstreamP);
|
||||
|
||||
//convert deltaP to mbar
|
||||
deltaP = deltaP * 1000;
|
||||
|
||||
// Synchroniseer deltaP met het Valve-object
|
||||
this.deltaPKlep = deltaP
|
||||
|
||||
// Opslaan in measurement container
|
||||
this.measurements.type("pressure").variant("predicted").position("delta").value(deltaP);
|
||||
this.logger.info('DeltaP updated to: ' + deltaP);
|
||||
|
||||
this.emitter.emit('deltaPChange', deltaP); // Emit event to notify valveGroupController of deltaP change
|
||||
this.logger.info('DeltaPChange emitted to valveGroupController');
|
||||
}
|
||||
|
||||
|
||||
// Als er een nieuwe flow door de klep komt doordat de pompen harder zijn gaan pompen, dan update deze functie dit ook in de valve attributes en measurements
|
||||
//NOTE: samenvoegen met updateFlow als header node er is
|
||||
updateFlowKlep(q){
|
||||
//q must be in Nm3/h
|
||||
// Opslaan in measurement container van valve object
|
||||
this.measurements.type("flow").variant("predicted").position("downstream").value(q);
|
||||
this.logger.info('FlowKlep updated to: ' + q);
|
||||
this.logger.info('Calculating new deltaP based on new flow');
|
||||
this.updateDeltaPKlep(q,this.kv,this.downstreamP,this.rho,this.T); //update deltaP based on new flow
|
||||
}
|
||||
|
||||
updatePosition() { //update alle parameters nadat er een verandering is geweest in stand van klep
|
||||
if (this.state.getCurrentState() == "operational" || this.state.getCurrentState() == "accelerating" || this.state.getCurrentState() == "decelerating") {
|
||||
|
||||
this.logger.debug('Calculating new deltaP');
|
||||
const currentPosition = this.state.getCurrentPosition();
|
||||
const currentFlow = this.measurements.type("flow").variant("predicted").position("downstream").getCurrentValue(); // haal de flow op uit de measurement containe
|
||||
//const valveSize = 125; //NOTE: nu nog hardcoded maar moet een attribute van de valve worden
|
||||
this.predictKv.fDimension = 125; //load valve size by defining fdimension in predict class
|
||||
//const vCurve = this.loadSpecs().v_curve[valveSize]; // haal de curve op van de valve
|
||||
//const Spline = require('cubic-spline'); // spline library -> nodig om kv waarde te benaderen op curve
|
||||
const x = currentPosition; // dit is de positie van de klep waarvoor we delta P willen berekenen
|
||||
const y = this.predictKv.y(x); // haal de waarde van kv op uit de spline
|
||||
|
||||
this.kv = y; //update de kv waarde in de valve class
|
||||
if (this.kv < 0.1){
|
||||
this.kv = 0.1; //minimum waarde voor kv
|
||||
}
|
||||
this.logger.debug(`Kv value for position valve ${x} is ${this.kv}`); // log de waarde van kv
|
||||
this.updateDeltaPKlep(currentFlow,this.kv,this.downstreamP,this.rho,this.T); //update deltaP
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
getOutput() {
|
||||
|
||||
// Improved output object generation
|
||||
const output = {};
|
||||
//build the output object
|
||||
this.measurements.getTypes().forEach(type => {
|
||||
this.measurements.getVariants().forEach(variant => {
|
||||
this.measurements.getPositions().forEach(position => {
|
||||
|
||||
const value = this.measurements.type(type).variant(variant).position(position).getCurrentValue(); //get the current value of the measurement
|
||||
|
||||
|
||||
if (value != null) {
|
||||
output[`${position}_${variant}_${type}`] = value;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
//fill in the rest of the output object
|
||||
output["state"] = this.state.getCurrentState();
|
||||
output["percentageOpen"] = this.state.getCurrentPosition();
|
||||
output["moveTimeleft"] = this.state.getMoveTimeLeft();
|
||||
output["mode"] = this.currentMode;
|
||||
|
||||
//this.logger.debug(`Output: ${JSON.stringify(output)}`);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Valve;
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user