thermferm/thermferm.c

changeset 184
db997a04fde3
parent 182
dd69ccba8fa8
child 185
4f34271cf1e7
--- a/thermferm/thermferm.c	Tue Aug 05 15:45:23 2014 +0200
+++ b/thermferm/thermferm.c	Tue Aug 05 16:24:17 2014 +0200
@@ -319,19 +319,6 @@
 	}
     }
 
-#ifdef HAVE_WIRINGPI_H
-    rc = piThreadCreate(my_units_loop);
-#else
-    rc = pthread_create(&threads[t], NULL, my_units_loop, (void *)t );
-#endif
-    if (rc) {
-	fprintf(stderr, "my_units_loop thread didn't start rc=%d\n", rc);
-	syslog(LOG_NOTICE, "my_units_loop thread didn't start rc=%d", rc);
-#ifndef HAVE_WIRINGPI_H
-    } else {
-	t++;
-#endif
-    }
 
 #ifdef HAVE_WIRINGPI_H
     lcd_buf_write(1, (char *)"    ThermFerm   ");
@@ -365,14 +352,14 @@
 
 	    for (unit = Config.units; unit; unit = unit->next) {
 #ifdef HAVE_WIRINGPI_H
-		    lcd_buf_write(row++, "Unit %s              ", unit->name);
-		    lcd_buf_write(row++, "Mode %s              ", UNITMODE[unit->mode]);
-		    if (unit->air_address) {
-		    	lcd_buf_write(row++, " Air %.3f %cC         ", unit->air_temperature / 1000.0, 0xdf);
-		    }
-		    if (unit->beer_address) {
-			lcd_buf_write(row++, "Beer %.3f %cC         ", unit->beer_temperature / 1000.0, 0xdf);
-		    }
+		lcd_buf_write(row++, "Unit %s              ", unit->name);
+		lcd_buf_write(row++, "Mode %s              ", UNITMODE[unit->mode]);
+		if (unit->air_address) {
+		    lcd_buf_write(row++, " Air %.3f %cC         ", unit->air_temperature / 1000.0, 0xdf);
+		}
+		if (unit->beer_address) {
+		    lcd_buf_write(row++, "Beer %.3f %cC         ", unit->beer_temperature / 1000.0, 0xdf);
+		}
 #endif
 
 		if (unit->air_address) {
@@ -424,6 +411,15 @@
 			unit->beer_state = 2;
 		    }
 		}
+
+		/*
+		 * Manual switching
+		 */
+		if (unit->mode == UNITMODE_NONE) {
+		    device_out(unit->heater_address, unit->heater_state);
+		    device_out(unit->cooler_address, unit->cooler_state);
+		    device_out(unit->fan_address, unit->fan_state);
+		}
 	    }
 
 #ifdef HAVE_WIRINGPI_H
@@ -435,52 +431,53 @@
 		piddelay = 0;
 
 		for (unit = Config.units; unit; unit = unit->next) {
-		    if (unit->mode != UNITMODE_OFF) {
-
-			if (unit->mode != UNITMODE_NONE) {
-			    /*
-			     * PID controller
-			     */
-			    sp = unit->beer_set;
-			    pv = unit->beer_temperature / 1000.0;
-			    if (unit->mode == UNITMODE_FRIDGE) {
+		    if ((unit->mode == UNITMODE_FRIDGE) || (unit->mode == UNITMODE_BEER) || (unit->mode == UNITMODE_PROFILE)) {
+			/*
+			 * PID controller
+			 */
+			sp = unit->beer_set;
+			pv = unit->beer_temperature / 1000.0;
+			if (unit->mode == UNITMODE_FRIDGE) {
 				sp = unit->fridge_set;
 				pv = unit->air_temperature / 1000.0;
-			    } else if (unit->mode == UNITMODE_PROFILE) {
+			} else if (unit->mode == UNITMODE_PROFILE) {
 				sp = unit->prof_target;
-			    }
+			}
 
-			    unit->PID_err_old = err;
-			    err = sp - pv;
-			    if (err < unit->idle_rangeH && err > unit->idle_rangeL)
-				err = 0;
-			    P_err = err;
-			    I_err += unit->PID_err_old;
-			    D_err = err - unit->PID_err_old;
+			unit->PID_err_old = err;
+			err = sp - pv;
+			if (err < unit->idle_rangeH && err > unit->idle_rangeL)
+			    err = 0;
+			P_err = err;
+			I_err += unit->PID_err_old;
+			D_err = err - unit->PID_err_old;
 
-			    /*
-			     * 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",
+			/*
+			 * 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);
-			    }
+			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);
+			}
+		    } else {
+			err = 0.0;
+			I_err = 0.0;
+			unit->PID_err_old = 0.0;
 		    }
 		}
 	    }

mercurial