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