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