brewco/simulator.c

Sat, 12 Dec 2015 19:31:35 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 12 Dec 2015 19:31:35 +0100
changeset 455
f84501d8dd87
parent 438
7d1ec160d751
child 456
045db83dd013
permissions
-rw-r--r--

Most parts of the simulator are working, needs some tuning.

438
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /*****************************************************************************
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2 * Copyright (C) 2015
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 *
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 * Michiel Broek <mbroek at mbse dot eu>
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 *
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 * This file is part of the mbsePi-apps
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 *
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 * This is free software; you can redistribute it and/or modify it
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9 * under the terms of the GNU General Public License as published by the
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 * Free Software Foundation; either version 2, or (at your option) any
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 * later version.
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 *
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 * mbsePi-apps is distributed in the hope that it will be useful, but
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 * General Public License for more details.
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 *
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 * along with ThermFerm; see the file COPYING. If not, write to the Free
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 *****************************************************************************/
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 #include "brewco.h"
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 #include "simulator.h"
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26 #ifdef USE_SIMULATOR
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28 extern int my_shutdown;
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29 extern int debug;
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
30 extern sys_config Config;
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32 int SIM_hlt_value = 0;
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 int SIM_mlt_value = 0;
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 #ifdef HAVE_WIRINGPI_H
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37 PI_THREAD (my_simulator_loop)
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38 #else
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39 void *my_simulator_loop(void *threadid)
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40 #endif
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 {
455
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
42 time_t now, last;
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
43 int loops = 0;
438
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44 // double hlt_heat_transfer, mlt_heat_transfer;
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 // double vhc_air = 0.00121, vhc_water = 4.1796;
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47 syslog(LOG_NOTICE, "Thread my_simulator_loop started");
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48 if (debug)
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49 fprintf(stdout, "Thread my_simulator_loop started\n");
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
50
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
51 /*
455
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
52 * Initial temperatures
438
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53 */
455
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
54 Config.simulator->hlt_heater_temp = Config.simulator->hlt_temperature;
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
55 Config.simulator->mlt_heater_temp = Config.simulator->mlt_temperature;
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
56 last = now = time(NULL);
438
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57
455
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
58 /*
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
59 * About 50 mSec loops
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
60 */
438
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
61 for (;;) {
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
62 if (my_shutdown)
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
63 break;
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
64
455
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
65 /*
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
66 * First calculate the heat loss for the HLT and MLT liquids.
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
67 * Then decrease both liquid temperatures and consider the volume.
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
68 */
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
69 Config.simulator->hlt_temperature -= (Config.simulator->hlt_temperature - Config.simulator->room_temperature) /
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
70 (40000 * (Config.simulator->hlt_heater_volume / 2));
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
71 Config.simulator->mlt_temperature -= (Config.simulator->mlt_temperature - Config.simulator->room_temperature) /
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
72 (40000 * (Config.simulator->mlt_heater_volume / 2));
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
73
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
74 /*
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
75 * If heating, calculate the heating element temperature but not higher then 250 degrees celcius.
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
76 * I have no data about real temperatures of electric elements, so this is a rough guess.
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
77 * If not heating, the elements are couling towards the current liquid temperature.
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
78 */
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
79 Config.simulator->hlt_heater_state = SIM_hlt_value;
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
80 if (SIM_hlt_value) {
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
81 Config.simulator->hlt_heater_temp += (250 - Config.simulator->hlt_heater_temp) / 2000;
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
82 } else {
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
83 Config.simulator->hlt_heater_temp -= (Config.simulator->hlt_heater_temp - Config.simulator->hlt_temperature) / 1000;
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
84 }
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
85 Config.simulator->mlt_heater_state = SIM_mlt_value;
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
86 if (SIM_mlt_value) {
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
87 Config.simulator->mlt_heater_temp += (250 - Config.simulator->mlt_heater_temp) / 2000;
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
88 } else {
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
89 Config.simulator->mlt_heater_temp -= (Config.simulator->mlt_heater_temp - Config.simulator->mlt_temperature) / 1000;
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
90 }
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
91
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
92 /*
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
93 * Shift the liquid temperature towards the heating elements temperature,
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
94 * but never higher then 100 degrees celcius.
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
95 */
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
96 Config.simulator->hlt_temperature += (Config.simulator->hlt_heater_temp - Config.simulator->hlt_temperature) / (10000 * Config.simulator->hlt_heater_volume);
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
97 if (Config.simulator->hlt_temperature > 100.25)
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
98 Config.simulator->hlt_temperature = 100.25;
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
99 Config.simulator->mlt_temperature += (Config.simulator->mlt_heater_temp - Config.simulator->mlt_temperature) / (10000 * Config.simulator->mlt_heater_volume);
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
100 if (Config.simulator->mlt_temperature > 100.25)
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
101 Config.simulator->mlt_temperature = 100.25;
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
102
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
103 loops++;
438
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
104 now = time(NULL);
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
105 if (now != last) {
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
106 last = now;
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
107 /*
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
108 * Each second
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
109 */
455
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
110 if (debug)
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
111 fprintf(stdout, "HLT temp=%f plate=%f val=%d MLT temp=%f plate=%f val=%d Room %.1f loops=%d\n",
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
112 Config.simulator->hlt_temperature, Config.simulator->hlt_heater_temp, Config.simulator->hlt_heater_state,
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
113 Config.simulator->mlt_temperature, Config.simulator->mlt_heater_temp, Config.simulator->mlt_heater_state,
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
114 Config.simulator->room_temperature, loops);
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
115 loops = 0;
438
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
116 }
455
f84501d8dd87 Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents: 438
diff changeset
117 usleep(50000);
438
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
118 }
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
119
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
120 syslog(LOG_NOTICE, "Thread my_simulator_loop stopped");
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
121 if (debug)
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
122 fprintf(stdout, "Thread my_simulator_loop stopped\n");
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
123 return 0;
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
124 }
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
125
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
126
7d1ec160d751 Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
127 #endif

mercurial