PID changes and algorithm tuning.

Sun, 15 Feb 2015 23:09:00 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 15 Feb 2015 23:09:00 +0100
changeset 311
f3b0e9ac9bcb
parent 310
53774295e14a
child 312
7b0f819a3805

PID changes and algorithm tuning.

thermferm/server.c file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
www-thermferm/units.php file | annotate | diff | comparison | revisions
--- a/thermferm/server.c	Sun Feb 15 20:38:54 2015 +0100
+++ b/thermferm/server.c	Sun Feb 15 23:09:00 2015 +0100
@@ -1616,8 +1616,8 @@
 		srv_send((char *)"IDLE_RANGE_L,%.1f", unit->idle_rangeL);
 		srv_send((char *)"IDLE_RANGE_H,%.1f", unit->idle_rangeH);
 		srv_send((char *)"PID_KP,%.2f", unit->PID_Kp);
+		srv_send((char *)"PID_KI,%.2f", unit->PID_Ki);
 		srv_send((char *)"PID_KD,%.2f", unit->PID_Kd);
-		srv_send((char *)"PID_KI,%.2f", unit->PID_Ki);
 		srv_send((char *)".");
 		return 1;
 	    }
--- a/thermferm/thermferm.c	Sun Feb 15 20:38:54 2015 +0100
+++ b/thermferm/thermferm.c	Sun Feb 15 23:09:00 2015 +0100
@@ -836,7 +836,7 @@
     prof_step		*step;
     int			rc, run = 1, seconds = 0, minutes = 0, piddelay = 0, temp, deviation;
     int			run_seconds, run_minutes, run_hours, tot_minutes;
-    float		err = 0.0, sp, pv, P_err, D_err, Out;
+    float		sp, pv, P_err = 0.0, D_err, Out;
 #ifdef HAVE_WIRINGPI_H
     struct tm		*tm;
     int			row, key;
@@ -1303,7 +1303,7 @@
 	    }
 
 	    piddelay++;
-	    if (piddelay == 15) {
+	    if (piddelay == 5) {
 		piddelay = 0;
 
 		for (unit = Config.units; unit; unit = unit->next) {
@@ -1320,21 +1320,21 @@
 				sp = unit->prof_target;
 			}
 
-//			unit->PID_err_old = err;
-			err = sp - pv;
-			if (err < unit->idle_rangeH && err > unit->idle_rangeL) {
-			    err = 0;
-			    unit->PID_I_err -= unit->PID_err_old;
-			} else {
-			    unit->PID_I_err += unit->PID_err_old;
+			P_err = sp - pv;
+			if (P_err < unit->idle_rangeH && P_err > unit->idle_rangeL) {
+			    P_err = 0.0;
 			}
-			/* Limit intergral error */
-			if (unit->PID_I_err < -10.0)
-			    unit->PID_I_err = -10.0;
-			if (unit->PID_I_err > 10.0)
-			    unit->PID_I_err = 10.0;
-			P_err = err;
-			D_err = err - unit->PID_err_old;
+			unit->PID_I_err += (unit->PID_Ki * P_err);
+//			    unit->PID_I_err -= unit->PID_err_old;
+//			} else {
+//			    unit->PID_I_err += unit->PID_err_old;
+//			}
+			/* 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;
+			D_err = P_err - unit->PID_err_old;
 
 			/*
 			 * A postive value means heating, a negative value cooling.
@@ -1343,20 +1343,22 @@
 			 * Increase Kd until a little damping.
 			 * Increase Ki after Kp and Kd are set until longterm convergence.
 			 */
-			Out = (10.0*P_err) + (0.0*unit->PID_I_err) + (0.0*D_err);
-			//Out = (10.0*P_err) + (0.1*unit->PID_I_err) + (5*D_err);
-			//     Kp 0.1        Ki 0.3                   Kd 0.02
-			if (err != 0.0) {
+			Out = (unit->PID_Kp * P_err) + unit->PID_I_err - (unit->PID_Kd * D_err);
+			if (Out > 100.0)
+			    Out = 100.0;
+			if (Out < -100.0)
+			    Out = -100.0;
+//			if (P_err != 0.0) {
 			    if (debug)
-			    	fprintf(stdout, "sp=%.2f pv=%.2f err_old=%.2f err=%.2f P_err=%.2f I_err=%.2f D_err=%.2f Out=%.2f\n",
-					sp, pv, unit->PID_err_old, err, P_err, unit->PID_I_err, D_err, Out);
-			    syslog(LOG_NOTICE, "sp=%.2f pv=%.2f err_old=%.2f err=%.2f P_err=%.2f I_err=%.2f D_err=%.2f Out=%.2f",
-					sp, pv, unit->PID_err_old, err, P_err, unit->PID_I_err, D_err, Out);
-			}
-			unit->PID_err_old = err;
+			    	fprintf(stdout, "sp=%.2f pv=%.2f err_old=%.2f P_err=%.2f I_err=%.2f D_err=%.2f Out=%.2f\n",
+					sp, pv, unit->PID_err_old, P_err, unit->PID_I_err, D_err, Out);
+			    syslog(LOG_NOTICE, "sp=%.2f pv=%.2f err_old=%.2f P_err=%.2f I_err=%.2f D_err=%.2f Out=%.2f",
+					sp, pv, unit->PID_err_old, P_err, unit->PID_I_err, D_err, Out);
+//			}
+			unit->PID_err_old = P_err;
 
 			if (unit->heater_address) {
-			    if (Out >= 2) {
+			    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);
@@ -1380,7 +1382,7 @@
 			    device_out(unit->heater_address, unit->heater_state);
 			}
 			if (unit->cooler_address) {
-			    if (Out <= -2) {
+			    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);
@@ -1424,7 +1426,7 @@
 			    device_out(unit->fan_address, unit->fan_state);
 			}
 		    } else {
-			err = 0.0;
+			P_err = 0.0;
 			unit->PID_I_err = 0.0;
 			unit->PID_err_old = 0.0;
 		    }
--- a/www-thermferm/units.php	Sun Feb 15 20:38:54 2015 +0100
+++ b/www-thermferm/units.php	Sun Feb 15 23:09:00 2015 +0100
@@ -172,7 +172,7 @@
 	isset($_POST['BeerAddress']) && isset($_POST['HeaterAddress']) && isset($_POST['CoolerAddress']) && isset($_POST['LightAddress']) &&
 	isset($_POST['HeaterDelay']) && isset($_POST['CoolerDelay']) && isset($_POST['LightDelay']) && isset($_POST['PSUAddress']) &&
 	isset($_POST['FanAddress']) && isset($_POST['DoorAddress']) && isset($_POST['TempSetMin']) && isset($_POST['TempSetMax']) &&
-	isset($_POST['PID_Kp']) && isset($_POST['PID_Kd']) && isset($_POST['PID_Li']) &&
+	isset($_POST['PID_Kp']) && isset($_POST['PID_Kd']) && isset($_POST['PID_Ki']) &&
 	isset($_POST['IdleRangeL']) && isset($_POST['IdleRangeH']) && isset($_POST['key']) && isset($_POST['command'])) {
 
 	if ($_POST['key'] == 'Cancel')

mercurial