brewco/simulator.c

Sun, 27 Dec 2015 20:52:52 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 27 Dec 2015 20:52:52 +0100
changeset 479
0e6a1163e6e3
parent 478
fe8bf61cde06
permissions
-rw-r--r--

Bild target temperature 0.1 degree higher. Removed debug log from the simulator

/*****************************************************************************
 * 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) / 
		(80000 * (Config.simulator->hlt_heater_volume / 2));
	Config.simulator->mlt_temperature -= (Config.simulator->mlt_temperature - Config.simulator->room_temperature) / 
		(80000 * (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.0 * (Config.simulator->hlt_heater_power / 2000.0);
	} else {
	    Config.simulator->hlt_heater_temp -= (Config.simulator->hlt_heater_temp - Config.simulator->hlt_temperature) / 250;
	}
	Config.simulator->mlt_heater_state = SIM_mlt_value;
	if (SIM_mlt_value) {
	    Config.simulator->mlt_heater_temp += (250 - Config.simulator->mlt_heater_temp) / 2000.0 * (Config.simulator->mlt_heater_power / 2000.0);
	} else {
	    Config.simulator->mlt_heater_temp -= (Config.simulator->mlt_heater_temp - Config.simulator->mlt_temperature) / 250;
	}

	/*
	 * If cooling, bring down the MLT temperature. Assume 14 degrees coolwater.
	 * The cooler is turned on or off by sending SIGUSR1 or SIGUSR2 to this program.
	 */
	if (SIM_cooler) {
	    Config.simulator->mlt_temperature -= (Config.simulator->mlt_temperature - 14) / (175 * 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.0 * Config.simulator->hlt_heater_volume * (2000.0 / Config.simulator->hlt_heater_power));
	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.0 * Config.simulator->mlt_heater_volume * (2000.0 / Config.simulator->mlt_heater_power));
	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