view tempctrl.c @ 63:4d914a1fd487

Fix printf typo from last commit. Cast away volatile to avoid compiler warning.
author darius@Inchoate
date Thu, 30 Oct 2008 20:09:32 +1030
parents e955aa7047ed
children 1ef17cd8af7a
line wrap: on
line source

/*
 * Temperature control logic
 *
 * Copyright (c) 2008
 *      Daniel O'Connor <darius@dons.net.au>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <avr/wdt.h>
#include <util/crc16.h>

#include "cons.h"
#include "1wire.h"
#include "tempctrl.h"
#include "ds1307.h"

typedef struct {
    int32_t	sec;
    int32_t	usec;
} time_t;

/* Holds all the settings needed */
typedef struct {
    uint8_t	fermenter_ROM[8];
    uint8_t	fridge_ROM[8];
    uint8_t	ambient_ROM[8];
    int16_t	target_temp;
    uint16_t	hysteresis;
    /* How much to under/overshoot on heating/cooling */
    int16_t	minheatovershoot;
    int16_t	mincoolovershoot;

    /* Minimum time the cooler can be on/off */
    int16_t	mincoolontime;
    int16_t	mincoolofftime;

    /* Minimum time the heater can be on/off */
    int16_t	minheatontime;
    int16_t	minheatofftime;

#define TC_MODE_AUTO	'a'	/* Automatic control */
#define TC_MODE_HEAT	'h'	/* Force heating */
#define TC_MODE_COOL	'c'	/* Force cooling */
#define TC_MODE_IDLE	'i'	/* Force idle */
#define TC_MODE_NOTHING	'n'	/* Do nothing (like idle but log nothing) */
    char	mode;
    
    /* Bit patterns for various modes */
    uint8_t	coolbits;
    uint8_t	heatbits;
    uint8_t	idlebits;

    /* Check/stale times */
    int16_t	check_interval;
    int16_t	stale_factor;

#ifndef WITHUSB
    /* Beep if stale */
    int8_t	dobeep;
#endif
} __attribute__((packed)) settings_t;

/* Current settings in RAM */
static settings_t	settings;

/* Our map of EEPROM */
struct {
    settings_t	settings;
    uint16_t	crc;
} ee_area __attribute__((section(".eeprom")));

/* Defaults that are shoved into EEPROM if it isn't inited */
const PROGMEM settings_t	default_settings = {
    .fermenter_ROM = { 0x10, 0xeb, 0x48, 0x21, 0x01, 0x08, 0x00, 0xdf },
    .fridge_ROM =  { 0x10, 0xa6, 0x2a, 0xc4, 0x00, 0x08, 0x00, 0x11 },
    .ambient_ROM = { 0x10, 0x97, 0x1b, 0xfe, 0x00, 0x08, 0x00, 0xd1 },
    .target_temp = 1400,
    .hysteresis = 100,
    .minheatovershoot = 50,
    .mincoolovershoot = -50,
    .mincoolontime = 300,
    .mincoolofftime = 600,
    .minheatontime = 60,
    .minheatofftime = 60,
    .mode = TC_MODE_IDLE,
    .coolbits = _BV(6),
    .heatbits = _BV(7),
    .idlebits = 0x00,
    .check_interval = 10,
    .stale_factor = 3,
#ifndef WITHUSB
    .dobeep = 0
#endif
};

/* Local variable declarations */
volatile static time_t	now;
#ifndef WITHUSB
volatile static uint8_t beeping = 0;
volatile static uint8_t lasttoggle = 0;
#endif

/* Local function prototypes */
static void		tempctrl_load_or_init_settings(void);
static void		tempctrl_default_settings(void);
static void		tempctrl_write_settings(void);
static void		setstate(char state);
static const PROGMEM char*state2long(char s);
static void		printtemp(char *name, int tmp, char *trailer);

/* 
 * tempctrl_init
 *
 * Setup timer, should be called with interrupts disabled.
 *
 */
