37 PI_THREAD (my_simulator_loop) |
37 PI_THREAD (my_simulator_loop) |
38 #else |
38 #else |
39 void *my_simulator_loop(void *threadid) |
39 void *my_simulator_loop(void *threadid) |
40 #endif |
40 #endif |
41 { |
41 { |
42 time_t now, last = (time_t)0; |
42 time_t now, last; |
43 int seconds = 0; |
43 int loops = 0; |
44 // double hlt_heat_transfer, mlt_heat_transfer; |
44 // double hlt_heat_transfer, mlt_heat_transfer; |
45 // double vhc_air = 0.00121, vhc_water = 4.1796; |
45 // double vhc_air = 0.00121, vhc_water = 4.1796; |
46 |
46 |
47 syslog(LOG_NOTICE, "Thread my_simulator_loop started"); |
47 syslog(LOG_NOTICE, "Thread my_simulator_loop started"); |
48 if (debug) |
48 if (debug) |
49 fprintf(stdout, "Thread my_simulator_loop started\n"); |
49 fprintf(stdout, "Thread my_simulator_loop started\n"); |
50 |
50 |
51 /* |
51 /* |
52 * Heater and cooler have the air temperature |
52 * Initial temperatures |
53 */ |
53 */ |
54 Config.simulator->hlt_heater_temp = Config.simulator->mlt_heater_temp = Config.simulator->room_temperature; |
54 Config.simulator->hlt_heater_temp = Config.simulator->hlt_temperature; |
|
55 Config.simulator->mlt_heater_temp = Config.simulator->mlt_temperature; |
|
56 last = now = time(NULL); |
55 |
57 |
|
58 /* |
|
59 * About 50 mSec loops |
|
60 */ |
56 for (;;) { |
61 for (;;) { |
57 if (my_shutdown) |
62 if (my_shutdown) |
58 break; |
63 break; |
59 |
64 |
|
65 /* |
|
66 * First calculate the heat loss for the HLT and MLT liquids. |
|
67 * Then decrease both liquid temperatures and consider the volume. |
|
68 */ |
|
69 Config.simulator->hlt_temperature -= (Config.simulator->hlt_temperature - Config.simulator->room_temperature) / |
|
70 (40000 * (Config.simulator->hlt_heater_volume / 2)); |
|
71 Config.simulator->mlt_temperature -= (Config.simulator->mlt_temperature - Config.simulator->room_temperature) / |
|
72 (40000 * (Config.simulator->mlt_heater_volume / 2)); |
|
73 |
|
74 /* |
|
75 * If heating, calculate the heating element temperature but not higher then 250 degrees celcius. |
|
76 * I have no data about real temperatures of electric elements, so this is a rough guess. |
|
77 * If not heating, the elements are couling towards the current liquid temperature. |
|
78 */ |
|
79 Config.simulator->hlt_heater_state = SIM_hlt_value; |
|
80 if (SIM_hlt_value) { |
|
81 Config.simulator->hlt_heater_temp += (250 - Config.simulator->hlt_heater_temp) / 2000; |
|
82 } else { |
|
83 Config.simulator->hlt_heater_temp -= (Config.simulator->hlt_heater_temp - Config.simulator->hlt_temperature) / 1000; |
|
84 } |
|
85 Config.simulator->mlt_heater_state = SIM_mlt_value; |
|
86 if (SIM_mlt_value) { |
|
87 Config.simulator->mlt_heater_temp += (250 - Config.simulator->mlt_heater_temp) / 2000; |
|
88 } else { |
|
89 Config.simulator->mlt_heater_temp -= (Config.simulator->mlt_heater_temp - Config.simulator->mlt_temperature) / 1000; |
|
90 } |
|
91 |
|
92 /* |
|
93 * Shift the liquid temperature towards the heating elements temperature, |
|
94 * but never higher then 100 degrees celcius. |
|
95 */ |
|
96 Config.simulator->hlt_temperature += (Config.simulator->hlt_heater_temp - Config.simulator->hlt_temperature) / (10000 * Config.simulator->hlt_heater_volume); |
|
97 if (Config.simulator->hlt_temperature > 100.25) |
|
98 Config.simulator->hlt_temperature = 100.25; |
|
99 Config.simulator->mlt_temperature += (Config.simulator->mlt_heater_temp - Config.simulator->mlt_temperature) / (10000 * Config.simulator->mlt_heater_volume); |
|
100 if (Config.simulator->mlt_temperature > 100.25) |
|
101 Config.simulator->mlt_temperature = 100.25; |
|
102 |
|
103 loops++; |
60 now = time(NULL); |
104 now = time(NULL); |
61 if (now != last) { |
105 if (now != last) { |
62 last = now; |
106 last = now; |
63 /* |
107 /* |
64 * Each second |
108 * Each second |
65 */ |
109 */ |
66 seconds++; |
110 if (debug) |
67 |
111 fprintf(stdout, "HLT temp=%f plate=%f val=%d MLT temp=%f plate=%f val=%d Room %.1f loops=%d\n", |
68 /* |
112 Config.simulator->hlt_temperature, Config.simulator->hlt_heater_temp, Config.simulator->hlt_heater_state, |
69 * First calculate the heat loss for the HLT and MLT liquids. |
113 Config.simulator->mlt_temperature, Config.simulator->mlt_heater_temp, Config.simulator->mlt_heater_state, |
70 * Then decrease both liquid temperatures. |
114 Config.simulator->room_temperature, loops); |
71 */ |
115 loops = 0; |
72 |
|
73 /* |
|
74 * If heating, calculate the heating element temperature. |
|
75 * If not heating, shift towards the liquid temperature. |
|
76 */ |
|
77 |
|
78 /* |
|
79 * Shift the liquid temperature towards the heating elements temperature, |
|
80 * but never higher then 100 degrees celcius. |
|
81 */ |
|
82 |
|
83 /* |
|
84 * If heating, calculate temperature of the heating plate. If heating is off but |
|
85 * the plate is warmer then the air, calculate the cooling down temperature. |
|
86 * Finally, calculate the new air and plate temperature. |
|
87 */ |
|
88 |
|
89 // syslog(LOG_NOTICE, "air=%.3f beer=%.3f heater=%.3f cooler=%.3f", simulator->air_temperature, simulator->beer_temperature, |
|
90 // simulator->s_heat_temp, simulator->s_cool_temp); |
|
91 |
|
92 // if (debug) |
|
93 // fprintf(stdout, "sqm_room_air=%f air=%f air_heat_transfer=%f air_change=%f sqm_beer_air=%f beer=%f beer_heat_transfer=%f\n", |
|
94 // sqm_room_air, simulator->air_temperature, air_heat_transfer, air_change, |
|
95 // sqm_beer_air, simulator->beer_temperature, beer_heat_transfer); |
|
96 } |
116 } |
97 usleep(100000); |
117 usleep(50000); |
98 } |
118 } |
99 |
119 |
100 syslog(LOG_NOTICE, "Thread my_simulator_loop stopped"); |
120 syslog(LOG_NOTICE, "Thread my_simulator_loop stopped"); |
101 if (debug) |
121 if (debug) |
102 fprintf(stdout, "Thread my_simulator_loop stopped\n"); |
122 fprintf(stdout, "Thread my_simulator_loop stopped\n"); |