thermferm/thermferm.c

changeset 179
417ee898fb02
parent 177
48d19ec83a49
child 182
dd69ccba8fa8
--- a/thermferm/thermferm.c	Mon Aug 04 19:31:03 2014 +0200
+++ b/thermferm/thermferm.c	Mon Aug 04 23:34:22 2014 +0200
@@ -232,7 +232,7 @@
     time_t		now, last = (time_t)0;
     units_list		*unit;
     int			rc, run = 1, seconds = 0, minutes = 0;
-    float		err = 0.0, sp, pv, P_err, I_err, D_err, Out;
+    float		err = 0.0, sp, pv, P_err, I_err = 0.0, D_err, Out;
 #ifdef HAVE_WIRINGPI_H
     struct tm		*tm;
     int			row;
@@ -346,15 +346,15 @@
 		for (unit = Config.units; unit; unit = unit->next) {
 		    if (unit->mode != UNITMODE_OFF) {
 
-			if (0) {
+			if (unit->mode != UNITMODE_NONE) {
 			    /*
 			     * PID controller
 			     */
 			    sp = unit->beer_set;
-			    pv = unit->beer_temperature;
+			    pv = unit->beer_temperature / 1000.0;
 			    if (unit->mode == UNITMODE_FRIDGE) {
 				sp = unit->fridge_set;
-				pv = unit->air_temperature;
+				pv = unit->air_temperature / 1000.0;
 			    } else if (unit->mode == UNITMODE_PROFILE) {
 				sp = unit->prof_target;
 			    }
@@ -367,11 +367,28 @@
 			    I_err += unit->PID_err_old;
 			    D_err = err - unit->PID_err_old;
 
-			    Out = (0.1*P_err) + (0.3*I_err) + (0.02*D_err);
-			    //     Kp            Ki            Kd
+			    /*
+			     * A postive value means heating, a negative value cooling.
+			     */
+			    Out = (5.0*P_err) + (0.25*I_err) + (-1.5*D_err);
+			    //     Kp 0.1        Ki 0.3        Kd 0.02
 			    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, I_err, D_err, Out);
+			    if (unit->heater_address) {
+				if (Out >= 2)
+				    unit->heater_state = 100;
+				else
+				    unit->heater_state = 0;
+				device_out(unit->heater_address, unit->heater_state);
+			    }
+			    if (unit->cooler_address) {
+				if (Out <= -2)
+				    unit->cooler_state = 100;
+				else
+				    unit->cooler_state = 0;
+				device_out(unit->cooler_address, unit->cooler_state);
+			    }
 			}
 
 			snprintf(target, 39, "NA");

mercurial