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