void
tempctrl_init(void) {
    /* Setup timer */
    /* 16Mhz / 1024 = 15625 Hz / 125 = 125 Hz = IRQ every 8 ms */

    /* CTC mode, no output on pin, Divide clock by 1024 */
    TCCR2 = _BV(WGM21)| _BV(CS22) | _BV(CS21) | _BV(CS20);
    
    /* Compare with ... */
    OCR2 = 125;
    
    /* Enable interrupt for match on A */
    TIMSK = _BV(OCIE2);

    now.sec = 0;
    now.usec = 0;

    tempctrl_load_or_init_settings();
}

/*
 * Timer 2 Compare IRQ
 *
 * Update time counter
 */

ISR(TIMER2_COMP_vect) {
    wdt_reset();

    now.usec += 8000; /* 1000000 * 1 / F_CPU / 1024 / 125 */
    while (now.usec > 1000000) {
	now.usec -= 1000000;
	now.sec++;
    }

#ifndef WITHUSB
    if (beeping) {
	lasttoggle++;
	// 63 * 8ms = ~0.5s
	if (lasttoggle > 63) {
	    DDRB ^= _BV(PB3);
	    lasttoggle = 0;
	}
    } else {
	DDRB &= ~_BV(PB3);
	lasttoggle = 0;
    }
#endif    
}

/* 
 * tempctrl_update
 *
 * Should be called in a normal context, could run things that take a long time.
 * (ie 1wire bus stuff)
 *
 */
void
tempctrl_update(void) {
    /* State variables */
    static int32_t	checktime = 0;		// Time of next check
    static int32_t	lastdata = INT32_MIN;	// Last time we got data

    static int16_t	fermenter_temp = 0;	// Fermenter temperature
    static int16_t	fridge_temp = 0;	// Fridge temperature
    static int16_t	ambient_temp = 0;	// Ambient temperature
    static int32_t	lastheaton = INT32_MIN;	// Last time the heater was on
    static int32_t	lastheatoff = INT32_MIN;// Last time the heater was off
    static int32_t	lastcoolon = INT32_MIN;	// Last time the cooler was on
    static int32_t	lastcooloff = INT32_MIN;// Last time the cooler was off
    static char		currstate = 'i';	// Current state
    /* We init to times to INT32_MIN so that things function properly when
     * now < settings.minheat/cool/on/offtime */
    
    /* Temporary variables */
    int32_t		t, tempt;
    int16_t		diff;
    char		nextstate;
    int			forced;
    int			stale;
    
    t = gettod();
    /* Time to check temperatures? */
    if (t < checktime)
	return;
    
    checktime = t + settings.check_interval;

    /* Don't do any logging, just force idle and leave */
    if (settings.mode == TC_MODE_NOTHING) {
	nextstate = 'i';
	goto setstate;
    }
    
    /* Update our temperatures */
    tempt = OWGetTemp(settings.fermenter_ROM);
    fridge_temp = OWGetTemp(settings.fridge_ROM);
    ambient_temp = OWGetTemp(settings.ambient_ROM);
    
    /* We only care about this one, only update the value we decide on
     * only if it is valid
     */
    if (fermenter_temp > OW_TEMP_BADVAL) {
	fermenter_temp = tempt;
	lastdata = t;
    }
    
    /* Check for stale data */
    if (lastdata + (settings.check_interval * settings.stale_factor) < t)
	stale = 1;
    else
	stale = 0;

    /* Default to remaining as we are */
    nextstate = '-';

    /* Temperature diff, -ve => too cold, +ve => too warm */
    diff = fermenter_temp - settings.target_temp;

    switch (currstate) {
	case 'i':
	    /* If we're idle then only heat or cool if the temperate difference is out of the
	     * hysteresis band
	     */
	    if (abs(diff) > settings.hysteresis) {
		if (diff < 0 && settings.minheatofftime + lastheatoff < t)
		    nextstate = 'h';
		else if (diff > 0 && settings.mincoolofftime + lastcooloff < t)
		    nextstate = 'c';
	    }
	    break;
	    
	case 'c':
	    /* Work out if we should go idle (based on min on time & overshoot) */
	    if (diff + settings.mincoolovershoot < 0 &&
		settings.mincoolontime + lastcoolon < t)
		nextstate = 'i';
	    break;

	case 'h':
	    if (diff - settings.minheatovershoot > 0 &&
		settings.minheatontime + lastheaton < t)
		nextstate = 'i';
	    break;

	default:
	    printf_P(PSTR("\r\nUnknown state %c, going to idle\n"), currstate);
	    nextstate = 'i';
	    break;
    }

    /* Override if we have stale data */
    if (stale)
	nextstate = 'i';
    
#ifndef WITHUSB
/* Handle beeping */
    if (settings.dobeep && stale)
	beeping = 1;
    else
	beeping = 0;
#endif
	
    /* Handle state forcing */
    if (settings.mode != TC_MODE_AUTO)
	forced = 1;
    else
	forced = 0;
    
    if (settings.mode == TC_MODE_IDLE)
	nextstate = 'i';
    else if (settings.mode == TC_MODE_HEAT)
	nextstate = 'h';
    else if (settings.mode == TC_MODE_COOL)
	nextstate = 'c';

    // Keep track of when we last turned things on or off
    switch  (nextstate) {
	case 'c':
	    if (currstate == 'h')
		lastheatoff = t;
	    lastcoolon = t;
	    break;

	case 'h':
	    if (currstate == 'c')
		lastcooloff = t;
	    lastheaton = t;
	    break;

	default:
	    if (currstate == 'c')
		lastcooloff = t;
	    if (currstate == 'h')
		lastheatoff = t;
    }
    
    if (nextstate != '-')
	currstate = nextstate;

#if 0
    printf_P(PSTR("Time: %10ld, "), t);
#else
    ds1307_printtime(PSTR(""), PSTR(": "));
#endif
    printtemp(PSTR("Target"), settings.target_temp, PSTR(", "));
    printtemp(PSTR("Fermenter"), tempt, PSTR(", ")); // Use actual value from sensor
    printtemp(PSTR("Fridge"), fridge_temp, PSTR(", "));
    printtemp(PSTR("Ambient"), ambient_temp, PSTR(", "));
    printf_P(PSTR("State: %S, Flags: %S%S\r\n"), state2long(currstate), 
	     forced ? PSTR("F") : PSTR(""), 
	     stale ? PSTR("S") : PSTR(""));

  setstate:
    setstate(currstate);
}

