thermferm/simulator.c

Mon, 25 Mar 2024 17:14:56 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 25 Mar 2024 17:14:56 +0100
changeset 650
0b215e4b814e
parent 646
e3edc783006b
child 652
16d3d4b58b5b
permissions
-rw-r--r--

Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.

/*****************************************************************************
 * Copyright (C) 2014-2024
 *   
 * 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"

int			my_simulator_state = 0;

#ifdef USE_SIMULATOR

extern int		my_shutdown;
extern sys_config	Config;

int			SIMcooling = 0;
int			SIMheating = 0;
int			SIMfan = 0;
int			SIMlight = 0;


void *my_simulator_loop(void *threadid)
{
    simulator_list	*simulator;
    time_t		now, last = (time_t)0;
    int			seconds = 0;
    double		k_room_air, sqm_room_air, thick_room_air, air_heat_transfer;
    double		air_change, vhc_air = 0.00121;

    my_simulator_state = 1;
    syslog(LOG_NOTICE, "Thread my_simulator_loop started");

    for (simulator = Config.simulators; simulator; simulator = simulator->next) {
	/*
	 * Heater and cooler have the air temperature
	 */
	simulator->s_heat_temp = simulator->s_cool_temp = simulator->room_temperature;
    }

    for (;;) {
	if (my_shutdown)
	    break;

	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))) / 60.0;
		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) {
		    if (simulator->s_heat_temp < simulator->heater_temp) {
			simulator->s_heat_temp += 0.05;
			if (simulator->s_heat_temp > simulator->air_temperature)
			    simulator->air_temperature += ((simulator->s_heat_temp - simulator->air_temperature) / 100.0);
		    }
		} else {
		    /*
		     * Follow the air temperature
		     */
		    simulator->s_heat_temp -= (simulator->s_heat_temp - simulator->air_temperature) / 25.0;
		}

	    	/* 
	    	 * 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) {
		    if (simulator->s_cool_temp > simulator->cooler_temp) {
			simulator->s_cool_temp -= 0.05;
			if (simulator->s_cool_temp < simulator->air_temperature)
			    simulator->air_temperature -= ((simulator->air_temperature - simulator->s_cool_temp) / 100.0);
		    }
		} else {
		    simulator->s_cool_temp -= (simulator->s_cool_temp - simulator->air_temperature) / 25.0;
		}

	    	/*
	    	 * Calculate final temperature of the beer and the air.
	    	 */
		// Cheap trick, just follow slowly the air temp.
		simulator->beer_temperature += ((simulator->air_temperature - simulator->beer_temperature) / 500.0);
		simulator->air_temperature += ((simulator->beer_temperature - simulator->air_temperature) / 2500.0);
		simulator->chiller_temperature = simulator->cooler_temp;	// Libk these
	    }
	    usleep(100000);
	}
	usleep(50000);
    }

    syslog(LOG_NOTICE, "Thread my_simulator_loop stopped");
    my_simulator_state = 0;
    return 0;
}


#endif

mercurial