Mercurial > public > hwos_code
comparison src/p2_deco.c @ 0:11d4fc797f74
init
| author | heinrichsweikamp |
|---|---|
| date | Wed, 24 Apr 2013 19:22:45 +0200 |
| parents | |
| children | 5d4a4fb0e8a6 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:11d4fc797f74 |
|---|---|
| 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). | |
| 87 // | |
| 88 // TODO: | |
| 89 // | |
| 90 // Literature: | |
| 91 // Bühlmann, Albert: Tauchmedizin; 4. Auflage [2002]; | |
| 92 // 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 | |
| 93 // Morrison, Stuart; 2000; DIY DECOMPRESSION; http://www.lizardland.co.uk/DIYDeco.html | |
| 94 // Balthasar, Steffen; Dekompressionstheorie I: Neo Haldane Modelle; http://www.txfreak.de/dekompressionstheorie_1.pdf | |
| 95 // Baker, Erik C.; Clearing Up The Confusion About "Deep Stops" | |
| 96 // Baker, Erik C.; Understanding M-values; http://www.txfreak.de/understanding_m-values.pdf | |
| 97 // | |
| 98 // | |
| 99 | |
| 100 // ********************* | |
| 101 // ** I N C L U D E S ** | |
| 102 // ********************* | |
| 103 #include <math.h> | |
| 104 | |
| 105 // *********************************************** | |
| 106 // ** V A R I A B L E S D E F I N I T I O N S ** | |
| 107 // *********************************************** | |
| 108 | |
| 109 #include "p2_definitions.h" | |
| 110 #define TEST_MAIN | |
| 111 #include "shared_definitions.h" | |
| 112 | |
| 113 // Water vapour partial pressure in the lumb. | |
| 114 #define ppWater 0.0627 | |
| 115 #define METER_TO_BAR 0.09985 | |
| 116 #define BAR_TO_METER 10.0150 // (1.0/METER_TO_BAR) | |
| 117 | |
| 118 // Surface security factor | |
| 119 #define SURFACE_DESAT_FACTOR 0.7042 | |
| 120 | |
| 121 // ************************* | |
| 122 // ** P R O T O T Y P E S ** | |
| 123 // ************************* | |
| 124 | |
| 125 static void calc_hauptroutine(void); | |
| 126 static void calc_nullzeit(void); | |
| 127 | |
| 128 static void calc_tissue(PARAMETER unsigned char period); | |
| 129 static void calc_limit(void); | |
| 130 | |
| 131 static void clear_tissue(void); | |
| 132 static void calc_ascenttime(void); | |
| 133 static void update_startvalues(void); | |
| 134 static void clear_deco_table(void); | |
| 135 static unsigned char update_deco_table(void); | |
| 136 | |
| 137 static void sim_tissue(PARAMETER unsigned char period); | |
| 138 static void sim_limit(PARAMETER float GF_current); | |
| 139 static void sim_extra_time(void); | |
| 140 static void calc_dive_interval(void); | |
| 141 | |
| 142 static void calc_gradient_factor(void); | |
| 143 static void calc_wo_deco_step_1_min(void); | |
| 144 | |
| 145 static void calc_hauptroutine_data_input(void); | |
| 146 static void calc_hauptroutine_update_tissues(void); | |
| 147 static void calc_hauptroutine_calc_deco(void); | |
| 148 static void sim_ascent_to_first_stop(void); | |
| 149 | |
| 150 static unsigned char gas_switch_deepest(void); | |
| 151 static void gas_switch_set(void); | |
| 152 | |
| 153 static unsigned char calc_nextdecodepth(void); | |
| 154 | |
| 155 //---- Bank 5 parameters ----------------------------------------------------- | |
| 156 #ifndef UNIX | |
| 157 # pragma udata bank5=0x500 | |
| 158 #endif | |
| 159 | |
| 160 static float GF_low; | |
| 161 static float GF_high; | |
| 162 static float GF_delta; | |
| 163 static float locked_GF_step; // GF_delta / low_depth | |
| 164 | |
| 165 static unsigned char temp_depth_limit; | |
| 166 static unsigned char low_depth; // Depth of deepest stop | |
| 167 | |
| 168 // Simulation context: used to predict ascent. | |
| 169 static unsigned char sim_lead_tissue_no; // Leading compatiment number. | |
| 170 static float sim_lead_tissue_limit; // Buhlmann tolerated pressure. | |
| 171 | |
| 172 // Real context: what we are doing now. | |
| 173 static float calc_lead_tissue_limit; // | |
| 174 | |
| 175 static unsigned char internal_deco_time[NUM_STOPS]; | |
| 176 static unsigned char internal_deco_depth[NUM_STOPS]; | |
| 177 | |
| 178 static float cns_vault; | |
| 179 static float low_depth_vault; | |
| 180 static float pres_tissue_N2_vault[NUM_COMP]; | |
| 181 static float pres_tissue_He_vault[NUM_COMP]; | |
| 182 | |
| 183 //---- Bank 6 parameters ----------------------------------------------------- | |
| 184 #ifndef UNIX | |
| 185 # pragma udata bank6=0x600 | |
| 186 #endif | |
| 187 | |
| 188 static unsigned char ci; | |
| 189 static float pres_respiration; | |
| 190 static float pres_surface; | |
| 191 static float temp_deco; | |
| 192 static float ppN2; | |
| 193 static float ppHe; | |
| 194 static float temp_tissue; | |
| 195 static float N2_ratio; // Breathed gas nitrogen ratio. | |
| 196 static float He_ratio; // Breathed gas helium ratio. | |
| 197 static float var_N2_a; // Bühlmann a, for current N2 tissue. | |
| 198 static float var_N2_b; // Bühlmann b, for current N2 tissue. | |
| 199 static float var_He_a; // Bühlmann a, for current He tissue. | |
| 200 static float var_He_b; // Bühlmann b, for current He tissue. | |
| 201 static float var_N2_e; // Exposition, for current N2 tissue. | |
| 202 static float var_He_e; // Exposition, for current He tissue. | |
| 203 static float var_N2_ht; // Half-time for current N2 tissue. | |
| 204 static float var_He_ht; // Half-time for current N2 tissue. | |
| 205 | |
| 206 static float pres_diluent; // new in v.101 | |
| 207 static float const_ppO2; // new in v.101 | |
| 208 | |
| 209 static unsigned char sim_gas_last_depth; // Depth of last used gas, to detected a gas switch. | |
| 210 static unsigned char sim_gas_last_used; // Number of last used gas, to detected a gas switch. | |
| 211 static unsigned short sim_dive_mins; // Simulated dive time. | |
| 212 static float calc_N2_ratio; // Simulated (switched) nitrogen ratio. | |
| 213 static float calc_He_ratio; // Simulated (switched) helium ratio. | |
| 214 static float CNS_fraction; // new in v.101 | |
| 215 static float float_saturation_multiplier; // new in v.101 | |
| 216 static float float_desaturation_multiplier; // new in v.101 | |
| 217 static float float_deco_distance; // new in v.101 | |
| 218 //static char flag_in_divemode; // new in v.108 | |
| 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 // flag_in_divemode = 0; | |
| 901 char_O_deco_status = 0; | |
| 902 char_O_nullzeit = 0; | |
| 903 int_O_ascenttime = 0; | |
| 904 char_O_gradient_factor = 0; | |
| 905 char_O_relative_gradient_GF = 0; | |
| 906 | |
| 907 calc_lead_tissue_limit = 0.0; | |
| 908 char_O_gtissue_no = 0; | |
| 909 } | |
| 910 | |
| 911 ////////////////////////////////////////////////////////////////////////////// | |
| 912 // calc_hauptroutine | |
| 913 // | |
| 914 // this is the major code in dive mode calculates: | |
| 915 // the tissues, | |
| 916 // the bottom time, | |
| 917 // and simulates the ascend with all deco stops. | |
| 918 // | |
| 919 // The deco_state sequence is : | |
| 920 // 3 (at surface) | |
| 921 // +---> 0 : calc nullzeit | |
| 922 // | 2 : simulate ascent to first stop (at 10m/min, less that 16x 1min simu) | |
| 923 // | +-> 1 : simulate up to 16min of stops. | |
| 924 // | +------< not finished | |
| 925 // +--------< finish | |
| 926 // | |
| 927 // Added steps 6,5 for @+5 calculation: | |
| 928 // 6 = ascent to first stop (same as 2), except continue to 7 | |
| 929 // 7 = same as 1, except loop to 7. | |
| 930 // | |
| 931 static void calc_hauptroutine(void) | |
| 932 { | |
| 933 static unsigned char backup_gas_used; | |
| 934 static unsigned char backup_gas_depth; | |
| 935 | |
| 936 calc_hauptroutine_data_input(); | |
| 937 | |
| 938 calc_hauptroutine_update_tissues(); | |
| 939 calc_gradient_factor(); | |
| 940 | |
| 941 // toggle between calculation for nullzeit (bottom time), | |
| 942 // deco stops | |
| 943 // and more deco stops (continue) | |
| 944 switch( char_O_deco_status ) | |
| 945 { | |
| 946 case 3: //---- At surface: start a new dive ------------------------------ | |
| 947 clear_deco_table(); | |
| 948 copy_deco_table(); | |
| 949 int_O_ascenttime = 0; // Reset DTR. | |
| 950 int_O_extra_ascenttime = 0; | |
| 951 char_O_nullzeit = 0; // Reset bottom time. | |
| 952 char_O_deco_status = 0; // Calc bottom-time/nullzeit next iteration. | |
| 953 | |
| 954 // Values that should be reset just once for the full real dive. | |
| 955 // This is used to record the lowest stop for the whole dive, | |
| 956 // Including ACCROSS all simulated ascent. | |
| 957 low_depth = 0; | |
| 958 locked_GF_step = 0.0; | |
| 959 | |
| 960 // Reset gas switch history. | |
| 961 backup_gas_used = sim_gas_last_used = 0; | |
| 962 backup_gas_depth = sim_gas_last_depth = 0; | |
| 963 sim_dive_mins = 0; | |
| 964 break; | |
| 965 | |
| 966 case 0: //---- bottom time ----------------------------------------------- | |
| 967 default: | |
| 968 gas_switch_find_current(); // Lookup for current gas & time. | |
| 969 gas_switch_set(); // setup calc_ratio's | |
| 970 | |
| 971 calc_nullzeit(); | |
| 972 if( char_O_nullzeit > 0 ) // Some NDL time left ? | |
| 973 { | |
| 974 char_O_deco_status = 0; // YES: recalc ndl next time. | |
| 975 clear_deco_table(); // Also clear stops ! | |
| 976 copy_deco_table(); | |
| 977 char_O_deco_last_stop = 0; // And last stop (OSTC menu anim) | |
| 978 } | |
| 979 else | |
| 980 char_O_deco_status = 2; // NO: calc ascent next time. | |
| 981 break; | |
| 982 | |
| 983 case 2: //---- Simulate ascent to first stop ----------------------------- | |
| 984 case 6: // @+5min variation | |
| 985 // Check proposed gas at begin of ascent simulation | |
| 986 sim_dive_mins = int_I_divemins; // Init current time. | |
| 987 | |
| 988 gas_switch_find_current(); // Lookup for current gas & time. | |
| 989 gas_switch_set(); // setup calc_ratio's | |
| 990 | |
| 991 backup_gas_used = sim_gas_last_used; // And save for later simu steps. | |
| 992 backup_gas_depth = sim_gas_last_depth; // And save for later simu steps. | |
| 993 | |
| 994 sim_ascent_to_first_stop(); | |
| 995 | |
| 996 // Calc stops next time (deco or gas switch). | |
| 997 char_O_deco_status = 1 | ( char_O_deco_status & 4 ); | |
| 998 break; | |
| 999 | |
| 1000 case 1: //---- Simulate stops -------------------------------------------- | |
| 1001 case 5: // @+5 variation. | |
| 1002 calc_hauptroutine_calc_deco(); | |
| 1003 | |
| 1004 // If simulation is finished, restore the GF low reference, so that | |
| 1005 // next ascent simulation is done from the current depth: | |
| 1006 if( (char_O_deco_status & 3) == 0 ) | |
| 1007 { | |
| 1008 sim_gas_last_used = backup_gas_used; | |
| 1009 sim_gas_last_depth = backup_gas_depth; | |
| 1010 } | |
| 1011 break; | |
| 1012 } | |
| 1013 } | |
| 1014 | |
| 1015 ////////////////////////////////////////////////////////////////////////////// | |
| 1016 // calc_hauptroutine_data_input | |
| 1017 // | |
| 1018 // Reset all C-code dive parameters from their ASM-code values. | |
| 1019 // Detect gas change condition. | |
| 1020 // | |
| 1021 void calc_hauptroutine_data_input(void) | |
| 1022 { | |
| 1023 overlay short int_temp; | |
| 1024 overlay unsigned char g; | |
| 1025 | |
| 1026 pres_respiration = int_I_pres_respiration * 0.001; | |
| 1027 pres_surface = int_I_pres_surface * 0.001; | |
| 1028 N2_ratio = char_I_N2_ratio * 0.01; | |
| 1029 He_ratio = char_I_He_ratio * 0.01; | |
| 1030 float_deco_distance = char_I_deco_distance * 0.01; // Get offset is in mbar. | |
| 1031 | |
| 1032 // ____________________________________________________ | |
| 1033 // | |
| 1034 // _____________ G A S _ C H A N G E S ________________ | |
| 1035 // ____________________________________________________ | |
| 1036 | |
| 1037 // Keep a margin of 150mbar = 1.50m | |
| 1038 int_temp = (int_I_pres_respiration - int_I_pres_surface) | |
| 1039 + MBAR_REACH_GASCHANGE_AUTO_CHANGE_OFF; | |
| 1040 | |
| 1041 // Gas are selectable if we did not pass the change depth by more than 1.50m: | |
| 1042 for(g=0; g < NUM_GAS; ++g) | |
| 1043 { | |
| 1044 deco_gas_change[g] = 0; | |
| 1045 if(char_I_deco_gas_change[g]) | |
| 1046 if( int_temp > 100 *(short)char_I_deco_gas_change[g] ) | |
| 1047 deco_gas_change[g] = char_I_deco_gas_change[g]; | |
| 1048 } | |
| 1049 | |
| 1050 const_ppO2 = char_I_const_ppO2 * 0.01; | |
| 1051 float_desaturation_multiplier = char_I_desaturation_multiplier * 0.01; | |
| 1052 float_saturation_multiplier = char_I_saturation_multiplier * 0.01; | |
| 1053 GF_low = char_I_GF_Low_percentage * 0.01; | |
| 1054 GF_high = char_I_GF_High_percentage * 0.01; | |
| 1055 GF_delta = GF_high - GF_low; | |
| 1056 } | |
| 1057 | |
| 1058 ////////////////////////////////////////////////////////////////////////////// | |
| 1059 // | |
| 1060 // | |
| 1061 void calc_hauptroutine_update_tissues(void) | |
| 1062 { | |
| 1063 assert( 0.00 <= N2_ratio && N2_ratio <= 1.00 ); | |
| 1064 assert( 0.00 <= He_ratio && He_ratio <= 1.00 ); | |
| 1065 assert( (N2_ratio + He_ratio) <= 0.95 ); | |
| 1066 assert( 0.800 < pres_respiration && pres_respiration < 14.0 ); | |
| 1067 | |
| 1068 pres_diluent = pres_respiration; | |
| 1069 if( char_I_const_ppO2 != 0 ) | |
| 1070 { | |
| 1071 overlay float flush_ppO2 = pres_respiration * (1.0 - N2_ratio - He_ratio); | |
| 1072 | |
| 1073 pres_diluent -= const_ppO2; | |
| 1074 pres_diluent /= N2_ratio + He_ratio; | |
| 1075 if( pres_diluent < 0.0 ) | |
| 1076 pres_diluent = 0.0; | |
| 1077 if( pres_diluent > pres_respiration ) | |
| 1078 pres_diluent = pres_respiration; | |
| 1079 | |
| 1080 char_O_diluent = (unsigned char)(pres_diluent/pres_respiration*100.0 + 0.5); | |
| 1081 | |
| 1082 if( flush_ppO2 > 2.545) flush_ppO2 = 2.55; | |
| 1083 if( flush_ppO2 < 0.0 ) flush_ppO2 = 0.0; | |
| 1084 char_O_flush_ppO2 = (unsigned char)(flush_ppO2*100.0 + 0.5); | |
| 1085 } | |
| 1086 | |
| 1087 if( pres_diluent > ppWater ) | |
| 1088 { | |
| 1089 overlay float EAD, END; | |
| 1090 | |
| 1091 ppN2 = N2_ratio * (pres_diluent - ppWater); | |
| 1092 ppHe = He_ratio * (pres_diluent - ppWater); | |
| 1093 | |
| 1094 // EAD : Equivalent Air Dive. Equivalent depth for the same N2 level | |
| 1095 // with plain air. | |
| 1096 // ppN2 = 79% * (P_EAD - ppWater) | |
| 1097 // EAD = (P_EAD - Psurface) * 10 | |
| 1098 // ie: EAD = (ppN2 / 0.7902 + ppWater -Psurface) * 10 | |
| 1099 EAD = (ppN2 / 0.7902 + ppWater - pres_surface) * BAR_TO_METER; | |
| 1100 if( EAD < 0.0 || EAD > 245.5 ) EAD = 0.0; | |
| 1101 char_O_EAD = (unsigned char)(EAD + 0.5); | |
| 1102 | |
| 1103 // END : Equivalent Narcotic Dive. | |
| 1104 // Here we count O2 as narcotic too. Hence everything but helium (has a narcosis factor of | |
| 1105 // 0.23 btw). Hence the formula becomes: | |
| 1106 // END * BarPerMeter * (1.0 - 0.0) - ppWater + Psurface == Pambient - ppHe - ppWater | |
| 1107 // ie: END = (Pambient - ppHe - Psurface) * BAR_TO_METER | |
| 1108 // | |
| 1109 // Source cited: | |
| 1110 // The Physiology and Medicine of Diving by Peter Bennett and David Elliott, | |
| 1111 // 4th edition, 1993, W.B.Saunders Company Ltd, London. | |
| 1112 END = (pres_respiration - ppHe - pres_surface) * BAR_TO_METER; | |
| 1113 if( END < 0.0 || END > 245.5 ) END = 0.0; | |
| 1114 char_O_END = (unsigned char)(END + 0.5); | |
| 1115 } | |
| 1116 else // new in v.101 | |
| 1117 { | |
| 1118 ppN2 = 0.0; | |
| 1119 ppHe = 0.0; | |
| 1120 char_O_EAD = char_O_END = 0; | |
| 1121 } | |
| 1122 | |
| 1123 if(!char_I_step_is_1min) | |
| 1124 calc_tissue(0); | |
| 1125 else | |
| 1126 calc_tissue(1); | |
| 1127 | |
| 1128 // Calc limit for surface, ie. GF_high. | |
| 1129 calc_limit(); | |
| 1130 | |
| 1131 int_O_gtissue_limit = (short)(calc_lead_tissue_limit * 1000); | |
| 1132 int_O_gtissue_press = (short)((pres_tissue_N2[char_O_gtissue_no] + pres_tissue_He[char_O_gtissue_no]) * 1000); | |
| 1133 } | |
| 1134 | |
| 1135 | |
| 1136 ////////////////////////////////////////////////////////////////////////////// | |
| 1137 // Compute stops. | |
| 1138 // | |
| 1139 // Note: because this can be very long, break on 16 iterations, and set state | |
| 1140 // to 0 when finished, or to 1 when needing to continue. | |
| 1141 // Note: because each iteration might be very long too (~ 66 ms in 1.84beta), | |
| 1142 // break the loop when total time > 512msec. | |
| 1143 // | |
| 1144 void calc_hauptroutine_calc_deco(void) | |
| 1145 { | |
| 1146 overlay unsigned char loop; | |
| 1147 | |
| 1148 for(loop = 0; loop < 32; ++loop) | |
| 1149 { | |
| 1150 // Limit loops to 512ms, using timer 5: | |
| 1151 if( tmr5() & (512*32) ) | |
| 1152 break; | |
| 1153 // Do not ascent while doing a gas switch ? | |
| 1154 if( calc_nextdecodepth() ) | |
| 1155 { | |
| 1156 if( temp_depth_limit == 0 ) | |
| 1157 goto Surface; | |
| 1158 | |
| 1159 //---- We hit a stop at temp_depth_limit --------------------- | |
| 1160 temp_deco = temp_depth_limit * METER_TO_BAR // Convert to relative bar, | |
| 1161 + pres_surface; // To absolute. | |
| 1162 if( !update_deco_table() ) // Adds a one minute stops. | |
| 1163 goto Surface; // Deco table full: abort... | |
| 1164 } | |
| 1165 else | |
| 1166 { | |
| 1167 //---- No stop ----------------------------------------------- | |
| 1168 temp_deco -= (10*METER_TO_BAR); // Ascend 10m, no wait. | |
| 1169 | |
| 1170 //---- Finish computations once surface is reached ----------- | |
| 1171 if( temp_deco <= pres_surface ) | |
| 1172 { | |
| 1173 Surface: | |
| 1174 if( char_O_deco_status == 1 ) // Don't in @+5min variant. | |
| 1175 copy_deco_table(); | |
| 1176 | |
| 1177 calc_ascenttime(); | |
| 1178 char_O_deco_status = 0; // calc nullzeit next time. | |
| 1179 char_O_deco_last_stop = 0; // Surface reached (to animate menu) | |
| 1180 return; | |
| 1181 } | |
| 1182 } | |
| 1183 //---- Then update tissue -------------------------------------------- | |
| 1184 sim_dive_mins++; // Advance simulated time by 1 minute. | |
| 1185 gas_switch_set(); // Apply any simulated gas change, once validated. | |
| 1186 sim_alveolar_presures(); // Updates ppN2 and ppHe. | |
| 1187 sim_tissue(1); // Simulate compartiments for 1 minute. | |
| 1188 } | |
| 1189 | |
| 1190 // Surface not reached, need more stops... for menu animation. | |
| 1191 char_O_deco_last_stop = temp_depth_limit; // Reached depth. | |
| 1192 } | |
| 1193 | |
| 1194 | |
| 1195 ////////////////////////////////////////////////////////////////////////////// | |
| 1196 // Simulation ascention to first deco stop. | |
| 1197 // | |
| 1198 // Note: because we ascent with a constant speed (10m/mn, ie. 1bar/mn), | |
| 1199 // there is no need to break on more that 16 iterations | |
| 1200 // (or we are already in deep shit). | |
| 1201 // | |
| 1202 // Input: pres_respiration | |
| 1203 // Output: temp_deco | |
| 1204 // | |
| 1205 // if char_O_deco_status indicate @+5 variant, add extra time at current depth, | |
| 1206 // before ascent. | |
| 1207 void sim_ascent_to_first_stop(void) | |
| 1208 { | |
| 1209 overlay unsigned char fast = 1; // 1min or 2sec steps. | |
| 1210 | |
| 1211 update_startvalues(); | |
| 1212 clear_deco_table(); | |
| 1213 | |
| 1214 temp_deco = pres_respiration; // Starts from current real depth. | |
| 1215 | |
| 1216 // Are we doing the special @+5min variation ? | |
| 1217 if(char_O_deco_status & 4) | |
| 1218 sim_extra_time(); | |
| 1219 | |
| 1220 //---- Loop until first stop, gas switch, or surface is reached ---------- | |
| 1221 for(;;) | |
| 1222 { | |
| 1223 overlay float old_deco = temp_deco; // Pamb backup (bars) | |
| 1224 | |
| 1225 // Try ascending 1 full minute (fast) or 2sec (!fast): | |
| 1226 if( fast ) | |
| 1227 temp_deco -= 10*METER_TO_BAR; // 1 min, at 10m/min. ~ 1bar. | |
| 1228 else | |
| 1229 temp_deco -= (10.0/30.0)*METER_TO_BAR; // 2sec at 10m/min. | |
| 1230 | |
| 1231 if( temp_deco < pres_surface ) // But don't go over surface. | |
| 1232 temp_deco = pres_surface; | |
| 1233 | |
| 1234 // Recompute sim_lead_tissue_limit at GF_low (deepest stop), because | |
| 1235 // one minute passed. | |
| 1236 sim_limit(GF_low); | |
| 1237 | |
| 1238 // Did we reach deepest remaining stop ? | |
| 1239 if( temp_deco < sim_lead_tissue_limit ) | |
| 1240 { | |
| 1241 temp_deco = old_deco; // Restore last correct depth, | |
| 1242 | |
| 1243 if( fast ) | |
| 1244 { | |
| 1245 fast = 0; // Retry with 2sec steps. | |
| 1246 continue; | |
| 1247 } | |
| 1248 else | |
| 1249 break; // Done... | |
| 1250 } | |
| 1251 | |
| 1252 // Did we reach surface ? | |
| 1253 // NOTE: we should round BEFORE checking surface is reached. | |
| 1254 temp_depth_limit = (unsigned char)(0.5 + (temp_deco - pres_surface) * BAR_TO_METER); | |
| 1255 if( temp_depth_limit == 0 ) | |
| 1256 { | |
| 1257 temp_deco = pres_surface; // Yes: finished ! | |
| 1258 break; | |
| 1259 } | |
| 1260 | |
| 1261 // Check for gas change below new depth ? | |
| 1262 if( gas_switch_deepest() ) | |
| 1263 { | |
| 1264 assert( temp_depth_limit > 0); | |
| 1265 | |
| 1266 temp_deco = temp_depth_limit * METER_TO_BAR + pres_surface; | |
| 1267 break; | |
| 1268 } | |
| 1269 | |
| 1270 if( fast ) | |
| 1271 sim_dive_mins++; // Advance simulated time by 1 minute. | |
| 1272 sim_alveolar_presures(); // temp_deco --> ppN2/ppHe | |
| 1273 sim_tissue(fast); // and update tissues for 1 min. | |
| 1274 } | |
| 1275 } | |
| 1276 | |
| 1277 ////////////////////////////////////////////////////////////////////////////// | |
| 1278 // Simulation extra time at the current depth. | |
| 1279 // | |
| 1280 // This routine is used for @+5min feature. | |
| 1281 void sim_extra_time(void) | |
| 1282 { | |
| 1283 overlay unsigned char extra = char_I_extra_time; | |
| 1284 do { | |
| 1285 sim_dive_mins++; // Advance simulated time by 1 minute. | |
| 1286 sim_tissue(1); // and update tissues for 1 min. | |
| 1287 } while( --extra != 0 ); | |
| 1288 } | |
| 1289 | |
| 1290 ////////////////////////////////////////////////////////////////////////////// | |
| 1291 // calc_tissue | |
| 1292 // | |
| 1293 // optimized in v.101 | |
| 1294 // | |
| 1295 static void calc_tissue(PARAMETER unsigned char period) | |
| 1296 { | |
| 1297 assert( 0.00 <= ppN2 && ppN2 < 11.2 ); // 80% N2 at 130m | |
| 1298 assert( 0.00 <= ppHe && ppHe < 12.6 ); // 90% He at 130m | |
| 1299 | |
| 1300 for (ci=0;ci<NUM_COMP;ci++) | |
| 1301 { | |
| 1302 read_buhlmann_times(period); // 2 sec or 1 min period. | |
| 1303 | |
| 1304 // N2 | |
| 1305 temp_tissue = (ppN2 - pres_tissue_N2[ci]) * var_N2_e; | |
| 1306 temp_tissue_safety(); | |
| 1307 pres_tissue_N2[ci] += temp_tissue; | |
| 1308 | |
| 1309 // He | |
| 1310 temp_tissue = (ppHe - pres_tissue_He[ci]) * var_He_e; | |
| 1311 temp_tissue_safety(); | |
| 1312 pres_tissue_He[ci] += temp_tissue; | |
| 1313 } | |
| 1314 } | |
| 1315 | |
| 1316 ////////////////////////////////////////////////////////////////////////////// | |
| 1317 // calc_limit | |
| 1318 // | |
| 1319 // New in v.111 : separated from calc_tissue(), and depends on GF value. | |
| 1320 // | |
| 1321 static void calc_limit(void) | |
| 1322 { | |
| 1323 char_O_gtissue_no = 255; | |
| 1324 calc_lead_tissue_limit = 0.0; | |
| 1325 | |
| 1326 for(ci=0; ci<NUM_COMP;ci++) | |
| 1327 { | |
| 1328 overlay float N2 = pres_tissue_N2[ci]; | |
| 1329 overlay float He = pres_tissue_He[ci]; | |
| 1330 overlay float p = N2 + He; | |
| 1331 | |
| 1332 read_buhlmann_coefficients(); | |
| 1333 var_N2_a = (var_N2_a * N2 + var_He_a * He) / p; | |
| 1334 var_N2_b = (var_N2_b * N2 + var_He_b * He) / p; | |
| 1335 | |
| 1336 // Apply the Eric Baker's varying gradient factor correction. | |
| 1337 // Note: the correction factor depends both on GF and b, | |
| 1338 // Actual values are in the 1.5 .. 1.0 range (for a GF=30%), | |
| 1339 // so that can change who is the leading gas... | |
| 1340 // Note: Also depends of the GF. So the calcul is different for | |
| 1341 // GF_low, current GF, or GF_high... | |
| 1342 // *BUT* calc_tissue() is used to compute bottom time, | |
| 1343 // hence what would happend at surface, | |
| 1344 // hence at GF_high. | |
| 1345 if( char_I_deco_model != 0 ) | |
| 1346 p = ( p - var_N2_a * GF_high) * var_N2_b | |
| 1347 / (GF_high + var_N2_b * (1.0 - GF_high)); | |
| 1348 else | |
| 1349 p = (p - var_N2_a) * var_N2_b; | |
| 1350 if( p < 0.0 ) p = 0.0; | |
| 1351 | |
| 1352 if( p > calc_lead_tissue_limit ) | |
| 1353 { | |
| 1354 char_O_gtissue_no = ci; | |
| 1355 calc_lead_tissue_limit = p; | |
| 1356 } | |
| 1357 } | |
| 1358 | |
| 1359 assert( char_O_gtissue_no < NUM_COMP ); | |
| 1360 assert( 0.0 <= calc_lead_tissue_limit && calc_lead_tissue_limit <= 14.0); | |
| 1361 } | |
| 1362 | |
| 1363 ////////////////////////////////////////////////////////////////////////////// | |
| 1364 // calc_nullzeit | |
| 1365 // | |
| 1366 // calculates the remaining bottom time | |
| 1367 // | |
| 1368 // NOTE: Erik Baker's closed formula works for Nitroxes. Trimix adds a second | |
| 1369 // exponential term to the M-value equation, making it impossible to | |
| 1370 // invert... So we have to make a fast-simu until we find a better way. | |
| 1371 // | |
| 1372 // Input: pres_respiration | |
| 1373 // Output: char_O_nullzeit | |
| 1374 // | |
| 1375 static void calc_nullzeit(void) | |
| 1376 { | |
| 1377 //---- Compute ppN2 and ppHe --------------------------------------------- | |
| 1378 temp_deco = pres_respiration; | |
| 1379 sim_alveolar_presures(); | |
| 1380 | |
| 1381 char_O_nullzeit = 240; | |
| 1382 for(ci=0; ci<NUM_COMP; ci++) | |
| 1383 { | |
| 1384 //---- Read A/B values and loading factor for N2 and He -------------- | |
| 1385 overlay float tN2 = pres_tissue_N2[ci]; | |
| 1386 overlay float tHe = pres_tissue_He[ci]; | |
| 1387 overlay float t = tN2 + tHe; | |
| 1388 overlay unsigned char ndl; | |
| 1389 overlay unsigned char period = 10; | |
| 1390 | |
| 1391 read_buhlmann_coefficients(); | |
| 1392 read_buhlmann_times(2); // Starts with a 10min period. | |
| 1393 | |
| 1394 //---- Simulate for that tissue -------------------------------------- | |
| 1395 // NOTE: No need to simulate for longuer than the already found NDL. | |
| 1396 for(ndl=0; ndl<char_O_nullzeit;) | |
| 1397 { | |
| 1398 //---- Compute updated mix M-value at surface | |
| 1399 overlay float a = (var_N2_a * tN2 + var_He_a * tHe) / t; | |
| 1400 overlay float b = (var_N2_b * tN2 + var_He_b * tHe) / t; | |
| 1401 overlay float M0 = (a + pres_surface/b); | |
| 1402 | |
| 1403 //---- Add 10min/1min to N2/He tissues | |
| 1404 overlay float dTN2 = (ppN2 - tN2) * var_N2_e; | |
| 1405 overlay float dTHe = (ppHe - tHe) * var_He_e; | |
| 1406 | |
| 1407 //---- Apply security margin when using the non-GF model | |
| 1408 if( char_I_deco_model == 0 ) | |
| 1409 { | |
| 1410 dTN2 *= float_saturation_multiplier; | |
| 1411 dTHe *= float_saturation_multiplier; | |
| 1412 } | |
| 1413 else // Or GF-based model | |
| 1414 M0 = GF_high * (M0 - pres_surface) + pres_surface; | |
| 1415 | |
| 1416 //---- Simulate off-gasing while going to surface | |
| 1417 // TODO ! | |
| 1418 // dTN2 -= exp( ... ascent time ... ppN2...) | |
| 1419 // dTHe -= exp( ... ascent time ... ppHe...) | |
| 1420 | |
| 1421 //---- Ok now, and still ok to surface after 1 or 10 minutes ? | |
| 1422 if( (t <= M0) && (t + dTN2 + dTHe <= M0) ) | |
| 1423 { | |
| 1424 tN2 += dTN2; // YES: apply gas loadings, | |
| 1425 tHe += dTHe; | |
| 1426 t = tN2 + tHe; | |
| 1427 ndl += period; // increment NDL, | |
| 1428 continue; // and loop. | |
| 1429 } | |
| 1430 | |
| 1431 //---- Should we retry with smaller steps ? | |
| 1432 if( period == 10 ) | |
| 1433 { | |
| 1434 read_buhlmann_times(1); // 1min coefs. | |
| 1435 period = 1; | |
| 1436 continue; | |
| 1437 } | |
| 1438 | |
| 1439 //---- ELSE make a linear approx for the last minute | |
| 1440 // Usefull to have a meaningfull rounding of NDL. | |
| 1441 // But ONLY it positive (negativ casted to unsigned is bad). | |
| 1442 if( M0 > t ) | |
| 1443 ndl += (unsigned char)(0.5f + (M0-t)/(dTN2+dTHe)); | |
| 1444 break; | |
| 1445 } | |
| 1446 | |
| 1447 // Keep the shortest NDL found | |
| 1448 if( ndl < char_O_nullzeit ) | |
| 1449 char_O_nullzeit = ndl; | |
| 1450 } | |
| 1451 } | |
| 1452 | |
| 1453 ////////////////////////////////////////////////////////////////////////////// | |
| 1454 // calc_ascenttime | |
| 1455 // | |
| 1456 // Summup ascent from bottom to surface, at 1 bar/min, 1min for last 3 meters, | |
| 1457 // and all stops. | |
| 1458 // | |
| 1459 // Result in int_O_ascenttime, or int_O_extra_ascenttime if in @+5min variant. | |
| 1460 static void calc_ascenttime(void) | |
| 1461 { | |
| 1462 overlay unsigned char x; | |
| 1463 overlay unsigned short sum; | |
| 1464 | |
| 1465 // + 0.7 to count 1 minute ascent time from 3 metre to surface | |
| 1466 overlay float ascent = pres_respiration - pres_surface + 0.7; | |
| 1467 if (ascent < 0.0) | |
| 1468 ascent = 0.0; | |
| 1469 sum = (unsigned short)(ascent + 0.99); | |
| 1470 | |
| 1471 for(x=0; x<NUM_STOPS && internal_deco_depth[x]; x++) | |
| 1472 sum += (unsigned short)internal_deco_time[x]; | |
| 1473 | |
| 1474 if( char_O_deco_status == 1 ) | |
| 1475 int_O_ascenttime = sum; | |
| 1476 else | |
| 1477 int_O_extra_ascenttime = sum; | |
| 1478 | |
| 1479 if(int_O_ascenttime>999) | |
| 1480 int_O_ascenttime=999; // limit to 999' | |
| 1481 } | |
| 1482 | |
| 1483 ////////////////////////////////////////////////////////////////////////////// | |
| 1484 // update_startvalues | |
| 1485 // | |
| 1486 // updated in v.102 | |
| 1487 // | |
| 1488 void update_startvalues(void) | |
| 1489 { | |
| 1490 overlay unsigned char x; | |
| 1491 | |
| 1492 // Start ascent simulation with current tissue partial pressures. | |
| 1493 for(x=0; x<NUM_COMP; x++) | |
| 1494 { | |
| 1495 sim_pres_tissue_N2[x] = pres_tissue_N2[x]; | |
| 1496 sim_pres_tissue_He[x] = pres_tissue_He[x]; | |
| 1497 } | |
| 1498 | |
| 1499 // No leading tissue (yet) for this ascent simulation. | |
| 1500 sim_lead_tissue_limit = 0.0; | |
| 1501 sim_lead_tissue_no = 255; | |
| 1502 } | |
| 1503 | |
| 1504 ////////////////////////////////////////////////////////////////////////////// | |
| 1505 // sim_tissue | |
| 1506 // | |
| 1507 // optimized in v.101 | |
| 1508 // | |
| 1509 // Function very simular to calc_tissue, but: | |
| 1510 // + Use a 1min or 10min period. | |
| 1511 // + Do it on sim_pres_tissue, instead of pres_tissue. | |
| 1512 static void sim_tissue(PARAMETER unsigned char period) | |
| 1513 { | |
| 1514 assert( 0.00 <= ppN2 && ppN2 < 11.2 ); // 80% N2 at 130m | |
| 1515 assert( 0.00 <= ppHe && ppHe < 12.6 ); // 90% He at 130m | |
| 1516 | |
| 1517 for(ci=0; ci<NUM_COMP; ci++) | |
| 1518 { | |
| 1519 read_buhlmann_times(period); // 1 or 10 minute(s) interval | |
| 1520 | |
| 1521 // N2 | |
| 1522 temp_tissue = (ppN2 - sim_pres_tissue_N2[ci]) * var_N2_e; | |
| 1523 temp_tissue_safety(); | |
| 1524 sim_pres_tissue_N2[ci] += temp_tissue; | |
| 1525 | |
| 1526 // He | |
| 1527 temp_tissue = (ppHe - sim_pres_tissue_He[ci]) * var_He_e; | |
| 1528 temp_tissue_safety(); | |
| 1529 sim_pres_tissue_He[ci] += temp_tissue; | |
| 1530 } | |
| 1531 } | |
| 1532 | |
| 1533 ////////////////////////////////////////////////////////////////////////////// | |
| 1534 // sim_limit() | |
| 1535 // | |
| 1536 // New in v.111 | |
| 1537 // | |
| 1538 // Function separated from sim_tissue() to allow recomputing limit on | |
| 1539 // different depth, because it depends on current gradient factor. | |
| 1540 // | |
| 1541 static void sim_limit(PARAMETER float GF_current) | |
| 1542 { | |
| 1543 assert( 0.0 < GF_current && GF_current <= 1.0f); | |
| 1544 | |
| 1545 sim_lead_tissue_limit = 0.0; | |
| 1546 sim_lead_tissue_no = 0; // If no one is critic, keep first tissue. | |
| 1547 | |
| 1548 for(ci=0; ci<NUM_COMP; ci++) | |
| 1549 { | |
| 1550 overlay float N2 = sim_pres_tissue_N2[ci]; | |
| 1551 overlay float He = sim_pres_tissue_He[ci]; | |
| 1552 overlay float p = N2 + He; | |
| 1553 | |
| 1554 read_buhlmann_coefficients(); | |
| 1555 var_N2_a = (var_N2_a * N2 + var_He_a * He) / p; | |
| 1556 var_N2_b = (var_N2_b * N2 + var_He_b * He) / p; | |
| 1557 | |
| 1558 // Apply the Eric Baker's varying gradient factor correction. | |
| 1559 // Note: the correction factor depends both on GF and b, | |
| 1560 // Actual values are in the 1.5 .. 1.0 range (for a GF=30%), | |
| 1561 // so that can change who is the leading gas... | |
| 1562 // Note: Also depends of the GF_current... | |
| 1563 if( char_I_deco_model != 0 ) | |
| 1564 p = ( p - var_N2_a * GF_current) | |
| 1565 / (GF_current / var_N2_b + 1.0 - GF_current); | |
| 1566 else | |
| 1567 p = (p - var_N2_a) * var_N2_b; | |
| 1568 if( p < 0.0 ) p = 0.0; | |
| 1569 | |
| 1570 if( p > sim_lead_tissue_limit ) | |
| 1571 { | |
| 1572 sim_lead_tissue_no = ci; | |
| 1573 sim_lead_tissue_limit = p; | |
| 1574 } | |
| 1575 } // for ci | |
| 1576 | |
| 1577 assert( sim_lead_tissue_no < NUM_COMP ); | |
| 1578 assert( 0.0 <= sim_lead_tissue_limit && sim_lead_tissue_limit <= 14.0 ); | |
| 1579 } | |
| 1580 | |
| 1581 ////////////////////////////////////////////////////////////////////////////// | |
| 1582 // clear_deco_table | |
| 1583 // | |
| 1584 // unchanged in v.101 | |
| 1585 // | |
| 1586 static void clear_deco_table(void) | |
| 1587 { | |
| 1588 overlay unsigned char x; | |
| 1589 | |
| 1590 for(x=0; x<NUM_STOPS; ++x) | |
| 1591 { | |
| 1592 internal_deco_time [x] = 0; | |
| 1593 internal_deco_depth[x] = 0; | |
| 1594 } | |
| 1595 } | |
| 1596 | |
| 1597 ////////////////////////////////////////////////////////////////////////////// | |
| 1598 // update_deco_table | |
| 1599 // | |
| 1600 // Add 1 min to current stop. | |
| 1601 // | |
| 1602 // Inputs: | |
| 1603 // temp_depth_limit = stop's depth, in meters. | |
| 1604 // In/Out: | |
| 1605 // internal_deco_depth[] : depth (in metres) of each stops. | |
| 1606 // internal_deco_time [] : time (in minutes) of each stops. | |
| 1607 // | |
| 1608 static unsigned char update_deco_table() | |
| 1609 { | |
| 1610 overlay unsigned char x; | |
| 1611 assert( temp_depth_limit < 128 ); // Can't be negativ (overflown). | |
| 1612 assert( temp_depth_limit > 0 ); // No stop at surface... | |
| 1613 | |
| 1614 for(x=0; x<NUM_STOPS; ++x) | |
| 1615 { | |
| 1616 // Make sure deco-stops are recorded in order: | |
| 1617 assert( !internal_deco_depth[x] || temp_depth_limit <= (internal_deco_depth[x]& 0x7F) ); | |
| 1618 | |
| 1619 if( (internal_deco_depth[x] & 0x7F) == temp_depth_limit ) | |
| 1620 { | |
| 1621 // Do not overflow (max 255') | |
| 1622 if( internal_deco_time[x] < 255 ) | |
| 1623 { | |
| 1624 internal_deco_time[x]++; | |
| 1625 return 1; | |
| 1626 } | |
| 1627 // But store extra in the next stop... | |
| 1628 } | |
| 1629 | |
| 1630 if( internal_deco_depth[x] == 0 ) | |
| 1631 { | |
| 1632 internal_deco_depth[x] = temp_depth_limit; | |
| 1633 | |
| 1634 internal_deco_time[x] = 1; | |
| 1635 return 1; | |
| 1636 } | |
| 1637 } | |
| 1638 | |
| 1639 // Can't store stops at more than 96m. | |
| 1640 // Or stops at less that 3m too. | |
| 1641 // Just do nothing with that... | |
| 1642 return 0; | |
| 1643 } | |
| 1644 | |
| 1645 ////////////////////////////////////////////////////////////////////////////// | |
| 1646 // calc_gradient_factor | |
| 1647 // | |
| 1648 // optimized in v.101 (var_N2_a) | |
| 1649 // new code in v.102 | |
| 1650 // | |
| 1651 static void calc_gradient_factor(void) | |
| 1652 { | |
| 1653 overlay float gf; | |
| 1654 overlay float N2 = pres_tissue_N2[char_O_gtissue_no]; | |
| 1655 overlay float He = pres_tissue_He[char_O_gtissue_no]; | |
| 1656 | |
| 1657 assert( char_O_gtissue_no < NUM_COMP ); | |
| 1658 assert( 0.800 <= pres_respiration && pres_respiration < 14.0 ); | |
| 1659 | |
| 1660 // tissue > respiration (currently off-gasing) | |
| 1661 // GF = 0% when respiration == tissue, ie. bubbles are at equilibrium. | |
| 1662 // GF = 100% when respiration == limit. | |
| 1663 temp_tissue = N2 + He; | |
| 1664 if( temp_tissue <= pres_respiration ) | |
| 1665 gf = 0.0; | |
| 1666 else | |
| 1667 { | |
| 1668 overlay float limit = calc_lead_tissue_limit; | |
| 1669 // NOTE: in GF model, calc_lead_tissue_limit include already the | |
| 1670 // correction due to gradient factor. To compute the actual | |
| 1671 // current GF, we need to (re-)compute the raw ambiant-pressure | |
| 1672 // limit from the Bühlmann model. | |
| 1673 if( char_I_deco_model != 0 ) | |
| 1674 { | |
| 1675 ci = char_O_gtissue_no; | |
| 1676 read_buhlmann_coefficients(); | |
| 1677 var_N2_a = (var_N2_a * N2 + var_He_a * He) / temp_tissue; | |
| 1678 var_N2_b = (var_N2_b * N2 + var_He_b * He) / temp_tissue; | |
| 1679 limit = (temp_tissue - var_N2_a) * var_N2_b; | |
| 1680 } | |
| 1681 | |
| 1682 gf = (temp_tissue - pres_respiration) | |
| 1683 / (temp_tissue - limit) | |
| 1684 * 100.0; | |
| 1685 if( gf > 254.5 ) gf = 255.0; | |
| 1686 if( gf < 0.0 ) gf = 0.0; | |
| 1687 } | |
| 1688 char_O_gradient_factor = (unsigned char)(gf+0.5f); | |
| 1689 | |
| 1690 if( char_I_deco_model != 0 ) // calculate relative gradient factor | |
| 1691 { | |
| 1692 overlay float rgf; | |
| 1693 | |
| 1694 if( low_depth < 3 ) | |
| 1695 rgf = GF_high; | |
| 1696 else | |
| 1697 { | |
| 1698 overlay float temp1 = low_depth * METER_TO_BAR; | |
| 1699 overlay float temp2 = pres_respiration - pres_surface; | |
| 1700 | |
| 1701 if (temp2 <= 0) | |
| 1702 rgf = GF_high; | |
| 1703 else if (temp2 >= temp1) | |
| 1704 rgf = GF_low; | |
| 1705 else | |
| 1706 rgf = GF_low + (temp1 - temp2)/temp1*GF_delta; | |
| 1707 } | |
| 1708 | |
| 1709 rgf = gf / rgf; // gf is already in percent | |
| 1710 if( rgf < 0.0 ) rgf = 0.0; | |
| 1711 if( rgf > 254.5 ) rgf = 255.0; | |
| 1712 char_O_relative_gradient_GF = (unsigned char)(rgf+0.5f); | |
| 1713 } | |
| 1714 else | |
| 1715 char_O_relative_gradient_GF = char_O_gradient_factor; | |
| 1716 } | |
| 1717 | |
| 1718 ////////////////////////////////////////////////////////////////////////////// | |
| 1719 // deco_calc_desaturation_time | |
| 1720 // | |
| 1721 // FIXED N2_ratio | |
| 1722 // unchanged in v.101 | |
| 1723 // Inputs: int_I_pres_surface, ppWater, char_I_desaturation_multiplier | |
| 1724 // Outputs: int_O_desaturation_time, char_O_tissue_saturation[0..31] | |
| 1725 // | |
| 1726 void deco_calc_desaturation_time(void) | |
| 1727 { | |
| 1728 RESET_C_STACK | |
| 1729 | |
| 1730 assert( 800 < int_I_pres_surface && int_I_pres_surface < 1100 ); | |
| 1731 assert( 0 < char_I_desaturation_multiplier && char_I_desaturation_multiplier <= 100 ); | |
| 1732 | |
| 1733 N2_ratio = 0.7902; // FIXED sum as stated in bühlmann | |
| 1734 pres_surface = int_I_pres_surface * 0.001; | |
| 1735 ppN2 = N2_ratio * (pres_surface - ppWater); | |
| 1736 int_O_desaturation_time = 0; | |
| 1737 float_desaturation_multiplier = char_I_desaturation_multiplier * (0.01 * SURFACE_DESAT_FACTOR); | |
| 1738 | |
| 1739 for(ci=0; ci<NUM_COMP; ci++) | |
| 1740 { | |
| 1741 overlay unsigned short desat_time; // For a particular compartiment, in min. | |
| 1742 overlay float temp1; | |
| 1743 overlay float temp2; | |
| 1744 overlay float temp3; | |
| 1745 overlay float temp4; | |
| 1746 | |
| 1747 read_buhlmann_ht(); | |
| 1748 | |
| 1749 // saturation_time (for flight) and N2_saturation in multiples of halftime | |
| 1750 // version v.100: 1.1 = 10 percent distance to totally clean (totally clean is not possible, would take infinite time ) | |
| 1751 // new in version v.101: 1.07 = 7 percent distance to totally clean (totally clean is not possible, would take infinite time ) | |
| 1752 // changes in v.101: 1.05 = 5 percent dist to totally clean is new desaturation point for display and NoFly calculations | |
| 1753 // N2 | |
| 1754 temp1 = 1.05 * ppN2 - pres_tissue_N2[ci]; | |
| 1755 temp2 = ppN2 - pres_tissue_N2[ci]; | |
| 1756 if (temp2 >= 0.0) | |
| 1757 temp1 = 0.0; | |
| 1758 else | |
| 1759 temp1 = temp1 / temp2; | |
| 1760 | |
| 1761 if( 0.0 < temp1 && temp1 < 1.0 ) | |
| 1762 { | |
| 1763 // 0.6931 is ln(2), because the math function log() calculates with a base of e not 2 as requested. | |
| 1764 // minus because log is negative. | |
| 1765 temp1 = log(1.0 - temp1) / -0.6931; // temp1 is the multiples of half times necessary. | |
| 1766 temp2 = var_N2_ht * temp1 / float_desaturation_multiplier; // time necessary (in minutes ) for complete desaturation (see comment about 5 percent) | |
| 1767 } | |
| 1768 else | |
| 1769 { | |
| 1770 temp1 = 0.0; | |
| 1771 temp2 = 0.0; | |
| 1772 } | |
| 1773 | |
| 1774 // He | |
| 1775 temp3 = 0.1 - pres_tissue_He[ci]; | |
| 1776 if (temp3 >= 0.0) | |
| 1777 temp3 = 0.0; | |
| 1778 else | |
| 1779 temp3 = - temp3 / pres_tissue_He[ci]; | |
| 1780 | |
| 1781 if( 0.0 < temp3 && temp3 < 1.0 ) | |
| 1782 { | |
| 1783 temp3 = log(1.0 - temp3) / -0.6931; // temp1 is the multiples of half times necessary. | |
| 1784 // 0.6931 is ln(2), because the math function log() calculates with a base of e not 2 as requested. | |
| 1785 // minus because log is negative | |
| 1786 temp4 = var_He_ht * temp3 / float_desaturation_multiplier; // time necessary (in minutes ) for "complete" desaturation, new in v.101 float_desaturation_multiplier | |
| 1787 } | |
| 1788 else | |
| 1789 { | |
| 1790 temp3 = 0.0; | |
| 1791 temp4 = 0.0; | |
| 1792 } | |
| 1793 | |
| 1794 // saturation_time (for flight) | |
| 1795 if (temp4 > temp2) | |
| 1796 desat_time = (unsigned short)temp4; | |
| 1797 else | |
| 1798 desat_time = (unsigned short)temp2; | |
| 1799 | |
| 1800 if(desat_time > int_O_desaturation_time) | |
| 1801 int_O_desaturation_time = desat_time; | |
| 1802 | |
| 1803 // N2 saturation in multiples of halftime for display purposes | |
| 1804 temp2 = temp1 * 20.0; // 0 = 1/8, 120 = 0, 249 = 8 | |
| 1805 temp2 = temp2 + 80.0; // set center | |
| 1806 if (temp2 < 0.0) | |
| 1807 temp2 = 0.0; | |
| 1808 if (temp2 > 255.0) | |
| 1809 temp2 = 255.0; | |
| 1810 char_O_tissue_N2_saturation[ci] = (char)temp2; | |
| 1811 | |
| 1812 // He saturation in multiples of halftime for display purposes | |
| 1813 temp4 = temp3 * 20.0; // 0 = 1/8, 120 = 0, 249 = 8 | |
| 1814 temp4 = temp4 + 80.0; // set center | |
| 1815 if (temp4 < 0.0) | |
| 1816 temp4 = 0.0; | |
| 1817 if (temp4 > 255.0) | |
| 1818 temp4 = 255.0; | |
| 1819 char_O_tissue_He_saturation[ci] = (char)temp4; | |
| 1820 } // for | |
| 1821 } | |
| 1822 | |
| 1823 ////////////////////////////////////////////////////////////////////////////// | |
| 1824 // calc_wo_deco_step_1_min | |
| 1825 // | |
| 1826 // FIXED N2 Ratio | |
| 1827 // optimized in v.101 (...saturation_multiplier) | |
| 1828 // desaturation slowed down to 70,42% | |
| 1829 // | |
| 1830 static void calc_wo_deco_step_1_min(void) | |
| 1831 { | |
| 1832 assert( 800 < int_I_pres_surface && int_I_pres_surface < 1100 ); | |
| 1833 assert( 800 < int_I_pres_respiration && int_I_pres_respiration < 1100 ); | |
| 1834 assert( 100 <= char_I_saturation_multiplier && char_I_saturation_multiplier < 200 ); | |
| 1835 assert( 0 < char_I_desaturation_multiplier && char_I_desaturation_multiplier <= 100 ); | |
| 1836 | |
| 1837 N2_ratio = 0.7902; // FIXED, sum lt. buehlmann | |
| 1838 pres_respiration = pres_surface = int_I_pres_surface * 0.001; | |
| 1839 ppN2 = N2_ratio * (pres_respiration - ppWater); | |
| 1840 ppHe = 0.0; | |
| 1841 float_desaturation_multiplier = char_I_desaturation_multiplier * (0.01 * SURFACE_DESAT_FACTOR); | |
| 1842 float_saturation_multiplier = char_I_saturation_multiplier * 0.01; | |
| 1843 | |
| 1844 calc_tissue(1); // update the pressure in the tissues N2/He in accordance with the new ambient pressure | |
| 1845 | |
| 1846 clear_deco_table(); | |
| 1847 char_O_deco_status = 3; // surface new in v.102 : stays in surface state. | |
| 1848 char_O_nullzeit = 0; | |
| 1849 int_O_ascenttime = 0; | |
| 1850 int_O_extra_ascenttime = 0; | |
| 1851 calc_gradient_factor(); | |
| 1852 } | |
| 1853 | |
| 1854 ////////////////////////////////////////////////////////////////////////////// | |
| 1855 // calc_dive_interval | |
| 1856 // | |
| 1857 // Prepare tissue for delay before the next dive simulation. | |
| 1858 // | |
| 1859 // Inputs: char_I_dive_interval == delay before dive (in 10' steps). | |
| 1860 // Outputs: pres_tissue_N2/He[], CNS_fraction | |
| 1861 // | |
| 1862 // Should be protected by deco_push_tissues_to_vault(), | |
| 1863 // deco_pull_tissues_from_vault() | |
| 1864 // | |
| 1865 // desaturation slowed down to 70,42%. | |
| 1866 // | |
| 1867 static void calc_dive_interval(void) | |
| 1868 { | |
| 1869 overlay unsigned char t; | |
| 1870 overlay unsigned char backup_model; | |
| 1871 | |
| 1872 //---- Initialize simulation parameters ---------------------------------- | |
| 1873 N2_ratio = 0.7902; // FIXED, sum lt. buehlmann | |
| 1874 pres_respiration = pres_surface = int_I_pres_surface * 0.001; | |
| 1875 ppN2 = N2_ratio * (pres_respiration - ppWater); | |
| 1876 ppHe = 0.0; | |
| 1877 float_desaturation_multiplier = char_I_desaturation_multiplier * (0.01 * SURFACE_DESAT_FACTOR); | |
| 1878 float_saturation_multiplier = char_I_saturation_multiplier * 0.01; | |
| 1879 | |
| 1880 // Make sure SURFACE_DESAT_FACTOR is applied: | |
| 1881 backup_model = char_I_deco_model; | |
| 1882 char_I_deco_model = 0; | |
| 1883 | |
| 1884 //---- Perform simulation ------------------------------------------------ | |
| 1885 for(t=0; t<char_I_dive_interval; ++t) | |
| 1886 { | |
| 1887 calc_tissue(2); // period = 10min. | |
| 1888 CNS_fraction = 0.92587471 * CNS_fraction; // Half-time = 90min: (1/2)^(1/9) | |
| 1889 } | |
| 1890 assert( 0.0 <= CNS_fraction && CNS_fraction <= 2.56 ); | |
| 1891 int_O_CNS_fraction = (unsigned int)(CNS_fraction * 100.0 + 0.5); | |
| 1892 | |
| 1893 //---- Restore model ----------------------------------------------------- | |
| 1894 char_I_deco_model = backup_model; | |
| 1895 } | |
| 1896 | |
| 1897 ////////////////////////////////////////////////////////////////////////////// | |
| 1898 // deco_clear_CNS_fraction | |
| 1899 // | |
| 1900 // new in v.101 | |
| 1901 // | |
| 1902 void deco_clear_CNS_fraction(void) | |
| 1903 { | |
| 1904 RESET_C_STACK | |
| 1905 | |
| 1906 CNS_fraction = 0.0; | |
| 1907 int_O_CNS_fraction = 0; | |
| 1908 } | |
| 1909 | |
| 1910 ////////////////////////////////////////////////////////////////////////////// | |
| 1911 // deco_calc_CNS_fraction | |
| 1912 // | |
| 1913 // Input: char_I_actual_ppO2 : Current condition (in decibars). | |
| 1914 // char_I_step_is_1min : use 1min or 10min steps instead of 2sec. | |
| 1915 // CNS_fraction : velue before period. | |
| 1916 // Output: CNS_fraction, int_O_CNS_fraction | |
| 1917 // | |
| 1918 void deco_calc_CNS_fraction(void) | |
| 1919 { | |
| 1920 overlay float time_factor = 1.0f; | |
| 1921 RESET_C_STACK | |
| 1922 | |
| 1923 assert( 0.0 <= CNS_fraction && CNS_fraction <= 2.56 ); | |
| 1924 assert( char_I_actual_ppO2 > 15 ); | |
| 1925 | |
| 1926 if( char_I_step_is_1min == 1 ) | |
| 1927 time_factor = 30.0f; | |
| 1928 else if( char_I_step_is_1min == 2 ) | |
| 1929 time_factor = 300.0f; | |
| 1930 | |
| 1931 if (char_I_actual_ppO2 < 50) | |
| 1932 ; // no changes | |
| 1933 else if (char_I_actual_ppO2 < 60) | |
| 1934 CNS_fraction += time_factor/(-540.0 * char_I_actual_ppO2 + 54000.0); | |
| 1935 else if (char_I_actual_ppO2 < 70) | |
| 1936 CNS_fraction += time_factor/(-450.0 * char_I_actual_ppO2 + 48600.0); | |
| 1937 else if (char_I_actual_ppO2 < 80) | |
| 1938 CNS_fraction += time_factor/(-360.0 * char_I_actual_ppO2 + 42300.0); | |
| 1939 else if (char_I_actual_ppO2 < 90) | |
| 1940 CNS_fraction += time_factor/(-270.0 * char_I_actual_ppO2 + 35100.0); | |
| 1941 else if (char_I_actual_ppO2 < 110) | |
| 1942 CNS_fraction += time_factor/(-180.0 * char_I_actual_ppO2 + 27000.0); | |
| 1943 else if (char_I_actual_ppO2 < 150) | |
| 1944 CNS_fraction += time_factor/( -90.0 * char_I_actual_ppO2 + 17100.0); | |
| 1945 else if (char_I_actual_ppO2 < 160) | |
| 1946 CNS_fraction += time_factor/(-225.0 * char_I_actual_ppO2 + 37350.0); | |
| 1947 else if (char_I_actual_ppO2 < 165) | |
| 1948 CNS_fraction += time_factor*0.000755; // Arieli et all.(2002): Modeling pulmonary and CNS O2 toxicity... Formula (A1) based on value for 1.55 and c=20 | |
| 1949 else if (char_I_actual_ppO2 < 170) | |
| 1950 CNS_fraction += time_factor*0.00102; // example calculation: Sqrt((1.7/1.55)^20)*0.000404 | |
| 1951 else if (char_I_actual_ppO2 < 175) | |
| 1952 CNS_fraction += time_factor*0.00136; | |
| 1953 else if (char_I_actual_ppO2 < 180) | |
| 1954 CNS_fraction += time_factor*0.00180; | |
| 1955 else if (char_I_actual_ppO2 < 185) | |
| 1956 CNS_fraction += time_factor*0.00237; | |
| 1957 else if (char_I_actual_ppO2 < 190) | |
| 1958 CNS_fraction += time_factor*0.00310; | |
| 1959 else if (char_I_actual_ppO2 < 195) | |
| 1960 CNS_fraction += time_factor*0.00401; | |
| 1961 else if (char_I_actual_ppO2 < 200) | |
| 1962 CNS_fraction += time_factor*0.00517; | |
| 1963 else if (char_I_actual_ppO2 < 230) | |
| 1964 CNS_fraction += time_factor*0.0209; | |
| 1965 else | |
| 1966 CNS_fraction += time_factor*0.0482; // value for 2.5 | |
| 1967 | |
| 1968 if( CNS_fraction > 9.99) | |
| 1969 CNS_fraction = 9.99; | |
| 1970 if( CNS_fraction < 0.0 ) | |
| 1971 CNS_fraction = 0.0; | |
| 1972 | |
| 1973 int_O_CNS_fraction = (unsigned int)(100.0 * CNS_fraction + 0.5); | |
| 1974 } | |
| 1975 | |
| 1976 ////////////////////////////////////////////////////////////////////////////// | |
| 1977 // deco_calc_CNS_planning | |
| 1978 // | |
| 1979 // Compute CNS during predicted ascent. | |
| 1980 // | |
| 1981 // Note: Needs a call to deco_push_tissues_to_vault(), | |
| 1982 // deco_pull_tissues_from_vault() to avoid trashing everything... | |
| 1983 // | |
| 1984 // Input: CNS_fraction, char_O_deco_time[], char_O_deco_depth[] | |
| 1985 // Output: CNS_fraction, int_O_CNS_fraction | |
| 1986 // | |
| 1987 void deco_calc_CNS_planning(void) | |
| 1988 { | |
| 1989 overlay unsigned char backup_gas_last_depth; | |
| 1990 overlay unsigned char backup_gas_last_used; | |
| 1991 overlay unsigned short backup_dive_mins; | |
| 1992 overlay unsigned char backup_actual_ppO2; | |
| 1993 | |
| 1994 RESET_C_STACK | |
| 1995 | |
| 1996 // Backup state machine | |
| 1997 backup_gas_last_depth = sim_gas_last_depth; | |
| 1998 backup_gas_last_used = sim_gas_last_used; | |
| 1999 backup_dive_mins = sim_dive_mins; | |
| 2000 backup_actual_ppO2 = char_I_actual_ppO2; | |
| 2001 | |
| 2002 // Uses 1min CNS period: | |
| 2003 char_I_step_is_1min = 1; | |
| 2004 | |
| 2005 //---- Retrieve bottom Gas used, and set variables. | |
| 2006 sim_gas_last_used = char_I_first_gas; | |
| 2007 sim_gas_last_depth = 0; // Surface gas marker. | |
| 2008 gas_switch_set(); // Sets initial calc_N2/He_ratio | |
| 2009 | |
| 2010 //---- CCR mode : do the full TTS at once -------------------------------- | |
| 2011 if( char_I_const_ppO2 != 0 ) | |
| 2012 { | |
| 2013 overlay unsigned short t; // Needs 16bits here ! | |
| 2014 char_I_actual_ppO2 = char_I_const_ppO2; | |
| 2015 for(t=0; t<int_O_ascenttime; ++t) | |
| 2016 deco_calc_CNS_fraction(); | |
| 2017 } | |
| 2018 else //---- OC mode : have to follow all gas switches... ----------------- | |
| 2019 { | |
| 2020 overlay unsigned char i = 0; // Decostop loop counter | |
| 2021 overlay float actual_ppO2; | |
| 2022 overlay unsigned char time, t; | |
| 2023 | |
| 2024 //---- Ascent to surface delay | |
| 2025 // NOTE: count as if time is spent with bottom pressure, | |
| 2026 // AND the bottom gas | |
| 2027 actual_ppO2 = (pres_surface + char_I_bottom_depth * METER_TO_BAR) | |
| 2028 * (1.0 - calc_N2_ratio - calc_He_ratio); | |
| 2029 if( actual_ppO2 < 0.0 ) actual_ppO2 = 0.0; | |
| 2030 if( actual_ppO2 > 2.50 ) actual_ppO2 = 2.55; | |
| 2031 char_I_actual_ppO2 = (unsigned char)(100.0 * actual_ppO2 + 0.5); | |
| 2032 | |
| 2033 // Ascent time (rounded up): | |
| 2034 time = (unsigned char)(0.1 * char_I_bottom_depth + 0.5); | |
| 2035 | |
| 2036 for(t=0; t<time; ++t) | |
| 2037 { | |
| 2038 deco_calc_CNS_fraction(); | |
| 2039 sim_dive_mins++; | |
| 2040 } | |
| 2041 | |
| 2042 //---- Do all further stops ------------------------------------------ | |
| 2043 for(i=0; i<NUM_STOPS; ++i) | |
| 2044 { | |
| 2045 overlay unsigned char switch_gas; | |
| 2046 | |
| 2047 //---- Get next stop, possibly in reverse order ------------------ | |
| 2048 { | |
| 2049 time = char_O_deco_time[(NUM_STOPS-1)-i]; | |
| 2050 temp_depth_limit = char_O_deco_depth[(NUM_STOPS-1)-i]; | |
| 2051 } | |
| 2052 if( time == 0 ) continue; | |
| 2053 | |
| 2054 //---- Gas Switch ? ---------------------------------------------- | |
| 2055 switch_gas = temp_depth_limit & 0x80; // Switch flag. | |
| 2056 temp_depth_limit &= 0x7F; // True stop depth. | |
| 2057 | |
| 2058 if( switch_gas ) | |
| 2059 { | |
| 2060 gas_switch_deepest(); | |
| 2061 gas_switch_set(); | |
| 2062 } | |
| 2063 | |
| 2064 //---- Convert Depth and N2_ratio to ppO2 ------------------------ | |
| 2065 actual_ppO2 = (pres_surface + temp_depth_limit * METER_TO_BAR) | |
| 2066 * (1.0 - calc_N2_ratio - calc_He_ratio); | |
| 2067 if( actual_ppO2 < 0.0 ) actual_ppO2 = 0.0; | |
| 2068 if( actual_ppO2 > 2.50 ) actual_ppO2 = 2.55; | |
| 2069 char_I_actual_ppO2 = (unsigned char)(100.0 * actual_ppO2 + 0.5); | |
| 2070 | |
| 2071 //---- Apply the stop | |
| 2072 for(t=0; t<time; ++t) | |
| 2073 { | |
| 2074 deco_calc_CNS_fraction(); | |
| 2075 sim_dive_mins++; | |
| 2076 } | |
| 2077 } | |
| 2078 } | |
| 2079 | |
| 2080 //---- Back to normal mode... -------------------------------------------- | |
| 2081 char_I_step_is_1min = 0; | |
| 2082 sim_gas_last_depth = backup_gas_last_depth; | |
| 2083 sim_gas_last_used = backup_gas_last_used; | |
| 2084 sim_dive_mins = backup_dive_mins; | |
| 2085 char_I_actual_ppO2 = backup_actual_ppO2; | |
| 2086 } | |
| 2087 | |
| 2088 ////////////////////////////////////////////////////////////////////////////// | |
| 2089 // deco_calc_CNS_decrease_15min | |
| 2090 // | |
| 2091 // new in v.101 | |
| 2092 // | |
| 2093 // calculates the half time of 90 minutes in 6 steps of 15 min | |
| 2094 // (Used in sleepmode, for low battery mode). | |
| 2095 // | |
| 2096 // Output: int_O_CNS_fraction | |
| 2097 // Uses and Updates: CNS_fraction | |
| 2098 // | |
| 2099 void deco_calc_CNS_decrease_15min(void) | |
| 2100 { | |
| 2101 RESET_C_STACK | |
| 2102 assert( 0.0 <= CNS_fraction && CNS_fraction <= 2.56 ); | |
| 2103 | |
| 2104 CNS_fraction = 0.890899 * CNS_fraction; | |
| 2105 int_O_CNS_fraction = (unsigned int)(CNS_fraction * 100.0 + 0.5); | |
| 2106 } | |
| 2107 | |
| 2108 ////////////////////////////////////////////////////////////////////////////// | |
| 2109 // deco_calc_percentage | |
| 2110 // | |
| 2111 // new in v.101 | |
| 2112 // | |
| 2113 // calculates int_I_temp * char_I_temp / 100 | |
| 2114 // output is int_I_temp | |
| 2115 // | |
| 2116 // Used to compute NoFly remaining time. | |
| 2117 // | |
| 2118 void deco_calc_percentage(void) | |
| 2119 { | |
| 2120 RESET_C_STACK | |
| 2121 | |
| 2122 assert( 60 <= char_I_temp && char_I_temp <= 100 ); | |
| 2123 assert( int_I_temp < 5760 ); // Less than 4 days = 96h... | |
| 2124 | |
| 2125 int_I_temp = (unsigned short)(((float)int_I_temp * (float)char_I_temp) * 0.01 ); | |
| 2126 | |
| 2127 assert( int_I_temp < 5760 ); // Less than 96h too... | |
| 2128 } | |
| 2129 | |
| 2130 | |
| 2131 ////////////////////////////////////////////////////////////////////////////// | |
| 2132 // deco_gas_volumes | |
| 2133 // | |
| 2134 // new in v.111 | |
| 2135 // | |
| 2136 // calculates volumes for each gas. | |
| 2137 // | |
| 2138 // Input: char_I_bottom_depth, char_I_bottom_time for planned dive. | |
| 2139 // Gas list. | |
| 2140 // char_I_first_gas is the bottom gas. | |
| 2141 // decoplan (char_O_deco_depth, char_O_deco_time). | |
| 2142 // CF#54 == TRUE if shallowest stop first. | |
| 2143 // CF#56 == bottom liters/minutes (5 .. 50) or bar/min. | |
| 2144 // CF#57 == deco liters/minutes (5 .. 50) or bar/min. | |
| 2145 // Output: int_O_gas_volumes[0..4] in litters * 0.1 | |
| 2146 // | |
| 2147 void deco_gas_volumes(void) | |
| 2148 { | |
| 2149 overlay float volumes[NUM_GAS]; | |
| 2150 overlay float bottom_usage, deco_usage; | |
| 2151 overlay unsigned char i, deepest_first; | |
| 2152 overlay unsigned char gas, depth; | |
| 2153 RESET_C_STACK | |
| 2154 | |
| 2155 //---- initialize with bottom consumption -------------------------------- | |
| 2156 for(i=0; i<NUM_GAS; ++i) // Nothing yet... | |
| 2157 volumes[i] = 0.0; | |
| 2158 | |
| 2159 assert(1 <= char_I_first_gas && char_I_first_gas <= NUM_GAS); | |
| 2160 gas = char_I_first_gas - 1; | |
| 2161 | |
| 2162 bottom_usage = (float) char_I_bottom_usage; | |
| 2163 if( char_I_const_ppO2 == 0 && bottom_usage > 0.0 ) | |
| 2164 volumes[gas] | |
| 2165 = (char_I_bottom_depth*0.1 + 1.0) // Use Psurface = 1.0 bar. | |
| 2166 * char_I_bottom_time // in minutes. | |
| 2167 * bottom_usage; // In liter/minutes. | |
| 2168 | |
| 2169 //---- Ascent usage ------------------------------------------------------ | |
| 2170 deco_usage = (float) char_I_deco_usage; // In liter/minutes. | |
| 2171 | |
| 2172 depth = char_I_bottom_depth; | |
| 2173 | |
| 2174 for(i=0; i<NUM_STOPS; ++i) | |
| 2175 { | |
| 2176 overlay unsigned char newDepth, time; | |
| 2177 | |
| 2178 // Manage stops in reverse order (CF#54) | |
| 2179 if( deepest_first ) | |
| 2180 { | |
| 2181 time = char_O_deco_time[i]; | |
| 2182 if( time == 0 ) break; // End of table: done. | |
| 2183 | |
| 2184 newDepth = char_O_deco_depth[i] & 0x7F; | |
| 2185 } | |
| 2186 else | |
| 2187 { | |
| 2188 time = char_O_deco_time[31-i]; | |
| 2189 if( time == 0 ) continue; // not yet: still search table. | |
| 2190 | |
| 2191 newDepth = char_O_deco_depth[31-i] & 0x7F; | |
| 2192 } | |
| 2193 | |
| 2194 //---- Gas switch during this step ----------------------------------- | |
| 2195 { | |
| 2196 overlay unsigned char newGas = 0; | |
| 2197 overlay unsigned char newStop = 0; // NO CHANGE yet | |
| 2198 overlay unsigned char j; | |
| 2199 | |
| 2200 for(j=0; j<NUM_GAS; ++j) | |
| 2201 { | |
| 2202 // Skip gas without changing depth: | |
| 2203 if( ! char_I_deco_gas_change[j] ) | |
| 2204 continue; | |
| 2205 // Select gas changed between [newDepth .. depth] | |
| 2206 if( newDepth <= char_I_deco_gas_change[j] | |
| 2207 && char_I_deco_gas_change[j] <= depth ) | |
| 2208 { | |
| 2209 // Keep the DEEPEST gas in that range: | |
| 2210 // Note: that = means changing gas at BEGINNING of this stop. | |
| 2211 if( char_I_deco_gas_change[j] >= newStop ) | |
| 2212 { | |
| 2213 newGas = j; | |
| 2214 newStop = char_I_deco_gas_change[j]; | |
| 2215 } | |
| 2216 } | |
| 2217 } | |
| 2218 | |
| 2219 if( newStop ) // Did we find something ? | |
| 2220 { | |
| 2221 // usage BEFORE gas switch (if any), at 10m/min : | |
| 2222 if( deco_usage > 0.0 && depth > newStop ) | |
| 2223 // Plus usage during ascent to the next stop, at 10m/min. | |
| 2224 volumes[gas] += ((depth+newStop)*0.05 + 1.0) // average depth --> bar. | |
| 2225 * (depth-newStop)*0.1 // metre --> min | |
| 2226 * deco_usage; | |
| 2227 | |
| 2228 // Do gas switch: | |
| 2229 gas = newGas; | |
| 2230 depth = newStop; | |
| 2231 } | |
| 2232 } | |
| 2233 | |
| 2234 // usage AFTER gas switch (if any), at 10m/min : | |
| 2235 if( depth > newDepth ) | |
| 2236 volumes[gas] += ((depth+newDepth)*0.05 + 1.0) // average depth --> bar. | |
| 2237 * (depth-newDepth)*0.1 // metre --> min | |
| 2238 * deco_usage; | |
| 2239 | |
| 2240 // Do stop: | |
| 2241 depth = newDepth; | |
| 2242 | |
| 2243 // Usage at stop: | |
| 2244 if( deco_usage > 0.0 ) | |
| 2245 volumes[gas] += (depth*0.1 + 1.0) // depth --> bar. | |
| 2246 * time // in minutes. | |
| 2247 * deco_usage; // in xxx / min @ 1bar. | |
| 2248 else | |
| 2249 volumes[gas] = 65535.0; | |
| 2250 } | |
| 2251 | |
| 2252 // From last stop to surface | |
| 2253 if( deco_usage > 0.0 ) | |
| 2254 volumes[gas] += (depth*0.05 + 1.0) // avg depth --> bar. | |
| 2255 * depth * 0.1 // time to surface, in minutes. | |
| 2256 * deco_usage; // in xxx / min @ 1bar. | |
| 2257 | |
| 2258 //---- convert results for the ASM interface ----------------------------- | |
| 2259 for(i=0; i<NUM_GAS; ++i) | |
| 2260 if( volumes[i] > 65534.0 ) | |
| 2261 int_O_gas_volumes[i] = 65535; | |
| 2262 else | |
| 2263 int_O_gas_volumes[i] = (unsigned short)(volumes[i] + 0.5); | |
| 2264 } | |
| 2265 | |
| 2266 ////////////////////////////////////////////////////////////////////////////// | |
| 2267 | |
| 2268 void deco_push_tissues_to_vault(void) | |
| 2269 { | |
| 2270 overlay unsigned char x; | |
| 2271 RESET_C_STACK | |
| 2272 | |
| 2273 cns_vault = CNS_fraction; | |
| 2274 low_depth_vault = low_depth; | |
| 2275 | |
| 2276 for (x=0;x<NUM_COMP;x++) | |
| 2277 { | |
| 2278 pres_tissue_N2_vault[x] = pres_tissue_N2[x]; | |
| 2279 pres_tissue_He_vault[x] = pres_tissue_He[x]; | |
| 2280 } | |
| 2281 } | |
| 2282 | |
| 2283 void deco_pull_tissues_from_vault(void) | |
| 2284 { | |
| 2285 overlay unsigned char x; | |
| 2286 RESET_C_STACK | |
| 2287 | |
| 2288 for (x=0; x<NUM_COMP; x++) | |
| 2289 { | |
| 2290 pres_tissue_N2[x] = pres_tissue_N2_vault[x]; | |
| 2291 pres_tissue_He[x] = pres_tissue_He_vault[x]; | |
| 2292 } | |
| 2293 | |
| 2294 // Restore both CNS variable, too. | |
| 2295 CNS_fraction = cns_vault; | |
| 2296 int_O_CNS_fraction = (unsigned int)(CNS_fraction * 100.0 + 0.5); | |
| 2297 | |
| 2298 // GF history too: | |
| 2299 low_depth = low_depth_vault; | |
| 2300 locked_GF_step = GF_delta / low_depth; | |
| 2301 } | |
| 2302 | |
| 2303 ////////////////////////////////////////////////////////////////////////////// | |
| 2304 // | |
| 2305 #ifndef CROSS_COMPILE | |
| 2306 void main() {} | |
| 2307 #endif |
