Sat, 12 Dec 2015 21:20:46 +0100
Build a simulated immersion chiller. It will start on SIGUSR1 signal and stop on a SIGUSR2 signal. Hardcoded is te coolwater temperature at 14 degrees celcius.
438 | 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; | |
456
045db83dd013
Build a simulated immersion chiller. It will start on SIGUSR1 signal and stop on a SIGUSR2 signal. Hardcoded is te coolwater temperature at 14 degrees celcius.
Michiel Broek <mbroek@mbse.eu>
parents:
455
diff
changeset
|
34 | int SIM_cooler = FALSE; |
438 | 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 | { | |
455
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
43 | time_t now, last; |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
44 | int loops = 0; |
438 | 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 | /* | |
455
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
53 | * Initial temperatures |
438 | 54 | */ |
455
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
55 | 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
|
56 | 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
|
57 | last = now = time(NULL); |
438 | 58 | |
455
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
59 | /* |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
60 | * About 50 mSec loops |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
61 | */ |
438 | 62 | for (;;) { |
63 | if (my_shutdown) | |
64 | break; | |
65 | ||
455
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
66 | /* |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
67 | * 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
|
68 | * 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
|
69 | */ |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
70 | 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
|
71 | (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
|
72 | 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
|
73 | (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
|
74 | |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
75 | /* |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
76 | * 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
|
77 | * 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
|
78 | * 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
|
79 | */ |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
80 | 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
|
81 | if (SIM_hlt_value) { |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
82 | 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
|
83 | } else { |
456
045db83dd013
Build a simulated immersion chiller. It will start on SIGUSR1 signal and stop on a SIGUSR2 signal. Hardcoded is te coolwater temperature at 14 degrees celcius.
Michiel Broek <mbroek@mbse.eu>
parents:
455
diff
changeset
|
84 | Config.simulator->hlt_heater_temp -= (Config.simulator->hlt_heater_temp - Config.simulator->hlt_temperature) / 500; |
455
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
85 | } |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
86 | 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
|
87 | if (SIM_mlt_value) { |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
88 | 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
|
89 | } else { |
456
045db83dd013
Build a simulated immersion chiller. It will start on SIGUSR1 signal and stop on a SIGUSR2 signal. Hardcoded is te coolwater temperature at 14 degrees celcius.
Michiel Broek <mbroek@mbse.eu>
parents:
455
diff
changeset
|
90 | Config.simulator->mlt_heater_temp -= (Config.simulator->mlt_heater_temp - Config.simulator->mlt_temperature) / 500; |
045db83dd013
Build a simulated immersion chiller. It will start on SIGUSR1 signal and stop on a SIGUSR2 signal. Hardcoded is te coolwater temperature at 14 degrees celcius.
Michiel Broek <mbroek@mbse.eu>
parents:
455
diff
changeset
|
91 | } |
045db83dd013
Build a simulated immersion chiller. It will start on SIGUSR1 signal and stop on a SIGUSR2 signal. Hardcoded is te coolwater temperature at 14 degrees celcius.
Michiel Broek <mbroek@mbse.eu>
parents:
455
diff
changeset
|
92 | |
045db83dd013
Build a simulated immersion chiller. It will start on SIGUSR1 signal and stop on a SIGUSR2 signal. Hardcoded is te coolwater temperature at 14 degrees celcius.
Michiel Broek <mbroek@mbse.eu>
parents:
455
diff
changeset
|
93 | /* |
045db83dd013
Build a simulated immersion chiller. It will start on SIGUSR1 signal and stop on a SIGUSR2 signal. Hardcoded is te coolwater temperature at 14 degrees celcius.
Michiel Broek <mbroek@mbse.eu>
parents:
455
diff
changeset
|
94 | * If cooling, bring down the MLT temperature. Assume 14 degrees coolwater. |
045db83dd013
Build a simulated immersion chiller. It will start on SIGUSR1 signal and stop on a SIGUSR2 signal. Hardcoded is te coolwater temperature at 14 degrees celcius.
Michiel Broek <mbroek@mbse.eu>
parents:
455
diff
changeset
|
95 | */ |
045db83dd013
Build a simulated immersion chiller. It will start on SIGUSR1 signal and stop on a SIGUSR2 signal. Hardcoded is te coolwater temperature at 14 degrees celcius.
Michiel Broek <mbroek@mbse.eu>
parents:
455
diff
changeset
|
96 | if (SIM_cooler) { |
045db83dd013
Build a simulated immersion chiller. It will start on SIGUSR1 signal and stop on a SIGUSR2 signal. Hardcoded is te coolwater temperature at 14 degrees celcius.
Michiel Broek <mbroek@mbse.eu>
parents:
455
diff
changeset
|
97 | Config.simulator->mlt_temperature -= (Config.simulator->mlt_temperature - 14) / (250 * Config.simulator->hlt_heater_volume); |
455
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
98 | } |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
99 | |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
100 | /* |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
101 | * 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
|
102 | * 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
|
103 | */ |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
104 | 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
|
105 | 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
|
106 | 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
|
107 | 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
|
108 | 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
|
109 | 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
|
110 | |
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
111 | loops++; |
438 | 112 | now = time(NULL); |
113 | if (now != last) { | |
114 | last = now; | |
115 | /* | |
116 | * Each second | |
117 | */ | |
455
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
118 | if (debug) |
456
045db83dd013
Build a simulated immersion chiller. It will start on SIGUSR1 signal and stop on a SIGUSR2 signal. Hardcoded is te coolwater temperature at 14 degrees celcius.
Michiel Broek <mbroek@mbse.eu>
parents:
455
diff
changeset
|
119 | fprintf(stdout, "HLT temp=%f plate=%f val=%d MLT temp=%f plate=%f val=%d Room %.1f loops=%d cool=%s\n", |
455
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
120 | 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
|
121 | Config.simulator->mlt_temperature, Config.simulator->mlt_heater_temp, Config.simulator->mlt_heater_state, |
456
045db83dd013
Build a simulated immersion chiller. It will start on SIGUSR1 signal and stop on a SIGUSR2 signal. Hardcoded is te coolwater temperature at 14 degrees celcius.
Michiel Broek <mbroek@mbse.eu>
parents:
455
diff
changeset
|
122 | Config.simulator->room_temperature, loops, SIM_cooler ? "Yes":"No"); |
455
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
123 | loops = 0; |
438 | 124 | } |
455
f84501d8dd87
Most parts of the simulator are working, needs some tuning.
Michiel Broek <mbroek@mbse.eu>
parents:
438
diff
changeset
|
125 | usleep(50000); |
438 | 126 | } |
127 | ||
128 | syslog(LOG_NOTICE, "Thread my_simulator_loop stopped"); | |
129 | if (debug) | |
130 | fprintf(stdout, "Thread my_simulator_loop stopped\n"); | |
131 | return 0; | |
132 | } | |
133 | ||
134 | ||
135 | #endif |