Skip to content

Commit

Permalink
make inv state changeable [#188]
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm1957 committed Mar 11, 2024
1 parent 6267eef commit 0c5926e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions admin/jsonConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
"width": "5% ",
"title": "lblCtrlInvert",
"tooltip": "ttCtrInvert",
"hidden": "data.ctrlUseStateCfg",
"filter": false,
"sort": false,
"default": false
Expand Down
4 changes: 4 additions & 0 deletions lib/pid.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ class PidCtrl {
this.act = pAct;
}

setInv(pInv) {
this.param.inv = pInv;
}

// Y = (MAX-MIN) * XP * ( DIFF + 1/TN * INTEG(DIFF) + TV * DERIV(DIFF)) + OFFSET
// Y = KP * ( DIFF + 1/TN * INTEG(DIFF) + TV * DERIV(DIFF)) + OFFSET
//
Expand Down
20 changes: 17 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const STATES_CFG = {
dao: { folder: 'cfg', type: 'boolean', name: 'derivative act only', desc: 'descDao', role: 'value',
unit: '', acc: 'RO', init: false, warnAck: false },
inv: { folder: 'cfg', type: 'boolean', name: 'invert output', desc: 'descInv', role: 'value',
unit: '', acc: 'RO', init: false, warnAck: false },
unit: '', acc: 'RW', init: false, warnAck: true },

/* changeable params */
xp: { folder: 'para', type: 'number', name: 'prop factor', desc: 'descXp', role: 'level',
Expand Down Expand Up @@ -116,7 +116,7 @@ class Pid extends utils.Adapter {
kp: this.chgKp.bind(this),
useXp: null,
dao: null,
inv: null,
inv: this.chgInv.bind(this),
tn: this.chgTn.bind(this),
tv: this.chgTv.bind(this),
min: this.chgMin.bind(this),
Expand Down Expand Up @@ -192,6 +192,7 @@ class Pid extends utils.Adapter {
let _tv;
let _off;
let _sup;
let _inv;
const useXp = this.config.ctrlMode === 1;
if (controller.ctrlUseStateCfg) {
_KpXp = useXp
Expand All @@ -203,6 +204,7 @@ class Pid extends utils.Adapter {
_tv = (await this.getStateAsync(this.getExtId(ctrlIdTxt, 'tv')))?.val;
_off = (await this.getStateAsync(this.getExtId(ctrlIdTxt, 'off')))?.val;
_sup = (await this.getStateAsync(this.getExtId(ctrlIdTxt, 'sup')))?.val;
_inv = (await this.getStateAsync(this.getExtId(ctrlIdTxt, 'inv')))?.val;
} else {
_KpXp = controller.ctrlKpXp;
_tn = controller.ctrlTn;
Expand All @@ -211,6 +213,7 @@ class Pid extends utils.Adapter {
_tv = controller.ctrlTv;
_off = controller.ctrlOff;
_sup = controller.ctrlSup;
_inv = controller.ctrlInvert;
}

const KpXp = this.getNumParam(ctrlIdTxt, 'kpxp', _KpXp, 1);
Expand All @@ -220,6 +223,7 @@ class Pid extends utils.Adapter {
const tv = this.getNumParam(ctrlIdTxt, 'tv', _tv, 0);
const off = this.getNumParam(ctrlIdTxt, 'off', _off, 0);
const sup = this.getNumParam(ctrlIdTxt, 'sup', _sup, 0);
const inv = _inv;

if (min >= max) {
this.log.warn(`[C-${controller.ctrlId}] - ${min} >= ${min}, using min=0, max=100 instead`);
Expand Down Expand Up @@ -255,7 +259,7 @@ class Pid extends utils.Adapter {
sup: sup,
dao: this.config.ctrlActDiff || false,
useXp: useXp,
inv: controller.ctrlInvert || false,
inv: inv || false,
});

this.controllers[controller.ctrlId] = {
Expand Down Expand Up @@ -671,6 +675,16 @@ class Pid extends utils.Adapter {
//if (controller.running && !controller.cycle) await this.doUpdate(pCtrlId);
}

async chgInv(pId, pCtrlId, pVal) {
this.log.debug(`chgInv called (${pCtrlId}, ${pVal})`);

const controller = this.controllers[pCtrlId];
await controller.pidCtrl.setInv(pVal);
await this.setStateAsync(pId, { val: pVal, ack: true, q: 0x00 });
await this.updParamStates(pCtrlId);
//if (controller.running && !controller.cycle) await this.doUpdate(pCtrlId);
}

async chgMin(pId, pCtrlId, pVal) {
this.log.debug(`chgMin called (${pCtrlId}, ${pVal})`);

Expand Down

0 comments on commit 0c5926e

Please sign in to comment.