From a974290389490ecf435b547a718dde10e2a227ff Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Sun, 4 Dec 2022 03:19:07 +0300 Subject: [PATCH] Complex MBUS mapping, PID fix --- lighthub/item.cpp | 8 ++++ lighthub/itemCmd.cpp | 5 ++- lighthub/modules/out_modbus.cpp | 80 ++++++++++++++++++++++++++------- lighthub/modules/out_modbus.h | 2 +- lighthub/modules/out_pid.cpp | 35 +++++++++------ lighthub/textconst.h | 2 + 6 files changed, 99 insertions(+), 33 deletions(-) diff --git a/lighthub/item.cpp b/lighthub/item.cpp index 03fd01c..2423d16 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -1451,6 +1451,8 @@ int Item::SendStatus(int sendFlags) { case CMD_COOL: case CMD_DRY: case CMD_FAN: + case CMD_ENABLE: + case CMD_DISABLE: strcpy_P(cmdstr, ON_P); break; case CMD_OFF: @@ -1571,6 +1573,12 @@ int Item::SendStatus(int sendFlags) { case CMD_FAN: strcpy_P(cmdstr, FAN_ONLY_P); break; + case CMD_ENABLE: + strcpy_P(cmdstr, ENABLE_P); + break; + case CMD_DISABLE: + strcpy_P(cmdstr, DISABLE_P); + break; case CMD_ON: case CMD_XON: if (itemType == CH_THERMO) strcpy_P(cmdstr, AUTO_P); diff --git a/lighthub/itemCmd.cpp b/lighthub/itemCmd.cpp index 4423db1..2487553 100644 --- a/lighthub/itemCmd.cpp +++ b/lighthub/itemCmd.cpp @@ -1242,10 +1242,10 @@ if (isValue() && valMapping && valMapping->type == aJson_Array && aJson.getArray case aJson_Array: { aJsonObject *i = cmdMapping->child; - //if first array element is not array - this is default mapping value + //if first array element is not array - this is default mapping value, no reverse mapping, skipping if (i && i->type==aJson_Int) { - matchedCmd = i; + //matchedCmd = i; i=i->next; } @@ -1301,6 +1301,7 @@ if (valMapping && valMapping->type == aJson_Array && aJson.getArraySize(valMappi int diff = ((b-a)/(d-c))/2; return itemCmd().Int((uint32_t) constrain(map(getInt(),c,d,a,b)+diff,0,255)); } + if (valMapping && valMapping->type == aJson_NULL) return itemCmd(); return *this; } diff --git a/lighthub/modules/out_modbus.cpp b/lighthub/modules/out_modbus.cpp index 86edfa0..cece315 100644 --- a/lighthub/modules/out_modbus.cpp +++ b/lighthub/modules/out_modbus.cpp @@ -231,10 +231,15 @@ return (result == node.ku8MBSuccess); -int out_Modbus::findRegister(int registerNum, int posInBuffer, int regType) +itemCmd out_Modbus::findRegister(uint16_t registerNum, uint16_t posInBuffer, uint8_t regType, uint16_t registerFrom, uint16_t registerTo, bool doExecution, bool * submitParam) { aJsonObject * paramObj = store->parameters->child; - bool is8bit = false; + + bool tmpSubmitParam; + if (!submitParam) submitParam=&tmpSubmitParam; + *submitParam=true; + + //bool is8bit = false; while (paramObj) { aJsonObject *regObj=NULL; @@ -248,11 +253,14 @@ int out_Modbus::findRegister(int registerNum, int posInBuffer, int regType) { aJsonObject *typeObj = aJson.getObjectItem(paramObj, "type"); aJsonObject *mapObj = aJson.getObjectItem(paramObj, "map"); + aJsonObject *idObj = aJson.getObjectItem(paramObj, "id"); aJsonObject * itemParametersObj = aJson.getArrayItem(item->itemArg, 2); uint16_t data = node.getResponseBuffer(posInBuffer); int8_t regType = PAR_I16; uint32_t param =0; itemCmd mappedParam; + bool executeWithoutCheck=false; //Afler recurrent check, all dublicatess and suppressing checked by recurrent + bool submitRecurrentOut = false; //false if recurrent check find duplicates char buf[16]; //bool isSigned=false; @@ -308,15 +316,47 @@ int out_Modbus::findRegister(int registerNum, int posInBuffer, int regType) if (mapObj && (mapObj->type==aJson_Array || mapObj->type==aJson_Object)) { mappedParam=mappedParam.doReverseMapping(mapObj); - debugSerial << F("MBUSD: Mapped:")<type) ==aJson_Int && (nextRegObj->valueint>= registerFrom) && (nextRegObj->valueint<=registerTo)) + { + debugSerial<valueint,nextRegObj->valueint-registerFrom,registerType,registerFrom,registerTo,false,&submitRecurrentOut); + executeWithoutCheck=true; + } + else errorSerial<type==aJson_Int) + switch (idObj->valueint) + { + case S_CMD: + mappedParam.saveItem(item,SEND_COMMAND); + break; + case S_SET: + mappedParam.saveItem(item,SEND_PARAMETERS); + } + + if (itemParametersObj && itemParametersObj->type ==aJson_Object) { aJsonObject *execObj = aJson.getObjectItem(itemParametersObj,paramObj->name); if (execObj) { - bool submitParam=true; //Retrive previous data aJsonObject *lastMeasured = aJson.getObjectItem(execObj,"@S"); if (lastMeasured) @@ -324,7 +364,7 @@ int out_Modbus::findRegister(int registerNum, int posInBuffer, int regType) if (lastMeasured->type == aJson_Int) { if (lastMeasured->valueint == param) - submitParam=false; //supress repeating execution for same val + *submitParam=false; //supress repeating execution for same val else lastMeasured->valueint=param; } } @@ -332,32 +372,40 @@ int out_Modbus::findRegister(int registerNum, int posInBuffer, int regType) { debugSerial<name<type==aJson_Int && (settedValue->valueint == param)) { debugSerial<subtype & MB_NEED_SEND)) settedValue->valueint=param; } - //#endif } } } - if (!is8bit) return 1; + return mappedParam; } paramObj=paramObj->next; } -return is8bit; +return itemCmd(); } @@ -376,7 +424,7 @@ return is8bit; //if (readModbus(registerNum,MODBUS_HOLDING_REG_TYPE,1)) if (readModbus(registerNum,regType,1)) { - findRegister(registerNum,0,regType); + findRegister(registerNum,0,regType,registerNum,registerNum); // data = node.getResponseBuffer(j); } } @@ -392,7 +440,7 @@ return is8bit; { debugSerial<pid && (Status() == CST_INITIALIZED) && item && (item->getCm { if(store->pid->Compute() ) { - int alarmVal; - if (store->alarmArmed && (alarmVal=getAlarmVal()>=0)) store->output=alarmVal; + float alarmVal; + if (store->alarmArmed && ((alarmVal=getAlarmVal())>=0.)) store->output=alarmVal; debugSerial<itemArr->name<setpoint<input<<(" out:") << store->output <pid->GetKp() <pid->GetKi() <pid->GetKd(); if (store->alarmArmed) debugSerial << F(" Alarm"); debugSerial<output-store->prevOut)>OUTPUT_TRESHOLD) && !store->alarmArmed) + if (((abs(store->output-store->prevOut)>OUTPUT_TRESHOLD) || (item->getCmd() == CMD_ENABLE)) && !store->alarmArmed) { aJsonObject * oCmd = aJson.getArrayItem(item->itemArg, 1); + if (item->getCmd() == CMD_ENABLE) + { + executeCommand(oCmd,-1,itemCmd().Cmd(CMD_ON)); + item->setCmd(CMD_VOID); + } itemCmd value((float) (store->output));// * (100./255.))); value.setSuffix(S_SET); executeCommand(oCmd,-1,value); @@ -198,16 +203,16 @@ if (store && store->pid && (Status() == CST_INITIALIZED) && item && (item->getCm return 1;//store->pollingInterval; }; -int out_pid::getAlarmVal() +float out_pid::getAlarmVal() { aJsonObject * kPIDObj = aJson.getArrayItem(item->itemArg, 0); if (!kPIDObj || kPIDObj->type != aJson_Array) { errorSerial<type == aJson_Float) kP=param->valuefloat; else if (param->type == aJson_Int) kP=param->valueint; { - if (kP<0) + if (kP<0.) { if (!alarmValDefined) outAlarm = 0.; } else if (!alarmValDefined) outAlarm = 255.; } } +debugSerial<itemArg) return; { float outAlarm=getAlarmVal(); errorSerial<itemArr->name<=0) + if (outAlarm>=0.) { errorSerial<itemArg, 1); @@ -337,18 +343,19 @@ case S_CMD: case CMD_FAN: case CMD_DRY: - executeCommand(oCmd,-1,value); + executeCommand(oCmd,-1,value); return 1; + case CMD_ENABLE: item->setCmd(CMD_ENABLE); - item->SendStatus(SEND_COMMAND); - //executeCommand(oCmd,-1,value); + item->SendStatus(SEND_COMMAND); + executeCommand(oCmd,-1,value); return 1; case CMD_DISABLE: item->setCmd(CMD_DISABLE); item->SendStatus(SEND_COMMAND); - //executeCommand(oCmd,-1,value); + executeCommand(oCmd,-1,value); return 1; /* case CMD_OFF: @@ -361,11 +368,11 @@ case S_CMD: } */ default: - debugSerial<