# HG changeset patch # User Michiel Broek # Date 1407246269 -7200 # Node ID dd69ccba8fa88d0970a099492a859a78716b1b27 # Parent 4099412fca0919e962d62bdb6d8e03267ac4f65b Removing units thread diff -r 4099412fca09 -r dd69ccba8fa8 thermferm/thermferm.c --- a/thermferm/thermferm.c Tue Aug 05 11:57:05 2014 +0200 +++ b/thermferm/thermferm.c Tue Aug 05 15:44:29 2014 +0200 @@ -29,7 +29,6 @@ #include "lcd-pcf8574.h" #include "lcd-buffer.h" #include "futil.h" -#include "units.h" #include "xutil.h" @@ -104,6 +103,24 @@ +int read_w1_28(char *address, int *val) +{ + devices_list *device; + int tmp; + + for (device = Config.devices; device; device = device->next) { + if (strcmp(address, device->uuid) == 0) { + tmp = device->value; + *val = tmp; + return device->present; + } + } + + return DEVPRESENT_NO; +} + + + int main(int argc, char *argv[]) { int rc, c, i; @@ -231,7 +248,7 @@ char buf[1024], *filename, target[40], heater[40], cooler[40], fan[40], door[40]; time_t now, last = (time_t)0; units_list *unit; - int rc, run = 1, seconds = 0, minutes = 0; + int rc, run = 1, seconds = 0, minutes = 0, piddelay = 0, temp, deviation; float err = 0.0, sp, pv, P_err, I_err = 0.0, D_err, Out; #ifdef HAVE_WIRINGPI_H struct tm *tm; @@ -281,6 +298,27 @@ #endif } + /* + * Initialize units for processing + */ + for (unit = Config.units; unit; unit = unit->next) { + /* + * Safety, turn everything off + */ + unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; + if (unit->profile && (int)unit->prof_started && (unit->mode == UNITMODE_PROFILE)) { + syslog(LOG_NOTICE, "Starting unit %s profile %s", unit->name, unit->profile); + } else if (unit->mode == UNITMODE_BEER) { + syslog(LOG_NOTICE, "Starting unit %s beer cooler at %.1f degrees", unit->name, unit->beer_set); + } else if (unit->mode == UNITMODE_FRIDGE) { + syslog(LOG_NOTICE, "Starting unit %s as refridgerator at %.1f degrees", unit->name, unit->fridge_set); + } else if (unit->mode == UNITMODE_NONE) { + syslog(LOG_NOTICE, "Starting unit %s in inactive state", unit->name); + } else { + syslog(LOG_NOTICE, "Starting unit %s in off state", unit->name); + } + } + #ifdef HAVE_WIRINGPI_H rc = piThreadCreate(my_units_loop); #else @@ -323,9 +361,10 @@ tm = localtime(&now); lcd_buf_write(row++, " %02d-%02d-%04d ", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900); lcd_buf_write(row++, " %02d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec); +#endif for (unit = Config.units; unit; unit = unit->next) { -// if (unit->mode != UNITMODE_OFF) { +#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) { @@ -334,14 +373,66 @@ if (unit->beer_address) { lcd_buf_write(row++, "Beer %.3f %cC ", unit->beer_temperature / 1000.0, 0xdf); } -// } +#endif + + if (unit->air_address) { + rc = read_w1_28(unit->air_address, &temp); + if (rc == DEVPRESENT_YES) { + /* + * It is possible to have read errors or extreme values. + * This can happen with bad connections so we compare the + * value with the previous one. If the difference is too + * much, we don't send that value. That also means that if + * the next value is ok again, it will be marked invalid too. + * Maximum error is 40 degrees for now. + */ + deviation = 40000; + if ((unit->air_temperature == 0) || + (unit->air_temperature && (temp > (int)unit->air_temperature - deviation) && (temp < ((int)unit->air_temperature + deviation)))) { + unit->air_temperature = temp; + unit->air_state = 0; + } else { + syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->air_temperature, temp); + if (debug) { + fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->air_temperature, temp); + } + } + } else if (rc == DEVPRESENT_ERROR) { + unit->air_state = 1; + } else { + unit->air_state = 2; + } + } + + if (unit->beer_address) { + rc = read_w1_28(unit->beer_address, &temp); + if (rc == DEVPRESENT_YES) { + deviation = 40000; + if ((unit->beer_temperature == 0) || + (unit->beer_temperature && (temp > (int)unit->beer_temperature - deviation) && (temp < ((int)unit->beer_temperature + deviation)))) { + unit->beer_temperature = temp; + unit->beer_state = 0; + } else { + syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->beer_temperature, temp); + if (debug) { + fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->beer_temperature, temp); + } + } + } else if (rc == DEVPRESENT_ERROR) { + unit->beer_state = 1; + } else { + unit->beer_state = 2; + } + } } +#ifdef HAVE_WIRINGPI_H lcd_buf_show(); #endif - if (seconds == 60) { - seconds = 0; + piddelay++; + if (piddelay == 15) { + piddelay = 0; for (unit = Config.units; unit; unit = unit->next) { if (unit->mode != UNITMODE_OFF) { @@ -370,7 +461,7 @@ /* * A postive value means heating, a negative value cooling. */ - Out = (5.0*P_err) + (0.25*I_err) + (-1.5*D_err); + 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", @@ -390,6 +481,18 @@ device_out(unit->cooler_address, unit->cooler_state); } } + } + } + } + + if (seconds == 60) { + seconds = 0; + + /* + * Log temperature and status every minute if unit is active. + */ + for (unit = Config.units; unit; unit = unit->next) { + if (unit->mode != UNITMODE_OFF) { snprintf(target, 39, "NA"); snprintf(heater, 39, "NA"); @@ -442,6 +545,20 @@ } while (run); + /* + * Stop units processing in a neat way + */ + for (unit = Config.units; unit; unit = unit->next) { + /* + * Turn everything off + */ + unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_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); + syslog(LOG_NOTICE, "Stopped unit %s mode %s", unit->name, UNITMODE[unit->mode]); + } + if (debug) fprintf(stdout, (char *)"Out of loop\n"); diff -r 4099412fca09 -r dd69ccba8fa8 thermferm/units.c --- a/thermferm/units.c Tue Aug 05 11:57:05 2014 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,183 +0,0 @@ -/***************************************************************************** - * Copyright (C) 2014 - * - * Michiel Broek - * - * This file is part of the mbsePi-apps - * - * This is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2, or (at your option) any - * later version. - * - * mbsePi-apps is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with EC-65K; see the file COPYING. If not, write to the Free - * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - *****************************************************************************/ - -#include "thermferm.h" -#include "devices.h" -#include "units.h" -#include "xutil.h" - - -extern int debug; -extern sys_config Config; -extern int my_shutdown; - -extern const char UNITMODE[5][8]; - - - -int read_w1_28(char *address, int *val) -{ - devices_list *device; - int tmp; - - for (device = Config.devices; device; device = device->next) { - if (strcmp(address, device->uuid) == 0) { - tmp = device->value; - *val = tmp; - return device->present; - } - } - - return DEVPRESENT_NO; -} - - - -#ifdef HAVE_WIRINGPI_H -PI_THREAD (my_units_loop) -#else -void *my_units_loop(void *threadid) -#endif -{ - units_list *unit; - int rc, temp, deviation; - - /* - * Initialize units for processing - */ - for (unit = Config.units; unit; unit = unit->next) { - /* - * Safety, turn everything off - */ - unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; - if (unit->profile && (int)unit->prof_started && (unit->mode == UNITMODE_PROFILE)) { - syslog(LOG_NOTICE, "Starting unit %s profile %s", unit->name, unit->profile); - } else if (unit->mode == UNITMODE_BEER) { - syslog(LOG_NOTICE, "Starting unit %s beer cooler at %.1f degrees", unit->name, unit->beer_set); - } else if (unit->mode == UNITMODE_FRIDGE) { - syslog(LOG_NOTICE, "Starting unit %s as refridgerator at %.1f degrees", unit->name, unit->fridge_set); - } else if (unit->mode == UNITMODE_NONE) { - syslog(LOG_NOTICE, "Starting unit %s in inactive state", unit->name); - } else { - syslog(LOG_NOTICE, "Starting unit %s in off state", unit->name); - } - } - - syslog(LOG_NOTICE, "Thread my_units_loop started"); - if (debug) - fprintf(stdout, "Thread my_units_loop started\n"); - - /* - * Loop forever until the external shutdown variable is set. - */ - for (;;) { - - if (my_shutdown) - break; - - /* - * Update the state of all fermenter units - */ - for (unit = Config.units; unit; unit = unit->next) { - - if (my_shutdown) - break; - - if (unit->air_address) { - rc = read_w1_28(unit->air_address, &temp); - if (rc == DEVPRESENT_YES) { - /* - * It is possible to have read errors or extreme values. - * This can happen with bad connections so we compare the - * value with the previous one. If the difference is too - * much, we don't send that value. That also means that if - * the next value is ok again, it will be marked invalid too. - * Maximum error is 40 degrees for now. - */ - deviation = 40000; - if ((unit->air_temperature == 0) || - (unit->air_temperature && (temp > (int)unit->air_temperature - deviation) && (temp < ((int)unit->air_temperature + deviation)))) { - unit->air_temperature = temp; - unit->air_state = 0; - } else { - syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->air_temperature, temp); - if (debug) { - fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->air_temperature, temp); - } - } - } else if (rc == DEVPRESENT_ERROR) { - unit->air_state = 1; - } else { - unit->air_state = 2; - } - } - if (my_shutdown) - break; - - if (unit->beer_address) { - rc = read_w1_28(unit->beer_address, &temp); - if (rc == DEVPRESENT_YES) { - deviation = 40000; - if ((unit->beer_temperature == 0) || - (unit->beer_temperature && (temp > (int)unit->beer_temperature - deviation) && (temp < ((int)unit->beer_temperature + deviation)))) { - unit->beer_temperature = temp; - unit->beer_state = 0; - } else { - syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->beer_temperature, temp); - if (debug) { - fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->beer_temperature, temp); - } - } - } else if (rc == DEVPRESENT_ERROR) { - unit->beer_state = 1; - } else { - unit->beer_state = 2; - } - } - if (my_shutdown) - break; - - } - usleep(100000); - } - - /* - * Stop units processing in a neat way - */ - for (unit = Config.units; unit; unit = unit->next) { - /* - * Turn everything off - */ - unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_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); - syslog(LOG_NOTICE, "Stopped unit %s mode %s", unit->name, UNITMODE[unit->mode]); - } - - syslog(LOG_NOTICE, "Thread my_units_loop stopped"); - if (debug) - fprintf(stdout, "Thread my_units_loop stopped\n"); - return 0; -} - - diff -r 4099412fca09 -r dd69ccba8fa8 thermferm/units.h --- a/thermferm/units.h Tue Aug 05 11:57:05 2014 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -#ifndef UNITS_H -#define UNITS_H - - -#ifdef HAVE_WIRINGPI_H -PI_THREAD (my_units_loop); -#else -void *my_units_loop(void *); -#endif - -#endif