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