Sat, 12 Dec 2015 19:31:35 +0100
Most parts of the simulator are working, needs some tuning.
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; | |
34 | ||
35 | ||
36 | #ifdef HAVE_WIRINGPI_H | |
37 | PI_THREAD (my_simulator_loop) | |
38 | #else | |
39 | void *my_simulator_loop(void *threadid) | |
40 | #endif | |
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 | 44 | // double hlt_heat_transfer, mlt_heat_transfer; |
45 | // double vhc_air = 0.00121, vhc_water = 4.1796; | |
46 | ||
47 | syslog(LOG_NOTICE, "Thread my_simulator_loop started"); | |
48 | if (debug) | |
49 | fprintf(stdout, "Thread my_simulator_loop started\n"); | |
50 | ||
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 | 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 | 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 | 61 | for (;;) { |
62 | if (my_shutdown) | |
63 | break; | |
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 | 104 | now = time(NULL); |
105 | if (now != last) { | |
106 | last = now; | |
107 | /* | |
108 | * Each second | |
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 | 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 | 118 | } |
119 | ||
120 | syslog(LOG_NOTICE, "Thread my_simulator_loop stopped"); | |
121 | if (debug) | |
122 | fprintf(stdout, "Thread my_simulator_loop stopped\n"); | |
123 | return 0; | |
124 | } | |
125 | ||
126 | ||
127 | #endif |