Mercurial > public > hwos_code
annotate src/p2_deco.c @ 330:0532cac03ccd new_screen_layout
Merged default into new_screen_layout
author | Janos Kovacs <kovjanos@gmail.com> |
---|---|
date | Wed, 24 Jun 2015 22:19:32 +0200 |
parents | 0e9dcdcf03c1 |
children | 7812ec7ef694 |
rev | line source |
---|---|
0 | 1 // ************************************************************** |
2 // p2_deco.c | |
3 // | |
4 // Created on: 12.05.2009 | |
5 // Author: chsw | |
6 // | |
7 // ************************************************************** | |
8 | |
9 ////////////////////////////////////////////////////////////////////////////// | |
10 // OSTC - diving computer code | |
11 // Copyright (C) 2011 HeinrichsWeikamp GbR | |
12 // | |
13 // This program is free software: you can redistribute it and/or modify | |
14 // it under the terms of the GNU General Public License as published by | |
15 // the Free Software Foundation, either version 3 of the License, or | |
16 // (at your option) any later version. | |
17 // | |
18 // This program is distributed in the hope that it will be useful, | |
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 // GNU General Public License for more details. | |
22 // | |
23 // You should have received a copy of the GNU General Public License | |
24 // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 // | |
26 ////////////////////////////////////////////////////////////////////////////// | |
27 | |
28 // ***************************** | |
29 // ** I N T R O D U C T I O N ** | |
30 // ***************************** | |
31 // | |
32 // OSTC | |
33 // | |
34 // code: | |
35 // p2_deco_main_c_v101.c | |
36 // part2 of the OSTC code | |
37 // code with constant O2 partial pressure routines | |
38 // under construction !! | |
39 // | |
40 // summary: | |
41 // decompression routines | |
42 // for the OSTC experimental project | |
43 // written by Christian Weikamp | |
44 // last revision __________ | |
45 // comments added _________ | |
46 // | |
47 // additional files: | |
48 // p2_tables_v100.romdata (other files) | |
49 // 18f4685_ostc_v100.lkr (linker script) | |
50 // | |
51 // history: | |
52 // 01/03/08 v100: first release candidate | |
53 // 03/13/08 v101: start of programming ppO2 code | |
54 // 03/13/25 v101a: backup of interrim version with ppO2 calculation | |
55 // 03/13/25 v101: open circuit gas change during deco | |
56 // 03/13/25 v101: CNS_fraction calculation | |
57 // 03/13/26 v101: optimization of tissue calc routines | |
58 // 07/xx/08 v102a: debug of bottom time routine | |
59 // 09/xx/08 v102d: Gradient Factor Model implemenation | |
60 // 10/10/08 v104: renamed to build v103 for v118 stable | |
61 // 10/14/08 v104: integration of char_I_depth_last_deco for Gradient Model | |
62 // 03/31/09 v107: integration of FONT Incon24 | |
63 // 05/23/10 v109: 5 gas changes & 1 min timer | |
64 // 07/13/10 v110: cns vault added | |
65 // 12/25/10 v110: split in three files (deco.c, main.c, definitions.h) | |
66 // 2011/01/20: [jDG] Create a common file included in ASM and C code. | |
67 // 2011/01/24: [jDG] Make ascenttime an short. No more overflow! | |
68 // 2011/01/25: [jDG] Fusion deco array for both models. | |
69 // 2011/01/25: [jDG] Use CF(54) to reverse deco order. | |
70 // 2011/02/11: [jDG] Reworked gradient-factor implementation. | |
71 // 2011/02/15: [jDG] Fixed inconsistencies introduced by gas switch delays. | |
72 // 2011/03/21: [jDG] Added gas consumption (CF56 & CF57) evaluation for OCR mode. | |
73 // 2011/04/15: [jDG] Store low_depth in 32bits (w/o rounding), for a better stability. | |
74 // 2011/04/25: [jDG] Added 1mn mode for CNS calculation, to allow it for decoplanning. | |
75 // 2011/04/27: [jDG] Fixed char_O_gradient_factor calculation when model uses gradient-factor. | |
76 // 2011/05/02: [jDG] Added "Future TTS" function (CF58). | |
77 // 2011/05/17: [jDG] Various cleanups. | |
78 // 2011/08/08: [jDG] Computes CNS during deco planning ascent. | |
79 // 2011/11/24: [jDG] Slightly faster and better NDL computation. | |
80 // 2011/12/17: [mH] Remove of the useless debug stuff | |
81 // 2012/02/24: [jDG] Remove missed stop bug. | |
82 // 2012/02/25: [jDG] Looking for a more stable LOW grad factor reference. | |
83 // 2012/09/10: [mH] Fill char_O_deco_time_for_log for logbook write | |
84 // 2012/10/05: [jDG] Better deco_gas_volumes accuracy (average depth, switch between stop). | |
85 // 2013/03/05: [jDG] Should vault low_depth too. | |
86 // 2013/03/05: [jDG] Wrobell remark: ascent_to_first_stop works better with finer steps (2sec). | |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
87 // 2013/05/08: [jDG] A. Salm remark: NOAA tables for CNS are in ATA, not bar. |
115
72259a79eac2
FIX: Gas volume computation when using several travel mix.
heinrichsweikamp
parents:
114
diff
changeset
|
88 // 2013/12/21: [jDG] Fix CNS calculation in decoplan w/o marked gas switch |
72259a79eac2
FIX: Gas volume computation when using several travel mix.
heinrichsweikamp
parents:
114
diff
changeset
|
89 // 2014/06/16: [jDG] Fix Helium diluant. Fix volumes with many travel mix. |
324
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
90 // 2014/06/29: [mH] Compute int_O_ceiling |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
91 // 2015/06/12: [jDG] Fix NDL prediction while desaturating with the Buhlmann model. |
0 | 92 // |
93 // TODO: | |
94 // | |
95 // Literature: | |
313 | 96 // Buhlmann, Albert: Tauchmedizin; 4. Auflage [2002]; |
0 | 97 // Schr"oder, Kai & Reith, Steffen; 2000; S"attigungsvorg"ange beim Tauchen, das Modell ZH-L16, Funktionsweise von Tauchcomputern; http://www.achim-und-kai.de/kai/tausim/saett_faq |
98 // Morrison, Stuart; 2000; DIY DECOMPRESSION; http://www.lizardland.co.uk/DIYDeco.html | |
99 // Balthasar, Steffen; Dekompressionstheorie I: Neo Haldane Modelle; http://www.txfreak.de/dekompressionstheorie_1.pdf | |
100 // Baker, Erik C.; Clearing Up The Confusion About "Deep Stops" | |
101 // Baker, Erik C.; Understanding M-values; http://www.txfreak.de/understanding_m-values.pdf | |
102 // | |
103 // | |
104 | |
105 // ********************* | |
106 // ** I N C L U D E S ** | |
107 // ********************* | |
108 #include <math.h> | |
109 | |
110 // *********************************************** | |
111 // ** V A R I A B L E S D E F I N I T I O N S ** | |
112 // *********************************************** | |
113 | |
114 #include "p2_definitions.h" | |
115 #define TEST_MAIN | |
116 #include "shared_definitions.h" | |
117 | |
118 // Water vapour partial pressure in the lumb. | |
119 #define ppWater 0.0627 | |
120 #define METER_TO_BAR 0.09985 | |
121 #define BAR_TO_METER 10.0150 // (1.0/METER_TO_BAR) | |
122 | |
123 // Surface security factor | |
124 #define SURFACE_DESAT_FACTOR 0.7042 | |
125 | |
126 // ************************* | |
127 // ** P R O T O T Y P E S ** | |
128 // ************************* | |
129 | |
130 static void calc_hauptroutine(void); | |
131 static void calc_nullzeit(void); | |
132 | |
133 static void calc_tissue(PARAMETER unsigned char period); | |
134 static void calc_limit(void); | |
135 | |
136 static void clear_tissue(void); | |
137 static void calc_ascenttime(void); | |
138 static void update_startvalues(void); | |
139 static void clear_deco_table(void); | |
140 static unsigned char update_deco_table(void); | |
141 | |
142 static void sim_tissue(PARAMETER unsigned char period); | |
143 static void sim_limit(PARAMETER float GF_current); | |
144 static void sim_extra_time(void); | |
145 static void calc_dive_interval(void); | |
146 | |
147 static void calc_gradient_factor(void); | |
148 static void calc_wo_deco_step_1_min(void); | |
149 | |
150 static void calc_hauptroutine_data_input(void); | |
151 static void calc_hauptroutine_update_tissues(void); | |
152 static void calc_hauptroutine_calc_deco(void); | |
153 static void sim_ascent_to_first_stop(void); | |
154 | |
155 static unsigned char gas_switch_deepest(void); | |
156 static void gas_switch_set(void); | |
157 | |
158 static unsigned char calc_nextdecodepth(void); | |
159 | |
160 //---- Bank 5 parameters ----------------------------------------------------- | |
161 #ifndef UNIX | |
162 # pragma udata bank5=0x500 | |
163 #endif | |
164 | |
165 static float GF_low; | |
166 static float GF_high; | |
167 static float GF_delta; | |
168 static float locked_GF_step; // GF_delta / low_depth | |
169 | |
170 static unsigned char temp_depth_limit; | |
171 static unsigned char low_depth; // Depth of deepest stop | |
172 | |
173 // Simulation context: used to predict ascent. | |
174 static unsigned char sim_lead_tissue_no; // Leading compatiment number. | |
175 static float sim_lead_tissue_limit; // Buhlmann tolerated pressure. | |
176 | |
177 // Real context: what we are doing now. | |
178 static float calc_lead_tissue_limit; // | |
179 | |
180 static unsigned char internal_deco_time[NUM_STOPS]; | |
181 static unsigned char internal_deco_depth[NUM_STOPS]; | |
182 | |
183 static float cns_vault; | |
184 static float low_depth_vault; | |
185 static float pres_tissue_N2_vault[NUM_COMP]; | |
186 static float pres_tissue_He_vault[NUM_COMP]; | |
187 | |
188 //---- Bank 6 parameters ----------------------------------------------------- | |
189 #ifndef UNIX | |
190 # pragma udata bank6=0x600 | |
191 #endif | |
192 | |
193 static unsigned char ci; | |
324
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
194 static float pres_respiration; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
195 static float pres_surface; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
196 static float temp_deco; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
197 static float ppN2; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
198 static float ppHe; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
199 static float temp_tissue; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
200 static float N2_ratio; // Breathed gas nitrogen ratio. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
201 static float He_ratio; // Breathed gas helium ratio. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
202 static float var_N2_a; // Buhlmann a, for current N2 tissue. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
203 static float var_N2_b; // Buhlmann b, for current N2 tissue. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
204 static float var_He_a; // Buhlmann a, for current He tissue. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
205 static float var_He_b; // Buhlmann b, for current He tissue. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
206 static float var_N2_e; // Exposition, for current N2 tissue. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
207 static float var_He_e; // Exposition, for current He tissue. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
208 static float var_N2_ht; // Half-time for current N2 tissue. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
209 static float var_He_ht; // Half-time for current N2 tissue. |
0 | 210 |
211 static float pres_diluent; // new in v.101 | |
212 static float const_ppO2; // new in v.101 | |
213 | |
214 static unsigned char sim_gas_last_depth; // Depth of last used gas, to detected a gas switch. | |
215 static unsigned char sim_gas_last_used; // Number of last used gas, to detected a gas switch. | |
216 static unsigned short sim_dive_mins; // Simulated dive time. | |
324
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
217 static float calc_N2_ratio; // Simulated (switched) nitrogen ratio. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
218 static float calc_He_ratio; // Simulated (switched) helium ratio. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
219 static float CNS_fraction; // new in v.101 |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
220 static float float_saturation_multiplier; // new in v.101 |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
221 static float float_desaturation_multiplier; // new in v.101 |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
222 static float float_deco_distance; // new in v.101 |
0 | 223 |
224 static unsigned char deco_gas_change[NUM_GAS]; // new in v.109 | |
111 | 225 static unsigned char internal_deco_gas [NUM_STOPS]; |
0 | 226 |
227 //---- Bank 7 parameters ----------------------------------------------------- | |
228 #ifndef UNIX | |
229 # pragma udata bank7=0x700 | |
230 #endif | |
133
939f1e83c4c2
BUGFIX: Surface interval was not displayed correctly in some cases
heinrichsweikamp
parents:
126
diff
changeset
|
231 // Keep order of 0x700 variables |
0 | 232 float pres_tissue_N2[NUM_COMP]; |
233 float pres_tissue_He[NUM_COMP]; | |
234 float sim_pres_tissue_N2[NUM_COMP]; // 16 floats = 64 bytes. | |
235 float sim_pres_tissue_He[NUM_COMP]; // 16 floats = 64 bytes. | |
236 | |
237 //---- Bank 8 parameters ----------------------------------------------------- | |
238 #ifndef UNIX | |
239 # pragma udata overlay bank8=0x800 | |
240 static char md_pi_subst[256]; | |
241 # define C_STACK md_pi_subst // Overlay C-code data stack here, too. | |
242 #endif | |
243 | |
244 // Back to bank6 for further tmp data | |
245 #ifndef UNIX | |
246 # pragma udata bank6 | |
247 #endif | |
248 | |
249 ////////////////////////////////////////////////////////////////////////////// | |
250 ////////////////////////////////////////////////////////////////////////////// | |
251 ///////////////////////////// THE LOOKUP TABLES ////////////////////////////// | |
252 ////////////////////////////////////////////////////////////////////////////// | |
253 ////////////////////////////////////////////////////////////////////////////// | |
254 // | |
255 // End of PROM code is 17F00, So push tables on PROM top... | |
256 // | |
257 #ifndef UNIX | |
258 # pragma romdata buhlmann_tables = 0x1DD00 // Needs to be in UPPER bank. | |
259 #endif | |
260 | |
261 #include "p2_tables.romdata" // new table for deco_main_v.101 (var_N2_a modified) | |
262 | |
263 ////////////////////////////////////////////////////////////////////////////// | |
264 ////////////////////////////////////////////////////////////////////////////// | |
265 ////////////////////////////// THE SUBROUTINES /////////////////////////////// | |
266 ////////////////////////////////////////////////////////////////////////////// | |
267 ////////////////////////////////////////////////////////////////////////////// | |
268 // | |
269 // all new in v.102 | |
270 // moved from 0x0D000 to 0x0C000 in v.108 | |
271 #ifndef UNIX | |
272 # pragma code p2_deco = 0x0C000 | |
273 #endif | |
274 | |
275 ////////////////////////////////////////////////////////////////////////////// | |
276 ////////////////////////////////////////////////////////////////////////////// | |
277 /////////////////////// U T I L I T I E S ///////////////////////////////// | |
278 ////////////////////////////////////////////////////////////////////////////// | |
279 ////////////////////////////////////////////////////////////////////////////// | |
280 | |
281 ////////////////////////////////////////////////////////////////////////////// | |
282 // Bump to blue-screen when an assert is wrong | |
283 #ifdef __DEBUG | |
284 void assert_failed(PARAMETER short int line) | |
285 { | |
286 } | |
287 #endif | |
288 | |
289 ////////////////////////////////////////////////////////////////////////////// | |
290 // When calling C code from ASM context, the data stack pointer and | |
291 // frames should be reset. Bank8 is used by stack | |
292 | |
293 #ifdef CROSS_COMPILE | |
294 # define RESET_C_STACK | |
295 #else | |
296 # ifdef __DEBUG | |
297 # define RESET_C_STACK fillDataStack(); | |
298 void fillDataStack(void) | |
299 { | |
300 _asm | |
301 LFSR 1,C_STACK | |
302 MOVLW 0xCC | |
303 loop: MOVWF POSTINC1,0 | |
304 TSTFSZ FSR1L,0 | |
305 BRA loop | |
306 | |
307 LFSR 1,C_STACK | |
308 LFSR 2,C_STACK | |
309 _endasm | |
310 } | |
311 # else | |
312 # define RESET_C_STACK \ | |
313 _asm \ | |
314 LFSR 1, C_STACK \ | |
315 LFSR 2, C_STACK \ | |
316 _endasm | |
317 # endif | |
318 #endif | |
319 | |
320 ////////////////////////////////////////////////////////////////////////////// | |
321 // Fast subroutine to read timer 5. | |
313 | 322 // Note: result is in 1/32 of msecs (30,51757813 us/bit to be precise) |
0 | 323 static unsigned short tmr5(void) |
324 { | |
325 #ifndef CROSS_COMPILE | |
326 _asm | |
327 movff 0xf7c,PRODL // TMR5L | |
328 movff 0xf7d,PRODH // TMR5H | |
329 _endasm // result in PRODH:PRODL. | |
330 #else | |
331 return 0; | |
332 #endif | |
333 } | |
334 | |
335 | |
336 ////////////////////////////////////////////////////////////////////////////// | |
337 // read buhlmann tables A and B for compatriment ci | |
338 // | |
339 static void read_buhlmann_coefficients(void) | |
340 { | |
341 #ifndef CROSS_COMPILE | |
342 // Note: we don't use far rom pointer, because the | |
343 // 24 bits is too complex, hence we have to set | |
344 // the UPPER page ourself... | |
345 // --> Set zero if tables are moved to lower pages ! | |
346 _asm | |
347 movlw 1 | |
348 movwf TBLPTRU,0 | |
349 _endasm | |
350 #endif | |
351 | |
352 assert( ci < NUM_COMP ); | |
353 | |
354 // Use an interleaved array (AoS) to access coefficients with a | |
355 // single addressing. | |
356 { | |
357 overlay rom const float* ptr = &buhlmann_ab[4*ci]; | |
358 var_N2_a = *ptr++; | |
359 var_N2_b = *ptr++; | |
360 var_He_a = *ptr++; | |
361 var_He_b = *ptr++; | |
362 } | |
363 } | |
364 | |
365 ////////////////////////////////////////////////////////////////////////////// | |
366 // read buhlmann tables for compatriment ci | |
367 // If period == 0 : 2sec interval | |
368 // 1 : 1 min interval | |
369 // 2 : 10 min interval. | |
370 static void read_buhlmann_times(PARAMETER char period) | |
371 { | |
372 #ifndef CROSS_COMPILE | |
373 // Note: we don't use far rom pointer, because the | |
374 // 24 bits is to complex, hence we have to set | |
375 // the UPPER page ourself... | |
376 // --> Set zero if tables are moved to lower pages ! | |
377 _asm | |
378 movlw 1 | |
379 movwf TBLPTRU,0 | |
380 _endasm | |
381 #endif | |
382 | |
383 assert( ci < NUM_COMP ); | |
384 | |
385 // Integration intervals. | |
386 switch(period) | |
387 { | |
388 case 0: //---- 2 sec ----------------------------------------------------- | |
389 { | |
390 overlay rom const float* ptr = &e2secs[2*ci]; | |
391 var_N2_e = *ptr++; | |
392 var_He_e = *ptr++; | |
393 } | |
394 break; | |
395 | |
396 case 1: //---- 1 min ----------------------------------------------------- | |
397 { | |
398 overlay rom const float* ptr = &e1min[2*ci]; | |
399 var_N2_e = *ptr++; | |
400 var_He_e = *ptr++; | |
401 } | |
402 break; | |
403 | |
404 case 2: //---- 10 min ---------------------------------------------------- | |
405 { | |
406 overlay rom const float* ptr = &e10min[2*ci]; | |
407 var_N2_e = *ptr++; | |
408 var_He_e = *ptr++; | |
409 } | |
410 break; | |
411 | |
412 default: | |
413 assert(0); // Never go there... | |
414 } | |
415 } | |
416 | |
417 ////////////////////////////////////////////////////////////////////////////// | |
418 // read buhlmann tables for compatriment ci | |
419 // | |
420 static void read_buhlmann_ht(void) | |
421 { | |
422 | |
423 #ifndef CROSS_COMPILE | |
424 // Note: we don't use far rom pointer, because the | |
425 // 24 bits is to complex, hence we have to set | |
426 // the UPPER page ourself... | |
427 // --> Set zero if tables are moved to lower pages ! | |
428 _asm | |
429 movlw 1 | |
430 movwf TBLPTRU,0 | |
431 _endasm | |
432 #endif | |
433 | |
434 assert( ci < NUM_COMP ); | |
435 { | |
436 overlay rom const float* ptr = &buhlmann_ht[2*ci]; | |
437 var_N2_ht = *ptr++; | |
438 var_He_ht = *ptr++; | |
439 } | |
440 | |
441 assert( 4.0 <= var_N2_ht && var_N2_ht <= 635.0 ); | |
442 assert( 1.5099 <= var_He_ht && var_He_ht <= 240.03 ); | |
443 } | |
444 | |
445 ////////////////////////////////////////////////////////////////////////////// | |
446 // calc_nextdecodepth | |
447 // | |
448 // new in v.102 | |
449 // | |
450 // INPUT, changing during dive: | |
451 // temp_deco | |
452 // low_depth | |
453 // | |
454 // INPUT, fixed during dive: | |
455 // pres_surface | |
456 // GF_delta | |
457 // GF_high | |
458 // GF_low | |
459 // char_I_depth_last_deco | |
460 // float_deco_distance | |
461 // | |
462 // RETURN TRUE iff a stop is needed. | |
463 // | |
464 // OUTPUT | |
465 // locked_GF_step | |
466 // temp_depth_limt | |
467 // low_depth | |
468 // | |
469 static unsigned char calc_nextdecodepth(void) | |
470 { | |
471 //--- Max ascent speed --------------------------------------------------- | |
472 // Recompute leading gas limit, at current depth: | |
473 overlay float depth = (temp_deco - pres_surface) * BAR_TO_METER; | |
474 | |
475 // At most, ascent 1 minute, at 10m/min == 10.0 m. | |
476 overlay float min_depth = (depth > 10.0) ? (depth - 10.0) : 0.0; | |
477 | |
478 // Do we need to stop at current depth ? | |
479 overlay unsigned char need_stop = 0; | |
480 | |
481 assert( depth >= -0.2 ); // Allow for 200mbar of weather change. | |
482 | |
483 //---- ZH-L16 + GRADIENT FACTOR model ------------------------------------ | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
484 if( char_I_deco_model != 0 ) |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
485 { |
0 | 486 overlay unsigned char first_stop = 0; |
487 overlay float p; | |
488 | |
489 sim_limit( GF_low ); | |
490 p = sim_lead_tissue_limit - pres_surface; | |
491 if( p <= 0.0f ) | |
492 goto no_deco_stop; // We can surface directly... | |
493 | |
494 p *= BAR_TO_METER; | |
495 if( p < min_depth ) | |
496 goto no_deco_stop; // First stop is higher than 1' ascent. | |
497 | |
498 first_stop = 3 * (short)(0.99999 + p*0.333333); | |
499 assert( first_stop < 128 ); | |
500 | |
501 // Apply correction for the shallowest stop. | |
502 if( first_stop == 3 ) // new in v104 | |
503 first_stop = char_I_depth_last_deco; // Use last 3m..6m instead. | |
504 | |
505 // Store the deepest point needing a deco stop as the LOW reference for GF. | |
506 // NOTE: following stops will be validated using this LOW-HIGH gf scale, | |
507 // so if we want to keep coherency, we should not validate this stop | |
508 // yet, but apply the search to it, as for all the following stops afterward. | |
509 if( first_stop > low_depth ) | |
510 { | |
511 low_depth = first_stop; | |
512 locked_GF_step = GF_delta / first_stop; | |
513 } | |
514 | |
515 // We have a stop candidate. | |
516 // But maybe ascending to the next stop will diminish the constraint, | |
517 // because the GF might decrease more than the preassure gradient... | |
518 while(first_stop > 0) | |
519 { | |
520 overlay unsigned char next_stop; // Next depth (0..90m) | |
521 overlay float pres_stop; // Next pressure (bar) | |
522 | |
523 // Check max speed, or reaching surface. | |
524 if( first_stop <= min_depth ) | |
525 goto no_deco_stop; | |
526 | |
527 if( first_stop <= char_I_depth_last_deco ) // new in v104 | |
528 next_stop = 0; | |
529 else if( first_stop == 6 ) | |
530 next_stop = char_I_depth_last_deco; | |
531 else | |
532 next_stop = first_stop - 3; // Index of next (upper) stop. | |
533 | |
534 // Just a check we are indeed above LOW ref. | |
535 assert( next_stop < low_depth ); | |
536 | |
537 // Total preassure at the new stop candidate: | |
538 pres_stop = next_stop * METER_TO_BAR | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
539 + pres_surface; |
0 | 540 |
541 // Keep GF_low until a first stop depth is found: | |
542 sim_limit( GF_high - next_stop * locked_GF_step ); | |
543 | |
544 // Check upper limit (lowest pressure tolerated): | |
545 if( sim_lead_tissue_limit >= pres_stop ) // check if ascent to next deco stop is ok | |
546 goto deco_stop_found; | |
547 | |
548 // Else, validate that stop and loop... | |
549 first_stop = next_stop; | |
550 } | |
551 assert( first_stop == 0 ); | |
552 | |
553 no_deco_stop: | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
554 temp_depth_limit = min_depth; |
0 | 555 goto done; |
556 | |
557 // next stop is the last validated depth found, aka first_stop | |
558 deco_stop_found: | |
559 need_stop = 1; // Hit. | |
560 temp_depth_limit = first_stop; // Stop depth, in meter. | |
561 | |
562 done: | |
563 ; | |
564 } | |
565 else //---- ZH-L16 model ------------------------------------------------- | |
566 { | |
567 overlay float pres_gradient; | |
568 | |
569 // Original model | |
570 // optimized in v.101 | |
571 // char_I_depth_last_deco included in v.101 | |
572 | |
573 // Compute sim_lead_tissue_limit too, but just once. | |
574 sim_limit(1.0); | |
575 | |
576 pres_gradient = sim_lead_tissue_limit - pres_surface; | |
577 if (pres_gradient >= 0) | |
578 { | |
579 pres_gradient *= BAR_TO_METER/3; // Bar --> stop number; | |
580 temp_depth_limit = 3 * (short) (pres_gradient + 0.99); // --> metre : depth for deco | |
581 need_stop = 1; // Hit. | |
582 | |
583 // Implement last stop at 4m/5m/6m... | |
584 if( temp_depth_limit == 3 ) | |
585 temp_depth_limit = char_I_depth_last_deco; | |
586 } | |
587 else | |
588 temp_depth_limit = 0; | |
589 } | |
590 | |
591 //---- Check gas change -------------------------------------------------- | |
592 need_stop |= gas_switch_deepest(); // Update temp_depth_limit if there is a change, | |
593 | |
594 return need_stop; | |
595 } | |
596 | |
597 ////////////////////////////////////////////////////////////////////////////// | |
598 // copy_deco_table | |
599 // | |
600 // Buffer the stops, once computed, so we can continue to display them | |
601 // while computing the next set. | |
602 // | |
603 static void copy_deco_table(void) | |
604 { | |
605 // Copy depth of the first (deepest) stop, because when reversing | |
606 // order, it will be hard to find... | |
111 | 607 char_O_first_deco_depth = internal_deco_depth[0]; |
0 | 608 char_O_first_deco_time = internal_deco_time [0]; |
609 | |
610 { | |
611 overlay unsigned char x, y; | |
612 | |
613 for(x=0; x<NUM_STOPS; x++) | |
614 { | |
615 char_O_deco_depth[x] = internal_deco_depth[x]; | |
616 char_O_deco_time [x] = internal_deco_time [x]; | |
111 | 617 char_O_deco_gas [x] = internal_deco_gas [x]; |
0 | 618 } |
619 | |
620 //Now fill the char_O_deco_time_for_log array | |
621 //---- First: search the first non-null depth | |
622 for(x=(NUM_STOPS-1); x != 0; --x) | |
623 if( internal_deco_depth[x] != 0 ) break; | |
624 | |
625 //---- Second: copy to output table (in reverse order) | |
626 for(y=0; y<NUM_STOPS; y++, --x) | |
627 { | |
628 char_O_deco_time_for_log[y] = internal_deco_time [x]; | |
629 | |
630 // Stop only once the last transfer is done. | |
631 if( x == 0 ) break; | |
632 } | |
633 | |
634 //---- Third: fill table end with null | |
635 for(y++; y<NUM_STOPS; y++) | |
636 { | |
637 char_O_deco_time_for_log [y] = 0; | |
638 } | |
639 } | |
640 } | |
641 | |
642 ////////////////////////////////////////////////////////////////////////////// | |
643 // temp_tissue_safety // | |
644 // | |
645 // outsourced in v.102 | |
646 // | |
647 // Apply safety factors for brand ZH-L16 model. | |
648 // | |
649 static void temp_tissue_safety(void) | |
650 { | |
651 assert( 0.0 < float_desaturation_multiplier && float_desaturation_multiplier <= 1.0 ); | |
652 assert( 1.0 <= float_saturation_multiplier && float_saturation_multiplier <= 2.0 ); | |
653 | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
654 if( char_I_deco_model == 0 ) |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
655 { |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
656 if( temp_tissue < 0.0 ) |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
657 temp_tissue *= float_desaturation_multiplier; |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
658 else |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
659 temp_tissue *= float_saturation_multiplier; |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
660 } |
0 | 661 } |
662 | |
663 ////////////////////////////////////////////////////////////////////////////// | |
664 ////////////////////////////////////////////////////////////////////////////// | |
665 // ** THE JUMP-IN CODE ** | |
666 // ** for the asm code ** | |
667 ////////////////////////////////////////////////////////////////////////////// | |
668 ////////////////////////////////////////////////////////////////////////////// | |
669 | |
670 ////////////////////////////////////////////////////////////////////////////// | |
671 // Called every 2 seconds during diving. | |
672 // update tissues every time. | |
673 // | |
674 // Every 6 seconds (or slower when TTS > 16): | |
675 // - update deco table (char_O_deco_time/depth) with new values. | |
676 // - update ascent time, | |
677 // - set status to zero (so we can check there is new results). | |
678 // | |
679 void deco_calc_hauptroutine(void) | |
680 { | |
681 RESET_C_STACK | |
682 calc_hauptroutine(); | |
683 int_O_desaturation_time = 65535; | |
684 } | |
685 | |
686 ////////////////////////////////////////////////////////////////////////////// | |
687 // Reset decompression model: | |
688 // + Set all tissues to equilibrium with Air at ambient pressure. | |
689 // + Reset last stop to 0m | |
690 // + Reset all model output. | |
691 void deco_clear_tissue(void) | |
692 { | |
693 RESET_C_STACK | |
694 clear_tissue(); | |
695 } | |
696 | |
697 ////////////////////////////////////////////////////////////////////////////// | |
698 // Called every 1 min during decoplanning. | |
699 // Update tissues for 1 min. | |
700 // | |
701 void deco_calc_tissue(void) | |
702 { | |
703 RESET_C_STACK | |
704 calc_hauptroutine_update_tissues(); | |
705 } | |
706 | |
707 ////////////////////////////////////////////////////////////////////////////// | |
708 | |
709 void deco_calc_wo_deco_step_1_min(void) | |
710 { | |
711 RESET_C_STACK | |
712 calc_wo_deco_step_1_min(); | |
713 deco_calc_desaturation_time(); | |
714 } | |
715 | |
716 ////////////////////////////////////////////////////////////////////////////// | |
717 | |
718 void deco_calc_dive_interval(void) | |
719 { | |
720 RESET_C_STACK | |
721 calc_dive_interval(); | |
722 } | |
723 | |
724 ////////////////////////////////////////////////////////////////////////////// | |
725 // Find current gas in the list (if any). | |
726 // | |
727 // Input: char_I_current_gas = 1..6 | |
728 // | |
729 // Output: sim_gas_last_depth = 0..5, temp_depth_limit. | |
730 // | |
731 static void gas_switch_find_current(void) | |
732 { | |
323
19f7dc2b12d6
FIX minor assert: separate diluant list means current gas can go up to 10.
jDG
parents:
322
diff
changeset
|
733 assert( 0 < char_I_current_gas && char_I_current_gas <= (2*NUM_GAS) ); |
0 | 734 |
735 if( char_I_current_gas <= NUM_GAS ) // Gas1..Gas5 | |
736 { | |
737 sim_gas_last_used = char_I_current_gas; | |
738 | |
739 // Note: if current is first gas, we must find it, but not set | |
740 // last depth change to surface. | |
741 if( char_I_deco_gas_change[sim_gas_last_used-1] ) | |
742 sim_gas_last_depth = char_I_deco_gas_change[sim_gas_last_used-1]; | |
743 } | |
744 else | |
745 sim_gas_last_used = 0; // Gas 6 = manual set | |
746 } | |
747 | |
748 ////////////////////////////////////////////////////////////////////////////// | |
749 // Find deepest available gas. | |
750 // | |
751 // Input: temp_depth_limit, | |
752 // deco_gas_change[] | |
753 // sim_gas_depth_used, sim_dive_mins. | |
754 // | |
755 // RETURNS TRUE if a stop is needed for gas switch. | |
756 // | |
757 // Output: temp_depth_limit, sim_gas_depth_used IFF the is a switch. | |
758 // | |
759 // NOTE: might be called from bottom (when sim_gas_delay and sim_gas_depth_used | |
760 // are null), or during the ascent to make sure we are not passing a | |
761 // stop (in which case both can be already set). | |
762 // | |
763 static unsigned char gas_switch_deepest(void) | |
764 { | |
765 overlay unsigned char switch_deco = 0, switch_last = 0; | |
766 | |
767 if (char_I_const_ppO2 == 0) | |
768 { | |
769 overlay unsigned char j; | |
770 | |
771 // Loop over all enabled gas, to find the deepest one, | |
772 // above last used gas, but below temp_depth_limit. | |
773 for(j=0; j<NUM_GAS; ++j) | |
774 { | |
775 // Gas not (yet) allowed ? Skip ! | |
776 if( temp_depth_limit > deco_gas_change[j] ) | |
777 continue; | |
778 | |
779 // Gas deeper (or equal) than the current one ? Skip ! | |
780 if( sim_gas_last_depth && deco_gas_change[j] >= sim_gas_last_depth ) | |
781 continue; | |
782 | |
783 // First, or deeper ? | |
784 if( switch_deco < deco_gas_change[j] ) | |
785 { | |
786 switch_deco = deco_gas_change[j]; | |
787 switch_last = j+1; // 1..5 | |
788 } | |
789 } | |
790 } | |
791 | |
792 // If there is a better gas available | |
793 if( switch_deco ) | |
794 { | |
795 assert( !sim_gas_last_depth || sim_gas_last_depth > switch_deco ); | |
796 | |
797 sim_gas_last_depth = switch_deco; | |
798 sim_gas_last_used = switch_last; | |
799 return 0; | |
800 } | |
801 return 0; | |
802 } | |
803 | |
804 ////////////////////////////////////////////////////////////////////////////// | |
805 // Calculate gas switches | |
806 // | |
807 // | |
808 // Input: N2_ratio, He_ratio. | |
809 // sim_gas_last_used | |
810 // | |
811 // Output: calc_N2_ratio, calc_He_ratio | |
812 // | |
813 static void gas_switch_set(void) | |
814 { | |
815 assert( sim_gas_last_used <= NUM_GAS ); | |
816 | |
817 if( sim_gas_last_used == 0 ) // Gas6 = manualy set gas. | |
818 { | |
819 calc_N2_ratio = N2_ratio; | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
820 calc_He_ratio = He_ratio; |
0 | 821 } |
822 else | |
823 { | |
824 calc_N2_ratio = char_I_deco_N2_ratio[sim_gas_last_used-1] * 0.01; | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
825 calc_He_ratio = char_I_deco_He_ratio[sim_gas_last_used-1] * 0.01; |
0 | 826 } |
827 | |
828 assert( 0.0 <= calc_N2_ratio && calc_N2_ratio <= 0.95 ); | |
114
109df032cc54
minor fix: assert should pass for pure helium diluant
heinrichsweikamp
parents:
111
diff
changeset
|
829 assert( 0.0 <= calc_He_ratio && calc_He_ratio <= 1.00 ); |
0 | 830 assert( (calc_N2_ratio + calc_He_ratio) <= 1.00 ); |
831 } | |
832 | |
833 ////////////////////////////////////////////////////////////////////////////// | |
834 // | |
835 // Input: calc_N2_ratio, calc_He_ratio : simulated gas mix. | |
836 // temp_deco : simulated respiration pressure | |
837 // float_deco_distance : security factor. | |
838 // Water-vapor pressure inside limbs (ppWater). | |
839 // | |
840 // Output: ppN2, ppHe. | |
841 // | |
842 static void sim_alveolar_presures(void) | |
843 { | |
844 overlay float deco_diluent = temp_deco; // new in v.101 | |
845 | |
846 // Take deco offset into account, but not at surface. | |
847 // Note: this should be done on ambiant pressure, hence before | |
848 // computing the diluant partial pressure... | |
849 if( deco_diluent > pres_surface ) | |
850 deco_diluent += float_deco_distance; | |
851 | |
852 //---- CCR mode : deco gas switch ? -------------------------------------- | |
853 if( char_I_const_ppO2 != 0 ) | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
854 { |
0 | 855 // In CCR mode, use calc_XX_ratio instead of XX_ratio. |
856 // Note: PPO2 and ratios are known outside the lumbs, so there is no | |
857 // ppWater in the equations below: | |
858 deco_diluent -= const_ppO2; | |
859 deco_diluent /= calc_N2_ratio + calc_He_ratio; | |
860 | |
861 if (deco_diluent > temp_deco) | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
862 deco_diluent = temp_deco; |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
863 } |
0 | 864 |
865 if( deco_diluent > ppWater ) | |
866 { | |
867 ppN2 = calc_N2_ratio * (deco_diluent - ppWater); | |
868 ppHe = calc_He_ratio * (deco_diluent - ppWater); | |
869 } | |
870 else | |
871 { | |
872 ppN2 = 0.0; | |
873 ppHe = 0.0; | |
874 } | |
875 assert( 0.0 <= ppN2 && ppN2 < 14.0 ); | |
876 assert( 0.0 <= ppHe && ppHe < 14.0 ); | |
877 } | |
878 | |
879 ////////////////////////////////////////////////////////////////////////////// | |
880 // clear_tissue | |
881 // | |
882 // optimized in v.101 (var_N2_a) | |
883 // | |
884 // preload tissues with standard pressure for the given ambient pressure. | |
885 // Note: fixed N2_ratio for standard air. | |
886 // | |
887 static void clear_tissue(void) | |
888 { | |
889 overlay float p; | |
890 | |
891 // Kludge: the 0.0002 of 0.7902 are missing with standard air. | |
892 N2_ratio = 0.7902; | |
893 pres_respiration = int_I_pres_respiration * 0.001; | |
894 | |
895 p = N2_ratio * (pres_respiration - ppWater); | |
896 for(ci=0; ci<NUM_COMP; ci++) | |
897 { | |
313 | 898 // cycle through the 16 Buhlmann N2 tissues |
0 | 899 pres_tissue_N2[ci] = p; |
900 | |
313 | 901 // cycle through the 16 Buhlmann tissues for Helium |
0 | 902 pres_tissue_He[ci] = 0.0; |
903 } | |
904 | |
905 clear_deco_table(); | |
906 char_O_deco_status = 0; | |
907 char_O_nullzeit = 0; | |
908 int_O_ascenttime = 0; | |
909 char_O_gradient_factor = 0; | |
910 char_O_relative_gradient_GF = 0; | |
911 | |
912 calc_lead_tissue_limit = 0.0; | |
913 char_O_gtissue_no = 0; | |
914 } | |
915 | |
916 ////////////////////////////////////////////////////////////////////////////// | |
917 // calc_hauptroutine | |
918 // | |
919 // this is the major code in dive mode calculates: | |
920 // the tissues, | |
921 // the bottom time, | |
922 // and simulates the ascend with all deco stops. | |
923 // | |
924 // The deco_state sequence is : | |
925 // 3 (at surface) | |
926 // +---> 0 : calc nullzeit | |
927 // | 2 : simulate ascent to first stop (at 10m/min, less that 16x 1min simu) | |
928 // | +-> 1 : simulate up to 16min of stops. | |
929 // | +------< not finished | |
930 // +--------< finish | |
931 // | |
932 // Added steps 6,5 for @+5 calculation: | |
933 // 6 = ascent to first stop (same as 2), except continue to 7 | |
934 // 7 = same as 1, except loop to 7. | |
935 // | |
936 static void calc_hauptroutine(void) | |
937 { | |
938 static unsigned char backup_gas_used; | |
939 static unsigned char backup_gas_depth; | |
940 | |
941 calc_hauptroutine_data_input(); | |
942 | |
943 calc_hauptroutine_update_tissues(); | |
944 calc_gradient_factor(); | |
945 | |
946 // toggle between calculation for nullzeit (bottom time), | |
947 // deco stops | |
948 // and more deco stops (continue) | |
949 switch( char_O_deco_status ) | |
950 { | |
951 case 3: //---- At surface: start a new dive ------------------------------ | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
952 clear_deco_table(); |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
953 copy_deco_table(); |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
954 int_O_ascenttime = 0; // Reset DTR. |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
955 int_O_extra_ascenttime = 0; |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
956 char_O_nullzeit = 0; // Reset bottom time. |
0 | 957 char_O_deco_status = 0; // Calc bottom-time/nullzeit next iteration. |
958 | |
959 // Values that should be reset just once for the full real dive. | |
960 // This is used to record the lowest stop for the whole dive, | |
961 // Including ACCROSS all simulated ascent. | |
962 low_depth = 0; | |
963 locked_GF_step = 0.0; | |
964 | |
965 // Reset gas switch history. | |
966 backup_gas_used = sim_gas_last_used = 0; | |
967 backup_gas_depth = sim_gas_last_depth = 0; | |
968 sim_dive_mins = 0; | |
969 break; | |
970 | |
971 case 0: //---- bottom time ----------------------------------------------- | |
972 default: | |
973 gas_switch_find_current(); // Lookup for current gas & time. | |
974 gas_switch_set(); // setup calc_ratio's | |
975 | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
976 calc_nullzeit(); |
0 | 977 if( char_O_nullzeit > 0 ) // Some NDL time left ? |
978 { | |
979 char_O_deco_status = 0; // YES: recalc ndl next time. | |
980 clear_deco_table(); // Also clear stops ! | |
981 copy_deco_table(); | |
982 char_O_deco_last_stop = 0; // And last stop (OSTC menu anim) | |
983 } | |
984 else | |
985 char_O_deco_status = 2; // NO: calc ascent next time. | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
986 break; |
0 | 987 |
988 case 2: //---- Simulate ascent to first stop ----------------------------- | |
989 case 6: // @+5min variation | |
990 // Check proposed gas at begin of ascent simulation | |
991 sim_dive_mins = int_I_divemins; // Init current time. | |
992 | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
993 gas_switch_find_current(); // Lookup for current gas & time. |
0 | 994 gas_switch_set(); // setup calc_ratio's |
995 | |
996 backup_gas_used = sim_gas_last_used; // And save for later simu steps. | |
997 backup_gas_depth = sim_gas_last_depth; // And save for later simu steps. | |
998 | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
999 sim_ascent_to_first_stop(); |
0 | 1000 |
1001 // Calc stops next time (deco or gas switch). | |
1002 char_O_deco_status = 1 | ( char_O_deco_status & 4 ); | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1003 break; |
0 | 1004 |
1005 case 1: //---- Simulate stops -------------------------------------------- | |
1006 case 5: // @+5 variation. | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1007 calc_hauptroutine_calc_deco(); |
0 | 1008 |
1009 // If simulation is finished, restore the GF low reference, so that | |
1010 // next ascent simulation is done from the current depth: | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1011 if( (char_O_deco_status & 3) == 0 ) |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1012 { |
0 | 1013 sim_gas_last_used = backup_gas_used; |
1014 sim_gas_last_depth = backup_gas_depth; | |
1015 } | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1016 break; |
0 | 1017 } |
1018 } | |
1019 | |
1020 ////////////////////////////////////////////////////////////////////////////// | |
1021 // calc_hauptroutine_data_input | |
1022 // | |
1023 // Reset all C-code dive parameters from their ASM-code values. | |
1024 // Detect gas change condition. | |
1025 // | |
1026 void calc_hauptroutine_data_input(void) | |
1027 { | |
1028 overlay short int_temp; | |
1029 overlay unsigned char g; | |
1030 | |
1031 pres_respiration = int_I_pres_respiration * 0.001; | |
1032 pres_surface = int_I_pres_surface * 0.001; | |
1033 N2_ratio = char_I_N2_ratio * 0.01; | |
1034 He_ratio = char_I_He_ratio * 0.01; | |
313 | 1035 float_deco_distance = char_I_deco_distance * 0.01; // Get offset in BAR |
0 | 1036 |
1037 // ____________________________________________________ | |
1038 // | |
1039 // _____________ G A S _ C H A N G E S ________________ | |
1040 // ____________________________________________________ | |
1041 | |
1042 // Keep a margin of 150mbar = 1.50m | |
1043 int_temp = (int_I_pres_respiration - int_I_pres_surface) | |
1044 + MBAR_REACH_GASCHANGE_AUTO_CHANGE_OFF; | |
1045 | |
1046 // Gas are selectable if we did not pass the change depth by more than 1.50m: | |
1047 for(g=0; g < NUM_GAS; ++g) | |
1048 { | |
1049 deco_gas_change[g] = 0; | |
1050 if(char_I_deco_gas_change[g]) | |
1051 if( int_temp > 100 *(short)char_I_deco_gas_change[g] ) | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1052 deco_gas_change[g] = char_I_deco_gas_change[g]; |
0 | 1053 } |
1054 | |
1055 const_ppO2 = char_I_const_ppO2 * 0.01; | |
1056 float_desaturation_multiplier = char_I_desaturation_multiplier * 0.01; | |
1057 float_saturation_multiplier = char_I_saturation_multiplier * 0.01; | |
1058 GF_low = char_I_GF_Low_percentage * 0.01; | |
1059 GF_high = char_I_GF_High_percentage * 0.01; | |
1060 GF_delta = GF_high - GF_low; | |
1061 } | |
1062 | |
1063 ////////////////////////////////////////////////////////////////////////////// | |
1064 // | |
1065 // | |
1066 void calc_hauptroutine_update_tissues(void) | |
1067 { | |
1068 assert( 0.00 <= N2_ratio && N2_ratio <= 1.00 ); | |
1069 assert( 0.00 <= He_ratio && He_ratio <= 1.00 ); | |
114
109df032cc54
minor fix: assert should pass for pure helium diluant
heinrichsweikamp
parents:
111
diff
changeset
|
1070 assert( (N2_ratio + He_ratio) <= 1.00 ); |
0 | 1071 assert( 0.800 < pres_respiration && pres_respiration < 14.0 ); |
1072 | |
1073 pres_diluent = pres_respiration; | |
1074 if( char_I_const_ppO2 != 0 ) | |
1075 { | |
1076 overlay float flush_ppO2 = pres_respiration * (1.0 - N2_ratio - He_ratio); | |
1077 | |
1078 pres_diluent -= const_ppO2; | |
1079 pres_diluent /= N2_ratio + He_ratio; | |
1080 if( pres_diluent < 0.0 ) | |
1081 pres_diluent = 0.0; | |
1082 if( pres_diluent > pres_respiration ) | |
1083 pres_diluent = pres_respiration; | |
1084 | |
1085 char_O_diluent = (unsigned char)(pres_diluent/pres_respiration*100.0 + 0.5); | |
1086 | |
1087 if( flush_ppO2 > 2.545) flush_ppO2 = 2.55; | |
1088 if( flush_ppO2 < 0.0 ) flush_ppO2 = 0.0; | |
1089 char_O_flush_ppO2 = (unsigned char)(flush_ppO2*100.0 + 0.5); | |
1090 } | |
1091 | |
1092 if( pres_diluent > ppWater ) | |
1093 { | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1094 overlay float EAD, END; |
0 | 1095 |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1096 ppN2 = N2_ratio * (pres_diluent - ppWater); |
0 | 1097 ppHe = He_ratio * (pres_diluent - ppWater); |
1098 | |
1099 // EAD : Equivalent Air Dive. Equivalent depth for the same N2 level | |
1100 // with plain air. | |
1101 // ppN2 = 79% * (P_EAD - ppWater) | |
1102 // EAD = (P_EAD - Psurface) * 10 | |
1103 // ie: EAD = (ppN2 / 0.7902 + ppWater -Psurface) * 10 | |
1104 EAD = (ppN2 / 0.7902 + ppWater - pres_surface) * BAR_TO_METER; | |
1105 if( EAD < 0.0 || EAD > 245.5 ) EAD = 0.0; | |
1106 char_O_EAD = (unsigned char)(EAD + 0.5); | |
1107 | |
1108 // END : Equivalent Narcotic Dive. | |
1109 // Here we count O2 as narcotic too. Hence everything but helium (has a narcosis factor of | |
1110 // 0.23 btw). Hence the formula becomes: | |
1111 // END * BarPerMeter * (1.0 - 0.0) - ppWater + Psurface == Pambient - ppHe - ppWater | |
1112 // ie: END = (Pambient - ppHe - Psurface) * BAR_TO_METER | |
1113 // | |
1114 // Source cited: | |
1115 // The Physiology and Medicine of Diving by Peter Bennett and David Elliott, | |
1116 // 4th edition, 1993, W.B.Saunders Company Ltd, London. | |
1117 END = (pres_respiration - ppHe - pres_surface) * BAR_TO_METER; | |
1118 if( END < 0.0 || END > 245.5 ) END = 0.0; | |
1119 char_O_END = (unsigned char)(END + 0.5); | |
1120 } | |
1121 else // new in v.101 | |
1122 { | |
1123 ppN2 = 0.0; | |
1124 ppHe = 0.0; | |
1125 char_O_EAD = char_O_END = 0; | |
1126 } | |
1127 | |
1128 if(!char_I_step_is_1min) | |
1129 calc_tissue(0); | |
1130 else | |
1131 calc_tissue(1); | |
1132 | |
1133 // Calc limit for surface, ie. GF_high. | |
1134 calc_limit(); | |
1135 | |
126 | 1136 // Fill int_O_ceiling if ceiling is below the surface |
1137 if ((calc_lead_tissue_limit-pres_surface)>0) | |
1138 int_O_ceiling = (short)((calc_lead_tissue_limit-pres_surface)*1000); | |
1139 else | |
1140 int_O_ceiling = 0; | |
1141 | |
0 | 1142 int_O_gtissue_press = (short)((pres_tissue_N2[char_O_gtissue_no] + pres_tissue_He[char_O_gtissue_no]) * 1000); |
1143 } | |
1144 | |
1145 | |
1146 ////////////////////////////////////////////////////////////////////////////// | |
1147 // Compute stops. | |
1148 // | |
1149 // Note: because this can be very long, break on 16 iterations, and set state | |
1150 // to 0 when finished, or to 1 when needing to continue. | |
1151 // Note: because each iteration might be very long too (~ 66 ms in 1.84beta), | |
1152 // break the loop when total time > 512msec. | |
1153 // | |
1154 void calc_hauptroutine_calc_deco(void) | |
1155 { | |
1156 overlay unsigned char loop; | |
1157 | |
34 | 1158 for(loop = 0; loop < 16; ++loop) |
0 | 1159 { |
1160 // Limit loops to 512ms, using timer 5: | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1161 if( tmr5() & (512*32) ) |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1162 break; |
34 | 1163 |
0 | 1164 if( calc_nextdecodepth() ) |
1165 { | |
1166 if( temp_depth_limit == 0 ) | |
1167 goto Surface; | |
1168 | |
1169 //---- We hit a stop at temp_depth_limit --------------------- | |
1170 temp_deco = temp_depth_limit * METER_TO_BAR // Convert to relative bar, | |
1171 + pres_surface; // To absolute. | |
1172 if( !update_deco_table() ) // Adds a one minute stops. | |
1173 goto Surface; // Deco table full: abort... | |
1174 } | |
1175 else | |
1176 { | |
1177 //---- No stop ----------------------------------------------- | |
1178 temp_deco -= (10*METER_TO_BAR); // Ascend 10m, no wait. | |
1179 | |
1180 //---- Finish computations once surface is reached ----------- | |
1181 if( temp_deco <= pres_surface ) | |
1182 { | |
1183 Surface: | |
1184 if( char_O_deco_status == 1 ) // Don't in @+5min variant. | |
1185 copy_deco_table(); | |
1186 | |
1187 calc_ascenttime(); | |
1188 char_O_deco_status = 0; // calc nullzeit next time. | |
1189 char_O_deco_last_stop = 0; // Surface reached (to animate menu) | |
1190 return; | |
1191 } | |
1192 } | |
1193 //---- Then update tissue -------------------------------------------- | |
1194 sim_dive_mins++; // Advance simulated time by 1 minute. | |
1195 gas_switch_set(); // Apply any simulated gas change, once validated. | |
1196 sim_alveolar_presures(); // Updates ppN2 and ppHe. | |
1197 sim_tissue(1); // Simulate compartiments for 1 minute. | |
1198 } | |
1199 | |
1200 // Surface not reached, need more stops... for menu animation. | |
1201 char_O_deco_last_stop = temp_depth_limit; // Reached depth. | |
1202 } | |
1203 | |
1204 | |
1205 ////////////////////////////////////////////////////////////////////////////// | |
1206 // Simulation ascention to first deco stop. | |
1207 // | |
1208 // Note: because we ascent with a constant speed (10m/mn, ie. 1bar/mn), | |
1209 // there is no need to break on more that 16 iterations | |
1210 // (or we are already in deep shit). | |
1211 // | |
1212 // Input: pres_respiration | |
1213 // Output: temp_deco | |
1214 // | |
1215 // if char_O_deco_status indicate @+5 variant, add extra time at current depth, | |
1216 // before ascent. | |
1217 void sim_ascent_to_first_stop(void) | |
1218 { | |
1219 overlay unsigned char fast = 1; // 1min or 2sec steps. | |
1220 | |
1221 update_startvalues(); | |
1222 clear_deco_table(); | |
1223 | |
1224 temp_deco = pres_respiration; // Starts from current real depth. | |
1225 | |
1226 // Are we doing the special @+5min variation ? | |
1227 if(char_O_deco_status & 4) | |
1228 sim_extra_time(); | |
1229 | |
1230 //---- Loop until first stop, gas switch, or surface is reached ---------- | |
1231 for(;;) | |
1232 { | |
1233 overlay float old_deco = temp_deco; // Pamb backup (bars) | |
1234 | |
1235 // Try ascending 1 full minute (fast) or 2sec (!fast): | |
1236 if( fast ) | |
1237 temp_deco -= 10*METER_TO_BAR; // 1 min, at 10m/min. ~ 1bar. | |
1238 else | |
1239 temp_deco -= (10.0/30.0)*METER_TO_BAR; // 2sec at 10m/min. | |
1240 | |
1241 if( temp_deco < pres_surface ) // But don't go over surface. | |
1242 temp_deco = pres_surface; | |
1243 | |
1244 // Recompute sim_lead_tissue_limit at GF_low (deepest stop), because | |
1245 // one minute passed. | |
1246 sim_limit(GF_low); | |
1247 | |
1248 // Did we reach deepest remaining stop ? | |
1249 if( temp_deco < sim_lead_tissue_limit ) | |
1250 { | |
1251 temp_deco = old_deco; // Restore last correct depth, | |
1252 | |
1253 if( fast ) | |
1254 { | |
1255 fast = 0; // Retry with 2sec steps. | |
1256 continue; | |
1257 } | |
1258 else | |
1259 break; // Done... | |
1260 } | |
1261 | |
1262 // Did we reach surface ? | |
1263 // NOTE: we should round BEFORE checking surface is reached. | |
1264 temp_depth_limit = (unsigned char)(0.5 + (temp_deco - pres_surface) * BAR_TO_METER); | |
1265 if( temp_depth_limit == 0 ) | |
1266 { | |
1267 temp_deco = pres_surface; // Yes: finished ! | |
1268 break; | |
1269 } | |
1270 | |
1271 // Check for gas change below new depth ? | |
1272 if( gas_switch_deepest() ) | |
1273 { | |
1274 assert( temp_depth_limit > 0); | |
1275 | |
1276 temp_deco = temp_depth_limit * METER_TO_BAR + pres_surface; | |
1277 break; | |
1278 } | |
1279 | |
1280 if( fast ) | |
1281 sim_dive_mins++; // Advance simulated time by 1 minute. | |
1282 sim_alveolar_presures(); // temp_deco --> ppN2/ppHe | |
1283 sim_tissue(fast); // and update tissues for 1 min. | |
1284 } | |
1285 } | |
1286 | |
1287 ////////////////////////////////////////////////////////////////////////////// | |
1288 // Simulation extra time at the current depth. | |
1289 // | |
1290 // This routine is used for @+5min feature. | |
1291 void sim_extra_time(void) | |
1292 { | |
1293 overlay unsigned char extra = char_I_extra_time; | |
1294 do { | |
1295 sim_dive_mins++; // Advance simulated time by 1 minute. | |
1296 sim_tissue(1); // and update tissues for 1 min. | |
1297 } while( --extra != 0 ); | |
1298 } | |
1299 | |
1300 ////////////////////////////////////////////////////////////////////////////// | |
1301 // calc_tissue | |
1302 // | |
1303 // optimized in v.101 | |
1304 // | |
1305 static void calc_tissue(PARAMETER unsigned char period) | |
1306 { | |
1307 assert( 0.00 <= ppN2 && ppN2 < 11.2 ); // 80% N2 at 130m | |
1308 assert( 0.00 <= ppHe && ppHe < 12.6 ); // 90% He at 130m | |
1309 | |
1310 for (ci=0;ci<NUM_COMP;ci++) | |
1311 { | |
1312 read_buhlmann_times(period); // 2 sec or 1 min period. | |
1313 | |
1314 // N2 | |
1315 temp_tissue = (ppN2 - pres_tissue_N2[ci]) * var_N2_e; | |
1316 temp_tissue_safety(); | |
1317 pres_tissue_N2[ci] += temp_tissue; | |
1318 | |
1319 // He | |
1320 temp_tissue = (ppHe - pres_tissue_He[ci]) * var_He_e; | |
1321 temp_tissue_safety(); | |
1322 pres_tissue_He[ci] += temp_tissue; | |
1323 } | |
1324 } | |
1325 | |
1326 ////////////////////////////////////////////////////////////////////////////// | |
1327 // calc_limit | |
1328 // | |
1329 // New in v.111 : separated from calc_tissue(), and depends on GF value. | |
1330 // | |
1331 static void calc_limit(void) | |
1332 { | |
1333 char_O_gtissue_no = 255; | |
1334 calc_lead_tissue_limit = 0.0; | |
1335 | |
1336 for(ci=0; ci<NUM_COMP;ci++) | |
1337 { | |
1338 overlay float N2 = pres_tissue_N2[ci]; | |
1339 overlay float He = pres_tissue_He[ci]; | |
1340 overlay float p = N2 + He; | |
1341 | |
1342 read_buhlmann_coefficients(); | |
1343 var_N2_a = (var_N2_a * N2 + var_He_a * He) / p; | |
1344 var_N2_b = (var_N2_b * N2 + var_He_b * He) / p; | |
1345 | |
1346 // Apply the Eric Baker's varying gradient factor correction. | |
1347 // Note: the correction factor depends both on GF and b, | |
1348 // Actual values are in the 1.5 .. 1.0 range (for a GF=30%), | |
1349 // so that can change who is the leading gas... | |
1350 // Note: Also depends of the GF. So the calcul is different for | |
1351 // GF_low, current GF, or GF_high... | |
1352 // *BUT* calc_tissue() is used to compute bottom time, | |
1353 // hence what would happend at surface, | |
1354 // hence at GF_high. | |
1355 if( char_I_deco_model != 0 ) | |
1356 p = ( p - var_N2_a * GF_high) * var_N2_b | |
1357 / (GF_high + var_N2_b * (1.0 - GF_high)); | |
1358 else | |
1359 p = (p - var_N2_a) * var_N2_b; | |
1360 if( p < 0.0 ) p = 0.0; | |
1361 | |
1362 if( p > calc_lead_tissue_limit ) | |
1363 { | |
1364 char_O_gtissue_no = ci; | |
1365 calc_lead_tissue_limit = p; | |
1366 } | |
1367 } | |
1368 | |
1369 assert( char_O_gtissue_no < NUM_COMP ); | |
1370 assert( 0.0 <= calc_lead_tissue_limit && calc_lead_tissue_limit <= 14.0); | |
1371 } | |
1372 | |
1373 ////////////////////////////////////////////////////////////////////////////// | |
1374 // calc_nullzeit | |
1375 // | |
1376 // calculates the remaining bottom time | |
1377 // | |
1378 // NOTE: Erik Baker's closed formula works for Nitroxes. Trimix adds a second | |
1379 // exponential term to the M-value equation, making it impossible to | |
1380 // invert... So we have to make a fast-simu until we find a better way. | |
1381 // | |
1382 // Input: pres_respiration | |
1383 // Output: char_O_nullzeit | |
1384 // | |
1385 static void calc_nullzeit(void) | |
1386 { | |
1387 //---- Compute ppN2 and ppHe --------------------------------------------- | |
1388 temp_deco = pres_respiration; | |
1389 sim_alveolar_presures(); | |
1390 | |
1391 char_O_nullzeit = 240; | |
1392 for(ci=0; ci<NUM_COMP; ci++) | |
1393 { | |
1394 //---- Read A/B values and loading factor for N2 and He -------------- | |
1395 overlay float tN2 = pres_tissue_N2[ci]; | |
1396 overlay float tHe = pres_tissue_He[ci]; | |
1397 overlay float t = tN2 + tHe; | |
1398 overlay unsigned char ndl; | |
1399 overlay unsigned char period = 10; | |
1400 | |
1401 read_buhlmann_coefficients(); | |
1402 read_buhlmann_times(2); // Starts with a 10min period. | |
1403 | |
1404 //---- Simulate for that tissue -------------------------------------- | |
1405 // NOTE: No need to simulate for longuer than the already found NDL. | |
1406 for(ndl=0; ndl<char_O_nullzeit;) | |
1407 { | |
1408 //---- Compute updated mix M-value at surface | |
1409 overlay float a = (var_N2_a * tN2 + var_He_a * tHe) / t; | |
1410 overlay float b = (var_N2_b * tN2 + var_He_b * tHe) / t; | |
1411 overlay float M0 = (a + pres_surface/b); | |
1412 | |
1413 //---- Add 10min/1min to N2/He tissues | |
1414 overlay float dTN2 = (ppN2 - tN2) * var_N2_e; | |
1415 overlay float dTHe = (ppHe - tHe) * var_He_e; | |
1416 | |
1417 //---- Apply security margin when using the non-GF model | |
1418 if( char_I_deco_model == 0 ) | |
1419 { | |
324
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
1420 // NDL can be computed while ascending... SO we have |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
1421 // to check wether we are saturating or desaturating. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
1422 if( dTN2 > 0.0 ) dTN2 *= float_saturation_multiplier; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
1423 else dTN2 *= float_desaturation_multiplier; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
1424 |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
1425 if( dTHe > 0.0 ) dTHe *= float_saturation_multiplier; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
1426 else dTHe *= float_saturation_multiplier; |
0 | 1427 } |
1428 else // Or GF-based model | |
1429 M0 = GF_high * (M0 - pres_surface) + pres_surface; | |
1430 | |
1431 //---- Simulate off-gasing while going to surface | |
1432 // TODO ! | |
1433 // dTN2 -= exp( ... ascent time ... ppN2...) | |
1434 // dTHe -= exp( ... ascent time ... ppHe...) | |
1435 | |
1436 //---- Ok now, and still ok to surface after 1 or 10 minutes ? | |
1437 if( (t <= M0) && (t + dTN2 + dTHe <= M0) ) | |
1438 { | |
1439 tN2 += dTN2; // YES: apply gas loadings, | |
1440 tHe += dTHe; | |
1441 t = tN2 + tHe; | |
1442 ndl += period; // increment NDL, | |
1443 continue; // and loop. | |
1444 } | |
1445 | |
1446 //---- Should we retry with smaller steps ? | |
1447 if( period == 10 ) | |
1448 { | |
1449 read_buhlmann_times(1); // 1min coefs. | |
1450 period = 1; | |
1451 continue; | |
1452 } | |
1453 | |
1454 //---- ELSE make a linear approx for the last minute | |
1455 // Usefull to have a meaningfull rounding of NDL. | |
1456 // But ONLY it positive (negativ casted to unsigned is bad). | |
1457 if( M0 > t ) | |
1458 ndl += (unsigned char)(0.5f + (M0-t)/(dTN2+dTHe)); | |
1459 break; | |
1460 } | |
1461 | |
1462 // Keep the shortest NDL found | |
1463 if( ndl < char_O_nullzeit ) | |
1464 char_O_nullzeit = ndl; | |
1465 } | |
1466 } | |
1467 | |
1468 ////////////////////////////////////////////////////////////////////////////// | |
1469 // calc_ascenttime | |
1470 // | |
1471 // Summup ascent from bottom to surface, at 1 bar/min, 1min for last 3 meters, | |
1472 // and all stops. | |
1473 // | |
1474 // Result in int_O_ascenttime, or int_O_extra_ascenttime if in @+5min variant. | |
1475 static void calc_ascenttime(void) | |
1476 { | |
1477 overlay unsigned char x; | |
1478 overlay unsigned short sum; | |
1479 | |
1480 // + 0.7 to count 1 minute ascent time from 3 metre to surface | |
1481 overlay float ascent = pres_respiration - pres_surface + 0.7; | |
1482 if (ascent < 0.0) | |
1483 ascent = 0.0; | |
1484 sum = (unsigned short)(ascent + 0.99); | |
1485 | |
1486 for(x=0; x<NUM_STOPS && internal_deco_depth[x]; x++) | |
1487 sum += (unsigned short)internal_deco_time[x]; | |
1488 | |
1489 if( char_O_deco_status == 1 ) | |
1490 int_O_ascenttime = sum; | |
1491 else | |
1492 int_O_extra_ascenttime = sum; | |
1493 | |
1494 } | |
1495 | |
1496 ////////////////////////////////////////////////////////////////////////////// | |
1497 // update_startvalues | |
1498 // | |
1499 // updated in v.102 | |
1500 // | |
1501 void update_startvalues(void) | |
1502 { | |
1503 overlay unsigned char x; | |
1504 | |
1505 // Start ascent simulation with current tissue partial pressures. | |
1506 for(x=0; x<NUM_COMP; x++) | |
1507 { | |
1508 sim_pres_tissue_N2[x] = pres_tissue_N2[x]; | |
1509 sim_pres_tissue_He[x] = pres_tissue_He[x]; | |
1510 } | |
1511 | |
1512 // No leading tissue (yet) for this ascent simulation. | |
1513 sim_lead_tissue_limit = 0.0; | |
1514 sim_lead_tissue_no = 255; | |
1515 } | |
1516 | |
1517 ////////////////////////////////////////////////////////////////////////////// | |
1518 // sim_tissue | |
1519 // | |
1520 // optimized in v.101 | |
1521 // | |
1522 // Function very simular to calc_tissue, but: | |
1523 // + Use a 1min or 10min period. | |
1524 // + Do it on sim_pres_tissue, instead of pres_tissue. | |
1525 static void sim_tissue(PARAMETER unsigned char period) | |
1526 { | |
1527 assert( 0.00 <= ppN2 && ppN2 < 11.2 ); // 80% N2 at 130m | |
1528 assert( 0.00 <= ppHe && ppHe < 12.6 ); // 90% He at 130m | |
1529 | |
1530 for(ci=0; ci<NUM_COMP; ci++) | |
1531 { | |
1532 read_buhlmann_times(period); // 1 or 10 minute(s) interval | |
1533 | |
1534 // N2 | |
1535 temp_tissue = (ppN2 - sim_pres_tissue_N2[ci]) * var_N2_e; | |
1536 temp_tissue_safety(); | |
1537 sim_pres_tissue_N2[ci] += temp_tissue; | |
1538 | |
1539 // He | |
1540 temp_tissue = (ppHe - sim_pres_tissue_He[ci]) * var_He_e; | |
1541 temp_tissue_safety(); | |
1542 sim_pres_tissue_He[ci] += temp_tissue; | |
1543 } | |
1544 } | |
1545 | |
1546 ////////////////////////////////////////////////////////////////////////////// | |
1547 // sim_limit() | |
1548 // | |
1549 // New in v.111 | |
1550 // | |
1551 // Function separated from sim_tissue() to allow recomputing limit on | |
1552 // different depth, because it depends on current gradient factor. | |
1553 // | |
1554 static void sim_limit(PARAMETER float GF_current) | |
1555 { | |
1556 assert( 0.0 < GF_current && GF_current <= 1.0f); | |
1557 | |
1558 sim_lead_tissue_limit = 0.0; | |
1559 sim_lead_tissue_no = 0; // If no one is critic, keep first tissue. | |
1560 | |
1561 for(ci=0; ci<NUM_COMP; ci++) | |
1562 { | |
1563 overlay float N2 = sim_pres_tissue_N2[ci]; | |
1564 overlay float He = sim_pres_tissue_He[ci]; | |
1565 overlay float p = N2 + He; | |
1566 | |
1567 read_buhlmann_coefficients(); | |
1568 var_N2_a = (var_N2_a * N2 + var_He_a * He) / p; | |
1569 var_N2_b = (var_N2_b * N2 + var_He_b * He) / p; | |
1570 | |
1571 // Apply the Eric Baker's varying gradient factor correction. | |
1572 // Note: the correction factor depends both on GF and b, | |
1573 // Actual values are in the 1.5 .. 1.0 range (for a GF=30%), | |
1574 // so that can change who is the leading gas... | |
1575 // Note: Also depends of the GF_current... | |
1576 if( char_I_deco_model != 0 ) | |
1577 p = ( p - var_N2_a * GF_current) | |
1578 / (GF_current / var_N2_b + 1.0 - GF_current); | |
1579 else | |
1580 p = (p - var_N2_a) * var_N2_b; | |
1581 if( p < 0.0 ) p = 0.0; | |
1582 | |
1583 if( p > sim_lead_tissue_limit ) | |
1584 { | |
1585 sim_lead_tissue_no = ci; | |
1586 sim_lead_tissue_limit = p; | |
1587 } | |
1588 } // for ci | |
1589 | |
1590 assert( sim_lead_tissue_no < NUM_COMP ); | |
1591 assert( 0.0 <= sim_lead_tissue_limit && sim_lead_tissue_limit <= 14.0 ); | |
1592 } | |
1593 | |
1594 ////////////////////////////////////////////////////////////////////////////// | |
1595 // clear_deco_table | |
1596 // | |
1597 // unchanged in v.101 | |
1598 // | |
1599 static void clear_deco_table(void) | |
1600 { | |
1601 overlay unsigned char x; | |
1602 | |
1603 for(x=0; x<NUM_STOPS; ++x) | |
1604 { | |
1605 internal_deco_time [x] = 0; | |
1606 internal_deco_depth[x] = 0; | |
1607 } | |
1608 } | |
1609 | |
1610 ////////////////////////////////////////////////////////////////////////////// | |
1611 // update_deco_table | |
1612 // | |
1613 // Add 1 min to current stop. | |
1614 // | |
1615 // Inputs: | |
1616 // temp_depth_limit = stop's depth, in meters. | |
1617 // In/Out: | |
1618 // internal_deco_depth[] : depth (in metres) of each stops. | |
1619 // internal_deco_time [] : time (in minutes) of each stops. | |
1620 // | |
1621 static unsigned char update_deco_table() | |
1622 { | |
1623 overlay unsigned char x; | |
1624 assert( temp_depth_limit < 128 ); // Can't be negativ (overflown). | |
1625 assert( temp_depth_limit > 0 ); // No stop at surface... | |
1626 | |
1627 for(x=0; x<NUM_STOPS; ++x) | |
1628 { | |
1629 // Make sure deco-stops are recorded in order: | |
111 | 1630 assert( !internal_deco_depth[x] || temp_depth_limit <= internal_deco_depth[x] ); |
0 | 1631 |
111 | 1632 if( internal_deco_depth[x]== temp_depth_limit ) |
0 | 1633 { |
1634 // Do not overflow (max 255') | |
1635 if( internal_deco_time[x] < 255 ) | |
1636 { | |
1637 internal_deco_time[x]++; | |
1638 return 1; | |
1639 } | |
1640 // But store extra in the next stop... | |
1641 } | |
1642 | |
1643 if( internal_deco_depth[x] == 0 ) | |
1644 { | |
1645 internal_deco_depth[x] = temp_depth_limit; | |
1646 | |
1647 internal_deco_time[x] = 1; | |
111 | 1648 internal_deco_gas[x] = sim_gas_last_used; |
0 | 1649 return 1; |
1650 } | |
1651 } | |
1652 | |
1653 // Can't store stops at more than 96m. | |
1654 // Or stops at less that 3m too. | |
1655 // Just do nothing with that... | |
1656 return 0; | |
1657 } | |
1658 | |
1659 ////////////////////////////////////////////////////////////////////////////// | |
1660 // calc_gradient_factor | |
1661 // | |
1662 // optimized in v.101 (var_N2_a) | |
1663 // new code in v.102 | |
1664 // | |
1665 static void calc_gradient_factor(void) | |
1666 { | |
1667 overlay float gf; | |
1668 overlay float N2 = pres_tissue_N2[char_O_gtissue_no]; | |
1669 overlay float He = pres_tissue_He[char_O_gtissue_no]; | |
1670 | |
1671 assert( char_O_gtissue_no < NUM_COMP ); | |
1672 assert( 0.800 <= pres_respiration && pres_respiration < 14.0 ); | |
1673 | |
1674 // tissue > respiration (currently off-gasing) | |
1675 // GF = 0% when respiration == tissue, ie. bubbles are at equilibrium. | |
1676 // GF = 100% when respiration == limit. | |
1677 temp_tissue = N2 + He; | |
1678 if( temp_tissue <= pres_respiration ) | |
1679 gf = 0.0; | |
1680 else | |
1681 { | |
1682 overlay float limit = calc_lead_tissue_limit; | |
1683 // NOTE: in GF model, calc_lead_tissue_limit include already the | |
1684 // correction due to gradient factor. To compute the actual | |
1685 // current GF, we need to (re-)compute the raw ambiant-pressure | |
313 | 1686 // limit from the Buhlmann model. |
0 | 1687 if( char_I_deco_model != 0 ) |
1688 { | |
1689 ci = char_O_gtissue_no; | |
1690 read_buhlmann_coefficients(); | |
1691 var_N2_a = (var_N2_a * N2 + var_He_a * He) / temp_tissue; | |
1692 var_N2_b = (var_N2_b * N2 + var_He_b * He) / temp_tissue; | |
1693 limit = (temp_tissue - var_N2_a) * var_N2_b; | |
1694 } | |
1695 | |
1696 gf = (temp_tissue - pres_respiration) | |
1697 / (temp_tissue - limit) | |
1698 * 100.0; | |
1699 if( gf > 254.5 ) gf = 255.0; | |
1700 if( gf < 0.0 ) gf = 0.0; | |
1701 } | |
1702 char_O_gradient_factor = (unsigned char)(gf+0.5f); | |
1703 | |
1704 if( char_I_deco_model != 0 ) // calculate relative gradient factor | |
1705 { | |
1706 overlay float rgf; | |
1707 | |
1708 if( low_depth < 3 ) | |
1709 rgf = GF_high; | |
1710 else | |
1711 { | |
1712 overlay float temp1 = low_depth * METER_TO_BAR; | |
1713 overlay float temp2 = pres_respiration - pres_surface; | |
1714 | |
1715 if (temp2 <= 0) | |
1716 rgf = GF_high; | |
1717 else if (temp2 >= temp1) | |
1718 rgf = GF_low; | |
1719 else | |
1720 rgf = GF_low + (temp1 - temp2)/temp1*GF_delta; | |
1721 } | |
1722 | |
1723 rgf = gf / rgf; // gf is already in percent | |
1724 if( rgf < 0.0 ) rgf = 0.0; | |
1725 if( rgf > 254.5 ) rgf = 255.0; | |
1726 char_O_relative_gradient_GF = (unsigned char)(rgf+0.5f); | |
1727 } | |
1728 else | |
1729 char_O_relative_gradient_GF = char_O_gradient_factor; | |
1730 } | |
1731 | |
1732 ////////////////////////////////////////////////////////////////////////////// | |
1733 // deco_calc_desaturation_time | |
1734 // | |
1735 // FIXED N2_ratio | |
1736 // unchanged in v.101 | |
1737 // Inputs: int_I_pres_surface, ppWater, char_I_desaturation_multiplier | |
1738 // Outputs: int_O_desaturation_time, char_O_tissue_saturation[0..31] | |
1739 // | |
1740 void deco_calc_desaturation_time(void) | |
1741 { | |
1742 RESET_C_STACK | |
1743 | |
1744 assert( 800 < int_I_pres_surface && int_I_pres_surface < 1100 ); | |
1745 assert( 0 < char_I_desaturation_multiplier && char_I_desaturation_multiplier <= 100 ); | |
1746 | |
313 | 1747 N2_ratio = 0.7902; // FIXED sum as stated in buhlmann |
0 | 1748 pres_surface = int_I_pres_surface * 0.001; |
1749 ppN2 = N2_ratio * (pres_surface - ppWater); | |
1750 int_O_desaturation_time = 0; | |
1751 float_desaturation_multiplier = char_I_desaturation_multiplier * (0.01 * SURFACE_DESAT_FACTOR); | |
1752 | |
1753 for(ci=0; ci<NUM_COMP; ci++) | |
1754 { | |
1755 overlay unsigned short desat_time; // For a particular compartiment, in min. | |
1756 overlay float temp1; | |
1757 overlay float temp2; | |
1758 overlay float temp3; | |
1759 overlay float temp4; | |
1760 | |
1761 read_buhlmann_ht(); | |
1762 | |
1763 // saturation_time (for flight) and N2_saturation in multiples of halftime | |
1764 // version v.100: 1.1 = 10 percent distance to totally clean (totally clean is not possible, would take infinite time ) | |
1765 // new in version v.101: 1.07 = 7 percent distance to totally clean (totally clean is not possible, would take infinite time ) | |
1766 // changes in v.101: 1.05 = 5 percent dist to totally clean is new desaturation point for display and NoFly calculations | |
1767 // N2 | |
1768 temp1 = 1.05 * ppN2 - pres_tissue_N2[ci]; | |
1769 temp2 = ppN2 - pres_tissue_N2[ci]; | |
1770 if (temp2 >= 0.0) | |
1771 temp1 = 0.0; | |
1772 else | |
1773 temp1 = temp1 / temp2; | |
1774 | |
1775 if( 0.0 < temp1 && temp1 < 1.0 ) | |
1776 { | |
1777 // 0.6931 is ln(2), because the math function log() calculates with a base of e not 2 as requested. | |
1778 // minus because log is negative. | |
1779 temp1 = log(1.0 - temp1) / -0.6931; // temp1 is the multiples of half times necessary. | |
1780 temp2 = var_N2_ht * temp1 / float_desaturation_multiplier; // time necessary (in minutes ) for complete desaturation (see comment about 5 percent) | |
1781 } | |
1782 else | |
1783 { | |
1784 temp1 = 0.0; | |
1785 temp2 = 0.0; | |
1786 } | |
1787 | |
1788 // He | |
1789 temp3 = 0.1 - pres_tissue_He[ci]; | |
1790 if (temp3 >= 0.0) | |
1791 temp3 = 0.0; | |
1792 else | |
1793 temp3 = - temp3 / pres_tissue_He[ci]; | |
1794 | |
1795 if( 0.0 < temp3 && temp3 < 1.0 ) | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1796 { |
0 | 1797 temp3 = log(1.0 - temp3) / -0.6931; // temp1 is the multiples of half times necessary. |
1798 // 0.6931 is ln(2), because the math function log() calculates with a base of e not 2 as requested. | |
1799 // minus because log is negative | |
1800 temp4 = var_He_ht * temp3 / float_desaturation_multiplier; // time necessary (in minutes ) for "complete" desaturation, new in v.101 float_desaturation_multiplier | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1801 } |
0 | 1802 else |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1803 { |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1804 temp3 = 0.0; |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1805 temp4 = 0.0; |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1806 } |
0 | 1807 |
1808 // saturation_time (for flight) | |
1809 if (temp4 > temp2) | |
1810 desat_time = (unsigned short)temp4; | |
1811 else | |
1812 desat_time = (unsigned short)temp2; | |
1813 | |
1814 if(desat_time > int_O_desaturation_time) | |
1815 int_O_desaturation_time = desat_time; | |
1816 | |
1817 // N2 saturation in multiples of halftime for display purposes | |
1818 temp2 = temp1 * 20.0; // 0 = 1/8, 120 = 0, 249 = 8 | |
1819 temp2 = temp2 + 80.0; // set center | |
1820 if (temp2 < 0.0) | |
1821 temp2 = 0.0; | |
1822 if (temp2 > 255.0) | |
1823 temp2 = 255.0; | |
1824 char_O_tissue_N2_saturation[ci] = (char)temp2; | |
1825 | |
1826 // He saturation in multiples of halftime for display purposes | |
1827 temp4 = temp3 * 20.0; // 0 = 1/8, 120 = 0, 249 = 8 | |
1828 temp4 = temp4 + 80.0; // set center | |
1829 if (temp4 < 0.0) | |
1830 temp4 = 0.0; | |
1831 if (temp4 > 255.0) | |
1832 temp4 = 255.0; | |
1833 char_O_tissue_He_saturation[ci] = (char)temp4; | |
1834 } // for | |
1835 } | |
1836 | |
1837 ////////////////////////////////////////////////////////////////////////////// | |
1838 // calc_wo_deco_step_1_min | |
1839 // | |
1840 // FIXED N2 Ratio | |
1841 // optimized in v.101 (...saturation_multiplier) | |
1842 // desaturation slowed down to 70,42% | |
1843 // | |
1844 static void calc_wo_deco_step_1_min(void) | |
1845 { | |
1846 assert( 800 < int_I_pres_surface && int_I_pres_surface < 1100 ); | |
1847 assert( 800 < int_I_pres_respiration && int_I_pres_respiration < 1100 ); | |
1848 assert( 100 <= char_I_saturation_multiplier && char_I_saturation_multiplier < 200 ); | |
1849 assert( 0 < char_I_desaturation_multiplier && char_I_desaturation_multiplier <= 100 ); | |
1850 | |
1851 N2_ratio = 0.7902; // FIXED, sum lt. buehlmann | |
1852 pres_respiration = pres_surface = int_I_pres_surface * 0.001; | |
1853 ppN2 = N2_ratio * (pres_respiration - ppWater); | |
1854 ppHe = 0.0; | |
1855 float_desaturation_multiplier = char_I_desaturation_multiplier * (0.01 * SURFACE_DESAT_FACTOR); | |
1856 float_saturation_multiplier = char_I_saturation_multiplier * 0.01; | |
1857 | |
1858 calc_tissue(1); // update the pressure in the tissues N2/He in accordance with the new ambient pressure | |
1859 | |
1860 clear_deco_table(); | |
1861 char_O_deco_status = 3; // surface new in v.102 : stays in surface state. | |
1862 char_O_nullzeit = 0; | |
1863 int_O_ascenttime = 0; | |
1864 int_O_extra_ascenttime = 0; | |
1865 calc_gradient_factor(); | |
1866 } | |
1867 | |
1868 ////////////////////////////////////////////////////////////////////////////// | |
1869 // calc_dive_interval | |
1870 // | |
1871 // Prepare tissue for delay before the next dive simulation. | |
1872 // | |
1873 // Inputs: char_I_dive_interval == delay before dive (in 10' steps). | |
1874 // Outputs: pres_tissue_N2/He[], CNS_fraction | |
1875 // | |
1876 // Should be protected by deco_push_tissues_to_vault(), | |
1877 // deco_pull_tissues_from_vault() | |
1878 // | |
1879 // desaturation slowed down to 70,42%. | |
1880 // | |
1881 static void calc_dive_interval(void) | |
1882 { | |
1883 overlay unsigned char t; | |
1884 overlay unsigned char backup_model; | |
1885 | |
1886 //---- Initialize simulation parameters ---------------------------------- | |
1887 N2_ratio = 0.7902; // FIXED, sum lt. buehlmann | |
1888 pres_respiration = pres_surface = int_I_pres_surface * 0.001; | |
1889 ppN2 = N2_ratio * (pres_respiration - ppWater); | |
1890 ppHe = 0.0; | |
1891 float_desaturation_multiplier = char_I_desaturation_multiplier * (0.01 * SURFACE_DESAT_FACTOR); | |
1892 float_saturation_multiplier = char_I_saturation_multiplier * 0.01; | |
1893 | |
1894 // Make sure SURFACE_DESAT_FACTOR is applied: | |
1895 backup_model = char_I_deco_model; | |
1896 char_I_deco_model = 0; | |
1897 | |
1898 //---- Perform simulation ------------------------------------------------ | |
1899 for(t=0; t<char_I_dive_interval; ++t) | |
1900 { | |
1901 calc_tissue(2); // period = 10min. | |
1902 CNS_fraction = 0.92587471 * CNS_fraction; // Half-time = 90min: (1/2)^(1/9) | |
1903 } | |
322 | 1904 assert( 0.0 <= CNS_fraction && CNS_fraction <= 9.99 ); // 999 % |
1905 int_O_CNS_fraction = (unsigned short)(CNS_fraction * 100.0 + 0.5); | |
0 | 1906 |
1907 //---- Restore model ----------------------------------------------------- | |
1908 char_I_deco_model = backup_model; | |
1909 } | |
1910 | |
1911 ////////////////////////////////////////////////////////////////////////////// | |
1912 // deco_clear_CNS_fraction | |
1913 // | |
1914 // new in v.101 | |
1915 // | |
1916 void deco_clear_CNS_fraction(void) | |
1917 { | |
1918 RESET_C_STACK | |
1919 | |
1920 CNS_fraction = 0.0; | |
1921 int_O_CNS_fraction = 0; | |
1922 } | |
1923 | |
1924 ////////////////////////////////////////////////////////////////////////////// | |
1925 // deco_calc_CNS_fraction | |
1926 // | |
1927 // Input: char_I_actual_ppO2 : Current condition (in decibars). | |
1928 // char_I_step_is_1min : use 1min or 10min steps instead of 2sec. | |
1929 // CNS_fraction : velue before period. | |
1930 // Output: CNS_fraction, int_O_CNS_fraction | |
1931 // | |
1932 void deco_calc_CNS_fraction(void) | |
1933 { | |
1934 overlay float time_factor = 1.0f; | |
1935 RESET_C_STACK | |
1936 | |
322 | 1937 assert( 0.0 <= CNS_fraction && CNS_fraction <= 9.99 ); |
0 | 1938 assert( char_I_actual_ppO2 > 15 ); |
1939 | |
1940 if( char_I_step_is_1min == 1 ) | |
1941 time_factor = 30.0f; | |
1942 else if( char_I_step_is_1min == 2 ) | |
1943 time_factor = 300.0f; | |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1944 //------------------------------------------------------------------------ |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1945 // Don't increase CNS below 0.5 bar, but keep it steady. |
0 | 1946 if (char_I_actual_ppO2 < 50) |
1947 ; // no changes | |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1948 //------------------------------------------------------------------------ |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1949 // Below (and including) 1.60 bar |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1950 else if (char_I_actual_ppO2 < 61) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1951 CNS_fraction += time_factor/(-533.07 * char_I_actual_ppO2 + 54000.0); |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1952 else if (char_I_actual_ppO2 < 71) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1953 CNS_fraction += time_factor/(-444.22 * char_I_actual_ppO2 + 48600.0); |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1954 else if (char_I_actual_ppO2 < 81) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1955 CNS_fraction += time_factor/(-355.38 * char_I_actual_ppO2 + 42300.0); |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1956 else if (char_I_actual_ppO2 < 91) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1957 CNS_fraction += time_factor/(-266.53 * char_I_actual_ppO2 + 35100.0); |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1958 else if (char_I_actual_ppO2 < 111) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1959 CNS_fraction += time_factor/(-177.69 * char_I_actual_ppO2 + 27000.0); |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1960 else if (char_I_actual_ppO2 < 152) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1961 CNS_fraction += time_factor/( -88.84 * char_I_actual_ppO2 + 17100.0); |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1962 else if (char_I_actual_ppO2 < 167) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1963 CNS_fraction += time_factor/(-222.11 * char_I_actual_ppO2 + 37350.0); |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1964 //------------------------------------------------------------------------ |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1965 // Arieli et all.(2002): Modeling pulmonary and CNS O2 toxicity: |
322 | 1966 // J Appl Physiol 92: 248--256, 2002, doi:10.1152/japplphysiol.00434.2001 |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1967 // Formula (A1) based on value for 1.55 and c=20 |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1968 // example calculation: Sqrt((1.7/1.55)^20)*0.000404 |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1969 else if (char_I_actual_ppO2 < 172) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1970 CNS_fraction += time_factor*0.00102; |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1971 else if (char_I_actual_ppO2 < 177) |
0 | 1972 CNS_fraction += time_factor*0.00136; |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1973 else if (char_I_actual_ppO2 < 182) |
0 | 1974 CNS_fraction += time_factor*0.00180; |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1975 else if (char_I_actual_ppO2 < 187) |
0 | 1976 CNS_fraction += time_factor*0.00237; |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1977 else if (char_I_actual_ppO2 < 192) |
0 | 1978 CNS_fraction += time_factor*0.00310; |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1979 else if (char_I_actual_ppO2 < 198) |
0 | 1980 CNS_fraction += time_factor*0.00401; |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1981 else if (char_I_actual_ppO2 < 203) |
0 | 1982 CNS_fraction += time_factor*0.00517; |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
1983 else if (char_I_actual_ppO2 < 233) |
0 | 1984 CNS_fraction += time_factor*0.0209; |
1985 else | |
1986 CNS_fraction += time_factor*0.0482; // value for 2.5 | |
1987 | |
322 | 1988 if( CNS_fraction > 9.99) // Limit display to 999% |
0 | 1989 CNS_fraction = 9.99; |
1990 if( CNS_fraction < 0.0 ) | |
1991 CNS_fraction = 0.0; | |
1992 | |
322 | 1993 int_O_CNS_fraction = (unsigned short)(100.0 * CNS_fraction + 0.5); |
0 | 1994 } |
1995 | |
1996 ////////////////////////////////////////////////////////////////////////////// | |
1997 // deco_calc_CNS_planning | |
1998 // | |
1999 // Compute CNS during predicted ascent. | |
2000 // | |
2001 // Note: Needs a call to deco_push_tissues_to_vault(), | |
2002 // deco_pull_tissues_from_vault() to avoid trashing everything... | |
2003 // | |
2004 // Input: CNS_fraction, char_O_deco_time[], char_O_deco_depth[] | |
2005 // Output: CNS_fraction, int_O_CNS_fraction | |
2006 // | |
2007 void deco_calc_CNS_planning(void) | |
2008 { | |
2009 overlay unsigned char backup_gas_last_depth; | |
2010 overlay unsigned char backup_gas_last_used; | |
2011 overlay unsigned short backup_dive_mins; | |
2012 overlay unsigned char backup_actual_ppO2; | |
2013 | |
2014 RESET_C_STACK | |
2015 | |
2016 // Backup state machine | |
2017 backup_gas_last_depth = sim_gas_last_depth; | |
2018 backup_gas_last_used = sim_gas_last_used; | |
2019 backup_dive_mins = sim_dive_mins; | |
2020 backup_actual_ppO2 = char_I_actual_ppO2; | |
2021 | |
2022 // Uses 1min CNS period: | |
2023 char_I_step_is_1min = 1; | |
2024 | |
2025 //---- Retrieve bottom Gas used, and set variables. | |
2026 sim_gas_last_used = char_I_first_gas; | |
2027 sim_gas_last_depth = 0; // Surface gas marker. | |
2028 gas_switch_set(); // Sets initial calc_N2/He_ratio | |
2029 | |
2030 //---- CCR mode : do the full TTS at once -------------------------------- | |
2031 if( char_I_const_ppO2 != 0 ) | |
2032 { | |
2033 overlay unsigned short t; // Needs 16bits here ! | |
2034 char_I_actual_ppO2 = char_I_const_ppO2; | |
2035 for(t=0; t<int_O_ascenttime; ++t) | |
2036 deco_calc_CNS_fraction(); | |
2037 } | |
2038 else //---- OC mode : have to follow all gas switches... ----------------- | |
2039 { | |
2040 overlay unsigned char i = 0; // Decostop loop counter | |
2041 overlay float actual_ppO2; | |
2042 overlay unsigned char time, t; | |
2043 | |
2044 //---- Ascent to surface delay | |
2045 // NOTE: count as if time is spent with bottom pressure, | |
2046 // AND the bottom gas | |
2047 actual_ppO2 = (pres_surface + char_I_bottom_depth * METER_TO_BAR) | |
2048 * (1.0 - calc_N2_ratio - calc_He_ratio); | |
2049 if( actual_ppO2 < 0.0 ) actual_ppO2 = 0.0; | |
2050 if( actual_ppO2 > 2.50 ) actual_ppO2 = 2.55; | |
2051 char_I_actual_ppO2 = (unsigned char)(100.0 * actual_ppO2 + 0.5); | |
2052 | |
2053 // Ascent time (rounded up): | |
2054 time = (unsigned char)(0.1 * char_I_bottom_depth + 0.5); | |
2055 | |
2056 for(t=0; t<time; ++t) | |
2057 { | |
2058 deco_calc_CNS_fraction(); | |
2059 sim_dive_mins++; | |
2060 } | |
2061 | |
2062 //---- Do all further stops ------------------------------------------ | |
2063 for(i=0; i<NUM_STOPS; ++i) | |
2064 { | |
111 | 2065 overlay unsigned char stop_gas; |
0 | 2066 |
210
7dbc1d780ca5
do not clear char_O_deco_gas in copy_deco_table
heinrichsweikamp
parents:
135
diff
changeset
|
2067 //---- Get next stop --------------------------------------------- |
0 | 2068 { |
2069 time = char_O_deco_time[(NUM_STOPS-1)-i]; | |
2070 temp_depth_limit = char_O_deco_depth[(NUM_STOPS-1)-i]; | |
111 | 2071 stop_gas = char_O_deco_gas[(NUM_STOPS-1)-i]; |
0 | 2072 } |
2073 if( time == 0 ) continue; | |
2074 | |
2075 //---- Gas Switch ? ---------------------------------------------- | |
111 | 2076 if( stop_gas != sim_gas_last_used ) |
0 | 2077 { |
111 | 2078 sim_gas_last_depth = deco_gas_change[stop_gas-1]; |
2079 sim_gas_last_used = stop_gas; | |
0 | 2080 gas_switch_set(); |
2081 } | |
2082 | |
2083 //---- Convert Depth and N2_ratio to ppO2 ------------------------ | |
2084 actual_ppO2 = (pres_surface + temp_depth_limit * METER_TO_BAR) | |
2085 * (1.0 - calc_N2_ratio - calc_He_ratio); | |
2086 if( actual_ppO2 < 0.0 ) actual_ppO2 = 0.0; | |
2087 if( actual_ppO2 > 2.50 ) actual_ppO2 = 2.55; | |
2088 char_I_actual_ppO2 = (unsigned char)(100.0 * actual_ppO2 + 0.5); | |
2089 | |
2090 //---- Apply the stop | |
2091 for(t=0; t<time; ++t) | |
2092 { | |
2093 deco_calc_CNS_fraction(); | |
2094 sim_dive_mins++; | |
2095 } | |
2096 } | |
2097 } | |
2098 | |
2099 //---- Back to normal mode... -------------------------------------------- | |
2100 char_I_step_is_1min = 0; | |
2101 sim_gas_last_depth = backup_gas_last_depth; | |
2102 sim_gas_last_used = backup_gas_last_used; | |
2103 sim_dive_mins = backup_dive_mins; | |
2104 char_I_actual_ppO2 = backup_actual_ppO2; | |
2105 } | |
2106 | |
2107 ////////////////////////////////////////////////////////////////////////////// | |
2108 // deco_calc_CNS_decrease_15min | |
2109 // | |
2110 // new in v.101 | |
2111 // | |
2112 // calculates the half time of 90 minutes in 6 steps of 15 min | |
2113 // (Used in sleepmode, for low battery mode). | |
2114 // | |
2115 // Output: int_O_CNS_fraction | |
2116 // Uses and Updates: CNS_fraction | |
2117 // | |
2118 void deco_calc_CNS_decrease_15min(void) | |
2119 { | |
2120 RESET_C_STACK | |
322 | 2121 assert( 0.0 <= CNS_fraction && CNS_fraction <= 9.99 ); |
0 | 2122 |
2123 CNS_fraction = 0.890899 * CNS_fraction; | |
322 | 2124 int_O_CNS_fraction = (unsigned short)(CNS_fraction * 100.0 + 0.5); |
0 | 2125 } |
2126 | |
2127 ////////////////////////////////////////////////////////////////////////////// | |
2128 // deco_calc_percentage | |
2129 // | |
2130 // new in v.101 | |
2131 // | |
2132 // calculates int_I_temp * char_I_temp / 100 | |
2133 // output is int_I_temp | |
2134 // | |
2135 // Used to compute NoFly remaining time. | |
2136 // | |
2137 void deco_calc_percentage(void) | |
2138 { | |
2139 RESET_C_STACK | |
2140 | |
2141 assert( 60 <= char_I_temp && char_I_temp <= 100 ); | |
2142 assert( int_I_temp < 5760 ); // Less than 4 days = 96h... | |
2143 | |
2144 int_I_temp = (unsigned short)(((float)int_I_temp * (float)char_I_temp) * 0.01 ); | |
2145 | |
2146 assert( int_I_temp < 5760 ); // Less than 96h too... | |
2147 } | |
2148 | |
2149 ////////////////////////////////////////////////////////////////////////////// | |
2150 // deco_gas_volumes | |
2151 // | |
2152 // new in v.111 | |
2153 // | |
2154 // calculates volumes for each gas. | |
2155 // | |
2156 // Input: char_I_bottom_depth, char_I_bottom_time for planned dive. | |
2157 // Gas list. | |
2158 // char_I_first_gas is the bottom gas. | |
2159 // decoplan (char_O_deco_depth, char_O_deco_time). | |
298
2fe34fc0e2ae
new submenu for gas consumption, show actual mix instead of GAS1-GAS5 in deco planner
heinrichsweikamp
parents:
289
diff
changeset
|
2160 // char_I_bottom_usage is bottom liters/minutes (5 .. 50) or bar/min. |
2fe34fc0e2ae
new submenu for gas consumption, show actual mix instead of GAS1-GAS5 in deco planner
heinrichsweikamp
parents:
289
diff
changeset
|
2161 // char_I_deco_usage is deco liters/minutes (5 .. 50) or bar/min. |
0 | 2162 // Output: int_O_gas_volumes[0..4] in litters * 0.1 |
2163 // | |
2164 void deco_gas_volumes(void) | |
2165 { | |
2166 overlay float volumes[NUM_GAS]; | |
2167 overlay float bottom_usage, deco_usage; | |
135 | 2168 overlay unsigned char i; |
0 | 2169 overlay unsigned char gas, depth; |
2170 RESET_C_STACK | |
2171 | |
289 | 2172 //---- initialize -------------------------------------------------------- |
0 | 2173 for(i=0; i<NUM_GAS; ++i) // Nothing yet... |
2174 volumes[i] = 0.0; | |
2175 | |
298
2fe34fc0e2ae
new submenu for gas consumption, show actual mix instead of GAS1-GAS5 in deco planner
heinrichsweikamp
parents:
289
diff
changeset
|
2176 bottom_usage = char_I_bottom_usage; // In liter/minutes. |
2fe34fc0e2ae
new submenu for gas consumption, show actual mix instead of GAS1-GAS5 in deco planner
heinrichsweikamp
parents:
289
diff
changeset
|
2177 deco_usage = char_I_deco_usage; // In liter/minutes. |
289 | 2178 |
2179 // Early return if not defined: | |
2180 if( deco_usage <= 0.0 || bottom_usage <= 0.0 ) | |
2181 goto done; | |
2182 | |
2183 //---- Bottom usage ----------------------------------------------------- | |
0 | 2184 assert(1 <= char_I_first_gas && char_I_first_gas <= NUM_GAS); |
2185 gas = char_I_first_gas - 1; | |
2186 | |
289 | 2187 if( char_I_const_ppO2 == 0 ) |
0 | 2188 volumes[gas] |
2189 = (char_I_bottom_depth*0.1 + 1.0) // Use Psurface = 1.0 bar. | |
2190 * char_I_bottom_time // in minutes. | |
2191 * bottom_usage; // In liter/minutes. | |
2192 | |
2193 //---- Ascent usage ------------------------------------------------------ | |
2194 depth = char_I_bottom_depth; | |
2195 | |
2196 for(i=0; i<NUM_STOPS; ++i) | |
2197 { | |
288
08986d479b94
FIX gas_volume shall read gas switches from char_O_deco_gas
jdg@air
parents:
287
diff
changeset
|
2198 overlay unsigned char newDepth, time, newGas; |
0 | 2199 |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
2200 time = char_O_deco_time[i]; |
288
08986d479b94
FIX gas_volume shall read gas switches from char_O_deco_gas
jdg@air
parents:
287
diff
changeset
|
2201 if( time == 0 ) break; // End of stops. |
0 | 2202 |
288
08986d479b94
FIX gas_volume shall read gas switches from char_O_deco_gas
jdg@air
parents:
287
diff
changeset
|
2203 newDepth = char_O_deco_depth[i]; |
08986d479b94
FIX gas_volume shall read gas switches from char_O_deco_gas
jdg@air
parents:
287
diff
changeset
|
2204 newGas = char_O_deco_gas [i]-1; |
08986d479b94
FIX gas_volume shall read gas switches from char_O_deco_gas
jdg@air
parents:
287
diff
changeset
|
2205 |
08986d479b94
FIX gas_volume shall read gas switches from char_O_deco_gas
jdg@air
parents:
287
diff
changeset
|
2206 assert(0 < newDepth && newDepth <= depth); |
08986d479b94
FIX gas_volume shall read gas switches from char_O_deco_gas
jdg@air
parents:
287
diff
changeset
|
2207 assert(0 <= newGas && newGas < NUM_GAS); |
0 | 2208 |
289 | 2209 //---- usage BEFORE gas switch (if any), at 10m/min: |
288
08986d479b94
FIX gas_volume shall read gas switches from char_O_deco_gas
jdg@air
parents:
287
diff
changeset
|
2210 volumes[gas] += ((depth+newDepth)*0.05 + 1.0) // average depth --> bar. |
08986d479b94
FIX gas_volume shall read gas switches from char_O_deco_gas
jdg@air
parents:
287
diff
changeset
|
2211 * (depth-newDepth)*0.1 // metre --> min |
08986d479b94
FIX gas_volume shall read gas switches from char_O_deco_gas
jdg@air
parents:
287
diff
changeset
|
2212 * deco_usage; |
0 | 2213 |
289 | 2214 //---- Do gas switch, at new depth: |
288
08986d479b94
FIX gas_volume shall read gas switches from char_O_deco_gas
jdg@air
parents:
287
diff
changeset
|
2215 gas = newGas; |
0 | 2216 depth = newDepth; |
2217 | |
289 | 2218 //---- Usage at stop: |
2219 volumes[gas] += (depth*0.1 + 1.0) // depth --> bar. | |
2220 * time // in minutes. | |
2221 * deco_usage; // in xxx / min @ 1bar. | |
0 | 2222 } |
2223 | |
2224 // From last stop to surface | |
289 | 2225 volumes[gas] += (depth*0.05 + 1.0) // avg depth --> bar. |
2226 * depth * 0.1 // time to surface, in minutes. | |
2227 * deco_usage; // in xxx / min @ 1bar. | |
0 | 2228 |
2229 //---- convert results for the ASM interface ----------------------------- | |
289 | 2230 done: |
0 | 2231 for(i=0; i<NUM_GAS; ++i) |
2232 if( volumes[i] > 65534.0 ) | |
2233 int_O_gas_volumes[i] = 65535; | |
2234 else | |
2235 int_O_gas_volumes[i] = (unsigned short)(volumes[i] + 0.5); | |
2236 } | |
2237 | |
2238 ////////////////////////////////////////////////////////////////////////////// | |
2239 | |
2240 void deco_push_tissues_to_vault(void) | |
2241 { | |
2242 overlay unsigned char x; | |
2243 RESET_C_STACK | |
2244 | |
2245 cns_vault = CNS_fraction; | |
2246 low_depth_vault = low_depth; | |
2247 | |
2248 for (x=0;x<NUM_COMP;x++) | |
2249 { | |
2250 pres_tissue_N2_vault[x] = pres_tissue_N2[x]; | |
2251 pres_tissue_He_vault[x] = pres_tissue_He[x]; | |
2252 } | |
2253 } | |
2254 | |
2255 void deco_pull_tissues_from_vault(void) | |
2256 { | |
2257 overlay unsigned char x; | |
2258 RESET_C_STACK | |
2259 | |
2260 for (x=0; x<NUM_COMP; x++) | |
2261 { | |
2262 pres_tissue_N2[x] = pres_tissue_N2_vault[x]; | |
2263 pres_tissue_He[x] = pres_tissue_He_vault[x]; | |
2264 } | |
2265 | |
2266 // Restore both CNS variable, too. | |
2267 CNS_fraction = cns_vault; | |
322 | 2268 int_O_CNS_fraction = (unsigned short)(CNS_fraction * 100.0 + 0.5); |
0 | 2269 |
2270 // GF history too: | |
2271 low_depth = low_depth_vault; | |
2272 locked_GF_step = GF_delta / low_depth; | |
2273 } | |
2274 | |
2275 ////////////////////////////////////////////////////////////////////////////// | |
2276 // | |
2277 #ifndef CROSS_COMPILE | |
2278 void main() {} | |
2279 #endif |