/* Return 'time of day' (really uptime) */
int32_t
gettod(void) {
    int32_t	t;
    
    cli();
    t = now.sec;
    sei();

    return(t);
}

/*
 * Print out temperature (or NA + error code) with specified trailer
 */
static void
printtemp(char *name, int tmp, char *trailer) {
    if (tmp > OW_TEMP_BADVAL)
	printf_P(PSTR("%S: %d.%02d%S"), name, GETWHOLE(tmp), GETFRAC(tmp), trailer);
    else
	printf_P(PSTR("%S: NA (%d)%S"), name, tmp, trailer);
}

/* Read the settings from EEPROM
 * If the CRC fails then reload from flash
 */
static void
tempctrl_load_or_init_settings(void) {
    uint8_t	*dptr;
    uint16_t	crc, strcrc;
    int		i;
    
    crc = 0;
    eeprom_busy_wait();
    eeprom_read_block(&settings, &ee_area.settings, sizeof(settings_t));
    strcrc = eeprom_read_word(&ee_area.crc);
    
    dptr = (uint8_t *)&settings;
    
    for (i = 0; i < sizeof(settings_t); i++)
	crc = _crc16_update(crc, dptr[i]);

    /* All OK? */
    if (crc == strcrc)
	return;

    printf_P(PSTR("CRC mismatch got 0x%04x vs 0x%04x, setting defaults\r\n"), crc, strcrc);
    tempctrl_default_settings();
    tempctrl_write_settings();
}

/* Load in the defaults from flash */
static void
tempctrl_default_settings(void) {
    memcpy_P(&settings, &default_settings, sizeof(settings_t));
}

/* Write the current settings out to EEPROM */
static void
tempctrl_write_settings(void) {
    uint16_t	crc;
    uint8_t	*dptr;
    int		i;
    
    eeprom_busy_wait();
    eeprom_write_block(&settings, &ee_area.settings, sizeof(settings_t));
    
    dptr = (uint8_t *)&settings;
    crc = 0;
    for (i = 0; i < sizeof(settings_t); i++)
	crc = _crc16_update(crc, dptr[i]);

    eeprom_write_word(&ee_area.crc, crc);
}

