thermferm/simulator.c

Sat, 16 May 2015 17:39:30 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 16 May 2015 17:39:30 +0200
changeset 362
c92651a54969
parent 265
63a003914b72
child 363
468ec0d96cce
permissions
-rw-r--r--

Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0

/*****************************************************************************
 * Copyright (C) 2008-2014
 *   
 * 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 "thermferm.h"
#include "simulator.h"

#ifdef USE_SIMULATOR

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

int SIMcooling = 0;
int SIMheating = 0;


#ifdef HAVE_WIRINGPI_H
PI_THREAD (my_simulator_loop)
#else
void *my_simulator_loop(void *threadid)
#endif
{
    simulator_list	*simulator;
    time_t		now, last = (time_t)0;
    int			seconds = 0;
    double		k_room_air, k_beer_air, sqm_room_air, sqm_beer_air, thick_room_air, thick_beer_air, air_heat_transfer, beer_heat_transfer;
    double		air_change, beer_change, 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");

    for (;;) {
	for (simulator = Config.simulators; simulator; simulator = simulator->next) {
	    if (my_shutdown)
	    	break;

	    now = time(NULL);
	    if (now != last) {
		last = now;
		/*
		 * Each second
		 */
	    	seconds++;

		/*
	    	 * First, calculate temperature difference between the room and the air in the
	    	 * fridge. We use the volume air to roughly calculate the total area between
	    	 * the in and outside. Calculate the effect and shift the air temperature towards
	    	 * the room temperature.
	    	 */
		sqm_room_air = (cbrtl(simulator->volume_air) * cbrtl(simulator->volume_air) * 6) / 100;	/* square meters all fridge sides */
		thick_room_air = 0.04;	/* 4 cm walls	*/
		k_room_air = 0.03;	/* Polystrene	*/
		air_heat_transfer=(k_room_air * sqm_room_air * (simulator->room_temperature - simulator->air_temperature)) / thick_room_air;
		air_change = air_heat_transfer / (vhc_air * ((simulator->volume_air - simulator->volume_beer) * 1000));
		simulator->air_temperature += air_change;

	    	/*
	    	 * If heating, calculate temperature of the heating plate. If heating is off but
	    	 * the plate is warmer then the air, calculate the cooling down temperature.
	    	 * Finally, calculate the new air and plate temperature.
	    	 */
		if (SIMheating) {
		    simulator->air_temperature += 0.01;
		}

	    	/* 
	    	 * If cooling, calculate temperature of the cooling plate. If cooling is off but
	    	 * the plate is colder then the air, calculate the warming up temperature.
	    	 * Finsally, calculate the new air and plate temperature.
	    	 */
		if (SIMcooling) {
		    simulator->air_temperature -= 0.01;
		}

	    	/*
	    	 * Calculate the extra beer temperatur rise to simulate the heat produced by the
	    	 * fermentation process. Peak about one day after start and slowly decrease after
	    	 * that.
	    	 */
		sqm_beer_air = (cbrtl(simulator->volume_beer) * cbrtl(simulator->volume_beer) * 6) / 100; /* Simple, the beer is in a cubic box */
		thick_beer_air = 0.001;
		k_beer_air = 0.5;	/* HDPE */
		beer_heat_transfer=(k_beer_air * sqm_beer_air * (simulator->air_temperature - simulator->beer_temperature)) / thick_beer_air;
		beer_change = 0;

	    	/*
	    	 * Calculate final temperature of the beer and the air.
	    	 */
		if (debug)
		    fprintf(stdout, "sqm_room_air=%f air=%f air_heat_transfer=%f air_change=%f  sqm_beer_air=%f beer=%f beer_heat_transfer=%f\n",
			sqm_room_air, simulator->air_temperature, air_heat_transfer, air_change,
			sqm_beer_air, simulator->beer_temperature, beer_heat_transfer);
	    }
	    usleep(100000);
	}
	usleep(50000);
    }

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


#endif

mercurial