From 1316835cad928539681c794b797a411fba648721 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Wed, 29 Dec 2021 20:25:49 +0300 Subject: [PATCH] PID&Relays field tests & debug --- lighthub/item.cpp | 22 ++++++++++++++++++++++ lighthub/item.h | 1 + lighthub/main.cpp | 2 +- lighthub/modules/out_modbus.cpp | 5 +++-- lighthub/modules/out_pid.cpp | 32 +++++++++++++++++++++----------- lighthub/modules/out_relay.cpp | 10 +++++++--- lighthub/options.h | 2 +- 7 files changed, 56 insertions(+), 18 deletions(-) diff --git a/lighthub/item.cpp b/lighthub/item.cpp index 75ef684..6d5a349 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -336,6 +336,28 @@ int Item::getArg(short n) //Return arg int or first array element if Arg is arra else return 0;//-2; } + +float Item::getFloatArg(short n) //Return arg float or first array element if Arg is array +{ + if (!itemArg) return 0;//-1; + if (!n) + { + if (itemArg->type == aJson_Int) return itemArg->valueint; + else if (itemArg->type == aJson_Float) return itemArg->valuefloat; + else return 0; + } + + if ((itemArg->type == aJson_Array) && ( n < aJson.getArraySize(itemArg))) + { + aJsonObject * obj = aJson.getArrayItem(itemArg, n); + if (obj->type == aJson_Int) return obj->valueint; + if (obj->type == aJson_Float) return obj->valuefloat; + return 0; + } + + else return 0;//-2; +} + short Item::getArgCount() { if (!itemArg) return 0; diff --git a/lighthub/item.h b/lighthub/item.h index fba2e21..08748f3 100644 --- a/lighthub/item.h +++ b/lighthub/item.h @@ -119,6 +119,7 @@ class Item int Ctrl(char * payload, char * subItem=NULL); int getArg(short n=0); + float getFloatArg(short n=0); short getArgCount(); //int getVal(short n); //From VAL array. Negative if no array long int getVal(); //From int val OR array diff --git a/lighthub/main.cpp b/lighthub/main.cpp index 7e6b7fb..e5e274e 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -2573,7 +2573,7 @@ void thermoLoop(void) { if (tStore.timestamp16) //Valid temperature { - if (isTimeOver(tStore.timestamp16,millisNZ(8) & 0xFFFF,PERIOD_THERMOSTAT_FAILED,0xFFFF)) + if (isTimeOver(tStore.timestamp16,millisNZ(8) & 0xFFFF,PERIOD_THERMOSTAT_FAILED >> 8,0xFFFF)) { errorSerial<name<name); diff --git a/lighthub/modules/out_modbus.cpp b/lighthub/modules/out_modbus.cpp index 4e96a1a..5a3ab02 100644 --- a/lighthub/modules/out_modbus.cpp +++ b/lighthub/modules/out_modbus.cpp @@ -423,6 +423,7 @@ int out_Modbus::getChanType() int out_Modbus::Ctrl(itemCmd cmd, char* subItem, bool toExecute) { + return 0; //int chActive = item->isActive(); //bool toExecute = (chActive>0); //itemCmd st(ST_UINT32,CMD_VOID); @@ -431,9 +432,9 @@ aJsonObject *templateParamObj = NULL; short mappedCmdVal = 0; // trying to find parameter in template with name == subItem (NB!! standard suffixes dint working here) -if (subItem && strlen (subItem)) templateParamObj = aJson.getObjectItem(store->parameters, subItem); +if (subItem && strlen (subItem) && store) templateParamObj = aJson.getObjectItem(store->parameters, subItem); -if (!templateParamObj) +if (!templateParamObj && store) { // Trying to find template parameter where id == suffixCode templateParamObj = store->parameters->child; diff --git a/lighthub/modules/out_pid.cpp b/lighthub/modules/out_pid.cpp index 8808b0a..89fb02f 100644 --- a/lighthub/modules/out_pid.cpp +++ b/lighthub/modules/out_pid.cpp @@ -30,24 +30,32 @@ bool out_pid::getConfig() } double outMin=0.; //UNUSED double outMax=255.;//UNUSED - unsigned int alarmTO=0; + float dT=5.; + uint32_t alarmTO=PERIOD_THERMOSTAT_FAILED; aJsonObject * param; switch (aJson.getArraySize(kPIDObj)) - { case 7: //kP,kI,kD, alarmTO, alarmVal, outMin, outMax - param = aJson.getArrayItem(kPIDObj, 6); + { case 8: //kP,kI,kD,dT, alarmTO, alarmVal, outMin, outMax + param = aJson.getArrayItem(kPIDObj, 7); if (param->type == aJson_Float) outMax=param->valuefloat; else if (param->type == aJson_Int) outMax=param->valueint; - case 6: //kP,kI,kD, alarmTO, alarmVal, outMin - param = aJson.getArrayItem(kPIDObj, 5); + case 7: //kP,kI,kD,dT alarmTO, alarmVal, outMin + param = aJson.getArrayItem(kPIDObj, 6); if (param->type == aJson_Float) outMin=param->valuefloat; else if (param->type == aJson_Int) outMin=param->valueint; - case 5: //kP,kI,kD, alarmTO, alarmVal - case 4: //kP,kI,kD, alarmTO + case 6: //kP,kI,kD,dT, alarmTO, alarmVal + case 5: //kP,kI,kD,dT, alarmTO + param = aJson.getArrayItem(kPIDObj, 4); + if (param->type == aJson_Float) alarmTO=param->valuefloat; + else if (param->type == aJson_Int) alarmTO=param->valueint; + + case 4: //kP,kI,kD,dT param = aJson.getArrayItem(kPIDObj, 3); - if (param->type == aJson_Int) alarmTO=param->valueint; + if (param->type == aJson_Float) dT=param->valuefloat; + else if (param->type == aJson_Int) dT=param->valueint; + case 3: //kP,kI,kD param = aJson.getArrayItem(kPIDObj, 2); if (param->type == aJson_Float) kD=param->valuefloat; @@ -88,7 +96,7 @@ bool out_pid::getConfig() if (!store->pid) return false; store->pid->SetMode(AUTOMATIC); //store->pid->SetOutputLimits(outMin,outMax); - store->pid->SetSampleTime(5000); + store->pid->SetSampleTime(dT*1000.0); store->alarmTimer=millis(); store->alarmArmed=false; store->alarmTimeout=alarmTO; //in sec @@ -161,6 +169,8 @@ if (store && store->pid && (Status() == CST_INITIALIZED) && item && (item->getCm if (item->getCmd() != CMD_OFF) { if(store->pid->Compute() && !store->alarmArmed) + { + debugSerial<itemArr->name<setpoint<input<<(" out:") << store->output<output-store->prevOut)>OUTPUT_TRESHOLD) { aJsonObject * oCmd = aJson.getArrayItem(item->itemArg, 1); @@ -169,8 +179,8 @@ if (store && store->pid && (Status() == CST_INITIALIZED) && item && (item->getCm executeCommand(oCmd,-1,value); store->prevOut=store->output; } - - if(!store->alarmArmed && isTimeOver(store->alarmTimer,millis(),store->alarmTimeout*1000) ) + } + if(!store->alarmArmed && store->alarmTimeout && isTimeOver(store->alarmTimer,millis(),store->alarmTimeout*1000) ) { store->alarmArmed=true; alarm(true); diff --git a/lighthub/modules/out_relay.cpp b/lighthub/modules/out_relay.cpp index 7001df3..7435cc2 100644 --- a/lighthub/modules/out_relay.cpp +++ b/lighthub/modules/out_relay.cpp @@ -13,6 +13,8 @@ static int driverStatus = CST_UNKNOWN; void out_relay::getConfig() { inverted=false; + + if (!item) return; pin=item->getArg(0); if (pin<0) { @@ -20,9 +22,11 @@ void out_relay::getConfig() inverted=true; } if(pin==0 || pin>=PINS_COUNT) pin=32; - period = item->getArg(1); - - } + + period = item->getFloatArg(1)*1000.0; + if (!period) period = 5000UL; + +} #define ACTIVE (inverted)?LOW:HIGH #define INACTIVE (inverted)?HIGH:LOW diff --git a/lighthub/options.h b/lighthub/options.h index 35d6aef..be2c0f5 100644 --- a/lighthub/options.h +++ b/lighthub/options.h @@ -72,7 +72,7 @@ #define TIMEOUT_REINIT 5000UL #define TIMEOUT_RETAIN 8000UL #define INTERVAL_1W 5000UL -#define PERIOD_THERMOSTAT_FAILED (600 * 1000UL)>>8 +#define PERIOD_THERMOSTAT_FAILED (600 * 1000UL) //#define T_ATTEMPTS 200 //#define IET_TEMP 0