/* Set the relays to match the desired state */
static void
setstate(char state) {
    switch (state) {
	case 'c':
	    PORTC = settings.coolbits;
	    break;

	case 'h':
	    PORTC = settings.heatbits;
	    break;

	default:
	    printf_P(PSTR("Unknown state %c, setting idle\r\n"), state);
	    /* fallthrough */

	case 'i':
	    PORTC = settings.idlebits;
	    break;
    }
}

/* Handle user command
 *
 */
void
tempctrl_cmd(char *buf) {
    char	cmd[6];
    int16_t	data;
    uint8_t 	ROM[8];
    
    if (sscanf_P(buf, PSTR("tc %5s"), cmd, &data) == 0) {
	printf_P(PSTR("Unable to parse tc subcommand\r\n"));
	return;
    }
	
    if (!strcasecmp_P(cmd, PSTR("help"))) {
	printf_P(PSTR(
		     "tc help         This help\r\n"
		     "tc save         Save settings to EEPROM\r\n"
		     "tc load         Load or default settings from EEPROM\r\n"
		     "tc dflt         Load defaults from flash\r\n"
		     "tc list         List current settings\r\n"
		     "tc mode [achin] Change control mode, must be one of\r\n"
		     "                 a    Auto\r\n"
		     "                 c    Always cool\r\n"
		     "                 h    Always heat\r\n"
		     "                 i    Always idle\r\n"
		     "                 n    Like idle but don't log anything\r\n"
#ifndef WITHUSB
		     "tc beep [01]    Enable/disable beeping when data is stale\r\n"
#endif
		     "tc X Y          Set X to Y where X is one of\r\n"
		     "                 targ Target temperature\r\n"
		     "                 hys  Hysteresis range\r\n"
		     "                 mhov Minimum heat overshoot\r\n"
		     "                 mcov Minimum cool overshoot\r\n"
		     "                 mcon Minimum cool on time\r\n"
		     "                 mcoff        Minimum cool off time\r\n"
		     "                 mhin Minimum heat on time\r\n"
		     "                 mhoff        Minimum heat off time\r\n"
		     "tc A B          Set temperature sensor ID\r\n"
		     "                 Where A is ferm, frg or amb\r\n"
		     "                 and B is of the form xx:xx:xx:xx:xx:xx:xx:xx\r\n"
		     "\r\n"
		     "                Times are in seconds\r\n"
		     "                Temperatures are in hundredths of degrees Celcius\r\n"
		     ));
	return;
    }
	
    if (!strcasecmp_P(cmd, PSTR("save"))) {
	tempctrl_write_settings();
	return;
    }
    if (!strcasecmp_P(cmd, PSTR("load"))) {
	tempctrl_load_or_init_settings();
	return;
    }
    if (!strcasecmp_P(cmd, PSTR("dflt"))) {
	tempctrl_default_settings();
	return;
    }
    if (!strcasecmp_P(cmd, PSTR("list"))) {
	printf_P(PSTR("Fermenter ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n"
		      "Fridge ROM ID    %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n"
		      "Ambient ROM ID   %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n"
		      "Mode - %c, Target - %d, Hystersis - %d\r\n"
		      "Min heat overshoot - %d, Min cool overshoot - %d\r\n"
		      "Min cool on time - %d, Min cool off time - %d\r\n"
		      "Min heat on time - %d, Min heat off time - %d\r\n"
#ifndef WITHUSB
		      "Beep on stale - %S\r\n"
#endif
		     ),
		 settings.fermenter_ROM[0], settings.fermenter_ROM[1], settings.fermenter_ROM[2], settings.fermenter_ROM[3], 
		 settings.fermenter_ROM[4], settings.fermenter_ROM[5], settings.fermenter_ROM[6], settings.fermenter_ROM[7], 
		 settings.fridge_ROM[0], settings.fridge_ROM[1], settings.fridge_ROM[2], settings.fridge_ROM[3], 
		 settings.fridge_ROM[4], settings.fridge_ROM[5], settings.fridge_ROM[6], settings.fridge_ROM[7], 
		 settings.ambient_ROM[0], settings.ambient_ROM[1], settings.ambient_ROM[2], settings.ambient_ROM[3], 
		 settings.ambient_ROM[4], settings.ambient_ROM[5], settings.ambient_ROM[6], settings.ambient_ROM[7], 
		 settings.mode, settings.target_temp, settings.hysteresis,
		 settings.minheatovershoot, settings.mincoolovershoot,
		 settings.mincoolontime, settings.minheatontime,
		 settings.minheatontime, settings.minheatofftime,
#ifndef WITHUSB
		 settings.dobeep ? PSTR("yes") : PSTR("no")
#endif
	    );
	return;
    }
    if (!strcasecmp_P(cmd, PSTR("mode"))) {
	switch (buf[8]) {
	    case TC_MODE_AUTO:
	    case TC_MODE_HEAT:
	    case TC_MODE_COOL:
	    case TC_MODE_IDLE:
	    case TC_MODE_NOTHING:
		settings.mode = buf[8];
		break;
		    
	    default:
		printf_P(PSTR("Unknown mode character '%c'\r\n"), buf[8]);
		break;
	}
	return;
    }
#ifndef WITHUSB
    if (!strcasecmp_P(cmd, PSTR("beep"))) {
	if (buf[8] == '1' || buf[8] == 'y')
	    settings.dobeep = 1;
	else if (buf[8] == '0' || buf[8] == 'n')
	    settings.dobeep = 0;
	else
	    printf_P(PSTR("Expected a y/1 or n/0\r\n"));
	return;
    }
#endif
    if (!strcasecmp_P(cmd, PSTR("ferm")) ||
	!strcasecmp_P(cmd, PSTR("frg")) ||
	!strcasecmp_P(cmd, PSTR("amb"))) {

	if (sscanf_P((char *)cmd, PSTR("tc %5s %hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx"), cmd,
		     &ROM[0], &ROM[1], &ROM[2], &ROM[3],
		     &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 9) {
	    printf_P(PSTR("Unable to parse ROM ID\r\n"));
	} else {
	    if (!strcasecmp_P(cmd, PSTR("ferm")))
		memcpy(settings.fermenter_ROM, ROM, sizeof(ROM));
	    if (!strcasecmp_P(cmd, PSTR("frg")))
		memcpy(settings.fridge_ROM, ROM, sizeof(ROM));
	    if (!strcasecmp_P(cmd, PSTR("amb")))
		memcpy(settings.ambient_ROM, ROM, sizeof(ROM));
	}
    }
    
    if (sscanf_P(buf, PSTR("tc %5s %d"), cmd, &data) != 2) {
	printf_P(PSTR("Unable to parse tc subcommand & value\r\n"));
	return;
    }

    if (!strcasecmp_P(cmd, PSTR("targ"))) {
	settings.target_temp = data;
    } else if (!strcasecmp_P(cmd, PSTR("hys"))) {
	settings.hysteresis = data;
    } else if (!strcasecmp_P(cmd, PSTR("mhov"))) {
	settings.minheatovershoot = data;
    } else if (!strcasecmp_P(cmd, PSTR("mcov"))) {
	settings.mincoolovershoot = data;
    } else if (!strcasecmp_P(cmd, PSTR("mcon"))) {
	settings.mincoolontime = data;
    } else if (!strcasecmp_P(cmd, PSTR("mcoff"))) {
	settings.mincoolofftime = data;
    } else if (!strcasecmp_P(cmd, PSTR("mhon"))) {
	settings.minheatontime = data;
    } else if (!strcasecmp_P(cmd, PSTR("mhoff"))) {
	settings.minheatofftime = data;
    } else {
	printf_P(PSTR("Unknown setting\r\n"));
	return;
    }
}

static const PROGMEM char*
state2long(char s) {
    switch (s) {
	case 'i':
	    return PSTR("idle");
	    break;
	    
	case 'c':
	    return PSTR("cool");
	    break;
	    
	case 'h':
	    return PSTR("heat");
	    break;
	    
	case '-':
	    return PSTR("-");
	    break;
	    
	default:
	    return PSTR("unknown");
	    break;
    }
}