Fixed switching problems

Tue, 05 Aug 2014 16:24:17 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 05 Aug 2014 16:24:17 +0200
changeset 184
db997a04fde3
parent 183
6c3accd7e60e
child 185
4f34271cf1e7

Fixed switching problems

thermferm/Makefile file | annotate | diff | comparison | revisions
thermferm/server.c file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
--- a/thermferm/Makefile	Tue Aug 05 15:45:23 2014 +0200
+++ b/thermferm/Makefile	Tue Aug 05 16:24:17 2014 +0200
@@ -63,6 +63,6 @@
 lcd-pcf8574.o: thermferm.h lcd-pcf8574.h
 thermferm.o: lock.h logger.h rdconfig.h devices.h server.h thermferm.h lcd-pcf8574.h lcd-buffer.h futil.h xutil.h
 xutil.o: thermferm.h xutil.h
-server.o: rdconfig.h thermferm.h logger.h server.h xutil.h
+server.o: rdconfig.h thermferm.h logger.h server.h xutil.h devices.h
 rdconfig.o: rdconfig.h thermferm.h futil.h xutil.h
 # End of generated dependencies
--- a/thermferm/server.c	Tue Aug 05 15:45:23 2014 +0200
+++ b/thermferm/server.c	Tue Aug 05 16:24:17 2014 +0200
@@ -23,6 +23,7 @@
 #include "rdconfig.h"
 #include "thermferm.h"
 #include "logger.h"
+#include "devices.h"
 #include "server.h"
 #include "xutil.h"
 
@@ -611,63 +612,6 @@
 
 
 /*
- * Set new operating mode
- * MODE OFF|NONE|BEER|FRIDGE|PROFILE
- */
-int cmd_mode(char *buf)
-{
-    char	*opt;
-    units_list	*unit;
-
-    opt = strtok(buf, " \0");
-    opt = strtok(NULL, "\0");
-
-    if (opt == NULL) {
-	srv_send((char *)"502 Missing command option");
-	return 1;
-    }
-
-    if (current_unit == NULL) {
-	srv_send((char *)"401 No fermenter unit selected");
-	return 1;
-    }
-    for (unit = Config.units; unit; unit = unit->next) {
-	if (strcmp(current_unit, unit->uuid) == 0)
-	    break;
-    }
-
-    if (strcmp(opt, (char *)"OFF") == 0) {
-	if (unit->mode != UNITMODE_OFF) {
-	    unit->mode = UNITMODE_OFF;
-	    syslog(LOG_NOTICE, "Unit %s mode set to OFF", unit->name);
-	}
-	srv_send((char *)"218 Unit mode to OFF");
-	return 0;
-
-    } else if (strcmp(opt, (char *)"NONE") == 0) {
-	if (unit->mode != UNITMODE_NONE) {
-	    unit->mode = UNITMODE_NONE;
-	    syslog(LOG_NOTICE, "Unit %s mode set to NONE", unit->name);
-	    initlog(unit->name);
-	}
-	srv_send((char *)"218 Unit mode to NONE");
-	return 0;
-
-    } else if (strcmp(opt, (char *)"FRIDGE") == 0) {
-
-    } else if (strcmp(opt, (char *)"BEER") == 0) {
-
-    } else if (strcmp(opt, (char *)"PROFILE") == 0) {
-
-    }
-
-    srv_send((char *)"502 Unknown command option");
-    return 1;
-}
-
-
-
-/*
  * PROFILE		List profile status of current unit
  * PROFILE uuid,name	Rename profile name
  * PROFILE GETS uuid	Get profile steps list
@@ -1289,6 +1233,9 @@
 					unit->mode = i;
 					/* Allways turn everything off after a mode change */
 					unit->heater_state = unit->cooler_state = unit->fan_state = 0;
+					device_out(unit->heater_address, unit->heater_state);
+					device_out(unit->cooler_address, unit->cooler_state);
+					device_out(unit->fan_address, unit->fan_state);
 					break;
 				    }
 				}
@@ -1412,7 +1359,6 @@
 		srv_send((char *)"LIST                          List all fermenter units");
 		srv_send((char *)"LIST LOG                      List logfile data in 1 hour lines");
 		srv_send((char *)"LIST PROFILES                 List available profiles");
-		srv_send((char *)"MODE OFF|NONE|BEER|FRIDGE|PROFILE");
 //		srv_send((char *)"PROFILE                       Profile status of current unit");
 		srv_send((char *)"PROFILE uuid,name             Profile rename");
 		srv_send((char *)"PROFILE GETS uuid             Profile get steps list");
@@ -1443,9 +1389,6 @@
 #endif
 	    } else if (strncmp(buf, "LIST", 4) == 0) {
 		cmd_list(buf);
-	    } else if (strncmp(buf, "MODE", 4) == 0) {
-		if (cmd_mode(buf) == 0)
-		    wrconfig();
 	    } else if (strncmp(buf, "PROFILE", 7) == 0) {
 		if (cmd_profile(buf) == 0)
 		    wrconfig();
--- 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