# HG changeset patch # User Michiel Broek # Date 1424115669 -3600 # Node ID 8448fcf3d7994f951caf7180d51aba1b0eece790 # Parent 7b0f819a3805aa4accf585a4c0e3ec616f6e8799 Code cleanup diff -r 7b0f819a3805 -r 8448fcf3d799 thermferm/rdconfig.c --- a/thermferm/rdconfig.c Mon Feb 16 19:52:42 2015 +0100 +++ b/thermferm/rdconfig.c Mon Feb 16 20:41:09 2015 +0100 @@ -933,7 +933,7 @@ unit->idle_rangeL = -1.0; unit->prof_started = unit->prof_paused = unit->prof_primary_done = (time_t)0; unit->prof_percent = 0; - unit->PID_err_old = unit->PID_I_err = unit->PID_Kp = unit->PID_Kd = unit->PID_Ki = 0.0; + unit->PID_dState = unit->PID_iState = unit->PID_Kp = unit->PID_Kd = unit->PID_Ki = 0.0; cur = cur->xmlChildrenNode; while (cur != NULL) { diff -r 7b0f819a3805 -r 8448fcf3d799 thermferm/server.c --- a/thermferm/server.c Mon Feb 16 19:52:42 2015 +0100 +++ b/thermferm/server.c Mon Feb 16 20:41:09 2015 +0100 @@ -1505,7 +1505,7 @@ unit->idle_rangeL = -1.0; unit->prof_started = unit->prof_paused = unit->prof_primary_done = (time_t)0; unit->prof_percent = 0; - unit->PID_err_old = unit->PID_I_err = unit->PID_Kp = unit->PID_Kd = unit->PID_Ki = 0.0; + unit->PID_dState = unit->PID_iState = unit->PID_Kp = unit->PID_Kd = unit->PID_Ki = 0.0; /* * Block main process @@ -1851,7 +1851,7 @@ syslog(LOG_NOTICE, "Fermenter unit %s mode %s to %s", unit->uuid, UNITMODE[unit->mode], UNITMODE[i]); unit->mode = i; /* Allways turn everything off after a mode change */ - unit->PID_I_err = unit->PID_err_old = 0.0; + unit->PID_iState = unit->PID_dState = 0.0; unit->heater_state = unit->cooler_state = unit->fan_state = unit->light_state = 0; unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0; device_out(unit->heater_address, unit->heater_state); @@ -1921,7 +1921,7 @@ /* * Reset all output devices */ - unit->PID_I_err = unit->PID_err_old = 0.0; + unit->PID_iState = unit->PID_dState = 0.0; unit->heater_state = unit->cooler_state = unit->fan_state = unit->light_state = 0; unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0; device_out(unit->heater_address, unit->heater_state); diff -r 7b0f819a3805 -r 8448fcf3d799 thermferm/thermferm.c --- a/thermferm/thermferm.c Mon Feb 16 19:52:42 2015 +0100 +++ b/thermferm/thermferm.c Mon Feb 16 20:41:09 2015 +0100 @@ -250,7 +250,7 @@ syslog(LOG_NOTICE, "Mode from %s to %s via panel interface", UNITMODE[current_unit->mode], UNITMODE[mode]); current_unit->mode = mode; /* Allways turn everything off after a mode change */ - current_unit->PID_I_err = current_unit->PID_err_old = 0.0; + current_unit->PID_iState = current_unit->PID_dState = 0.0; current_unit->heater_state = current_unit->cooler_state = current_unit->fan_state = 0; current_unit->heater_wait = current_unit->cooler_wait = current_unit->fan_wait = 0; device_out(current_unit->heater_address, current_unit->heater_state); @@ -1330,14 +1330,14 @@ /* * Calculate the intergral state with appropriate limiting */ - unit->PID_I_err += P_err; + unit->PID_iState += P_err; /* Limit integral error */ - if (unit->PID_I_err < -100.0) - unit->PID_I_err = -100.0; - if (unit->PID_I_err > 100.0) - unit->PID_I_err = 100.0; - iTerm = unit->PID_I_err * unit->PID_Ki; - dTerm = unit->PID_err_old * unit->PID_Kd; + if (unit->PID_iState < -100.0) + unit->PID_iState = -100.0; + if (unit->PID_iState > 100.0) + unit->PID_iState = 100.0; + iTerm = unit->PID_iState * unit->PID_Ki; + dTerm = (unit->PID_dState - pv) * unit->PID_Kd; /* * A postive value means heating, a negative value cooling. @@ -1351,87 +1351,85 @@ Out = 100.0; if (Out < -100.0) Out = -100.0; -// if /* (P_err != 0.0) */ (i == 1) { - if (debug) - fprintf(stdout, "sp=%.2f pv=%.2f err_old=%.2f P_err=%.2f I_err=%.2f Out=%.2f\n", - sp, pv, unit->PID_err_old, P_err, unit->PID_I_err, Out); - syslog(LOG_NOTICE, "sp=%.2f pv=%.2f err_old=%.2f P_err=%.2f I_err=%.2f Out=%.2f pTerm=%.2f iTerm=%.2f dTerm=%.2f, N=%.2f", - sp, pv, unit->PID_err_old, P_err, unit->PID_I_err, Out, pTerm, iTerm, dTerm, pTerm + dTerm + iTerm); -// } - unit->PID_err_old = P_err; + if (debug) + fprintf(stdout, "sp=%.2f pv=%.2f dState=%.2f P_err=%.2f iState=%.2f Out=%.2f\n", + sp, pv, unit->PID_dState, P_err, unit->PID_iState, Out); + syslog(LOG_NOTICE, "sp=%.2f pv=%.2f dState=%.2f P_err=%.2f iState=%.2f Out=%.2f pTerm=%.2f iTerm=%.2f dTerm=%.2f, N=%.2f", + sp, pv, unit->PID_dState, P_err, unit->PID_iState, Out, pTerm, iTerm, dTerm, pTerm + dTerm + iTerm); + unit->PID_dState = pv; if (unit->heater_address) { - if (Out >= 1) { - if (unit->heater_wait < unit->heater_delay) { - unit->heater_wait++; - syslog(LOG_NOTICE, "heater_wait + %d/%d", unit->heater_wait, unit->heater_delay); - } else { - if (! unit->heater_state && ! unit->cooler_state) { - syslog(LOG_NOTICE, "Heater Off => On"); - unit->heater_state = 100; - } - } + if (Out >= 1) { + if (unit->heater_wait < unit->heater_delay) { + unit->heater_wait++; + syslog(LOG_NOTICE, "heater_wait + %d/%d", unit->heater_wait, unit->heater_delay); } else { - if (unit->heater_wait > 0) { - unit->heater_wait--; - syslog(LOG_NOTICE, "heater_wait - %d/%d", unit->heater_wait, unit->heater_delay); - } else { - if (unit->heater_state) { - syslog(LOG_NOTICE, "Heater On => Off"); - unit->heater_state = 0; - } + if (! unit->heater_state && ! unit->cooler_state) { + syslog(LOG_NOTICE, "Heater Off => On"); + unit->heater_state = 100; } } - device_out(unit->heater_address, unit->heater_state); + } else { + if (unit->heater_wait > 0) { + unit->heater_wait--; + syslog(LOG_NOTICE, "heater_wait - %d/%d", unit->heater_wait, unit->heater_delay); + } else { + if (unit->heater_state) { + syslog(LOG_NOTICE, "Heater On => Off"); + unit->heater_state = 0; + } + } + } + device_out(unit->heater_address, unit->heater_state); } if (unit->cooler_address) { - if (Out <= -1) { - if (unit->cooler_wait < unit->cooler_delay) { - unit->cooler_wait++; - syslog(LOG_NOTICE, "cooler_wait + %d/%d", unit->cooler_wait, unit->cooler_delay); - } else { - if (! unit->cooler_state && ! unit->heater_state) { - syslog(LOG_NOTICE, "Cooler Off => On"); - unit->cooler_state = 100; - } - } + if (Out <= -1) { + if (unit->cooler_wait < unit->cooler_delay) { + unit->cooler_wait++; + syslog(LOG_NOTICE, "cooler_wait + %d/%d", unit->cooler_wait, unit->cooler_delay); } else { - if (unit->cooler_wait > 0) { - unit->cooler_wait--; - syslog(LOG_NOTICE, "cooler_wait - %d/%d", unit->cooler_wait, unit->cooler_delay); - } else { - if (unit->cooler_state) { - syslog(LOG_NOTICE, "Cooler On => Off"); - unit->cooler_state = 0; - } + if (! unit->cooler_state && ! unit->heater_state) { + syslog(LOG_NOTICE, "Cooler Off => On"); + unit->cooler_state = 100; } } - device_out(unit->cooler_address, unit->cooler_state); + } else { + if (unit->cooler_wait > 0) { + unit->cooler_wait--; + syslog(LOG_NOTICE, "cooler_wait - %d/%d", unit->cooler_wait, unit->cooler_delay); + } else { + if (unit->cooler_state) { + syslog(LOG_NOTICE, "Cooler On => Off"); + unit->cooler_state = 0; + } + } + } + device_out(unit->cooler_address, unit->cooler_state); } if (unit->heater_address && unit->cooler_address && unit->fan_address) { - /* - * If the temperature difference between air and beer is more then - * xxx degrees, turn the fan on to make an airflow. - * Maybe, run the fan too if the heater is on because the heater in - * most cases will be some sort of radiating heat device. - * For cooling ??? dunno yet. - */ - if (((unit->air_temperature - unit->beer_temperature) > 1000) || + /* + * If the temperature difference between air and beer is more then + * xxx degrees, turn the fan on to make an airflow. + * Maybe, run the fan too if the heater is on because the heater in + * most cases will be some sort of radiating heat device. + * For cooling ??? dunno yet. + */ + if (((unit->air_temperature - unit->beer_temperature) > 1000) || ((unit->air_temperature - unit->beer_temperature) < -1000)) { - if (! unit->fan_state) - syslog(LOG_NOTICE, "Fan Off => On"); - unit->fan_state = 100; - } else { - if (unit->fan_state) - syslog(LOG_NOTICE, "Fan On => Off"); - unit->fan_state = 0; - } - device_out(unit->fan_address, unit->fan_state); + if (! unit->fan_state) + syslog(LOG_NOTICE, "Fan Off => On"); + unit->fan_state = 100; + } else { + if (unit->fan_state) + syslog(LOG_NOTICE, "Fan On => Off"); + unit->fan_state = 0; + } + device_out(unit->fan_address, unit->fan_state); } } else { - P_err = 0.0; - unit->PID_I_err = 0.0; - unit->PID_err_old = 0.0; + P_err = 0.0; + unit->PID_iState = 0.0; + unit->PID_dState = 0.0; } } diff -r 7b0f819a3805 -r 8448fcf3d799 thermferm/thermferm.h --- a/thermferm/thermferm.h Mon Feb 16 19:52:42 2015 +0100 +++ b/thermferm/thermferm.h Mon Feb 16 20:41:09 2015 +0100 @@ -151,8 +151,8 @@ float prof_peak_abs; /* Profile absolute peak temp */ float prof_peak_rel; /* Profile relative peak temp */ time_t prof_primary_done; /* Profile primary is done */ - float PID_I_err; /* PID Integral error */ - float PID_err_old; /* PID old error value */ + double PID_iState; /* PID Integral state */ + double PID_dState; /* PID last measured value */ float PID_Kp; /* PID Kp setting */ float PID_Kd; /* PID Kd setting */ float PID_Ki; /* PID Ki setting */