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