Mercurial > public > ostc4
annotate Discovery/Src/simulation.c @ 446:f1257a32f2d4 minor_improvments
Introduced configuration header for variant managment:
At the moment several defines are distributed across the code allowing special SW builds (e.g. logging simulated dives). To make these options more transparent and easier to configurate a separate header has been added. With this header an new option for an optical pressure sensor was introduced.
author | ideenmodellierer |
---|---|
date | Thu, 05 Mar 2020 22:31:50 +0100 |
parents | 1203255481e4 |
children | b2f8a39c11ea |
rev | line source |
---|---|
38 | 1 /////////////////////////////////////////////////////////////////////////////// |
2 /// -*- coding: UTF-8 -*- | |
3 /// | |
4 /// \file Discovery/Src/simulation.c | |
5 /// \brief Contains dive simulation functionality | |
6 /// \author Heinrichs Weikamp gmbh | |
7 /// \date 13-Oct-2014 | |
8 /// | |
9 /// \details | |
10 /// The simulation uses "extern SDiveState stateSim" defined in dataCentral.h" | |
11 /// | |
12 /// simulation_start(void) sets stateUsed to stateSim and initializes simulation | |
13 /// simulation_UpdateLifeData should be called at least once per second | |
14 /// simulation_end() sets stateUsed back to stateReal | |
15 /// | |
16 /// $Id$ | |
17 /////////////////////////////////////////////////////////////////////////////// | |
18 /// \par Copyright (c) 2014-2018 Heinrichs Weikamp gmbh | |
19 /// | |
20 /// This program is free software: you can redistribute it and/or modify | |
21 /// it under the terms of the GNU General Public License as published by | |
22 /// the Free Software Foundation, either version 3 of the License, or | |
23 /// (at your option) any later version. | |
24 /// | |
25 /// This program is distributed in the hope that it will be useful, | |
26 /// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
28 /// GNU General Public License for more details. | |
29 /// | |
30 /// You should have received a copy of the GNU General Public License | |
31 /// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
32 ////////////////////////////////////////////////////////////////////////////// | |
33 | |
34 #include <string.h> | |
35 #include "simulation.h" | |
36 | |
37 #include "decom.h" | |
38 #include "calc_crush.h" | |
39 #include "data_exchange.h" | |
308
1203255481e4
cleanup: introduce function setAvgDepth
Jan Mulder <jlmulder@xs4all.nl>
parents:
307
diff
changeset
|
40 #include "data_exchange_main.h" |
38 | 41 #include "timer.h" |
42 #include "check_warning.h" | |
43 #include "vpm.h" | |
44 #include "buehlmann.h" | |
45 #include "logbook_miniLive.h" | |
46 | |
47 //Private state variables | |
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
48 static float sim_aim_depth_meter; |
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
49 static _Bool sim_heed_decostops = 1; |
38 | 50 |
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
51 static const float sim_descent_rate_meter_per_min = 20; |
38 | 52 |
53 | |
54 //Private functions | |
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
233
diff
changeset
|
55 static float sim_get_ambient_pressure(SDiveState * pDiveState); |
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
56 static void sim_reduce_deco_time_one_second(SDiveState* pDiveState); |
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
57 static void simulation_set_aim_depth(int depth_meter); |
38 | 58 |
59 /** | |
60 ****************************************************************************** | |
61 * @brief sets heed_decostops_while_ascending | |
62 ****************************************************************************** | |
63 * @param heed_decostops_while_ascending : true -> deco_stops are considered while ascending | |
64 * @return void | |
65 */ | |
66 void simulation_set_heed_decostops(_Bool heed_decostops_while_ascending) | |
67 { | |
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
68 sim_heed_decostops = heed_decostops_while_ascending; |
38 | 69 } |
70 | |
71 /** | |
72 ****************************************************************************** | |
73 * @brief start of simulation | |
74 ****************************************************************************** | |
75 * @return void | |
76 */ | |
77 void simulation_start(int aim_depth) | |
78 { | |
79 copyDiveSettingsToSim(); | |
80 copyVpmRepetetiveDataToSim(); | |
81 //vpm_init(&stateSimGetPointerWrite()->vpm, stateSimGetPointerWrite()->diveSettings.vpm_conservatism, 0, 0); | |
82 stateSimGetPointerWrite()->lifeData.counterSecondsShallowDepth = 0; | |
83 stateSimGetPointerWrite()->mode = MODE_DIVE; | |
84 if(aim_depth <= 0) | |
85 aim_depth = 20; | |
86 simulation_set_aim_depth(aim_depth); | |
87 timer_init(); | |
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
88 set_stateUsedToSim(); |
38 | 89 stateSim.lifeData.boolResetAverageDepth = 1; |
90 decoLock = DECO_CALC_init_as_is_start_of_dive; | |
91 | |
92 stateSim.lifeData.apnea_total_max_depth_meter = 0; | |
93 } | |
94 | |
95 /** | |
96 ****************************************************************************** | |
97 * @brief end of simulation | |
98 ****************************************************************************** | |
99 * | |
100 * @return void | |
101 */ | |
102 void simulation_exit(void) | |
103 { | |
104 timer_Stopwatch_Stop(); | |
105 set_stateUsedToReal(); | |
106 } | |
107 | |
108 /** | |
109 ****************************************************************************** | |
110 * @brief simulates change of Lifedata (saturation, depth change, etc.) within one second | |
111 ****************************************************************************** | |
112 * | |
113 * @param checkOncePerSecond : true -> simulation in real time (function is evaluated only once per second) | |
114 * and copy of parts of LifeData from SmallCPU with each call from HAL_TIM_PeriodElapsedCallback() | |
115 * : false -> fast simulation (many simulation cycles per second are possible) | |
116 * @return void | |
117 */ | |
118 void simulation_UpdateLifeData( _Bool checkOncePerSecond) | |
119 { | |
120 SDiveState * pDiveState = &stateSim; | |
121 | |
122 static int last_second = -1; | |
123 static _Bool two_second = 0; | |
124 static float lastPressure_bar = 0; | |
125 | |
126 if(checkOncePerSecond) | |
127 { | |
128 pDiveState->lifeData.temperature_celsius = stateRealGetPointer()->lifeData.temperature_celsius; | |
129 pDiveState->lifeData.compass_heading = stateRealGetPointer()->lifeData.compass_heading; | |
130 pDiveState->lifeData.battery_charge = stateRealGetPointer()->lifeData.battery_charge; | |
446
f1257a32f2d4
Introduced configuration header for variant managment:
ideenmodellierer
parents:
308
diff
changeset
|
131 #ifdef ENABLE_BOTTLE_SENSOR |
f1257a32f2d4
Introduced configuration header for variant managment:
ideenmodellierer
parents:
308
diff
changeset
|
132 pDiveState->lifeData.bottle_bar[pDiveState->lifeData.actualGas.GasIdInSettings] = stateRealGetPointer()->lifeData.bottle_bar[stateRealGetPointer()->lifeData.actualGas.GasIdInSettings]; |
f1257a32f2d4
Introduced configuration header for variant managment:
ideenmodellierer
parents:
308
diff
changeset
|
133 pDiveState->lifeData.bottle_bar_age_MilliSeconds[pDiveState->lifeData.actualGas.GasIdInSettings] = stateRealGetPointer()->lifeData.bottle_bar_age_MilliSeconds[stateRealGetPointer()->lifeData.actualGas.GasIdInSettings]; |
f1257a32f2d4
Introduced configuration header for variant managment:
ideenmodellierer
parents:
308
diff
changeset
|
134 #endif |
38 | 135 int now = current_second(); |
136 if( last_second == now) | |
137 return; | |
138 last_second = now; | |
139 | |
140 if(!two_second) | |
141 two_second = 1; | |
142 else | |
143 { | |
144 two_second = 0; | |
145 if(lastPressure_bar >= 0) | |
146 { | |
147 //2 seconds * 30 == 1 minute, bar * 10 = meter | |
148 pDiveState->lifeData.ascent_rate_meter_per_min = (lastPressure_bar - pDiveState->lifeData.pressure_ambient_bar) * 30 * 10; | |
149 } | |
150 lastPressure_bar = pDiveState->lifeData.pressure_ambient_bar; | |
151 } | |
152 } | |
153 else if(pDiveState->lifeData.depth_meter <= (float)(decom_get_actual_deco_stop(pDiveState) + 0.001)) | |
154 sim_reduce_deco_time_one_second(pDiveState); | |
155 | |
156 pDiveState->lifeData.ppO2Sensor_bar[0] = stateRealGetPointer()->lifeData.ppO2Sensor_bar[0]; | |
157 pDiveState->lifeData.ppO2Sensor_bar[1] = stateRealGetPointer()->lifeData.ppO2Sensor_bar[1]; | |
158 pDiveState->lifeData.ppO2Sensor_bar[2] = stateRealGetPointer()->lifeData.ppO2Sensor_bar[2]; | |
159 pDiveState->lifeData.sensorVoltage_mV[0] = stateRealGetPointer()->lifeData.sensorVoltage_mV[0]; | |
160 pDiveState->lifeData.sensorVoltage_mV[1] = stateRealGetPointer()->lifeData.sensorVoltage_mV[1]; | |
161 pDiveState->lifeData.sensorVoltage_mV[2] = stateRealGetPointer()->lifeData.sensorVoltage_mV[2]; | |
162 | |
163 pDiveState->lifeData.dive_time_seconds += 1; | |
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
233
diff
changeset
|
164 pDiveState->lifeData.pressure_ambient_bar = sim_get_ambient_pressure(pDiveState); |
38 | 165 |
166 if(is_ambient_pressure_close_to_surface(&pDiveState->lifeData)) // new hw 170214 | |
167 { | |
168 if(!(stateSimGetPointer()->lifeData.counterSecondsShallowDepth)) | |
169 { | |
170 if(pDiveState->diveSettings.diveMode != DIVEMODE_Apnea) | |
171 pDiveState->lifeData.counterSecondsShallowDepth = settingsGetPointer()->timeoutDiveReachedZeroDepth - 15; | |
172 else | |
173 { | |
174 pDiveState->lifeData.apnea_last_dive_time_seconds = pDiveState->lifeData.dive_time_seconds; | |
175 if(pDiveState->lifeData.apnea_last_dive_time_seconds > pDiveState->lifeData.dive_time_seconds_without_surface_time) | |
176 pDiveState->lifeData.apnea_last_dive_time_seconds = pDiveState->lifeData.dive_time_seconds_without_surface_time; | |
177 pDiveState->lifeData.apnea_last_max_depth_meter = pDiveState->lifeData.max_depth_meter; | |
178 pDiveState->lifeData.counterSecondsShallowDepth = 1; | |
179 } | |
180 } | |
181 } | |
182 else | |
183 { | |
184 pDiveState->lifeData.counterSecondsShallowDepth = 0; | |
185 } | |
186 | |
304
43b44f8d4fb0
bugfix, simulator: fix the 1 sec difference between stopwatch and divetime
Jan Mulder <jlmulder@xs4all.nl>
parents:
300
diff
changeset
|
187 if(!is_ambient_pressure_close_to_surface(&pDiveState->lifeData) && !(stateSimGetPointer()->lifeData.counterSecondsShallowDepth) ) |
43b44f8d4fb0
bugfix, simulator: fix the 1 sec difference between stopwatch and divetime
Jan Mulder <jlmulder@xs4all.nl>
parents:
300
diff
changeset
|
188 { |
43b44f8d4fb0
bugfix, simulator: fix the 1 sec difference between stopwatch and divetime
Jan Mulder <jlmulder@xs4all.nl>
parents:
300
diff
changeset
|
189 pDiveState->lifeData.dive_time_seconds_without_surface_time += 1; |
43b44f8d4fb0
bugfix, simulator: fix the 1 sec difference between stopwatch and divetime
Jan Mulder <jlmulder@xs4all.nl>
parents:
300
diff
changeset
|
190 } |
43b44f8d4fb0
bugfix, simulator: fix the 1 sec difference between stopwatch and divetime
Jan Mulder <jlmulder@xs4all.nl>
parents:
300
diff
changeset
|
191 |
38 | 192 pDiveState->lifeData.depth_meter = (pDiveState->lifeData.pressure_ambient_bar - pDiveState->lifeData.pressure_surface_bar) * 10.0f; |
193 if(pDiveState->lifeData.max_depth_meter < pDiveState->lifeData.depth_meter) | |
194 pDiveState->lifeData.max_depth_meter = pDiveState->lifeData.depth_meter; | |
195 | |
196 /* apnoe specials | |
197 */ | |
198 if(pDiveState->diveSettings.diveMode == DIVEMODE_Apnea) | |
199 { | |
200 if(pDiveState->lifeData.max_depth_meter > pDiveState->lifeData.apnea_total_max_depth_meter) | |
201 pDiveState->lifeData.apnea_total_max_depth_meter = pDiveState->lifeData.max_depth_meter; | |
202 | |
203 if(pDiveState->lifeData.counterSecondsShallowDepth) | |
204 { | |
205 pDiveState->lifeData.dive_time_seconds = 0; | |
206 pDiveState->lifeData.max_depth_meter = 0; | |
207 pDiveState->lifeData.boolResetAverageDepth = 1; | |
208 } | |
209 } | |
210 | |
308
1203255481e4
cleanup: introduce function setAvgDepth
Jan Mulder <jlmulder@xs4all.nl>
parents:
307
diff
changeset
|
211 setAvgDepth(pDiveState); |
38 | 212 |
213 /* Exposure Tissues | |
214 */ | |
215 decom_tissues_exposure(1, &pDiveState->lifeData); | |
216 decom_oxygen_calculate_cns_exposure(1, &pDiveState->lifeData.actualGas, pDiveState->lifeData.pressure_ambient_bar, &pDiveState->lifeData.cns); | |
233
9f0efc4df01e
cleanup, bugfix: do not exit simulator on 5h dive time
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
217 |
38 | 218 if(stateSimGetPointer()->lifeData.counterSecondsShallowDepth) |
219 { | |
220 stateSimGetPointerWrite()->lifeData.counterSecondsShallowDepth += 1; | |
221 if(stateSimGetPointer()->lifeData.counterSecondsShallowDepth >= settingsGetPointer()->timeoutDiveReachedZeroDepth) | |
222 simulation_exit(); | |
223 } | |
224 vpm_crush(pDiveState); | |
225 } | |
226 | |
227 /** | |
228 ****************************************************************************** | |
229 * @brief adds extra time for fast simulation | |
230 ****************************************************************************** | |
231 *@param minutes | |
232 * @return float : new pressure | |
233 */ | |
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
234 static void simulation_add_time(int minutes) |
38 | 235 { |
236 for(int i = 0; i < 60 * minutes; i++) | |
237 { | |
238 simulation_UpdateLifeData(0); | |
239 updateMiniLiveLogbook(0); | |
240 timer_UpdateSecond(0); | |
241 } | |
242 } | |
243 | |
244 /** | |
245 ****************************************************************************** | |
246 * @brief get aim_depth | |
247 ****************************************************************************** | |
248 * @return sim_aim_depth_meter; | |
249 */ | |
250 | |
251 uint16_t simulation_get_aim_depth(void) | |
252 { | |
253 return (uint16_t)sim_aim_depth_meter; | |
254 } | |
255 | |
256 /** | |
257 ****************************************************************************** | |
258 * @brief get heed decostops | |
259 ****************************************************************************** | |
260 * @return true if ascend follows decostops; | |
261 */ | |
262 | |
263 _Bool simulation_get_heed_decostops(void) | |
264 { | |
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
265 return sim_heed_decostops; |
38 | 266 } |
267 | |
268 /** | |
269 ****************************************************************************** | |
270 * @brief sets aim_depth | |
271 ****************************************************************************** | |
272 *@param depth_meter | |
273 * @return float : new pressure | |
274 */ | |
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
275 static void simulation_set_aim_depth(int depth_meter) |
38 | 276 { |
277 sim_aim_depth_meter = depth_meter; | |
278 } | |
279 | |
280 /** | |
281 ****************************************************************************** | |
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
233
diff
changeset
|
282 * @brief simulates ambient pressure depending on aim depth |
38 | 283 ****************************************************************************** |
284 * @note if aim_depth != actual depth, the depth change within one second | |
285 * (depending on descent or ascent) rate is calculated | |
286 * @param SDiveState* pDiveState: | |
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
233
diff
changeset
|
287 * @return float : new ambient pressure |
38 | 288 */ |
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
233
diff
changeset
|
289 static float sim_get_ambient_pressure(SDiveState * pDiveState) |
38 | 290 { |
291 //Calc next depth | |
292 uint8_t actual_deco_stop = decom_get_actual_deco_stop(pDiveState); | |
293 float depth_meter = pDiveState->lifeData.depth_meter; | |
294 float surface_pressure_bar = pDiveState->lifeData.pressure_surface_bar; | |
295 if(depth_meter < sim_aim_depth_meter) | |
296 { | |
297 depth_meter = depth_meter + sim_descent_rate_meter_per_min / 60; | |
298 if(depth_meter > sim_aim_depth_meter) | |
299 depth_meter = sim_aim_depth_meter; | |
300 } | |
301 else if(depth_meter > sim_aim_depth_meter) | |
302 { | |
303 | |
304 depth_meter -= pDiveState->diveSettings.ascentRate_meterperminute / 60; | |
305 if(depth_meter < sim_aim_depth_meter) | |
306 depth_meter = sim_aim_depth_meter; | |
307 | |
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
308 if(sim_heed_decostops && depth_meter < actual_deco_stop) |
38 | 309 { |
310 if(actual_deco_stop < (depth_meter + pDiveState->diveSettings.ascentRate_meterperminute / 60)) | |
311 depth_meter = actual_deco_stop; | |
312 else | |
313 depth_meter += pDiveState->diveSettings.ascentRate_meterperminute / 60; | |
314 } | |
315 | |
316 } | |
317 | |
318 return surface_pressure_bar + depth_meter / 10; | |
319 } | |
320 | |
321 | |
322 /** | |
323 ****************************************************************************** | |
324 * @brief Reduces deco time of deepest stop by one second | |
325 ****************************************************************************** | |
326 * @note called during fast simulation | |
327 * @param SDiveState* pDiveState: | |
328 * @return void | |
329 */ | |
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
233
diff
changeset
|
330 static void sim_reduce_deco_time_one_second(SDiveState* pDiveState) |
38 | 331 { |
332 SDecoinfo* pDecoinfo; | |
333 if(pDiveState->diveSettings.deco_type.ub.standard == GF_MODE) | |
334 pDecoinfo = &pDiveState->decolistBuehlmann; | |
335 else | |
336 pDecoinfo = &pDiveState->decolistVPM; | |
337 | |
338 //Reduce deco time of deepest stop by one second | |
339 for(int i = DECOINFO_STRUCT_MAX_STOPS -1 ;i >= 0; i--) | |
340 { | |
341 if(pDecoinfo->output_stop_length_seconds[i] > 0) | |
342 { | |
343 pDecoinfo->output_stop_length_seconds[i]--; | |
344 break; | |
345 } | |
346 } | |
347 } | |
348 | |
349 SDecoinfo* simulation_decoplaner(uint16_t depth_meter, uint16_t intervall_time_minutes, uint16_t dive_time_minutes, uint8_t *gasChangeListDepthGas20x2) | |
350 { | |
351 uint8_t ptrGasChangeList = 0; // new hw 160704 | |
352 | |
353 SDiveState * pDiveState = &stateSim; | |
354 copyDiveSettingsToSim(); | |
355 vpm_init(&pDiveState->vpm, pDiveState->diveSettings.vpm_conservatism, 0, 0); | |
356 //buehlmann_init(); | |
357 //timer_init(); | |
358 memset(&pDiveState->events,0, sizeof(SEvents)); | |
359 pDiveState->diveSettings.internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero = 0; | |
360 //Calc desaturation during intervall (with Air) | |
361 setActualGasAir(&pDiveState->lifeData); | |
362 if(intervall_time_minutes > 0) | |
363 { | |
364 decom_tissues_exposure(intervall_time_minutes * 60, &pDiveState->lifeData); | |
365 decom_oxygen_calculate_cns_degrade(&pDiveState->lifeData.cns, intervall_time_minutes * 60); | |
366 } | |
367 | |
368 //Switch to first Gas | |
369 setActualGasFirst(&pDiveState->lifeData); | |
370 | |
371 // new hw 160704 | |
372 if(gasChangeListDepthGas20x2) | |
373 { | |
374 gasChangeListDepthGas20x2[ptrGasChangeList++] = 0; | |
375 gasChangeListDepthGas20x2[ptrGasChangeList++] = pDiveState->lifeData.actualGas.GasIdInSettings; | |
376 gasChangeListDepthGas20x2[0] =0; // depth zero | |
377 } | |
378 | |
379 //Going down / descent | |
380 simulation_set_aim_depth(depth_meter); | |
381 for(int i = 0; i < 60 * dive_time_minutes; i++) | |
382 { | |
383 simulation_UpdateLifeData(0); | |
384 check_warning2(pDiveState); | |
385 if(pDiveState->warnings.betterGas) | |
386 { | |
387 setActualGas(&pDiveState->lifeData,actualBetterGasId(),pDiveState->lifeData.actualGas.setPoint_cbar); | |
388 if(gasChangeListDepthGas20x2 && (pDiveState->diveSettings.diveMode == DIVEMODE_OC)) | |
389 { | |
390 gasChangeListDepthGas20x2[ptrGasChangeList++] = pDiveState->lifeData.depth_meter; | |
391 gasChangeListDepthGas20x2[ptrGasChangeList++] = actualBetterGasId(); | |
392 } | |
393 } | |
394 } | |
395 | |
396 decom_CreateGasChangeList(&pDiveState->diveSettings, &pDiveState->lifeData); // was there before and needed for buehlmann_calc_deco and vpm_calc | |
397 | |
398 // new hw 160704 | |
399 if(gasChangeListDepthGas20x2 && (pDiveState->diveSettings.diveMode == DIVEMODE_OC)) | |
400 { | |
401 // change direction from better gas to deco gas | |
402 gasChangeListDepthGas20x2[ptrGasChangeList++] = 255; | |
403 gasChangeListDepthGas20x2[ptrGasChangeList++] = 255; | |
404 | |
405 // ascend (deco) gases | |
406 for(int i=1; i<=5;i++) | |
407 { | |
408 if(pDiveState->diveSettings.decogaslist[i].change_during_ascent_depth_meter_otherwise_zero == 0) | |
409 break; | |
410 gasChangeListDepthGas20x2[ptrGasChangeList++] = pDiveState->diveSettings.decogaslist[i].change_during_ascent_depth_meter_otherwise_zero; | |
411 gasChangeListDepthGas20x2[ptrGasChangeList++] = pDiveState->diveSettings.decogaslist[i].GasIdInSettings; | |
412 } | |
413 gasChangeListDepthGas20x2[0] = 0; | |
414 } | |
415 | |
416 // deco and ascend calc | |
417 if(pDiveState->diveSettings.deco_type.ub.standard == GF_MODE) | |
418 { | |
419 /* this does modify the cns now 11.06.2015 */ | |
420 buehlmann_calc_deco(&pDiveState->lifeData,&pDiveState->diveSettings,&pDiveState->decolistBuehlmann); | |
421 pDiveState->lifeData.cns += buehlmann_get_gCNS(); | |
422 return &pDiveState->decolistBuehlmann; | |
423 } | |
424 else | |
425 { | |
426 /* this does modify the cns now 11.06.2015 */ | |
427 vpm_calc(&pDiveState->lifeData,&pDiveState->diveSettings,&pDiveState->vpm,&pDiveState->decolistVPM, DECOSTOPS); | |
428 pDiveState->lifeData.cns += vpm_get_CNS(); | |
429 return &pDiveState->decolistVPM; | |
430 } | |
431 } | |
432 | |
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
433 static float sGChelper_bar(uint16_t depth_meter) |
38 | 434 { |
435 SDiveState * pDiveState = &stateSim; | |
436 float ambient, surface, density, meter; | |
437 | |
438 surface = pDiveState->lifeData.pressure_surface_bar; | |
439 | |
440 if(!depth_meter) | |
441 return surface; | |
442 | |
443 density = ((float)( 100 + settingsGetPointer()->salinity)) / 100.0f; | |
444 meter = depth_meter * (0.09807f * density); | |
445 ambient = (meter + surface); | |
446 | |
447 return ambient; | |
448 } | |
449 | |
450 | |
451 /** | |
452 ****************************************************************************** | |
453 * @brief simulation_helper_change_points | |
454 ****************************************************************************** | |
455 * @param | |
456 * @return void | |
457 */ | |
458 void simulation_helper_change_points(SSimDataSummary *outputSummary, uint16_t depth_meter, uint16_t dive_time_minutes, SDecoinfo *decoInfoInput, const uint8_t *gasChangeListDepthGas20x2) | |
459 { | |
460 uint8_t ptrDecoInfo = 0; | |
461 uint16_t actualDepthPoint = 0; | |
462 uint16_t nextDepthPoint = 0; | |
463 uint8_t actualConsumGasId = 0; | |
464 uint8_t nextGasChangeMeter = 0; | |
465 uint8_t ptrChangeList = 0; | |
466 | |
467 float timeThis = 0; | |
468 float timeSummary = 0; | |
469 float sim_descent_rate_meter_per_min_local = 10; | |
470 float sim_ascent_rate_meter_per_min_local = 10; | |
471 | |
472 SDiveState * pDiveState = &stateSim; | |
473 | |
474 uint8_t depthDecoNext, depthLast, depthSecond, depthInc; | |
475 | |
476 if(pDiveState->diveSettings.deco_type.ub.standard == GF_MODE) | |
477 { | |
478 sim_descent_rate_meter_per_min_local = sim_descent_rate_meter_per_min; // const float | |
479 sim_ascent_rate_meter_per_min_local = pDiveState->diveSettings.ascentRate_meterperminute; | |
480 } | |
481 else | |
482 { | |
483 sim_descent_rate_meter_per_min_local = sim_descent_rate_meter_per_min; // const float | |
484 sim_ascent_rate_meter_per_min_local = 10;// fix in vpm_calc_deco(); | |
485 } | |
486 | |
487 outputSummary->descentRateMeterPerMinute = sim_descent_rate_meter_per_min_local; | |
488 outputSummary->ascentRateMeterPerMinute = sim_ascent_rate_meter_per_min_local; | |
489 | |
490 // bottom gas ppO2 | |
491 if(gasChangeListDepthGas20x2) | |
492 { | |
493 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
494 actualConsumGasId = gasChangeListDepthGas20x2[ptrChangeList++]; | |
495 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
496 | |
497 while(actualDepthPoint < depth_meter) | |
498 { | |
499 if(nextGasChangeMeter && (nextGasChangeMeter < depth_meter) && (gasChangeListDepthGas20x2[ptrChangeList] != 255)) // list has 255,255 for turn from travel to deco | |
500 { | |
501 nextDepthPoint = nextGasChangeMeter; | |
502 } | |
503 else | |
504 { | |
505 nextDepthPoint = depth_meter; | |
506 } | |
507 | |
508 if(actualConsumGasId > 5) // safety first | |
509 actualConsumGasId = 0; | |
510 | |
511 actualDepthPoint = nextDepthPoint; | |
512 | |
513 if(actualDepthPoint != depth_meter) | |
514 { | |
515 actualConsumGasId = gasChangeListDepthGas20x2[ptrChangeList++]; | |
516 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
517 } | |
518 } | |
519 } | |
520 else | |
521 { | |
522 actualConsumGasId = pDiveState->lifeData.actualGas.GasIdInSettings; | |
523 nextGasChangeMeter = 0; | |
524 } | |
525 outputSummary->ppO2AtBottom = (sGChelper_bar(depth_meter) - WATER_VAPOUR_PRESSURE) * pDiveState->diveSettings.gas[actualConsumGasId].oxygen_percentage / 100.0f; | |
526 | |
527 | |
528 // going down | |
529 actualDepthPoint = 0; | |
530 nextDepthPoint = depth_meter; | |
531 | |
532 timeThis = ((float)(nextDepthPoint - actualDepthPoint)) / sim_descent_rate_meter_per_min_local; | |
533 timeSummary += timeThis; | |
534 outputSummary->timeToBottom = (uint16_t)timeThis; | |
535 | |
536 // bottom time | |
537 timeThis = ((float)dive_time_minutes) - timeSummary; | |
538 timeSummary += timeThis; | |
539 outputSummary->timeAtBottom = (uint16_t)timeSummary; | |
540 | |
541 | |
542 // ascend to first deco stop | |
543 actualDepthPoint = depth_meter; // that is where we are | |
544 timeThis = 0; | |
545 | |
546 if(!decoInfoInput->output_stop_length_seconds[0]) // NDL dive | |
547 { | |
548 depthLast = 0; | |
549 ptrDecoInfo = 0; | |
550 depthDecoNext = 0; | |
551 } | |
552 else | |
553 { | |
554 // prepare deco stop list | |
555 depthLast = (uint8_t)(stateUsed->diveSettings.last_stop_depth_bar * 10); | |
556 depthSecond = (uint8_t)(stateUsed->diveSettings.input_second_to_last_stop_depth_bar * 10); | |
557 depthInc = (uint8_t)(stateUsed->diveSettings.input_next_stop_increment_depth_bar * 10); | |
558 | |
559 for(ptrDecoInfo=DECOINFO_STRUCT_MAX_STOPS-1; ptrDecoInfo>0; ptrDecoInfo--) | |
560 if(decoInfoInput->output_stop_length_seconds[ptrDecoInfo]) break; | |
561 | |
562 if(ptrDecoInfo == 0) | |
563 { | |
564 depthDecoNext = depthLast; | |
565 } | |
566 else | |
567 depthDecoNext = depthSecond + (( ptrDecoInfo - 1 )* depthInc); | |
568 } | |
569 | |
570 nextDepthPoint = depthDecoNext; | |
571 if(actualDepthPoint > nextDepthPoint) | |
572 { | |
573 // flip signs! It's going up | |
574 timeThis = ((float)(actualDepthPoint - nextDepthPoint)) / sim_ascent_rate_meter_per_min_local; | |
575 actualDepthPoint = nextDepthPoint; // that is where we are | |
576 } | |
577 timeSummary += timeThis; | |
578 outputSummary->timeToFirstStop = (uint16_t)timeSummary; | |
579 outputSummary->depthMeterFirstStop = actualDepthPoint; | |
580 | |
581 //ascent | |
582 nextDepthPoint = 0; | |
583 timeThis = 0; | |
584 if(actualDepthPoint > nextDepthPoint) // only if deco | |
585 { | |
586 // ascent time | |
587 timeThis = ((float)(actualDepthPoint - nextDepthPoint)) / sim_ascent_rate_meter_per_min_local; | |
588 | |
589 // deco stop time | |
590 for(ptrDecoInfo=0;ptrDecoInfo < DECOINFO_STRUCT_MAX_STOPS; ptrDecoInfo++) | |
591 { | |
592 timeThis += decoInfoInput->output_stop_length_seconds[ptrDecoInfo] / 60; | |
593 if(!decoInfoInput->output_stop_length_seconds[ptrDecoInfo]) break; | |
594 } | |
595 } | |
596 timeSummary += timeThis; | |
597 outputSummary->timeToSurface = (uint16_t)timeSummary; | |
598 | |
599 } | |
600 | |
601 | |
602 /** | |
603 ****************************************************************************** | |
604 * @brief simulation_gas_consumption | |
605 ****************************************************************************** | |
606 * @note called by openEdit_PlanResult() in tMenuEditPlanner.c | |
607 * @note the ascend and descend time is taken from pDiveState->lifeData.ascent_rate_meter_per_min and const float sim_descent_rate_meter_per_min | |
608 * @param outputConsumptionList list from 1 to 5 for gas 1 to 5 | |
609 * @param depth_meter for descend | |
610 * @param dive_time_minutes for descend and bottom time | |
611 * @param the calculated deco list | |
612 * @param gasConsumTravelInput: how many l/min for all but deco stops | |
613 * @param gasConsumDecoInput: how many l/min for deco stops only | |
614 * @return void | |
615 */ | |
616 | |
617 void simulation_gas_consumption(uint16_t *outputConsumptionList, uint16_t depth_meter, uint16_t dive_time_minutes, SDecoinfo *decoInfoInput, uint8_t gasConsumTravelInput, uint8_t gasConsumDecoInput, const uint8_t *gasChangeListDepthGas20x2) | |
618 { | |
619 uint8_t ptrDecoInfo = 0; | |
620 uint8_t ptrChangeList = 0; | |
621 uint8_t actualConsumGasId = 0; | |
622 uint8_t nextGasChangeMeter = 0; | |
623 uint16_t actualDepthPoint = 0; | |
624 uint16_t nextDepthPoint = 0; | |
625 uint16_t inBetweenDepthPoint = 0; | |
626 float timeThis = 0; | |
627 float consumThis = 0; | |
628 float timeSummary = 0; | |
629 float outputConsumptionTempFloat[6]; | |
630 float sim_descent_rate_meter_per_min_local = 10; | |
631 float sim_ascent_rate_meter_per_min_local = 10; | |
632 | |
633 SDiveState * pDiveState = &stateSim; | |
634 | |
51
8f8ea3a32e82
Resolved warnings pointing to possible invalid memory access
Ideenmodellierer
parents:
38
diff
changeset
|
635 uint8_t depthDecoNext = 0; |
8f8ea3a32e82
Resolved warnings pointing to possible invalid memory access
Ideenmodellierer
parents:
38
diff
changeset
|
636 uint8_t depthLast = 0; |
8f8ea3a32e82
Resolved warnings pointing to possible invalid memory access
Ideenmodellierer
parents:
38
diff
changeset
|
637 uint8_t depthSecond = 0; |
8f8ea3a32e82
Resolved warnings pointing to possible invalid memory access
Ideenmodellierer
parents:
38
diff
changeset
|
638 uint8_t depthInc = 0; |
38 | 639 |
640 for(int i = 1; i < 6; i++) | |
641 outputConsumptionTempFloat[i] = 0; | |
642 | |
643 if(gasChangeListDepthGas20x2) | |
644 { | |
645 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
646 actualConsumGasId = gasChangeListDepthGas20x2[ptrChangeList++]; | |
647 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
648 } | |
649 else | |
650 { | |
651 actualConsumGasId = pDiveState->lifeData.actualGas.GasIdInSettings; | |
652 nextGasChangeMeter = 0; | |
653 } | |
654 | |
655 if(pDiveState->diveSettings.deco_type.ub.standard == GF_MODE) | |
656 { | |
657 sim_descent_rate_meter_per_min_local = sim_descent_rate_meter_per_min; // const float | |
658 sim_ascent_rate_meter_per_min_local = pDiveState->diveSettings.ascentRate_meterperminute; | |
659 } | |
660 else | |
661 { | |
662 sim_descent_rate_meter_per_min_local = sim_descent_rate_meter_per_min; // const float | |
663 sim_ascent_rate_meter_per_min_local = 10;// fix in vpm_calc_deco(); | |
664 } | |
665 | |
666 // while((nextGasChangeMeter < depth_meter) && (actualDepthPoint < depth_meter)) | |
667 while(actualDepthPoint < depth_meter) | |
668 { | |
669 if(nextGasChangeMeter && (nextGasChangeMeter < depth_meter) && (gasChangeListDepthGas20x2[ptrChangeList] != 255)) // list has 255,255 for turn from travel to deco | |
670 { | |
671 nextDepthPoint = nextGasChangeMeter; | |
672 } | |
673 else | |
674 { | |
675 nextDepthPoint = depth_meter; | |
676 } | |
677 | |
678 if(actualConsumGasId > 5) // safety first | |
679 actualConsumGasId = 0; | |
680 | |
681 timeThis = ((float)(nextDepthPoint - actualDepthPoint)) / sim_descent_rate_meter_per_min_local; | |
682 if(actualDepthPoint) // not if on surface | |
683 { | |
684 consumThis = ((float)gasConsumTravelInput) * sGChelper_bar(actualDepthPoint) * timeThis; | |
685 } | |
686 consumThis += ((float)gasConsumTravelInput) * sGChelper_bar(nextDepthPoint -actualDepthPoint) * timeThis / 2; | |
687 outputConsumptionTempFloat[actualConsumGasId] += consumThis; | |
688 timeSummary += timeThis; | |
689 | |
690 actualDepthPoint = nextDepthPoint; | |
691 | |
692 if(actualDepthPoint != depth_meter) | |
693 { | |
694 actualConsumGasId = gasChangeListDepthGas20x2[ptrChangeList++]; | |
695 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
696 } | |
697 } | |
698 | |
699 // bottom Time | |
700 timeThis = ((float)dive_time_minutes) - timeSummary; | |
701 | |
702 if(timeThis > 0) | |
703 { | |
704 consumThis = ((float)gasConsumTravelInput) * sGChelper_bar(depth_meter) * timeThis; | |
705 outputConsumptionTempFloat[actualConsumGasId] += consumThis; | |
706 } | |
707 | |
708 // ascend with deco stops prepare | |
709 if(gasChangeListDepthGas20x2) | |
710 { | |
711 ptrChangeList++;// gasChangeListDepthGas20x2[ptrChangeList++]; // should be the 255 | |
712 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
713 } | |
714 else | |
715 { | |
716 nextGasChangeMeter = 0; | |
717 } | |
718 | |
719 | |
720 if(!decoInfoInput->output_stop_length_seconds[0]) // NDL dive | |
721 { | |
722 depthLast = 0; | |
723 ptrDecoInfo = 0; | |
724 } | |
725 else | |
726 { | |
727 // prepare deco stop list | |
728 depthLast = (uint8_t)(stateUsed->diveSettings.last_stop_depth_bar * 10); | |
729 depthSecond = (uint8_t)(stateUsed->diveSettings.input_second_to_last_stop_depth_bar * 10); | |
730 depthInc = (uint8_t)(stateUsed->diveSettings.input_next_stop_increment_depth_bar * 10); | |
731 | |
732 for(ptrDecoInfo=DECOINFO_STRUCT_MAX_STOPS-1; ptrDecoInfo>0; ptrDecoInfo--) | |
733 if(decoInfoInput->output_stop_length_seconds[ptrDecoInfo]) break; | |
734 } | |
735 | |
736 actualDepthPoint = depth_meter; // that is where we are | |
737 | |
738 // ascend with deco stops | |
739 while(actualDepthPoint) | |
740 { | |
741 if(ptrDecoInfo == 0) | |
742 { | |
743 depthDecoNext = depthLast; | |
744 } | |
745 else | |
746 depthDecoNext = depthSecond + (( ptrDecoInfo - 1 )* depthInc); | |
747 | |
748 if(nextGasChangeMeter && (nextGasChangeMeter > depthDecoNext)) | |
749 { | |
750 nextDepthPoint = nextGasChangeMeter; | |
751 } | |
752 else | |
753 { | |
754 nextDepthPoint = depthDecoNext; | |
755 } | |
756 | |
757 if(actualConsumGasId > 5) // safety first | |
758 actualConsumGasId = 0; | |
759 | |
760 if(actualDepthPoint > nextDepthPoint) | |
761 { | |
762 // flip signs! It's going up | |
763 timeThis = ((float)(actualDepthPoint - nextDepthPoint)) / sim_ascent_rate_meter_per_min_local; | |
764 inBetweenDepthPoint = nextDepthPoint + ((actualDepthPoint - nextDepthPoint)/2); | |
765 consumThis = ((float)gasConsumDecoInput) * sGChelper_bar(inBetweenDepthPoint) * timeThis; | |
766 /* | |
767 if(nextDepthPoint) | |
768 { | |
769 consumThis = ((float)gasConsumDecoInput) * sGChelper_bar(nextDepthPoint) * timeThis; | |
770 } | |
771 else | |
772 { | |
773 consumThis = 0; | |
774 } | |
775 consumThis += ((float)gasConsumDecoInput) * sGChelper_bar(actualDepthPoint - nextDepthPoint) * timeThis / 2; | |
776 */ | |
777 outputConsumptionTempFloat[actualConsumGasId] += consumThis; | |
778 } | |
779 | |
780 if(nextGasChangeMeter && (nextDepthPoint == nextGasChangeMeter)) | |
781 { | |
782 actualConsumGasId = gasChangeListDepthGas20x2[ptrChangeList++]; | |
783 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
784 } | |
785 | |
786 if(actualConsumGasId > 5) // safety first | |
787 actualConsumGasId = 0; | |
788 | |
789 if(nextDepthPoint && (nextDepthPoint == depthDecoNext)) | |
790 { | |
791 if(decoInfoInput->output_stop_length_seconds[ptrDecoInfo]) | |
792 { | |
793 timeThis = ((float)(decoInfoInput->output_stop_length_seconds[ptrDecoInfo])) / 60.0f; | |
794 consumThis = ((float)gasConsumDecoInput) * sGChelper_bar(nextDepthPoint) * timeThis; | |
795 outputConsumptionTempFloat[actualConsumGasId] += consumThis; | |
796 } | |
797 if(ptrDecoInfo != 0) | |
798 { | |
799 ptrDecoInfo--; | |
800 } | |
801 else | |
802 { | |
803 depthLast = 0; | |
804 } | |
805 } | |
806 actualDepthPoint = nextDepthPoint; | |
807 } | |
808 | |
809 // copy and return | |
810 for(int i = 1; i < 6; i++) | |
811 outputConsumptionList[i] = (uint16_t)(outputConsumptionTempFloat[i]); | |
812 } | |
813 | |
814 /** | |
815 ****************************************************************************** | |
816 * @brief Simulator control during simulated dive | |
817 ****************************************************************************** | |
818 * @note called by user via tHomeDiveMenuControl() | |
819 * @param void | |
820 * @return void | |
821 */ | |
822 | |
823 | |
824 void Sim_Descend (void) | |
825 { | |
826 stateSimGetPointerWrite()->lifeData.counterSecondsShallowDepth = 0; | |
827 if(simulation_get_aim_depth() < 200) | |
828 simulation_set_aim_depth(simulation_get_aim_depth() + 1); | |
829 } | |
830 | |
831 | |
832 void Sim_Ascend (void) | |
833 { | |
834 if(simulation_get_aim_depth() > 0) | |
835 simulation_set_aim_depth(simulation_get_aim_depth() - 1); | |
836 } | |
837 | |
838 | |
839 void Sim_Divetime (void) | |
840 { | |
841 simulation_add_time(5); | |
842 } | |
843 | |
844 | |
845 void Sim_Quit (void) | |
846 { | |
847 if(stateSimGetPointer()->lifeData.counterSecondsShallowDepth) | |
848 { | |
849 simulation_exit(); | |
850 return; | |
851 } | |
852 | |
853 if(simulation_get_aim_depth() > 0) | |
854 { | |
855 simulation_set_aim_depth(0); | |
856 } | |
857 else | |
858 { | |
859 stateSimGetPointerWrite()->lifeData.depth_meter = 0; | |
860 if(stateSimGetPointer()->diveSettings.diveMode == DIVEMODE_Apnea) | |
861 { | |
862 stateSimGetPointerWrite()->lifeData.counterSecondsShallowDepth = 1; | |
863 } | |
864 else | |
865 { | |
866 stateSimGetPointerWrite()->lifeData.counterSecondsShallowDepth = settingsGetPointer()->timeoutDiveReachedZeroDepth - 15; | |
867 } | |
868 } | |
869 } |