brewco/simulator.c

Sat, 12 Dec 2015 21:20:46 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 12 Dec 2015 21:20:46 +0100
changeset 456
045db83dd013
parent 455
f84501d8dd87
child 459
1f88be70f253
permissions
-rw-r--r--

Build a simulated immersion chiller. It will start on SIGUSR1 signal and stop on a SIGUSR2 signal. Hardcoded is te coolwater temperature at 14 degrees celcius.

/*****************************************************************************
 * Copyright (C) 2015
 *   
 * Michiel Broek <mbroek at mbse dot eu>
 *
 * 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 ThermFerm; see the file COPYING.  If not, write to the Free
 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *****************************************************************************/

#include "brewco.h"
#include "simulator.h"

#ifdef USE_SIMULATOR

extern int		my_shutdown;
extern int		debug;
extern sys_config	Config;

int			SIM_hlt_value = 0;
int			SIM_mlt_value = 0;
int			SIM_cooler = FALSE;


#ifdef HAVE_WIRINGPI_H
PI_THREAD (my_simulator_loop)
#else
void *my_simulator_loop(void *threadid)
#endif
{
    time_t		now, last;
    int			loops = 0;
//    double		hlt_heat_transfer, mlt_heat_transfer;
//    double		vhc_air = 0.00121, vhc_water = 4.1796;

    syslog(LOG_NOTICE, "Thread my_simulator_loop started");
    if (debug)
	fprintf(stdout, "Thread my_simulator_loop started\n");

    /*
     * Initial temperatures
     */
    Config.simulator->hlt_heater_temp = Config.simulator->hlt_temperature;
    Config.simulator->mlt_heater_temp = Config.simulator->mlt_temperature;
    last = now = time(NULL);

    /*
     * About 50 mSec loops
     */
    for (;;) {
	if (my_shutdown)
	    break;

	/*
	 * First calculate the heat loss for the HLT and MLT liquids.
	 * Then decrease both liquid temperatures and consider the volume.
	 */
	Config.simulator->hlt_temperature -= (Config.simulator->hlt_temperature - Config.simulator->room_temperature) / 
		(40000 * (Config.simulator->hlt_heater_volume / 2));
	Config.simulator->mlt_temperature -= (Config.simulator->mlt_temperature - Config.simulator->room_temperature) / 
		(40000 * (Config.simulator->mlt_heater_volume / 2));

	/*
	 * If heating, calculate the heating element temperature but not higher then 250 degrees celcius.
	 * I have no data about real temperatures of electric elements, so this is a rough guess.
	 * If not heating, the elements are couling towards the current liquid temperature.
	 */
	Config.simulator->hlt_heater_state = SIM_hlt_value;
	if (SIM_hlt_value) {
	    Config.simulator->hlt_heater_temp += (250 - Config.simulator->hlt_heater_temp) / 2000;
	} else {
	    Config.simulator->hlt_heater_temp -= (Config.simulator->hlt_heater_temp - Config.simulator->hlt_temperature) / 500;
	}
	Config.simulator->mlt_heater_state = SIM_mlt_value;
	if (SIM_mlt_value) {
	    Config.simulator->mlt_heater_temp += (250 - Config.simulator->mlt_heater_temp) / 2000;
	} else {
	    Config.simulator->mlt_heater_temp -= (Config.simulator->mlt_heater_temp - Config.simulator->mlt_temperature) / 500;
	}

	/*
	 * If cooling, bring down the MLT temperature. Assume 14 degrees coolwater.
	 */
	if (SIM_cooler) {
	    Config.simulator->mlt_temperature -= (Config.simulator->mlt_temperature - 14) / (250 * Config.simulator->hlt_heater_volume);
	}

	/*
	 * Shift the liquid temperature towards the heating elements temperature,
	 * but never higher then 100 degrees celcius.
	 */
	Config.simulator->hlt_temperature += (Config.simulator->hlt_heater_temp - Config.simulator->hlt_temperature) / (10000 * Config.simulator->hlt_heater_volume);
	if (Config.simulator->hlt_temperature > 100.25)
	    Config.simulator->hlt_temperature = 100.25;
	Config.simulator->mlt_temperature += (Config.simulator->mlt_heater_temp - Config.simulator->mlt_temperature) / (10000 * Config.simulator->mlt_heater_volume);
	if (Config.simulator->mlt_temperature > 100.25)
	    Config.simulator->mlt_temperature = 100.25;

	loops++;
	now = time(NULL);
	if (now != last) {
	    last = now;
	    /*
	     * Each second
	     */
	    if (debug)
		fprintf(stdout, "HLT temp=%f plate=%f val=%d   MLT temp=%f plate=%f val=%d   Room %.1f loops=%d  cool=%s\n",
			Config.simulator->hlt_temperature, Config.simulator->hlt_heater_temp, Config.simulator->hlt_heater_state,
			Config.simulator->mlt_temperature, Config.simulator->mlt_heater_temp, Config.simulator->mlt_heater_state,
			Config.simulator->room_temperature, loops, SIM_cooler ? "Yes":"No");
	    loops = 0;
	}
	usleep(50000);
    }

    syslog(LOG_NOTICE, "Thread my_simulator_loop stopped");
    if (debug)
	fprintf(stdout, "Thread my_simulator_loop stopped\n");
    return 0;
}


#endif

mercurial