Mercurial > public > hwos_code
annotate src/p2_deco.c @ 656:8af5aefbcdaf default tip
Update to 3.31 beta
| author | heinrichsweikamp |
|---|---|
| date | Thu, 27 Nov 2025 18:32:58 +0100 |
| parents | 75e90cd0c2c3 |
| children |
| rev | line source |
|---|---|
| 582 | 1 // *************************************************************************** |
| 650 | 2 // p2_deco.c combined next generation V3.19.4 |
| 0 | 3 // |
| 4 // Created on: 12.05.2009 | |
| 654 | 5 // Author: heinrichs weikamp gmbh, contributions by Ralph Lembcke and others |
| 0 | 6 // |
| 582 | 7 // *************************************************************************** |
| 0 | 8 |
| 9 ////////////////////////////////////////////////////////////////////////////// | |
| 10 // OSTC - diving computer code | |
| 654 | 11 // Copyright (C) 2018 heinrichs weikamp gmbh GmbH |
| 0 | 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 | |
| 623 | 15 // the Free Software Foundation, either version 3 of the License, or |
| 0 | 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 | |
| 623 | 20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 0 | 21 // GNU General Public License for more details. |
| 22 // | |
| 23 // You should have received a copy of the GNU General Public License | |
| 623 | 24 // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 0 | 25 // |
| 26 ////////////////////////////////////////////////////////////////////////////// | |
| 27 | |
| 623 | 28 // History: |
| 0 | 29 // 01/03/08 v100: first release candidate |
| 30 // 03/13/08 v101: start of programming ppO2 code | |
| 560 | 31 // 03/13/25 v101a: backup of interim version with ppO2 calculation |
| 0 | 32 // 03/13/25 v101: open circuit gas change during deco |
| 623 | 33 // 03/13/25 v101: CNS_fraction_real calculation |
| 0 | 34 // 03/13/26 v101: optimization of tissue calc routines |
| 623 | 35 // 07/xx/2008 v102a: debug of bottom time routine |
| 36 // 09/xx/2008 v102d: Gradient Factor Model implementation | |
| 37 // 10/10/2008 v104: renamed to build v103 for v118 stable | |
| 631 | 38 // 10/14/2008 v104: integration of char_I_last_stop_depth for Gradient Model |
| 623 | 39 // 03/31/2009 v107: integration of FONT Incon24 |
| 40 // 05/23/2010 v109: 5 gas changes & 1 min timer | |
| 41 // 07/13/2010 v110: cns vault added | |
| 42 // 12/25/2010 v110: split in three files (deco.c, main.c, definitions.h) | |
| 0 | 43 // 2011/01/20: [jDG] Create a common file included in ASM and C code. |
| 623 | 44 // 2011/01/24: [jDG] Make ascent time an short. No more overflow! |
| 0 | 45 // 2011/01/25: [jDG] Fusion deco array for both models. |
| 46 // 2011/01/25: [jDG] Use CF(54) to reverse deco order. | |
| 47 // 2011/02/11: [jDG] Reworked gradient-factor implementation. | |
| 48 // 2011/02/15: [jDG] Fixed inconsistencies introduced by gas switch delays. | |
| 49 // 2011/03/21: [jDG] Added gas consumption (CF56 & CF57) evaluation for OCR mode. | |
| 631 | 50 // 2011/04/15: [jDG] Store GF_depth in 32 bits (w/o rounding), for a better stability. |
| 560 | 51 // 2011/04/25: [jDG] Added 1mn mode for CNS calculation, to allow it for deco planning. |
| 0 | 52 // 2011/04/27: [jDG] Fixed char_O_gradient_factor calculation when model uses gradient-factor. |
| 53 // 2011/05/02: [jDG] Added "Future TTS" function (CF58). | |
| 54 // 2011/05/17: [jDG] Various cleanups. | |
| 55 // 2011/08/08: [jDG] Computes CNS during deco planning ascent. | |
| 56 // 2011/11/24: [jDG] Slightly faster and better NDL computation. | |
| 57 // 2011/12/17: [mH] Remove of the useless debug stuff | |
| 58 // 2012/02/24: [jDG] Remove missed stop bug. | |
| 59 // 2012/02/25: [jDG] Looking for a more stable LOW grad factor reference. | |
| 60 // 2012/09/10: [mH] Fill char_O_deco_time_for_log for logbook write | |
| 623 | 61 // 2012/10/05: [jDG] Better calc_gas_needs_ascent accuracy (average depth, switch between stop). |
| 631 | 62 // 2013/03/05: [jDG] Should vault GF_depth too. |
| 0 | 63 // 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
|
64 // 2013/05/08: [jDG] A. Salm remark: NOAA tables for CNS are in ATA, not bar. |
| 560 | 65 // 2013/12/21: [jDG] Fix CNS calculation in deco plan w/o marked gas switch |
| 66 // 2014/06/16: [jDG] Fix Helium diluent. Fix volumes with many travel mix. | |
|
324
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
67 // 2014/06/29: [mH] Compute int_O_ceiling |
|
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
68 // 2015/06/12: [jDG] Fix NDL prediction while desaturating with the Buhlmann model. |
| 560 | 69 // 2017/08/04: [mH] Switch to absolute GF everywhere and apply safety margin parameters to both models (GF and non-GF), fixes from Ralph Lembcke |
| 70 // 2017/10/31: [rl] enhancements for pSCR mode and introduction of 2nd deco plan computation | |
| 71 // 2017/12/31: [rl] completion of 2nd deco plan computation and various up-fixes | |
| 584 | 72 // 2018/02/17: [rl] switch-over to new ceiling rounding (V2.98a) |
| 0 | 73 // |
| 74 // | |
| 75 // Literature: | |
| 313 | 76 // Buhlmann, Albert: Tauchmedizin; 4. Auflage [2002]; |
| 604 | 77 // Schroeder, Kai & Reith, Steffen; 2000; Saettigungsvorgaenge beim Tauchen, das Modell ZH-L16, Funktionsweise von Tauchcomputern; http://www.achim-und-kai.de/kai/tausim/saett_faq |
| 0 | 78 // Morrison, Stuart; 2000; DIY DECOMPRESSION; http://www.lizardland.co.uk/DIYDeco.html |
| 79 // Balthasar, Steffen; Dekompressionstheorie I: Neo Haldane Modelle; http://www.txfreak.de/dekompressionstheorie_1.pdf | |
| 80 // Baker, Erik C.; Clearing Up The Confusion About "Deep Stops" | |
| 81 // Baker, Erik C.; Understanding M-values; http://www.txfreak.de/understanding_m-values.pdf | |
| 604 | 82 |
| 83 | |
| 84 // ********************************************************************************************************************************* | |
| 0 | 85 // |
| 604 | 86 // I N C L U D E S |
| 87 // | |
| 88 // ********************************************************************************************************************************* | |
| 89 | |
| 623 | 90 |
| 604 | 91 #include <math.h> |
| 650 | 92 #include <string.h> |
| 582 | 93 #include "p2_definitions.h" |
| 94 #define TEST_MAIN | |
| 95 #include "shared_definitions.h" | |
| 623 | 96 #include "configuration.inc" |
| 0 | 97 |
| 560 | 98 |
| 650 | 99 // work-around for a C18 compiler bug (to avoid a warning been thrown for valid code) |
| 100 #define memcpy(a,b,c) memcpy((a),(const void*)(b),(c)) | |
| 101 | |
| 102 | |
| 604 | 103 // ********************************************************************************************************************************* |
| 104 // | |
| 105 // C O N S T A N T S D E F I N I T I O N S | |
| 106 // | |
| 107 // ********************************************************************************************************************************* | |
| 108 | |
| 623 | 109 |
| 110 // deco engine scheduling | |
| 111 #define INVOKES_PER_SECOND 2 // number of invocations of the deco engine per second (use powers of 2 only: 1, 2, 4, ...) | |
| 628 | 112 |
| 113 #ifdef _hwos_sport | |
| 114 #define BUDGET_PER_SECOND 320 // [ms] total time budget per second for the deco engine, each invocation will preempt after BUDGET_PER_SECOND / INVOKES_PER_SECOND | |
| 115 #else | |
| 623 | 116 #define BUDGET_PER_SECOND 640 // [ms] total time budget per second for the deco engine, each invocation will preempt after BUDGET_PER_SECOND / INVOKES_PER_SECOND |
| 628 | 117 #endif |
| 604 | 118 |
| 560 | 119 // ambient pressure at different mountain heights |
| 120 #define P_ambient_1000m 0.880 // [bar] based on 990 hPa and 20°C at sea level, 15°C at altitude | |
| 121 #define P_ambient_2000m 0.782 // [bar] | |
| 122 #define P_ambient_3000m 0.695 // [bar] | |
| 123 | |
| 124 // ambient pressure in aircraft cabin during flying - worst case according to Buhlmann | |
| 125 #define P_ambient_fly 0.600 // [bar], 0.600 bar is the value used by Buhlmann for his flying-after-diving calculations | |
| 582 | 126 // 0.735 bar is a typical cabin pressure for nowadays commercial jet aircrafts |
| 560 | 127 // ----- |
| 128 // 0.135 bar safety margin | |
| 129 | |
| 130 // constants and factors | |
| 631 | 131 #define ppWater 0.06270 // water vapor partial pressure in the lungs |
| 132 #define METER_TO_BAR 0.09807 // conversion factor (1 m water column = 0.09807 bar) | |
| 133 #define BAR_TO_METER 10.19716 // conversion factor (1 bar = 10.19716 m ) | |
| 134 #define SURFACE_DESAT_FACTOR 0.70420 // surface desaturation safety factor | |
| 135 #define HYST 1.0E-06 // threshold for tissue graphics on-gassing / off-gassing visualization | |
| 643 | 136 #define GAS_DENSITY_He_FACTOR 17.64 // in multiples of 0.01 grams per liter |
| 137 #define GAS_DENSITY_N2_FACTOR 123.46 // in multiples of 0.01 grams per liter | |
| 138 #define GAS_DENSITY_O2_FACTOR 141.02 // in multiples of 0.01 grams per liter | |
| 560 | 139 |
| 140 // thresholds | |
| 628 | 141 #define CNS_LIMIT_WARNING 100 // threshold for CNS warning |
| 142 #define CNS_LIMIT_ATTENTION 70 // threshold for CNS attention | |
| 604 | 143 #define PRESSURE_LIMIT_WARNING 200 // threshold for pressure reading warning : 20.0 bar |
| 144 #define PRESSURE_LIMIT_ATTENTION 500 // threshold for pressure reading attention: 50.0 bar | |
| 631 | 145 #define GAS_NEEDS_ATTENTION 0.7 // threshold for gas needs attention [1 = 100%] |
| 604 | 146 #define O2_CONSUMPTION_LIMIT_ATTENTION 20 // threshold for O2 "SAC" attention: 2.0 l/min |
| 643 | 147 #define ppO2_GAP_TO_SETPOINT 20 // gap between setpoint and max. ppO2 of the pure diluent [cbar] |
| 623 | 148 #define ppO2_MARGIN_ON_MAX 3 // [cbar] margin on ppO2 max to compensate for surface pressures > 1.000 mbar |
| 631 | 149 #define STOP_CHAINING_LIMIT 3 // max. number of chained stop table entries before deco calculation is aborted |
| 628 | 150 |
| 623 | 151 |
| 643 | 152 |
| 623 | 153 // deco engine states and modes - (char_O_)main_status: controls current tissue and deco status calculation (as-is situation) |
| 154 #define CALC_VOLUME 0x01 // =1: calculate gas needs | |
| 155 #define CALCULATE_BOTTOM 0x02 // =1: calculate gas needs in deco calculator mode, =0: in dive mode | |
| 631 | 156 #define CAVE_MODE 0x04 // =1: calculate return path and gas needs using backtracking data |
| 157 #define GAS_CONTINGENCY 0x08 // =1: use a second best gas if best gas is all used up | |
| 623 | 158 #define TR_FUNCTIONS 0x10 // =1: calculate TR functions (pressure reading) processing |
| 159 #define EXTENDED_STOPS 0x20 // =1: allow placement of gas switches below the depth of the 1st stop | |
| 160 | |
| 161 #define MODE_MASK 0xC0 // mask for real tissues mode selection | |
| 162 #define MODE_LOOP 0x40 // =1: CCR (MODE_PSCR needs to be cleared) or pSCR mode | |
| 163 #define MODE_CCR 0x40 // to be used with == operator in combination with MODE_MASK only! | |
| 164 #define MODE_PSCR 0x80 // =1: pSCR mode (MODE_LOOP needs to be set, too) | |
| 165 | |
| 166 // deco engine states and modes - (char_O_)deco_status: controls deco plan calculation (to-be scenario) | |
| 167 #define PLAN_MASK 0x03 // bit mask covering normal & alternative plan flag | |
| 168 #define COMMAND_MASK 0x07 // bit mask covering all command flags | |
| 169 #define CALCULATING 0x00 // calculations are ongoing | |
| 170 #define START_NORM 0x01 // input: start calculation of a normal deco plan | |
| 171 #define CALC_NORM 0x01 // internal: calculating a normal deco plan | |
| 172 #define COMPLETED_NORM 0x01 // output: calculation of a normal deco plan has completed | |
| 173 #define START_ALT 0x02 // input: start calculation of an alternative deco plan | |
| 174 #define CALC_ALT 0x02 // internal: calculating an alternative deco plan | |
| 175 #define COMPLETED_ALT 0x02 // output: calculation of an alternative deco plan has completed | |
| 176 #define INITIALIZE 0x04 // input: initialize deco engine | |
| 631 | 177 #define INITIALIZE_START_NORM 0x05 // input: initialize deco engine and start calculation of a normal deco plan |
| 623 | 178 #define INITIALIZE_START_ALT 0x06 // input: initialize deco engine and start calculation of an alternative deco plan |
| 628 | 179 #define DECO_CALCULATOR_MODE 0x08 // input: deco engine is run from deco calculator |
| 623 | 180 |
| 181 #define BAILOUT_MODE 0x10 // =1: allow gas switches before first deco stop | |
| 631 | 182 #define DELAYED_ASCENT 0x20 // =1: figure in a delayed ascent / delayed turn of the dive (fTTS) |
| 623 | 183 |
| 184 // MODE_MASK 0xC0 // mask for simulated tissues mode selection | |
| 185 // MODE_LOOP 0x40 // =1: CCR (MODE_PSCR needs to be cleared) or pSCR mode | |
| 186 // MODE_CCR 0x40 // to be used with == operator in combination with MODE_MASK only! | |
| 187 // MODE_PSCR 0x80 // =1: pSCR mode (MODE_LOOP needs to be set, too) | |
| 188 | |
| 189 | |
| 190 // deco engine warnings - (char_O_)deco_warnings | |
| 604 | 191 #define DECO_WARNING_IBCD 0x01 // IBCD occurring now |
| 192 #define DECO_WARNING_IBCD_lock 0x02 // IBCD has occurred during the dive | |
| 193 #define DECO_WARNING_MBUBBLES 0x04 // micro bubbles likely to develop now | |
| 194 #define DECO_WARNING_MBUBBLES_lock 0x08 // ditto, but sometime during the dive | |
| 195 #define DECO_WARNING_OUTSIDE 0x10 // tissue pressures outside the Buhlmann model now | |
| 196 #define DECO_WARNING_OUTSIDE_lock 0x20 // tissue pressures outside the model sometime during the dive | |
| 197 #define DECO_ATTENTION_OUTSIDE 0x40 // tissue pressures are very close to the Buhlmann limit | |
| 631 | 198 #define DECO_WARNING_INCOMPLETE 0x80 // deco calculation incomplete due to too long compute time |
| 604 | 199 |
| 623 | 200 // deco engine status (char_O_)deco_info |
| 628 | 201 #define DECO_MODE 0x01 // =1: deco ppO2 levels are permitted |
| 604 | 202 #define IND_DOUBLE_SWITCH_FLAG 0x02 // =1: switch to other tank advice active |
| 631 | 203 #define GAS_NEEDS_fTTS 0x04 // =1: gas needs are calculated in fTTS mode |
| 623 | 204 #define DECO_ZONE 0x08 // =1: fTTS < TTS (not updated when in bailout mode) |
| 631 | 205 #define DECO_CEILING 0x10 // =1: deco obligation (ceiling > 0) |
| 206 #define DECO_STOPS_NORM 0x20 // =1: deco stops found in normal plan | |
| 207 #define DECO_STOPS_ALT 0x40 // =1: deco stops found in alternative plan | |
| 208 #define GAS_NEEDS_CAVE 0x80 // =1: indicated gas needs are calculated in cave mode | |
| 623 | 209 |
| 604 | 210 |
| 211 // deco engine control - tissue_increment | |
| 631 | 212 #define TIME_MASK 0x7F // =0: time increment is 2 or 6 seconds, 1..127: time increments is 1..127 minutes |
| 213 #define TISSUE_SELECTOR 0x80 // =0: calculate on simulated tissues, 1 : calculate on real tissues | |
| 623 | 214 |
| 215 | |
| 216 // deco engine control - next_planning_phase | |
| 217 #define PHASE_00_DONE 0x00 // calculation cycle finished | |
| 218 #define PHASE_10_DIVE_INIT 0x10 // once-per-dive initialization of the deco engine | |
| 628 | 219 #define PHASE_20_CYCLIC_INIT 0x20 // once-every-cycle initialization of the deco engine |
| 220 #define PHASE_30_EXTENDED_BOTTOM_TIME 0x30 // calculate extended bottom time | |
| 221 #define PHASE_40_BOTTOM_GAS_NEED 0x40 // calculate gas needs for bottom segment | |
| 222 #define PHASE_50_NDL_TIME 0x50 // calculate NDL time | |
| 631 | 223 #define PHASE_70_ASCENT_OR_RETURN 0x70 // calculate open water ascent or cave return |
| 650 | 224 #define PHASE_80_RESULTS 0x80 // results - NDL, TTS, TST |
| 225 #define PHASE_81_GAS_NEEDS_PRESSURES 0x81 // results - convert gas needs from volumes to pressures | |
| 623 | 226 #define PHASE_90_FINISH 0x90 // finish calculation cycle |
| 227 | |
| 228 | |
| 631 | 229 // gas & diluent - type and availability state |
| 230 // 0x01 // | 0: disabled, 1: first, 2: normal/work, 3: deco | |
| 231 // 0x02 // | | |
| 232 #define GAS_TYPE_MASK 0x03 // bit mask covering the type enumerator | |
| 233 #define GAS_AVAIL_LOST 0x04 // =1: gas/diluent lost flag (permanently unavailable) | |
| 234 #define GAS_AVAIL_STAGED 0x08 // =1: gas/diluent staged flag (temporary unavailable) | |
| 235 #define GAS_AVAIL_MASK 0x0C // bit mask covering the availability flags | |
| 236 #define GAS_NEED_ATTENTION 0x10 // =1: gas need >= attention threshold | |
| 237 #define GAS_NEED_WARNING 0x20 // =1: gas need >= warning threshold | |
| 238 #define GAS_NEED_MASK 0x30 // bit mask covering the need flags | |
| 239 #define GAS_NEARLY_USED_UP 0x40 // =1: the gas is nearly used up (= at attention threshold) | |
| 240 #define GAS_FULLY_USED_UP 0x80 // =1: the gas is fully used up (= at warning threshold) | |
| 241 | |
| 242 | |
| 560 | 243 // flags used with integer numbers |
| 604 | 244 #define INT_FLAG_INVALID 0x0400 // =1: value not valid |
| 623 | 245 #define INT_FLAG_NOT_COMPUTED_YET 0x0800 // =1: value not computed yet |
| 604 | 246 #define INT_FLAG_ZERO 0x0800 // =1: value is zero |
| 247 #define INT_FLAG_LOW 0x1000 // =1: value is below a lower warning threshold | |
| 248 #define INT_FLAG_NOT_AVAIL 0x1000 // =1: value is not available (not computed) | |
| 249 #define INT_FLAG_HIGH 0x2000 // =1: value is above an upper warning threshold | |
| 250 #define INT_FLAG_OUTDATED 0x2000 // =1: value has not been updated for too long | |
| 251 #define INT_FLAG_ATTENTION 0x4000 // =1: value exceeds the attention threshold | |
| 252 #define INT_FLAG_WARNING 0x8000 // =1: value exceeds the warning threshold | |
| 253 #define INT_FLAG_OUT_OF_RANGE 0x8000 // =1: value exceeds presentable range | |
| 254 | |
| 255 | |
| 256 | |
| 257 // ********************************************************************************************************************************* | |
| 258 // | |
| 259 // ** P R O T O T Y P E S ** | |
| 260 // | |
| 261 // The Functions are listed in sequence of intended usage / application. | |
| 262 // | |
| 263 // ********************************************************************************************************************************* | |
| 264 | |
| 623 | 265 // Functions used in Surface Mode |
| 604 | 266 static void calc_interval(PARAMETER unsigned char time_increment); |
| 267 // Calculates the tissue off-gassing under surface conditions. | |
| 268 static void calc_desaturation_time(void); // Calculates the desaturation and no-fly times. | |
| 269 static void clear_tissue(void); // Resets all tissues to surface pressure equilibrium state. | |
| 623 | 270 static void init_output_vars(void); // Initializes all deco engine output variables to defaults |
| 271 | |
| 272 // Main entry point in Dive Mode | |
| 604 | 273 static void calc_hauptroutine(void); // Sequences all calculations for the real tissues and the deco calculation. |
| 274 | |
| 623 | 275 // Functions dedicated to the real Tissues |
| 604 | 276 static void calc_hauptroutine_data_input(void);// Initializes environment data and sets gas ratios for the real tissues. |
| 277 | |
| 623 | 278 // Functions combined for real Tissues & Deco Calculations |
| 604 | 279 static void calc_alveolar_pressures(void); // Computes the partial pressures from the gas ratios and many more parameters, |
| 280 // needs either calc_hauptroutine_data_input() be called beforehand or | |
| 628 | 281 // gas_take_current() or gas_find_best()/gas_take_best() and gas_set_ratios(). |
| 604 | 282 static void calc_tissues(void); // Updates the tissues dependent on the partial pressures of N2 and He. |
| 283 static void calc_CNS(void); // Updates the CNS value dependent on the partial pressure of the O2. | |
| 650 | 284 static void calc_limit(PARAMETER float GF_parameter); |
| 631 | 285 // Calculates ceiling, current supersaturation factor and some more data. |
| 604 | 286 |
| 623 | 287 // Functions for TR |
| 288 #ifdef _rx_functions | |
| 289 static void calc_TR_functions(void); // Calculates SAC etc. | |
| 290 #endif | |
| 291 | |
| 631 | 292 // Functions for Cave Mode |
| 293 #ifdef _cave_mode | |
| 294 static void read_backtrack_data(void); // Gets the data of the next backtracking data set | |
| 295 #endif | |
| 296 | |
| 623 | 297 // Functions dedicated to Deco Calculations |
| 604 | 298 static void clear_deco_table(void); // Clears the deco stops table, invoked at the start of each calculation cycle. |
| 631 | 299 static void gas_take_current(void); // Take the actual currently used gas for ascent & deco calculation |
| 628 | 300 static unsigned char gas_find_best(void); // Searches for the best gas available. |
| 301 static void gas_take_best(void); // Switches to the best gas that has been found found before by gas_find_best(). | |
| 604 | 302 static void gas_set_ratios(void); // Sets the gas ratios for use in deco calculation (simulated tissues), |
| 628 | 303 // needs to be called after each gas change (gas_take_current/_better). |
| 623 | 304 static void calc_NDL_time_tissue(void); // Calculates the remaining NDL time for a given tissue. |
| 305 static unsigned char find_next_stop(void); // Finds the next stop when in a deco ascent. | |
| 631 | 306 static void update_deco_table(PARAMETER unsigned char time_increment); |
| 604 | 307 // Enters a new stop or extends an existing stop in the deco stops table. |
| 631 | 308 static void calc_required_volume(void); // Calculates gas volume required for a given depth, time and usage (SAC rate). |
| 650 | 309 static void convert_volume_to_pressure(PARAMETER unsigned char index); |
| 310 // Converts gas volumes into pressures and sets respective flags. | |
| 623 | 311 |
| 312 // Functions for Results Reporting | |
| 604 | 313 static void publish_deco_table(void); // Copies the internal deco stops table to the export interface. |
| 623 | 314 static void convert_cur_CNS_for_display(void); // Converts the current CNS value from float to integer. |
| 604 | 315 static void convert_sim_CNS_for_display(void); // Converts the end-of-dive CNS value from float to integer. |
| 623 | 316 static void convert_sat_for_display(void); // Converts leading tissue saturation value from float to integer, 1.0 = 100%. |
| 604 | 317 static void convert_ceiling_for_display(void); // Converts ceiling from float to integer in mbar relative pressure. |
| 318 | |
| 319 | |
| 623 | 320 // internal helper Functions |
| 321 static void load_tmr5(void); // Loads a hardware timer which is used for preemptive scheduling. | |
| 322 static void read_tmr5(void); // Reads a hardware timer which is used for preemptive scheduling. | |
| 323 static void read_CNS_ab_coefficient(void); // Reads the CNS a and b coefficients from a ROM table. | |
| 324 static void read_CNS_c_coefficient(void); // Reads the CNS c coefficient from a ROM table. | |
| 325 static void read_Buhlmann_coefficients(void); // Reads the Buhlmann a and b coefficients from a ROM table. | |
| 650 | 326 static void read_Buhlmann_times(PARAMETER unsigned char period); |
| 604 | 327 // Reads pre-computed tissue increment factors from a ROM table. |
| 328 static void read_Buhlmann_ht(void); // Reads the half-times from a ROM table. | |
| 329 static void adopt_Buhlmann_coefficients(void); // Computes average a and b coefficient by the N2/He tissue ratio. | |
| 330 static void push_tissues_to_vault(void); // Stores the state of the real tissues during simulator runs. | |
| 331 static void pull_tissues_from_vault(void); // Restores the state of the real tissues after a simulator run. | |
| 628 | 332 static void calc_N2_equilibrium(void); // Calculate partial pressure of N2 in respired air at surface pressure. |
| 333 static void get_saturation_factors(void); // Get, safeguard and convert the saturation and desaturation factors. | |
| 334 static void apply_saturation_factors(void); // Applies saturation and desaturation factors. | |
| 604 | 335 |
| 336 | |
| 337 // ********************************************************************************************************************************* | |
| 338 // | |
| 339 // V A R I A B L E S D E F I N I T I O N S | |
| 340 // | |
| 341 // ********************************************************************************************************************************* | |
| 0 | 342 |
| 650 | 343 |
| 344 //---- Bank 13 parameters ----------------------------------------------------- | |
| 345 #ifndef UNIX | |
| 346 # pragma udata overlay bank13=0xd00 | |
| 347 static char C_STACK[256]; // C-code data stack | |
| 348 # define C_STACK_ADDR C_STACK | |
| 349 #endif | |
| 350 | |
| 351 // 256 byte used, bank is full | |
| 352 | |
| 353 | |
| 354 //---- Bank 7 parameters ----------------------------------------------------- | |
| 355 #ifndef UNIX | |
| 356 # pragma udata bank7=0x700 | |
| 357 #endif | |
| 358 | |
| 359 // tissue pressures for the real tissues (128 byte) | |
| 360 | |
| 361 static float real_pres_tissue_N2[NUM_COMP]; // 16 floats = 64 bytes || keep order and position of these variables as | |
| 362 static float real_pres_tissue_He[NUM_COMP]; // 16 floats = 64 bytes || they are backed-up to & restored from EEPROM! | |
| 363 | |
| 364 // delta-pressures for the real tissue pressures (128 byte) | |
| 365 | |
| 366 static float real_pres_delta_N2[NUM_COMP]; // 16 floats = 64 bytes | |
| 367 static float real_pres_delta_He[NUM_COMP]; // 16 floats = 64 bytes | |
| 368 | |
| 369 // 256 byte used, bank is full | |
| 370 | |
| 371 | |
| 372 //---- Bank 8 parameters ----------------------------------------------------- | |
| 373 #ifndef UNIX | |
| 374 # pragma udata bank8=0x800 | |
| 375 #endif | |
| 376 | |
| 377 // tissue pressures for the simulated tissues (128 byte) | |
| 378 | |
| 379 static float sim_pres_tissue_N2[NUM_COMP]; // 16 floats = 64 bytes | |
| 380 static float sim_pres_tissue_He[NUM_COMP]; // 16 floats = 64 bytes | |
| 381 | |
| 382 // delta-pressures for the simulated tissues (128 byte) | |
| 383 | |
| 384 static float sim_pres_delta_N2[NUM_COMP]; // 16 floats = 64 bytes | |
| 385 static float sim_pres_delta_He[NUM_COMP]; // 16 floats = 64 bytes | |
| 386 | |
| 387 // 256 byte used, bank is full | |
| 388 | |
| 389 | |
| 390 //---- Bank 12 parameters ----------------------------------------------------- | |
| 391 | |
| 392 #ifndef UNIX | |
| 393 # pragma udata bank12=0xc00 | |
| 394 #endif | |
| 395 | |
| 396 // vault for backing up real tissue pressures (128 byte) | |
| 397 | |
| 398 static float vault_pres_tissue_N2[NUM_COMP]; // 16 floats = 64 bytes | |
| 399 static float vault_pres_tissue_He[NUM_COMP]; // 16 floats = 64 bytes | |
| 400 | |
| 401 // vault for backing up real delta-pressures (128 byte) | |
| 402 | |
| 403 static float vault_pres_delta_N2[NUM_COMP]; // 16 floats = 64 bytes | |
| 404 static float vault_pres_delta_He[NUM_COMP]; // 16 floats = 64 bytes | |
| 405 | |
| 406 // 256 byte used, bank is full | |
| 407 | |
| 408 | |
| 0 | 409 //---- Bank 5 parameters ----------------------------------------------------- |
| 410 #ifndef UNIX | |
| 411 # pragma udata bank5=0x500 | |
| 412 #endif | |
| 413 | |
| 650 | 414 // Timer5 Interface (3 byte) - Attention: keep order and keep at beginning of bank 5, i.e. at address 0x500 ! |
| 415 | |
| 416 static volatile unsigned short tmr5_value; // | timer 5 value buffer MUST be at address 0x500 | |
| 417 static volatile unsigned char tmr5_overflow; // | timer 5 overflow flag MUST be at address 0x502 | |
| 631 | 418 |
| 419 | |
| 420 // Environmental and Gas Data (51 byte) | |
| 560 | 421 |
| 582 | 422 static float pres_surface; // absolute pressure at the surface |
| 560 | 423 |
| 628 | 424 static float float_depth_real; // current real depth in meters, float |
| 425 static unsigned char char_depth_real; // current real depth in meters, integer | |
| 631 | 426 static unsigned char char_depth_start; // start value of simulated depth in meters, integer |
| 628 | 427 static unsigned char char_depth_sim; // current value of simulated depth in meters, integer |
| 604 | 428 |
| 429 static float real_pres_respiration; // current real depth in absolute pressure | |
| 430 static float real_O2_ratio; // real breathed gas oxygen ratio | |
| 431 static float real_N2_ratio; // real breathed gas nitrogen ratio | |
| 432 static float real_He_ratio; // real breathed gas helium ratio | |
| 433 static float real_pSCR_drop; // real ppO2 drop in pSCR loop | |
| 582 | 434 |
| 435 static float sim_pres_respiration; // simulated current depth in abs.pressure, used for deco calculations | |
| 436 static float sim_O2_ratio; // simulated breathed gas oxygen ratio | |
| 437 static float sim_N2_ratio; // simulated breathed gas nitrogen ratio | |
| 438 static float sim_He_ratio; // simulated breathed gas helium ratio | |
| 439 static float sim_pSCR_drop; // simulated ppO2 drop in pSCR loop | |
| 560 | 440 |
| 604 | 441 |
| 631 | 442 // general Deco Parameters (64 byte) |
| 623 | 443 |
| 444 static float GF_low; // gradient factor to determine 1st stop | |
| 445 static float GF_high; // gradient factor to determine surfacing | |
| 446 | |
| 631 | 447 static unsigned char GF_low_last; // last GF low, used to detect a GF change |
| 448 static unsigned char GF_high_last; // last GF high, used to detect a GF change | |
| 449 | |
| 450 static unsigned char GF_depth; // GF low reference depth in current calculation cycle | |
| 451 static unsigned char GF_depth_norm; // GF low reference depth in normal plan | |
| 452 static unsigned char GF_depth_alt; // GF low reference depth in alternative plan | |
| 453 | |
| 454 static float GF_slope; // (GF_high - GF_low) / GF_depth in current calculation cycle | |
| 455 static float GF_slope_norm; // (GF_high - GF_low) / GF_depth_norm in normal plan | |
| 456 static float GF_slope_alt; // (GF_high - GF_low) / GF_depth_alt in alternative plan | |
| 457 | |
| 623 | 458 static float float_saturation_multiplier; // safety factor for on-gassing rates |
| 459 static float float_desaturation_multiplier; // safety factor for off-gassing rates | |
| 460 | |
| 461 static unsigned char split_N2_He[NUM_COMP]; // used for calculating the desaturation time | |
| 631 | 462 static unsigned char deco_gas_type[NUM_GAS]; // type and state of the deco gases |
| 463 static unsigned char peer_tank[NUM_GAS]; // bit flag vector indicating peer tanks holding same gas | |
| 464 | |
| 465 | |
| 650 | 466 // real Context: what we are doing now (16 byte) |
| 467 | |
| 468 static float CNS_fraction_real; // current real CNS (1.00 = 100%) | |
| 623 | 469 static unsigned short IBCD_tissue_vector; // 16 bit vector to memorize all tissues that experience IBCD |
| 470 | |
| 471 static float pres_respiration_sac; // used in SAC calculation: current depth in absolute pressure | |
| 472 static float float_sac; // used in SAC calculation: SAC value in float | |
| 473 static unsigned short max_sac_rate; // used in SAC calculation: threshold for SAC rate attention | |
| 474 | |
| 475 | |
| 476 // simulated Context: used to calculate Ascent (11 byte) | |
| 477 | |
| 478 static float CNS_fraction_sim; // CNS after predicted ascent, 0.01 = 1%, as float | |
| 479 static unsigned short int_sim_CNS_fraction; // CNS after predicted ascent, 1 = 1%, as integer | |
| 480 static unsigned char NDL_tissue_start_norm; // tissue to start with when calculating the normal NDL time | |
| 481 static unsigned char NDL_tissue_start_alt; // tissue to start with when calculating the alternative NDL time | |
| 482 static unsigned char NDL_tissue_start; // tissue to start with in current cycle | |
| 483 static unsigned char NDL_tissue_lead; // tissue with the shortest NDL time found in current cycle | |
| 484 static unsigned char NDL_tissue; // tissue for which the NDL is calculated right now | |
| 485 | |
| 628 | 486 |
| 623 | 487 // Result Values from Calculation Functions (9 byte) |
| 488 | |
| 489 static float ceiling; // minimum tolerated relative pressure (i.e. without surface pressure) | |
| 490 static float lead_supersat; // supersaturation of the leading tissue, 1.0 = 100% | |
| 631 | 491 static unsigned char lead_tissue; // number of the leading tissue (0-15) |
| 623 | 492 |
| 493 | |
| 494 // Transfer Variables between calc_desaturation_time() and calc_desaturation_time_helper() (18 byte) | |
| 495 | |
| 496 static float desat_factor; // used to cache a pre-computed factor | |
| 631 | 497 static float var_ht; // half-time factor for the compartment |
| 498 static float pres_target; // target pressure for the compartment | |
| 499 static float pres_actual; // current pressure of the compartment | |
| 623 | 500 static unsigned short int_time; // time it takes for the compartment to reach the target pressure |
| 501 | |
| 502 | |
| 650 | 503 // Gas in Use and Gas Needs (66 byte) |
| 631 | 504 |
| 505 static unsigned char start_gas_num; // number of the gas/dil to start with | |
| 628 | 506 |
| 507 static unsigned char sim_gas_last_num; // number of the last used gas | |
| 623 | 508 static unsigned char sim_gas_current_num; // number of the currently used gas |
| 509 static unsigned char sim_gas_current_depth; // change depth of the currently used gas | |
| 510 | |
| 631 | 511 static unsigned char sim_gas_best_num; // number of the best gas available |
| 512 static unsigned char sim_gas_best_depth; // change depth of the best gas available | |
| 628 | 513 |
| 631 | 514 static float gas_volume_need[NUM_GAS]; // gas volumes required for ascent / cave return in liters |
| 515 static float gas_volume_avail[NUM_GAS]; // gas volumes available for ascent / cave return in liters | |
| 516 static float gas_volume_atten[NUM_GAS]; // attention threshold for gas volumes available | |
| 517 | |
| 518 | |
| 519 // Transfer Variables for calc_required_volume() (7 byte) | |
| 628 | 520 |
| 521 static unsigned char gas_needs_depth; // depth of the stop or half-way point | |
| 631 | 522 static unsigned char gas_needs_time; // duration of the stop, ascent or travel phase |
| 628 | 523 static unsigned char gas_needs_usage_rate; // gas usage in l/min |
| 631 | 524 static float gas_needs_volume_due; // computed amount of required gas volume |
| 525 | |
| 526 | |
| 650 | 527 // Transfer Values for convert_float_to_int() (6 byte) |
| 528 | |
| 529 static float float_value; // input value, float | |
| 530 static unsigned short int_value; // output value, 16 bit | |
| 531 | |
| 532 | |
| 533 // 251 byte used, 5 byte left in this bank (4 bytes per float, 2 bytes per short, 1 byte per char) | |
| 631 | 534 |
| 535 | |
| 536 //---- Bank 6 parameters ----------------------------------------------------- | |
| 537 #ifndef UNIX | |
| 538 # pragma udata bank6=0x600 | |
| 539 #endif | |
| 540 | |
| 650 | 541 // Modes, Sequencing and Indexing (17 byte) |
| 631 | 542 |
| 543 static unsigned char main_status; // shadow register for char_O_main_status | |
| 544 static unsigned char deco_status; // shadow register for char_O_deco_status | |
| 545 static unsigned char deco_info; // shadow register for char_O_deco_info | |
| 546 static unsigned char deco_warnings; // shadow register for char_O_deco_warnings | |
| 547 static unsigned char next_planning_phase; // next calculation phase to be executed | |
| 548 static unsigned char tissue_increment; // selector for real/simulated tissues and time increment | |
| 549 static unsigned char sequence_timer; // timer to sequence deco engine tasks | |
| 550 static unsigned char ci; // index to the Buhlmann tables (compartment index) | |
| 551 static unsigned char cns_i; // index to the CNS tables (ppO2 range index) | |
| 552 static unsigned char i; // general purpose loop counter and index | |
| 553 static unsigned char j; // general purpose loop counter and index | |
| 554 static unsigned char stop_index; // current stop table position | |
| 555 static unsigned char chained_stops; // counter for chained stop entries | |
| 556 static unsigned char backtrack_index; // index into the depth backtracking array char_I_backtrack_storage | |
| 557 static unsigned char backtrack_target_depth; // current backtracking target depth | |
| 558 static unsigned char backtrack_step_counter; // counter for number of 1/10 minute steps done | |
| 650 | 559 static unsigned char spare; // UNUSED YET, placed here for alignment purpose |
| 560 | |
| 561 | |
| 562 // Result Values from Calculation Functions (30 byte) | |
| 631 | 563 |
| 564 static float ppO2_O2; // ppO2 calculated for breathing pure oxygen in OC mode | |
| 565 static float ppO2_OC; // ppO2 calculated for breathing current gas in OC mode | |
| 566 static float ppO2_pSCR; // ppO2 calculated for breathing current gas in pSCR mode | |
| 567 | |
| 568 static float ppO2; // partial pressure of breathed oxygen | |
| 569 static float ppN2; // partial pressure of breathed nitrogen | |
| 570 static float ppHe; // partial pressure of breathed helium | |
| 571 | |
| 572 static unsigned char char_ppO2; // partial pressure of breathed oxygen, 100 = 1.00 bar | |
| 573 static unsigned char NDL_time; // time in full minutes until reaching no-deco limit (NDL) | |
| 574 static unsigned short TTS_time; // time in 1/10 minutes until finishing ascent / cave return | |
| 575 static unsigned short TST_time; // time in full minutes of all stops in ascent / cave return | |
| 576 | |
| 577 | |
| 578 // Buhlmann Model Parameters (40 byte) | |
| 579 | |
| 580 static float var_N2_a; // Buhlmann a for current N2 tissue | |
| 581 static float var_N2_b; // Buhlmann b for current N2 tissue | |
| 582 static float var_He_a; // Buhlmann a for current He tissue | |
| 583 static float var_He_b; // Buhlmann b for current He tissue | |
| 584 static float var_a; // Buhlmann a adopted to current N2/He ratio | |
| 585 static float var_b; // Buhlmann b adopted to current N2/He ratio | |
| 586 static float var_N2_e; // exposition for current N2 tissue | |
| 587 static float var_He_e; // exposition for current He tissue | |
| 588 static float var_N2_ht; // half-time for current N2 tissue | |
| 589 static float var_He_ht; // half-time for current He tissue | |
| 590 | |
| 591 | |
| 623 | 592 // Auxiliary Variables for Data Buffering (28 byte) |
| 593 | |
| 594 static float N2_equilibrium; // used for N2 tissue graphics scaling | |
| 595 static float temp_tissue; // auxiliary variable to buffer tissue pressures | |
| 596 static float float_pSCR_factor; // pre-computed factor for pSCR ppO2 drop calculation | |
| 597 static float calc_pres_tissue_N2; // auxiliary variable to buffer tissue N2 pressure | |
| 598 static float calc_pres_tissue_He; // auxiliary variable to buffer tissue He pressure | |
| 650 | 599 static float calc_pres_tissue; // auxiliary variable to buffer total tissue pressure |
| 623 | 600 static float old_pres_respiration; // auxiliary variable to buffer sim_pres_respiration |
| 601 | |
| 602 | |
| 650 | 603 // CNS Coefficients (10 byte) |
| 604 | |
| 605 static float var_cns_gain; // two coefficients approximation, gain | |
| 606 static float var_cns_offset; // two coefficients approximation, offset | |
| 607 static unsigned short var_cns_value; // one coefficient approximation, value | |
| 608 | |
| 609 | |
| 610 // Vault to back-up & restore Tissue related Data (6 byte) | |
| 611 | |
| 612 static float vault_CNS_fraction_real; // stores CNS percentage (1.0 = 100%) | |
| 613 static unsigned char vault_deco_warnings; // stores warnings status | |
| 614 static unsigned char vault_deco_info; // stores info status | |
| 615 | |
| 616 | |
| 617 // stops table (96 byte) | |
| 618 | |
| 619 static unsigned char internal_deco_depth[NUM_STOPS]; // depths of the stops in meters | |
| 620 static unsigned char internal_deco_time[NUM_STOPS]; // durations of the stops in minutes | |
| 621 static unsigned char internal_deco_gas[NUM_STOPS]; // gases used on the stops (0 / 1-5) | |
| 628 | 622 |
| 623 | |
| 624 // Performance Profiling (4 byte) | |
| 625 | |
| 626 static unsigned short profiling_runtime; // performance measurement: runtime of current invocation | |
| 627 static unsigned char profiling_runs; // performance measurement: invocations per deco calculation cycle | |
| 628 static unsigned char profiling_phase; // performance measurement: current calculation phase | |
| 629 | |
| 630 | |
| 650 | 631 // Function Parameters placed by the C Compiler (7 byte) |
| 632 /* | |
| 633 static unsigned char period | |
| 634 static float parameter | |
| 635 static unsigned char time_increment | |
| 636 static unsigned char time_interval | |
| 637 */ | |
| 638 | |
| 639 | |
| 640 // 238 byte used, 18 byte left in this bank (4 bytes per float, 2 bytes per short, 1 byte per char) | |
| 641 | |
| 0 | 642 |
| 582 | 643 |
| 604 | 644 // ********************************************************************************************************************************* |
| 645 // | |
| 646 // L O O K - U P T A B L E S | |
| 647 // | |
| 648 // ********************************************************************************************************************************* | |
| 582 | 649 |
| 0 | 650 #ifndef UNIX |
| 631 | 651 # pragma romdata Buhlmann_ht = 0x1DC00 // needs to be in the UPPER bank |
| 652 #endif | |
| 653 | |
| 654 rom const float Buhlmann_ht[2*16] = { | |
| 655 // Compartment half-times, in minutes | |
| 656 //--- N2 ---- He ---------------------- | |
| 657 4.0, 1.51, | |
| 658 8.0, 3.02, | |
| 659 12.5, 4.72, | |
| 660 18.5, 6.99, | |
| 661 27.0, 10.21, | |
| 662 38.3, 14.48, | |
| 663 54.3, 20.53, | |
| 664 77.0, 29.11, | |
| 665 109.0, 41.20, | |
| 666 146.0, 55.19, | |
| 667 187.0, 70.69, | |
| 668 239.0, 90.34, | |
| 669 305.0, 115.29, | |
| 670 390.0, 147.42, | |
| 671 498.0, 188.24, | |
| 672 635.0, 240.03 | |
| 673 }; | |
| 674 | |
| 675 | |
| 676 #ifndef UNIX | |
| 623 | 677 # pragma romdata CNS_tables = 0x1DC80 // needs to be in the UPPER bank |
| 678 #endif | |
| 679 | |
| 631 | 680 rom const float CNS_2_approx[2*11] = { |
| 681 // 2 coefficient approximation for ppO2 = 51 ... 160 cbar | |
| 682 // CNS increment per 2 sec = 1 / (gain*ppO2 + offset) with ppO2 in [cbar] | |
| 683 // gain offset for ppO2 cbar range | |
| 623 | 684 -533.07, 54000, // 51 - 60 (index 0) |
| 685 -444.22, 48600, // 61 - 70 (index 1) | |
| 686 -355.38, 42300, // 71 - 80 (index 2) | |
| 687 -266.53, 35100, // 81 - 90 (index 3) | |
| 688 -177.69, 27000, // 91 - 100 (index 4) | |
| 689 -177.69, 27000, // 101 - 110 (index 5) | |
| 690 -88.84, 17100, // 111 - 120 (index 6) | |
| 691 -88.84, 17100, // 121 - 130 (index 7) | |
| 692 -88.84, 17100, // 131 - 140 (index 8) | |
| 693 -88.84, 17100, // 141 - 150 (index 9) | |
| 694 -222.11, 37350 // 151 - 160 (index 10) | |
| 695 }; | |
| 696 | |
| 631 | 697 rom const unsigned short CNS_1_approx[1*18] = { |
| 698 // 1 coefficient approximation for ppO2 = 161 ... 250 cbar | |
| 699 // CNS increment per 2 sec = c / 100000.0 | |
| 700 // value in [1/100000] for ppO2 cbar range | |
| 623 | 701 75, // 161 - 165 (index 0) |
| 702 102, // 166 - 170 (index 1) | |
| 703 136, // 171 - 175 (index 2) | |
| 704 180, // 176 - 180 (index 3) | |
| 705 237, // 181 - 185 (index 4) | |
| 706 310, // 186 - 190 (index 5) | |
| 707 401, // 191 - 195 (index 6) | |
| 708 517, // 196 - 200 (index 7) | |
| 709 760, // 201 - 205 (index 8) | |
| 710 1100, // 206 - 210 (index 9) | |
| 711 1500, // 211 - 215 (index 10) | |
| 712 2090, // 216 - 220 (index 11) | |
| 713 2900, // 221 - 225 (index 12) | |
| 714 3900, // 226 - 230 (index 13) | |
| 715 4820, // 231 - 235 (index 14) | |
| 716 4820, // 236 - 240 (index 15) | |
| 717 4820, // 241 - 245 (index 16) | |
| 628 | 718 4820 // 246 - 250 (index 17) |
| 623 | 719 }; |
| 720 | |
| 721 | |
| 722 #ifndef UNIX | |
| 631 | 723 # pragma romdata Buhlmann_ab = 0x1DD00 // needs to be in the UPPER bank |
| 0 | 724 #endif |
| 725 | |
| 560 | 726 rom const float Buhlmann_ab[4*16] = { |
| 631 | 727 // Compartment a and b factors |
|
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
728 // Data ZH-L16C, from Bühlmann Tauchmedizin 2002, option 1a (4mn) |
| 631 | 729 // a for N2 b for N2 a for He b for He |
|
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
730 1.2599, 0.5050, 1.7424, 0.4245, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
731 1.0000, 0.6514, 1.3830, 0.5747, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
732 0.8618, 0.7222, 1.1919, 0.6527, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
733 0.7562, 0.7825, 1.0458, 0.7223, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
734 0.6200, 0.8126, 0.9220, 0.7582, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
735 0.5043, 0.8434, 0.8205, 0.7957, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
736 0.4410, 0.8693, 0.7305, 0.8279, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
737 0.4000, 0.8910, 0.6502, 0.8553, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
738 0.3750, 0.9092, 0.5950, 0.8757, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
739 0.3500, 0.9222, 0.5545, 0.8903, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
740 0.3295, 0.9319, 0.5333, 0.8997, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
741 0.3065, 0.9403, 0.5189, 0.9073, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
742 0.2835, 0.9477, 0.5181, 0.9122, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
743 0.2610, 0.9544, 0.5176, 0.9171, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
744 0.2480, 0.9602, 0.5172, 0.9217, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
745 0.2327, 0.9653, 0.5119, 0.9267 |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
746 }; |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
747 |
| 631 | 748 |
| 749 #ifndef UNIX | |
| 750 # pragma romdata e_tables = 0x1DE00 // needs to be in the UPPER bank | |
| 751 #endif | |
| 752 | |
| 753 rom const float e2sec[2*16] = { | |
| 754 // Integration constants for 2 seconds, | |
| 755 // result of 1 - 2^(-(2sec/60sec / HT)) | |
|
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
756 //---- N2 ------------- He ------------ |
| 623 | 757 5.75958E-03, 1.51848E-02, |
|
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
758 2.88395E-03, 7.62144E-03, |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
759 1.84669E-03, 4.88315E-03, |
| 582 | 760 1.24813E-03, 3.29997E-03, |
| 761 8.55371E-04, 2.26041E-03, | |
| 762 6.03079E-04, 1.59437E-03, | |
| 763 4.25414E-04, 1.12479E-03, | |
| 764 3.00019E-04, 7.93395E-04, | |
| 765 2.11949E-04, 5.60641E-04, | |
| 766 1.58240E-04, 4.18555E-04, | |
| 767 1.23548E-04, 3.26795E-04, | |
| 768 9.66686E-05, 2.55722E-04, | |
| 769 7.57509E-05, 2.00387E-04, | |
| 770 5.92416E-05, 1.56716E-04, | |
| 771 4.63943E-05, 1.22734E-04, | |
| 772 3.63850E-05, 9.62538E-05 | |
|
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
773 //------------------------------------- |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
774 }; |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
775 |
| 631 | 776 rom const float e6sec[2*16] = { |
| 777 // Integration constants for 6 seconds, | |
| 778 // result of 1 - 2^(-(6sec/60sec / HT)) | |
| 779 //---- N2 ------------- He ------------ | |
| 780 1.71794E-02, 4.48661E-02, | |
| 781 8.62691E-03, 2.26905E-02, | |
| 782 5.52983E-03, 1.45780E-02, | |
| 783 3.73973E-03, 9.86726E-03, | |
| 784 2.56392E-03, 6.76591E-03, | |
| 785 1.80815E-03, 4.77549E-03, | |
| 786 1.27570E-03, 3.37057E-03, | |
| 787 8.99786E-04, 2.37830E-03, | |
| 788 6.35713E-04, 1.68098E-03, | |
| 789 4.74646E-04, 1.25514E-03, | |
| 790 3.70598E-04, 9.80064E-04, | |
| 791 2.89978E-04, 7.66971E-04, | |
| 792 2.27236E-04, 6.01040E-04, | |
| 793 1.77714E-04, 4.70075E-04, | |
| 794 1.39176E-04, 3.68157E-04, | |
| 795 1.09151E-04, 2.88734E-04 | |
| 796 //------------------------------------- | |
| 797 }; | |
| 798 | |
|
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
799 rom const float e1min[2*16] = { |
| 631 | 800 // Integration constants for 1 minute, |
| 801 // result of 1 - 2^(-(1min / HT)) | |
|
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
802 //----- N2 --------- e 1min He -------- |
| 582 | 803 1.59104E-01, 3.68109E-01, |
| 804 8.29960E-02, 2.05084E-01, | |
| 805 5.39424E-02, 1.36579E-01, | |
| 806 3.67742E-02, 9.44046E-02, | |
| 807 2.53454E-02, 6.56359E-02, | |
| 808 1.79351E-02, 4.67416E-02, | |
| 809 1.26840E-02, 3.31991E-02, | |
| 810 8.96152E-03, 2.35301E-02, | |
| 811 6.33897E-03, 1.66832E-02, | |
| 812 4.73633E-03, 1.24808E-02, | |
| 813 3.69981E-03, 9.75753E-03, | |
| 814 2.89600E-03, 7.64329E-03, | |
| 815 2.27003E-03, 5.99417E-03, | |
| 816 1.77572E-03, 4.69082E-03, | |
| 817 1.39089E-03, 3.67548E-03, | |
| 818 1.09097E-03, 2.88359E-03 | |
|
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
819 //------------------------------------- |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
820 }; |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
821 |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
822 rom const float e10min[2*16] = { |
| 631 | 823 // Integration constants for 10 minutes, |
| 824 // result of 1 - 2^(-(10min / HT)) | |
|
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
825 //---- N2 -------------- He ----------- |
| 582 | 826 8.23223E-01, 9.89851E-01, |
| 827 5.79552E-01, 8.99258E-01, | |
| 828 4.25651E-01, 7.69737E-01, | |
| 829 3.12487E-01, 6.29027E-01, | |
| 830 2.26416E-01, 4.92821E-01, | |
| 831 1.65547E-01, 3.80407E-01, | |
| 832 1.19840E-01, 2.86538E-01, | |
| 833 8.60863E-02, 2.11886E-01, | |
| 834 6.16117E-02, 1.54849E-01, | |
| 835 4.63665E-02, 1.18026E-01, | |
| 836 3.63881E-02, 9.34005E-02, | |
| 837 2.85855E-02, 7.38569E-02, | |
| 838 2.24698E-02, 5.83504E-02, | |
| 839 1.76160E-02, 4.59303E-02, | |
| 840 1.38222E-02, 3.61528E-02, | |
| 841 1.08563E-02, 2.84646E-02 | |
|
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
842 //------------------------------------- |
|
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
843 }; |
| 0 | 844 |
| 604 | 845 |
| 846 // ********************************************************************************************************************************* | |
| 0 | 847 // |
| 604 | 848 // H E L P E R F U N C T I O N S |
| 849 // | |
| 850 // ********************************************************************************************************************************* | |
| 851 | |
| 852 | |
| 628 | 853 // p2deco code moved from 0x0D000 to 0x0C000 in v.108 |
| 634 | 854 // moved from 0x0C000 to 0x0B000 in v3.09 |
| 0 | 855 #ifndef UNIX |
| 634 | 856 # pragma code p2_deco = 0x0B000 |
| 0 | 857 #endif |
| 858 | |
| 859 | |
| 860 ////////////////////////////////////////////////////////////////////////////// | |
| 861 // Bump to blue-screen when an assert is wrong | |
| 623 | 862 #ifdef _DEBUG |
| 0 | 863 void assert_failed(PARAMETER short int line) |
| 864 { | |
| 865 } | |
| 866 #endif | |
| 867 | |
| 604 | 868 |
| 0 | 869 ////////////////////////////////////////////////////////////////////////////// |
| 628 | 870 // When calling C code from ASM context, the C data stack pointer need to be |
| 871 // reset. The C stack is located in bank 8. | |
| 0 | 872 |
| 873 #ifdef CROSS_COMPILE | |
| 584 | 874 # define RESET_C_STACK |
| 0 | 875 #else |
| 623 | 876 # ifdef _DEBUG |
| 584 | 877 # define RESET_C_STACK fillDataStack(); |
| 878 void fillDataStack(void) | |
| 879 { | |
| 880 _asm | |
| 650 | 881 LFSR 1,C_STACK_ADDR |
| 604 | 882 MOVLW 0xCC |
| 883 loop: MOVWF POSTINC1,0 | |
| 884 TSTFSZ FSR1L,0 | |
| 885 BRA loop | |
| 886 | |
| 650 | 887 LFSR 1,C_STACK_ADDR |
| 888 LFSR 2,C_STACK_ADDR | |
| 584 | 889 _endasm |
| 890 } | |
| 891 # else | |
| 650 | 892 # define RESET_C_STACK \ |
| 893 _asm \ | |
| 894 LFSR 1,C_STACK_ADDR \ | |
| 895 LFSR 2,C_STACK_ADDR \ | |
| 584 | 896 _endasm |
| 897 # endif | |
| 0 | 898 #endif |
| 899 | |
| 604 | 900 |
| 0 | 901 ////////////////////////////////////////////////////////////////////////////// |
| 623 | 902 // Reset timer 5 |
| 903 // | |
| 904 // Note: TMR5 is configured in 16 bit mode: a value written to TMR5H is buffered | |
| 905 // and will be written to TMR5 together with a successive write to TMR5L. | |
| 906 // As we don't know in which bank the code will be executed, we use either | |
| 907 // the bank-save "movff" command, or address mapping via access bank (",0"). | |
| 908 // | |
| 909 static void load_tmr5(void) | |
| 0 | 910 { |
| 911 #ifndef CROSS_COMPILE | |
| 584 | 912 _asm |
| 650 | 913 movff 0x501,0xF7D // bank-safe load TMR5H from C variable tmr5_value first |
| 914 movff 0x500,0xF7C // bank-safe load TMR5L from C variable tmr5_value thereafter | |
| 623 | 915 bcf 0xFBA,1,0 // clear timer 5 overrun flag (0xFBA = PIR5, bit 1 = TMR5IF) |
| 916 _endasm | |
| 0 | 917 #else |
| 623 | 918 return; |
| 919 #endif | |
| 920 } | |
| 921 | |
| 922 | |
| 923 ////////////////////////////////////////////////////////////////////////////// | |
| 924 // Read timer 5 | |
| 925 // | |
| 926 // Note: TMR5 reads in multiples of 1/32 ms, 30.51757813 us/bit to be precise. | |
| 927 // TMR5 is configured in 16 bit mode: on reading of TMR5L the contents of | |
| 928 // TMR5H is latched and can be read afterwards without potential rollover. | |
| 929 // As we don't know in which bank the code will be executed, we use either | |
| 930 // the bank-save "movff" command, or address mapping via access bank (",0"). | |
| 931 // | |
| 932 static void read_tmr5(void) | |
| 933 { | |
| 934 #ifndef CROSS_COMPILE | |
| 935 _asm | |
| 650 | 936 movff 0xF7C,0x500 // copy TMR5L to C variable tmr5_value, low byte first |
| 937 movff 0xF7D,0x501 // copy TMR5H to C variable tmr5_value, high byte thereafter | |
| 623 | 938 clrf WREG,0 // clear WREG to 0x00 = no overrun by default |
| 939 btfsc 0xFBA,1,0 // did timer 5 overrun? (0xFBA = PIR5, bit 1 = TMR5IF) | |
| 940 setf WREG,0 // YES - set WREG to 0xff = overrun detected | |
| 650 | 941 movff WREG,0x502 // copy WREG to C variable tmr5_overflow |
| 623 | 942 _endasm |
| 943 #else | |
| 944 return; | |
| 0 | 945 #endif |
| 946 } | |
| 947 | |
| 604 | 948 |
| 0 | 949 ////////////////////////////////////////////////////////////////////////////// |
| 631 | 950 // Read CNS coefficients gain and offset |
| 623 | 951 // |
| 952 static void read_CNS_ab_coefficient(void) | |
| 953 { | |
| 954 #ifndef CROSS_COMPILE | |
| 955 // Note: We don't use far ROM pointer, because handling | |
| 956 // 24 bit is to complex, hence we have to set the | |
| 957 // UPPER page ourself... | |
| 958 // -> set to zero if tables are moved to lower pages! | |
| 959 _asm | |
| 960 movlw 1 | |
| 961 movwf TBLPTRU,0 | |
| 962 _endasm | |
| 963 #endif | |
| 964 | |
| 965 { | |
| 631 | 966 overlay rom const float* ptr = &CNS_2_approx[2*cns_i]; |
| 967 var_cns_gain = *ptr++; | |
| 968 var_cns_offset = *ptr++; | |
| 623 | 969 } |
| 970 } | |
| 971 | |
| 972 | |
| 973 ////////////////////////////////////////////////////////////////////////////// | |
| 974 // Read CNS coefficient c | |
| 975 // | |
| 976 static void read_CNS_c_coefficient(void) | |
| 977 { | |
| 978 #ifndef CROSS_COMPILE | |
| 979 // Note: We don't use far ROM pointer, because handling | |
| 980 // 24 bit is to complex, hence we have to set the | |
| 981 // UPPER page ourself... | |
| 982 // -> set to zero if tables are moved to lower pages! | |
| 983 _asm | |
| 984 movlw 1 | |
| 985 movwf TBLPTRU,0 | |
| 986 _endasm | |
| 987 #endif | |
| 988 | |
| 989 { | |
| 631 | 990 overlay rom const unsigned short* ptr = &CNS_1_approx[cns_i]; |
| 991 var_cns_value = *ptr++; | |
| 623 | 992 } |
| 993 } | |
| 994 | |
| 995 ////////////////////////////////////////////////////////////////////////////// | |
| 996 // Read Buhlmann coefficients a and b for compartment ci | |
| 0 | 997 // |
| 560 | 998 static void read_Buhlmann_coefficients(void) |
| 0 | 999 { |
| 1000 #ifndef CROSS_COMPILE | |
| 604 | 1001 // Note: We don't use far ROM pointer, because handling |
| 1002 // 24 bit is too complex, hence we have to set the | |
| 1003 // UPPER page ourself... | |
| 628 | 1004 // -> set to zero if tables are moved to lower pages! |
| 584 | 1005 _asm |
| 604 | 1006 movlw 1 |
| 1007 movwf TBLPTRU,0 | |
| 584 | 1008 _endasm |
| 0 | 1009 #endif |
| 1010 | |
| 584 | 1011 assert( ci < NUM_COMP ); |
| 1012 | |
| 604 | 1013 // use an interleaved array (AoS) to access coefficients with a single addressing |
| 584 | 1014 { |
| 1015 overlay rom const float* ptr = &Buhlmann_ab[4*ci]; | |
| 1016 var_N2_a = *ptr++; | |
| 1017 var_N2_b = *ptr++; | |
| 1018 var_He_a = *ptr++; | |
| 1019 var_He_b = *ptr++; | |
| 1020 } | |
| 0 | 1021 } |
| 1022 | |
| 604 | 1023 |
| 0 | 1024 ////////////////////////////////////////////////////////////////////////////// |
| 623 | 1025 // Read Buhlmann increments for compartment ci |
| 604 | 1026 // If period == 0 : 2 sec interval |
| 1027 // 1 : 1 min interval | |
| 1028 // 2 : 10 min interval | |
| 650 | 1029 static void read_Buhlmann_times(PARAMETER unsigned char period) |
| 0 | 1030 { |
| 1031 #ifndef CROSS_COMPILE | |
| 604 | 1032 // Note: We don't use far ROM pointer, because handling |
| 1033 // 24 bit is to complex, hence we have to set the | |
| 631 | 1034 // UPPER page by hand... |
| 604 | 1035 // -> set to zero if tables are moved to lower pages! |
| 584 | 1036 _asm |
| 604 | 1037 movlw 1 |
| 1038 movwf TBLPTRU,0 | |
| 584 | 1039 _endasm |
| 0 | 1040 #endif |
| 1041 | |
| 584 | 1042 assert( ci < NUM_COMP ); |
| 1043 | |
| 623 | 1044 // Integration Intervals |
| 584 | 1045 switch(period) |
| 1046 { | |
| 631 | 1047 case 0: //---- 2 or 6 seconds -------------------------------------------- |
| 584 | 1048 { |
| 631 | 1049 // check which tissues are selected |
| 1050 if(tissue_increment & TISSUE_SELECTOR) | |
| 1051 { | |
| 1052 // real tissues - 2 seconds | |
| 1053 overlay rom const float* ptr = &e2sec[2*ci]; | |
| 1054 var_N2_e = *ptr++; | |
| 1055 var_He_e = *ptr++; | |
| 1056 } | |
| 1057 else | |
| 1058 { | |
| 1059 // simulated tissues - 6 seconds | |
| 1060 overlay rom const float* ptr = &e6sec[2*ci]; | |
| 1061 var_N2_e = *ptr++; | |
| 1062 var_He_e = *ptr++; | |
| 1063 } | |
| 584 | 1064 } |
| 1065 break; | |
| 1066 | |
| 631 | 1067 case 1: //---- 1 minutes ------------------------------------------------- |
| 584 | 1068 { |
| 1069 overlay rom const float* ptr = &e1min[2*ci]; | |
| 1070 var_N2_e = *ptr++; | |
| 1071 var_He_e = *ptr++; | |
| 1072 } | |
| 1073 break; | |
| 1074 | |
| 631 | 1075 case 2: //---- 10 minutes ------------------------------------------------ |
| 584 | 1076 { |
| 1077 overlay rom const float* ptr = &e10min[2*ci]; | |
| 1078 var_N2_e = *ptr++; | |
| 1079 var_He_e = *ptr++; | |
| 1080 } | |
| 623 | 1081 break; |
| 584 | 1082 |
| 1083 default: | |
| 623 | 1084 assert(0); // code execution shall never pass along here! |
| 584 | 1085 } |
| 0 | 1086 } |
| 1087 | |
| 604 | 1088 |
| 0 | 1089 ////////////////////////////////////////////////////////////////////////////// |
| 623 | 1090 // Read Buhlmann half-times for compartment ci |
| 0 | 1091 // |
| 560 | 1092 static void read_Buhlmann_ht(void) |
| 0 | 1093 { |
| 1094 | |
| 1095 #ifndef CROSS_COMPILE | |
| 604 | 1096 // Note: We don't use far ROM pointer, because handling |
| 1097 // 24 bit is to complex, hence we have to set the | |
| 1098 // UPPER page ourself... | |
| 1099 // -> Set to zero if tables are moved to lower pages! | |
| 584 | 1100 _asm |
| 604 | 1101 movlw 1 |
| 1102 movwf TBLPTRU,0 | |
| 584 | 1103 _endasm |
| 0 | 1104 #endif |
| 1105 | |
| 584 | 1106 assert( ci < NUM_COMP ); |
| 1107 { | |
| 1108 overlay rom const float* ptr = &Buhlmann_ht[2*ci]; | |
| 1109 var_N2_ht = *ptr++; | |
| 1110 var_He_ht = *ptr++; | |
| 1111 } | |
| 1112 | |
| 604 | 1113 assert( 4.0 <= var_N2_ht && var_N2_ht <= 635.0 ); |
| 584 | 1114 assert( 1.5099 <= var_He_ht && var_He_ht <= 240.03 ); |
| 0 | 1115 } |
| 1116 | |
| 604 | 1117 |
| 0 | 1118 ////////////////////////////////////////////////////////////////////////////// |
| 623 | 1119 // Calculate adopted Buhlmann coefficients |
| 1120 // | |
| 1121 // Input: var_N2_a, var_N2_b coefficients for N2 | |
| 1122 // var_He_a, var_He_b coefficients for He | |
| 1123 // calc_pres_tissue_N2 partial pressure of N2 in tissue | |
| 1124 // calc_pres_tissue_He partial pressure of He in tissue | |
| 650 | 1125 // calc_pres_tissue total pressure in tissue |
| 623 | 1126 // |
| 1127 // Output: var_a, var_b coefficients adopted by N2/He ratio | |
| 604 | 1128 // |
| 1129 static void adopt_Buhlmann_coefficients(void) | |
| 1130 { | |
| 1131 // adopt a and b coefficients to current N2/He ratio inside the tissue | |
| 623 | 1132 |
| 1133 #ifdef _helium | |
| 1134 | |
| 650 | 1135 var_a = (var_N2_a * calc_pres_tissue_N2 + var_He_a * calc_pres_tissue_He) / calc_pres_tissue; |
| 1136 var_b = (var_N2_b * calc_pres_tissue_N2 + var_He_b * calc_pres_tissue_He) / calc_pres_tissue; | |
| 623 | 1137 |
| 1138 #else | |
| 1139 | |
| 1140 var_a = var_N2_a; | |
| 1141 var_b = var_N2_b; | |
| 1142 | |
| 1143 #endif | |
| 1144 | |
| 1145 } | |
| 1146 | |
| 1147 | |
| 1148 ////////////////////////////////////////////////////////////////////////////// | |
| 1149 // Calculate partial pressure of N2 in respired air at surface pressure | |
| 1150 // | |
| 1151 // Input: pres_surface surface pressure | |
| 1152 // | |
| 1153 // Output: N2_equilibrium partial pressure of N2 in surface air | |
| 1154 // | |
| 1155 static void calc_N2_equilibrium(void) | |
| 1156 { | |
| 1157 N2_equilibrium = 0.7902 * (pres_surface - ppWater); | |
| 1158 } | |
| 1159 | |
| 1160 | |
| 1161 ////////////////////////////////////////////////////////////////////////////// | |
| 1162 // Get, safeguard and convert the saturation and desaturation factors | |
| 1163 // | |
| 1164 // Input: char_I_saturation_multiplier saturation factor (integer) | |
| 1165 // char_I_desaturation_multiplier desaturation factor (integer) | |
| 1166 // | |
| 1167 // Output: float_saturation_multiplier saturation factor (float) | |
| 1168 // float_desaturation_multiplier desaturation factor (float) | |
| 1169 // | |
| 1170 static void get_saturation_factors(void) | |
| 1171 { | |
| 1172 // safeguard input parameters that are constant during the course of the dive | |
| 1173 if( char_I_saturation_multiplier < 100 ) char_I_saturation_multiplier = 100; | |
| 1174 if( char_I_saturation_multiplier > 140 ) char_I_saturation_multiplier = 140; | |
| 1175 | |
| 1176 if( char_I_desaturation_multiplier < 60 ) char_I_desaturation_multiplier = 60; | |
| 1177 if( char_I_desaturation_multiplier > 100 ) char_I_desaturation_multiplier = 100; | |
| 1178 | |
| 1179 // convert input parameters to float numbers | |
| 1180 float_saturation_multiplier = 0.01 * char_I_saturation_multiplier; | |
| 1181 float_desaturation_multiplier = 0.01 * char_I_desaturation_multiplier; | |
| 1182 } | |
| 1183 | |
| 1184 | |
| 1185 ////////////////////////////////////////////////////////////////////////////// | |
| 1186 // apply_saturation_factors | |
| 1187 // | |
| 1188 // Apply safety factors for both ZH-L16 models. | |
| 1189 // | |
| 1190 // Modified: temp_tissue safeguarded tissue increment/decrement | |
| 1191 // | |
| 1192 static void apply_saturation_factors(void) | |
| 1193 { | |
| 1194 assert( 0.0 < float_desaturation_multiplier && float_desaturation_multiplier <= 1.0 ); | |
| 1195 assert( 1.0 <= float_saturation_multiplier && float_saturation_multiplier <= 2.0 ); | |
| 1196 | |
| 1197 if ( temp_tissue < 0.0 ) temp_tissue *= float_desaturation_multiplier; | |
| 1198 else temp_tissue *= float_saturation_multiplier; | |
| 1199 } | |
| 1200 | |
| 1201 | |
| 1202 ////////////////////////////////////////////////////////////////////////////// | |
| 1203 // convert_float_to_int | |
| 1204 // | |
| 1205 // Converts a float within range 0.0 - 9.99 into 16 bit integer scaled in 1/100. | |
| 1206 // | |
| 1207 static void convert_float_to_int(void) | |
| 1208 { | |
| 1209 if ( float_value < 0.005 ) int_value = 0; | |
| 1210 else if ( float_value >= 9.985 ) int_value = 999; | |
| 1211 else int_value = (unsigned short)(100 * float_value + 0.5); | |
| 1212 } | |
| 1213 | |
| 1214 | |
| 604 | 1215 |
| 1216 // ********************************************************************************************************************************* | |
| 1217 // | |
| 1218 // J U M P I N F U N C T I O N S | |
| 1219 // | |
| 1220 // ********************************************************************************************************************************* | |
| 1221 | |
| 582 | 1222 |
| 1223 ////////////////////////////////////////////////////////////////////////////// | |
| 1224 // deco_calc_hauptroutine | |
| 1225 // | |
| 1226 // called from: divemode.asm | |
| 1227 // | |
| 628 | 1228 // Called two times per second during diving, updates the |
| 1229 // tissues every second (i.e. on every second invocation). | |
| 582 | 1230 // |
| 628 | 1231 // On each computation cycle: |
| 582 | 1232 // - Updates deco table (char_O_deco_time/depth) with new values, |
| 1233 // - updates ascent time, and | |
| 628 | 1234 // - sets status to zero (so we can check a cycle has finished). |
| 582 | 1235 // |
| 1236 void deco_calc_hauptroutine(void) | |
| 1237 { | |
| 1238 RESET_C_STACK | |
| 1239 calc_hauptroutine(); | |
| 1240 } | |
| 1241 | |
| 623 | 1242 |
| 1243 ////////////////////////////////////////////////////////////////////////////// | |
| 1244 // deco_init_output_vars | |
| 1245 // | |
| 1246 // called from divemode.asm | |
| 1247 // | |
| 1248 // Initializes all output variables to their default values. | |
| 1249 // | |
| 1250 void deco_init_output_vars(void) | |
| 1251 { | |
| 1252 RESET_C_STACK | |
| 1253 init_output_vars(); | |
| 1254 } | |
| 1255 | |
| 628 | 1256 |
| 582 | 1257 ////////////////////////////////////////////////////////////////////////////// |
| 1258 // deco_clear_tissue | |
| 1259 // | |
| 1260 // called from: start.asm | |
| 1261 // menu_tree.asm | |
| 1262 // simulator.asm | |
| 1263 // | |
| 628 | 1264 // Sets all tissues to equilibrium with air at ambient pressure, |
| 582 | 1265 // resets all CNS values, any warnings and resets all model output. |
| 1266 // | |
| 1267 void deco_clear_tissue(void) | |
| 1268 { | |
| 1269 RESET_C_STACK | |
| 1270 clear_tissue(); | |
| 1271 } | |
| 1272 | |
| 623 | 1273 |
| 582 | 1274 ////////////////////////////////////////////////////////////////////////////// |
| 1275 // deco_calc_dive_interval | |
| 1276 // | |
| 1277 // called from: simulator.asm | |
| 1278 // | |
| 628 | 1279 // Updates tissues and CNS value for char_I_dive_interval minutes on air at |
| 1280 // ambient pressure and calculates resulting saturation and ceiling for a | |
| 1281 // GF-high of 100% (ceiling and saturation not used by simulator.asm) | |
| 582 | 1282 // |
| 1283 void deco_calc_dive_interval(void) | |
| 1284 { | |
| 1285 RESET_C_STACK | |
| 1286 calc_interval(char_I_dive_interval); | |
| 1287 } | |
| 1288 | |
| 623 | 1289 |
| 582 | 1290 ////////////////////////////////////////////////////////////////////////////// |
| 1291 // deco_calc_dive_interval_1min | |
| 1292 // | |
| 1293 // called from: start.asm | |
| 1294 // sleepmode.asm | |
| 1295 // surfmode.asm | |
| 1296 // menu_tree.asm | |
| 1297 // ghostwriter.asm | |
| 1298 // | |
| 604 | 1299 // Updates tissues and CNS value for 1 minute on air at ambient pressure and |
| 628 | 1300 // calculates resulting saturation and ceiling for a GF-high of 100%. |
| 582 | 1301 // |
| 1302 void deco_calc_dive_interval_1min(void) | |
| 1303 { | |
| 1304 RESET_C_STACK | |
| 1305 calc_interval(1); | |
| 1306 } | |
| 1307 | |
| 1308 | |
| 1309 ////////////////////////////////////////////////////////////////////////////// | |
| 604 | 1310 // deco_calc_dive_interval_10min |
| 582 | 1311 // |
| 1312 // called from: sleepmode.asm | |
| 1313 // | |
| 604 | 1314 // Updates tissues and CNS value for 10 minutes on air at ambient pressure and |
| 628 | 1315 // calculates resulting saturation and ceiling for a GF-high of 100%. |
| 582 | 1316 // |
| 1317 void deco_calc_dive_interval_10min(void) | |
| 1318 { | |
| 1319 RESET_C_STACK | |
| 1320 calc_interval(10); | |
| 1321 } | |
| 1322 | |
| 1323 | |
| 1324 ////////////////////////////////////////////////////////////////////////////// | |
| 1325 // deco_calc_desaturation_time | |
| 1326 // | |
| 1327 // called from: start.asm | |
| 1328 // surfmode.asm | |
| 1329 // menu_tree.asm | |
| 1330 // ghostwriter.asm | |
| 1331 // | |
| 1332 // Computes desaturation and no-fly times. | |
| 1333 // | |
| 1334 void deco_calc_desaturation_time(void) | |
| 1335 { | |
| 1336 RESET_C_STACK | |
| 1337 calc_desaturation_time(); | |
| 1338 } | |
| 1339 | |
| 623 | 1340 |
| 582 | 1341 ////////////////////////////////////////////////////////////////////////////// |
| 1342 // deco_push_tissues_to_vault | |
| 1343 // | |
| 1344 // called from: simulator.asm | |
| 1345 // | |
| 1346 // Makes a backup of the state of the real tissues and the deco engine. | |
| 1347 // | |
| 1348 void deco_push_tissues_to_vault(void) | |
| 1349 { | |
| 1350 RESET_C_STACK | |
| 1351 push_tissues_to_vault(); | |
| 1352 } | |
| 1353 | |
| 623 | 1354 |
| 582 | 1355 ////////////////////////////////////////////////////////////////////////////// |
| 1356 // deco_pull_tissues_from_vault | |
| 1357 // | |
| 1358 // called from: simulator.asm | |
| 1359 // ghostwriter.asm | |
| 1360 // | |
| 1361 // Restores the state of the real tissues and the deco engine from the backup. | |
| 1362 // | |
| 1363 void deco_pull_tissues_from_vault(void) | |
| 1364 { | |
| 1365 RESET_C_STACK | |
| 1366 pull_tissues_from_vault(); | |
| 1367 } | |
| 1368 | |
| 604 | 1369 |
| 1370 // ********************************************************************************************************************************* | |
| 1371 // | |
| 1372 // M A I N F U N C T I O N S | |
| 1373 // | |
| 1374 // ********************************************************************************************************************************* | |
| 582 | 1375 |
| 1376 | |
| 1377 ////////////////////////////////////////////////////////////////////////////// | |
| 628 | 1378 // Calculate the next deco stop |
| 0 | 1379 // |
| 1380 // INPUT, fixed during dive: | |
| 623 | 1381 // pres_surface : surface pressure (as absolute pressure) |
| 631 | 1382 // char_I_last_stop_depth : depth of the last deco stop |
| 623 | 1383 // |
| 628 | 1384 // INPUT, may change during dive: |
| 623 | 1385 // GF_high : GF high factor |
| 1386 // GF_low : GF low factor | |
| 631 | 1387 // GF_depth : GF low reference depth |
| 1388 // GF_slope : GF slope | |
| 1389 // | |
| 1390 // MODIFIED | |
| 623 | 1391 // char_depth_sim : simulated depth in meters |
| 0 | 1392 // |
| 1393 // OUTPUT | |
| 631 | 1394 // GF_depth_norm/_alt : updated GF low depth reference |
| 1395 // GF_slope_norm/_alt : updated GF slope | |
| 623 | 1396 // |
| 1397 // RETURN | |
| 631 | 1398 // TRUE: a deco stop is required, FALSE: no deco stop required |
| 623 | 1399 // |
| 1400 static unsigned char find_next_stop(void) | |
| 0 | 1401 { |
| 623 | 1402 overlay unsigned char depth_1min; |
| 1403 overlay unsigned char depth_limit; | |
| 628 | 1404 overlay unsigned char stop_depth; |
| 1405 overlay unsigned char next_stop; | |
| 631 | 1406 |
| 1407 | |
| 1408 // calculate the current deco ceiling (minimum relative pressure) | |
| 1409 if( char_I_model == 0 ) calc_limit(1.0); // straight Buhlmann | |
| 1410 else if( NDL_time ) calc_limit(GF_high); // with GF: not in deco | |
| 1411 else if( char_depth_sim >= GF_depth ) calc_limit(GF_low); // in deco, below or at low depth reference | |
| 1412 else calc_limit(GF_high - GF_slope * (float)char_depth_sim); // in deco, above low depth reference | |
| 1413 | |
| 1414 // convert the deco ceiling from relative pressure to meters, | |
| 1415 // rounded up (i.e. made deeper) to the next full meter | |
| 1416 depth_limit = (unsigned char)(ceiling * BAR_TO_METER + 0.99); | |
| 1417 | |
| 1418 | |
| 1419 #ifdef _cave_mode | |
| 1420 // in cave or open water mode? | |
| 1421 if( main_status & CAVE_MODE ) | |
| 623 | 1422 { |
| 631 | 1423 // cave mode |
| 1424 | |
| 1425 // does backtracking require descent or keeping depth? | |
| 1426 if( backtrack_target_depth >= char_depth_sim ) | |
| 1427 { | |
| 1428 // YES - decent to target depth or stay at current depth | |
| 1429 char_depth_sim = backtrack_target_depth; | |
| 1430 | |
| 1431 // - done, no stop required | |
| 1432 return(0); | |
| 1433 } | |
| 1434 else | |
| 1435 { | |
| 1436 // NO | |
| 1437 | |
| 1438 // target depth requires an ascent - determine ascent limit due to deco obligation | |
| 1439 stop_depth = depth_limit; | |
| 1440 | |
| 1441 // apply correction for the shallowest stop | |
| 1442 if( stop_depth && (stop_depth < char_I_last_stop_depth) ) stop_depth = char_I_last_stop_depth; | |
| 1443 } | |
| 623 | 1444 } |
| 1445 else | |
| 631 | 1446 #endif |
| 623 | 1447 { |
| 631 | 1448 // open water vertical ascent mode - determine the stop depth |
| 1449 | |
| 1450 // stop depth is depth limit rounded up (made deeper) to the next multiple of 3 meters | |
| 1451 stop_depth = 3 * ( (depth_limit + 2) / 3 ); | |
| 1452 | |
| 1453 // apply correction for the shallowest stop | |
| 1454 if( stop_depth == 3 ) stop_depth = char_I_last_stop_depth; | |
| 623 | 1455 } |
| 1456 | |
| 631 | 1457 // is the stop depth shallower than the current depth (can we ascent)? |
| 1458 if( stop_depth < char_depth_sim ) | |
| 623 | 1459 { |
| 631 | 1460 // YES - ascent by 1 meter |
| 1461 char_depth_sim--; | |
| 1462 | |
| 1463 // done, no stop needed | |
| 1464 return(0); | |
| 623 | 1465 } |
| 1466 | |
| 1467 // ----------------------------------------------------------------------- | |
| 631 | 1468 // we need to make a stop because we are not allowed to ascent any further |
| 623 | 1469 // ----------------------------------------------------------------------- |
| 1470 | |
| 631 | 1471 // set depth to stop depth |
| 628 | 1472 char_depth_sim = stop_depth; |
| 1473 | |
| 1474 // Apply correction in case the stop is to be placed deeper than a | |
| 1475 // previously recorded stop for a gas change. This may happen because | |
| 1476 // the deco stops are placed at the next deeper multiple of 3 meters | |
| 1477 // instead of the real stop's depth. Correction is to relocate the | |
| 1478 // deco stop to the depth of the last gas change. The resulting combined | |
| 1479 // stop's duration will be the sum of the configured gas change time plus | |
| 1480 // the duration of the deco stop itself. | |
| 1481 if( 0 < internal_deco_depth[stop_index] ) | |
| 1482 if( char_depth_sim > internal_deco_depth[stop_index] ) | |
| 631 | 1483 char_depth_sim = internal_deco_depth[stop_index]; |
| 1484 | |
| 656 | 1485 // using straight Buhlmann? |
| 1486 if( char_I_model == 0 ) | |
| 1487 { | |
| 1488 // yes, reached surface? | |
| 1489 if( char_depth_sim == 0 ) return(0); // yes, no stop when at the surface | |
| 1490 else return(1); // no, stop needed | |
| 1491 } | |
| 623 | 1492 // ----------------------------------------------------------------------- |
| 628 | 1493 // we need to make or hold a stop and we are using the GF extension |
| 623 | 1494 // ----------------------------------------------------------------------- |
| 1495 | |
| 631 | 1496 // is the stop_depth deeper than the GF low depth reference used up to now? |
| 1497 if( stop_depth > GF_depth ) | |
| 623 | 1498 { |
| 631 | 1499 // YES - update the GF low depth reference |
| 1500 GF_depth = stop_depth; | |
| 1501 GF_slope = (GF_high - GF_low) / (float)GF_depth; | |
| 623 | 1502 |
| 1503 // store for use in next cycles | |
| 1504 if( deco_status & CALC_NORM ) | |
| 560 | 1505 { |
| 631 | 1506 GF_depth_norm = GF_depth; |
| 1507 GF_slope_norm = GF_slope; | |
| 582 | 1508 } |
| 560 | 1509 else |
| 1510 { | |
| 631 | 1511 GF_depth_alt = GF_depth; |
| 1512 GF_slope_alt = GF_slope; | |
| 560 | 1513 } |
| 623 | 1514 } |
| 1515 | |
| 631 | 1516 #ifdef _cave_mode |
| 1517 // if in cave mode: done, stop needed | |
| 1518 if( main_status & CAVE_MODE ) return(1); | |
| 1519 #endif | |
| 1520 | |
| 1521 // We have a stop depth candidate. But with a steep GF slope, the stop(s) after | |
| 1522 // this first stop may be allowed to ascent to, too. This is because the gradient | |
| 623 | 1523 // factor that will be used at the next depth(s) will allow more tissue super- |
| 631 | 1524 // saturation, maybe so much more that the next stop(s) will be allowed to ascent |
| 1525 // to. So we have to probe the next stops that are within the reach of 1 minute | |
| 1526 // of ascent as well. | |
| 1527 | |
| 1528 // compute depth in meters that is reachable within 1 minute of ascent at 10 m/min | |
| 1529 depth_1min = ( char_depth_sim > 10 ) ? char_depth_sim - 10 : 0; | |
| 623 | 1530 |
| 628 | 1531 // probe all stop depths that are in reach of 1 minute of ascent |
| 631 | 1532 next_stop = stop_depth; |
| 628 | 1533 |
| 1534 while(next_stop > 0) | |
| 623 | 1535 { |
| 631 | 1536 // compute the depth of the next stop to probe |
| 1537 if ( next_stop <= char_I_last_stop_depth ) next_stop = 0; | |
| 1538 else if ( next_stop == 6 ) next_stop = char_I_last_stop_depth; | |
| 628 | 1539 else next_stop -= 3; |
| 1540 | |
| 631 | 1541 // done if the next stop would be above the 1 minute limit |
| 1542 if( next_stop < depth_1min ) return(1); | |
| 1543 | |
| 1544 // compute the ceiling for the next stop depth | |
| 1545 calc_limit(GF_high - GF_slope * (float)next_stop); | |
| 1546 | |
| 1547 // done if the next stop would be above the ceiling | |
| 1548 if( next_stop < (unsigned char)(ceiling * BAR_TO_METER + 0.99) ) return(1); | |
| 1549 | |
| 1550 // the next stop depth is allowed, ascent to it and redo trying further stop | |
| 1551 char_depth_sim = next_stop; | |
| 582 | 1552 } |
| 1553 | |
| 631 | 1554 // reached the surface, done with no further stop |
| 1555 return(0); | |
| 0 | 1556 } |
| 1557 | |
| 604 | 1558 |
| 0 | 1559 ////////////////////////////////////////////////////////////////////////////// |
| 631 | 1560 // Take the actual currently used gas for ascent & deco calculation |
| 1561 // | |
| 1562 // Input: start_gas_num number of the gas/dil to start with (1..5 or 6) | |
| 623 | 1563 // |
| 1564 // Output: sim_gas_current_num 1..6 or 0 for the manually configured gas/dil | |
| 1565 // sim_gas_current_depth change depth (MOD) of the gas/dil in meters | |
| 560 | 1566 // |
| 628 | 1567 static void gas_take_current(void) |
| 560 | 1568 { |
| 631 | 1569 assert( 1 <= start_gas_num && start_gas_num <= 6 ); |
| 1570 | |
| 1571 | |
| 1572 // check origin of the gas/diluent to start with | |
| 1573 if( start_gas_num <= NUM_GAS ) | |
| 582 | 1574 { |
| 631 | 1575 // pre-configured gas/diluent |
| 1576 | |
| 1577 // set gas number | |
| 1578 sim_gas_current_num = sim_gas_last_num = start_gas_num; | |
| 1579 | |
| 1580 // set change depth | |
| 1581 sim_gas_current_depth = char_I_deco_gas_change[start_gas_num-1]; | |
| 628 | 1582 |
| 1583 // capture case of non-configured change depth | |
| 1584 if( sim_gas_current_depth == 0 ) sim_gas_current_depth = 255; | |
| 582 | 1585 } |
| 1586 else | |
| 560 | 1587 { |
| 631 | 1588 // on-the-fly configured gas/diluent ("gas 6") |
| 1589 | |
| 1590 // set gas number | |
| 1591 sim_gas_current_num = sim_gas_last_num = 0; | |
| 1592 | |
| 1593 // set change depth | |
| 604 | 1594 sim_gas_current_depth = char_I_gas6_depth; |
| 560 | 1595 } |
| 1596 } | |
| 1597 | |
| 1598 | |
| 1599 ////////////////////////////////////////////////////////////////////////////// | |
| 628 | 1600 // Find the gas with the shallowest change depth below or at the current depth |
| 560 | 1601 // |
| 623 | 1602 // Input: char_depth_sim simulated depth in meters |
| 1603 // sim_gas_current_num number of the currently used gas/dil | |
| 631 | 1604 // deco_gas_type[] types and state of the gases/dils |
| 1605 // char_I_deco_gas_change[] change depths of the gases/dils | |
| 623 | 1606 // |
| 628 | 1607 // Modified: sim_gas_best_num index of the gas (1..5) - only if return value is true |
| 1608 // sim_gas_best_depth switch depth - only if return value is true | |
| 623 | 1609 // |
| 1610 // Return value is TRUE if a better gas is available | |
| 560 | 1611 // |
| 628 | 1612 static unsigned char gas_find_best(void) |
| 560 | 1613 { |
| 641 | 1614 overlay unsigned char switch_depth = sim_gas_current_depth; |
| 604 | 1615 overlay unsigned char switch_gas = 0; |
| 631 | 1616 |
| 582 | 1617 |
| 628 | 1618 // loop over all gases to find the shallowest one below or at current depth |
| 604 | 1619 for( j = 0; j < NUM_GAS; ++j ) |
| 560 | 1620 { |
| 631 | 1621 // is this gas available? |
| 1622 if( ( deco_gas_type[j] & GAS_TYPE_MASK ) // gas enabled (type > 0) ? | |
| 1623 && !( deco_gas_type[j] & GAS_AVAIL_MASK ) // neither lost nor staged ? | |
| 1624 ) | |
| 1625 | |
| 1626 // is it not a deco gas unless: | |
| 1627 // extended stops are activated | |
| 1628 // OR we are on a deco stop | |
| 1629 // OR we are in bailout but not in cave mode | |
| 1630 if( ( ( deco_gas_type[j] & GAS_TYPE_MASK ) < 3 ) | |
| 1631 || ( ( main_status & EXTENDED_STOPS ) ) | |
| 1632 || ( ( deco_status & CALC_NORM ) && ( deco_info & DECO_STOPS_NORM ) ) | |
| 1633 || ( ( deco_status & CALC_ALT ) && ( deco_info & DECO_STOPS_ALT ) ) | |
| 1634 || ( ( deco_status & BAILOUT_MODE ) && !( main_status & CAVE_MODE ) ) | |
| 1635 ) | |
| 1636 | |
| 1637 // is the change depth of the this gas deeper than or | |
| 604 | 1638 // at least equal to the current depth? |
| 623 | 1639 if( char_I_deco_gas_change[j] >= char_depth_sim ) |
| 604 | 1640 |
| 631 | 1641 // is the change depth of this gas shallower than |
| 1642 // the change depth of the best gas found so far, | |
| 1643 // or is it the first better gas found? | |
| 1644 if( char_I_deco_gas_change[j] < switch_depth ) | |
| 628 | 1645 |
| 1646 #ifdef _gas_contingency | |
| 631 | 1647 // is there still enough of this gas or shall we don't care? |
| 1648 if( !(deco_gas_type[j] & GAS_FULLY_USED_UP ) | |
| 1649 || !( main_status & GAS_CONTINGENCY ) | |
| 1650 || !( main_status & CALC_VOLUME ) | |
| 1651 ) | |
| 628 | 1652 #endif |
| 604 | 1653 |
| 631 | 1654 // if there is a yes to all these questions, we have a better gas! |
| 560 | 1655 { |
| 628 | 1656 // memorize this gas (1..5) and its change depth |
| 1657 switch_gas = j+1; | |
| 1658 switch_depth = char_I_deco_gas_change[j]; | |
| 560 | 1659 } |
| 631 | 1660 |
| 560 | 1661 } // continue looping through all gases to eventually find an even better gas |
| 1662 | |
| 628 | 1663 // has a best gas been found? |
| 560 | 1664 if( switch_gas ) |
| 1665 { | |
| 628 | 1666 // YES - export the best gas and its change depth |
| 1667 sim_gas_best_num = switch_gas; | |
| 1668 sim_gas_best_depth = switch_depth; | |
| 1669 | |
| 1670 // is the best gas different from the current gas? | |
| 1671 if( sim_gas_best_num != sim_gas_current_num ) | |
| 1672 { | |
| 1673 // YES - signal advice for a gas change | |
| 1674 return 1; | |
| 1675 } | |
| 560 | 1676 } |
| 628 | 1677 |
| 1678 // no best gas found or current gas is the best gas | |
| 1679 return 0; | |
| 1680 } | |
| 1681 | |
| 1682 | |
| 1683 ////////////////////////////////////////////////////////////////////////////// | |
| 1684 // Switch to the best gas | |
| 1685 // | |
| 1686 // Input: sim_gas_best_num index of the best gas (1..5) | |
| 1687 // sim_gas_best_depth switch depth of the best gas | |
| 1688 // | |
| 1689 // Modified: sim_gas_current_num index of the new gas (1..5) | |
| 1690 // sim_gas_current_depth switch depth of the new gas | |
| 1691 // | |
| 1692 static void gas_take_best(void) | |
| 1693 { | |
| 631 | 1694 // memorize current gas as last gas used |
| 1695 sim_gas_last_num = sim_gas_current_num; | |
| 1696 | |
| 628 | 1697 // set new gas |
| 1698 sim_gas_current_num = sim_gas_best_num; | |
| 1699 sim_gas_current_depth = sim_gas_best_depth; | |
| 0 | 1700 } |
| 1701 | |
| 604 | 1702 |
| 0 | 1703 ////////////////////////////////////////////////////////////////////////////// |
| 623 | 1704 // Set calc_N2/He/O2_ratios by sim_gas_current_num |
| 1705 // | |
| 1706 // Input: sim_gas_current_num index of gas to use | |
| 1707 // real_O2_ratio, real_He_ratio if gas = 0 (the manually set gas) | |
| 1708 // char_I_deco_O2/He_ratio[] if gas = 1..5 (the configured gases) | |
| 1709 // | |
| 1710 // Output: sim_N2_ratio, sim_He_ratio ratios of the inert gases | |
| 1711 // sim_pSCR_drop ppO2 drop in pSCR loop | |
| 0 | 1712 // |
| 582 | 1713 static void gas_set_ratios(void) |
| 0 | 1714 { |
| 582 | 1715 overlay float sim_IG_ratio; |
| 1716 | |
| 623 | 1717 assert( 0 <= sim_gas_current_num <= NUM_GAS ); |
| 1718 | |
| 1719 | |
| 1720 #ifdef _helium | |
| 582 | 1721 // get gas ratios |
| 623 | 1722 if( sim_gas_current_num == 0 ) |
| 560 | 1723 { |
| 604 | 1724 sim_O2_ratio = real_O2_ratio; |
| 1725 sim_He_ratio = real_He_ratio; | |
| 560 | 1726 } |
| 1727 else | |
| 1728 { | |
| 623 | 1729 sim_O2_ratio = 0.01 * char_I_deco_O2_ratio[sim_gas_current_num-1]; |
| 1730 sim_He_ratio = 0.01 * char_I_deco_He_ratio[sim_gas_current_num-1]; | |
| 560 | 1731 } |
| 1732 | |
| 582 | 1733 // inert gas ratio (local helper variable) |
| 604 | 1734 sim_IG_ratio = 1.00 - sim_O2_ratio; |
| 582 | 1735 |
| 1736 // N2 ratio | |
| 604 | 1737 sim_N2_ratio = sim_IG_ratio - sim_He_ratio; |
| 623 | 1738 #else |
| 1739 // get O2 ratio | |
| 1740 sim_O2_ratio = ( sim_gas_current_num == 0 ) ? real_O2_ratio : 0.01 * char_I_deco_O2_ratio[sim_gas_current_num-1]; | |
| 1741 | |
| 1742 // set H2 ratio to zero | |
| 1743 sim_He_ratio = 0.0; | |
| 1744 | |
| 1745 // inert gas ratio (local helper variable) | |
| 1746 sim_IG_ratio = 1.00 - sim_O2_ratio; | |
| 1747 | |
| 1748 // N2 ratio | |
| 1749 sim_N2_ratio = sim_IG_ratio; | |
| 1750 #endif | |
| 1751 | |
| 1752 #ifdef _ccr_pscr | |
| 582 | 1753 // ppO2 drop in pSCR loop |
| 604 | 1754 sim_pSCR_drop = sim_IG_ratio * float_pSCR_factor; |
| 623 | 1755 #endif |
| 582 | 1756 |
| 1757 | |
| 1758 assert( 0.0 <= sim_N2_ratio && sim_N2_ratio <= 0.95 ); | |
| 1759 assert( 0.0 <= sim_He_ratio && sim_He_ratio <= 0.95 ); | |
| 1760 assert( (sim_N2_ratio + sim_He_ratio) <= 0.95 ); | |
| 0 | 1761 } |
| 1762 | |
| 604 | 1763 |
| 0 | 1764 ////////////////////////////////////////////////////////////////////////////// |
| 604 | 1765 // Compute respired ppO2, ppN2 and ppHe |
| 0 | 1766 // |
| 631 | 1767 // Input: tissue_increment : selector for simulated/real tissues and gases |
| 623 | 1768 // main_status : breathing mode for real tissues |
| 1769 // deco_status : breathing mode for simulated tissues | |
| 604 | 1770 // sim_/real_O2_ratio : (simulated) O2 ratio breathed |
| 1771 // sim_/real_N2_ratio : (simulated) N2 ratio breathed | |
| 1772 // sim_/real_He_ratio : (simulated) He ratio breathed | |
| 1773 // sim_/real_pres_respiration : (simulated) respiration pressure [bar] | |
| 1774 // sim_/real_pSCR_drop : (simulated) pSCR O2 drop | |
| 1775 // pres_surface : surface pressure [bar] | |
| 1776 // char_I_const_ppO2 : ppO2 reported from sensors or setpoint [cbar] | |
| 1777 // ppWater : water-vapor pressure inside respiratory tract [bar] | |
| 0 | 1778 // |
| 604 | 1779 // Output: ppN2 : respired N2 partial pressure |
| 1780 // ppHe : respired He partial pressure | |
| 623 | 1781 // char_ppO2 : breathed ppO2 in %, used for CNS calculation |
| 0 | 1782 // |
| 582 | 1783 void calc_alveolar_pressures(void) |
| 0 | 1784 { |
| 604 | 1785 overlay float calc_pres_respiration; |
| 582 | 1786 overlay float calc_O2_ratio; |
| 1787 overlay float calc_N2_ratio; | |
| 623 | 1788 |
| 1789 #ifdef _helium | |
| 582 | 1790 overlay float calc_He_ratio; |
| 623 | 1791 #endif |
| 1792 | |
| 1793 #ifdef _ccr_pscr | |
| 582 | 1794 overlay float calc_pSCR_drop; |
| 623 | 1795 #endif |
| 582 | 1796 |
| 1797 overlay unsigned char status; | |
| 1798 | |
| 1799 | |
| 623 | 1800 assert( 0.00 <= real_N2_ratio && real_N2_ratio <= 1.00 ); |
| 1801 assert( 0.00 <= real_He_ratio && real_He_ratio <= 1.00 ); | |
| 1802 assert( (real_N2_ratio + real_He_ratio) <= 1.00 ); | |
| 604 | 1803 assert( 0.800 < real_pres_respiration && real_pres_respiration < 14.0 ); |
| 1804 | |
| 623 | 1805 assert( 0.00 <= sim_N2_ratio && real_N2_ratio <= 1.00 ); |
| 1806 assert( 0.00 <= sim_He_ratio && real_He_ratio <= 1.00 ); | |
| 1807 assert( (sim_N2_ratio + sim_He_ratio) <= 1.00 ); | |
| 582 | 1808 assert( 0.800 < sim_pres_respiration && sim_pres_respiration < 14.0 ); |
| 1809 | |
| 1810 | |
| 1811 // get input data according to context | |
| 623 | 1812 if( tissue_increment & TISSUE_SELECTOR ) |
| 582 | 1813 { |
| 1814 //---- real tissues ----------------------------------------------------------- | |
| 604 | 1815 calc_pres_respiration = real_pres_respiration; |
| 623 | 1816 |
| 1817 status = main_status; | |
| 604 | 1818 calc_O2_ratio = real_O2_ratio; |
| 1819 calc_N2_ratio = real_N2_ratio; | |
| 623 | 1820 |
| 1821 #ifdef _helium | |
| 604 | 1822 calc_He_ratio = real_He_ratio; |
| 623 | 1823 #endif |
| 1824 | |
| 1825 #ifdef _ccr_pscr | |
| 1826 calc_pSCR_drop = real_pSCR_drop; | |
| 1827 #endif | |
| 582 | 1828 } |
| 1829 else | |
| 1830 { | |
| 1831 //---- simulated tissues ------------------------------------------------------ | |
| 623 | 1832 |
| 640 | 1833 #ifdef _cave_mode |
| 1834 // in cave mode? | |
| 1835 if( !(main_status & CAVE_MODE) ) | |
| 1836 { | |
| 1837 #endif | |
| 1838 | |
| 1839 // (NO) - correct sim_pres_respiration if shallower than calculated stop depth | |
| 1840 calc_pres_respiration = ( real_pres_respiration < sim_pres_respiration ) ? real_pres_respiration : sim_pres_respiration; | |
| 1841 | |
| 1842 #ifdef _cave_mode | |
| 1843 } | |
| 1844 #endif | |
| 1845 | |
| 628 | 1846 |
| 623 | 1847 status = deco_status; |
| 604 | 1848 calc_O2_ratio = sim_O2_ratio; |
| 1849 calc_N2_ratio = sim_N2_ratio; | |
| 623 | 1850 |
| 1851 #ifdef _helium | |
| 604 | 1852 calc_He_ratio = sim_He_ratio; |
| 623 | 1853 #endif |
| 1854 | |
| 1855 #ifdef _ccr_pscr | |
| 1856 calc_pSCR_drop = sim_pSCR_drop; | |
| 1857 #endif | |
| 582 | 1858 } |
| 1859 | |
| 1860 //---- OC, CCR and Bailout Mode Gas Calculations ----------------------------------- | |
| 1861 | |
| 1862 // calculate ppO2 of pure oxygen | |
| 631 | 1863 ppO2_O2 = calc_pres_respiration - ppWater; |
| 604 | 1864 |
| 1865 // capture failure condition in case real_pres_respiration is < ppWater (should never happen...) | |
| 631 | 1866 if( ppO2_O2 < 0.0 ) ppO2_O2 = 0.0; |
| 582 | 1867 |
| 602 | 1868 // calculate ppO2 of the pure gas (OC, diluent) |
| 631 | 1869 ppO2_OC = ppO2_O2 * calc_O2_ratio; |
| 582 | 1870 |
| 623 | 1871 #ifdef _ccr_pscr |
| 1872 | |
| 582 | 1873 // calculate pSCR ppO2 |
| 631 | 1874 ppO2_pSCR = ppO2_OC - calc_pSCR_drop; |
| 1875 | |
| 1876 // capture failure condition in case ppO2_pSCR becomes negative | |
| 1877 if( ppO2_pSCR < 0.0 ) ppO2_pSCR = 0.0; | |
| 582 | 1878 |
| 1879 | |
| 1880 //---- Loop modes : adjust ppN2 and ppHe for change in ppO2 due to setpoint (CCR) or drop (pSCR) --- | |
| 623 | 1881 if( status & MODE_LOOP ) |
| 582 | 1882 { |
| 1883 overlay float const_ppO2; | |
| 604 | 1884 overlay float max_ppO2; |
| 582 | 1885 |
| 1886 // get the current sensor reading (CCR / pSCR if fitted) or the fixed setpoint (CCR) / a zero (pSCR) | |
| 1887 const_ppO2 = 0.01 * char_I_const_ppO2; | |
| 1888 | |
| 1889 // Limit the setpoint to the maximum physically possible ppO2. This prevents for | |
| 1890 // example calculating with a setpoint of 1.3 bar in only 2 meters of depth. | |
| 604 | 1891 // Additionally, the ppO2 can be further reduced to account for exhaled inert gases |
| 623 | 1892 // accumulating in the loop by the user-adjustable setting char_I_CC_max_frac_O2. |
| 604 | 1893 // (ppWater is neglected here) |
| 623 | 1894 max_ppO2 = 0.01 * char_I_CC_max_frac_O2 * calc_pres_respiration; |
| 604 | 1895 |
| 1896 if( const_ppO2 > max_ppO2 ) const_ppO2 = max_ppO2; | |
| 582 | 1897 |
| 1898 // check which kind of loop we are on | |
| 623 | 1899 if( status & MODE_PSCR ) |
| 560 | 1900 { |
| 582 | 1901 //---- pSCR Mode -------------------------------------------------------------------------- |
| 1902 | |
| 631 | 1903 // Use the sensor value if available and in real tissue context, else use calculated ppO2. |
| 1904 ppO2 = ( char_I_const_ppO2 && (tissue_increment & TISSUE_SELECTOR)) ? const_ppO2 : ppO2_pSCR; | |
| 582 | 1905 } |
| 560 | 1906 else |
| 1907 { | |
| 582 | 1908 //---- CCR Mode --------------------------------------------------------------------------- |
| 1909 | |
| 604 | 1910 // derive breathed ppO2 from (char_I_)const_ppO2, which holds sensor reading or selected setpoint |
| 582 | 1911 ppO2 = const_ppO2; |
| 560 | 1912 } |
| 1913 | |
| 604 | 1914 // adjust overall gas pressure for change in ppO2 due to setpoint (CCR) or drop (pSCR), |
| 1915 // capture potential failure conditions first: | |
| 1916 if( ( calc_pres_respiration < ppO2 ) // sensor reading or selected setpoint is higher than ambient pressure | |
| 623 | 1917 || ( calc_O2_ratio > 0.995 ) ) // diluent is pure O2, i.e. calc_N2_ratio + calc_He_ratio = 0 would give a div/0 |
| 560 | 1918 { |
| 604 | 1919 // failure condition present, set predetermined result |
| 1920 calc_pres_respiration = 0.0; | |
| 1921 } | |
| 1922 else | |
| 1923 { | |
| 1924 // no failure conditions present, equation can be executed | |
| 1925 calc_pres_respiration -= ppO2; | |
| 623 | 1926 #ifdef _helium |
| 604 | 1927 calc_pres_respiration /= calc_N2_ratio + calc_He_ratio; |
| 623 | 1928 #else |
| 1929 calc_pres_respiration /= calc_N2_ratio; | |
| 1930 #endif | |
| 560 | 1931 } |
| 582 | 1932 } |
| 1933 else | |
| 623 | 1934 #endif // _ccr_pscr |
| 604 | 1935 { |
| 1936 //---- OC mode --------------------------------------------------------------------------------- | |
| 582 | 1937 |
| 1938 // breathed ppO2 is ppO2 of pure gas | |
| 631 | 1939 ppO2 = ppO2_OC; |
| 560 | 1940 } |
| 1941 | |
| 582 | 1942 |
| 604 | 1943 //---- derive char_ppO2 in [cbar], used for calculating CNS% --------------------------------------- |
| 1944 | |
| 582 | 1945 if ( ppO2 < 0.01 ) char_ppO2 = 0; |
| 1946 else if ( ppO2 >= 2.545 ) char_ppO2 = 255; | |
| 1947 else char_ppO2 = (unsigned char)(100 * ppO2 + 0.5); | |
| 1948 | |
| 1949 | |
| 1950 //---- calculate ppN2 and ppHe --------------------------------------------------------------------- | |
| 1951 | |
| 604 | 1952 // compute ppN2 and ppHe, capture potential failure conditions first: |
| 1953 if( calc_pres_respiration > ppWater ) | |
| 582 | 1954 { |
| 604 | 1955 // subtract water vapor pressure |
| 1956 calc_pres_respiration -= ppWater; | |
| 1957 | |
| 1958 // calculate partial pressures | |
| 1959 ppN2 = calc_N2_ratio * calc_pres_respiration; | |
| 623 | 1960 |
| 1961 #ifdef _helium | |
| 604 | 1962 ppHe = calc_He_ratio * calc_pres_respiration; |
| 623 | 1963 #else |
| 1964 ppHe = 0.0; | |
| 1965 #endif | |
| 1966 | |
| 582 | 1967 } |
| 1968 else | |
| 1969 { | |
| 604 | 1970 // calculated respired pressure is < water vapor pressure, thus set ppN2 and ppHe to 0 |
| 582 | 1971 ppN2 = 0.0; |
| 1972 ppHe = 0.0; | |
| 1973 } | |
| 628 | 1974 |
| 634 | 1975 #ifdef _ccr_pscr |
| 628 | 1976 // calculating real tissues? |
| 1977 if( tissue_increment & TISSUE_SELECTOR ) | |
| 1978 { | |
| 1979 overlay unsigned char temp; | |
| 1980 | |
| 1981 // compute gas density of current mix in multiples of 0.01 grams per liter | |
| 643 | 1982 int_O_gas_density = (unsigned int)( GAS_DENSITY_He_FACTOR * ppHe + GAS_DENSITY_N2_FACTOR * ppN2 + GAS_DENSITY_O2_FACTOR * ppO2 ); |
| 628 | 1983 |
| 634 | 1984 // convert gas density into an 8 bit integer, scaling 0.1 grams per liter |
| 628 | 1985 temp = (unsigned char)( (int_O_gas_density + 9) / 10 ); |
| 1986 | |
| 1987 // limit to display max and set warning or attention flag | |
| 634 | 1988 if( temp > 99 ) int_O_gas_density = 999 | INT_FLAG_WARNING; |
| 1989 else if( temp >= char_I_gas_density_warn ) int_O_gas_density |= INT_FLAG_WARNING; | |
| 1990 else if( temp >= char_I_gas_density_att ) int_O_gas_density |= INT_FLAG_ATTENTION; | |
| 628 | 1991 } |
| 1992 #endif | |
| 0 | 1993 } |
| 1994 | |
| 604 | 1995 |
| 0 | 1996 ////////////////////////////////////////////////////////////////////////////// |
| 623 | 1997 // Initializes all output variables to their default values |
| 1998 // | |
| 1999 static void init_output_vars(void) | |
| 2000 { | |
| 2001 // clear the internal stops table from remains lasting from the previous dive or deco calculator run | |
| 2002 clear_deco_table(); | |
| 2003 | |
| 2004 // publish the cleared stops table to the display functions | |
| 2005 publish_deco_table(); | |
| 2006 | |
| 2007 // clear the published gas needs in volume and pressure | |
| 2008 for( i = 0; i < NUM_GAS; ++i ) | |
| 2009 { | |
| 2010 int_O_gas_need_vol[i] = 0; | |
| 2011 int_O_gas_need_pres[i] = 0 + INT_FLAG_ZERO + INT_FLAG_INVALID; | |
| 2012 } | |
| 2013 | |
| 2014 // values initially to be set to zero | |
| 2015 int_O_ceiling = 0; // ceiling depth in mbar | |
| 2016 char_O_deco_info = 0; // clear all deco information flags | |
| 2017 char_O_deco_warnings = 0; // clear all deco warning flags | |
| 2018 | |
| 2019 // default desaturation time to 24 hours (it will not be computed during a dive) | |
| 2020 int_O_desaturation_time = 1440; | |
| 2021 | |
| 2022 // initialize CNS values | |
| 2023 int_O_CNS_norm = 0 + INT_FLAG_INVALID; | |
| 2024 int_O_CNS_alt = 0 + INT_FLAG_INVALID; | |
| 2025 | |
| 2026 // initialize NDL times | |
| 631 | 2027 int_O_NDL_norm = 240; |
| 2028 int_O_NDL_alt = 240; | |
| 2029 | |
| 2030 // initialize stop times | |
| 2031 int_O_TST_norm = 0; | |
| 2032 int_O_TST_alt = 0 + INT_FLAG_INVALID + INT_FLAG_NOT_COMPUTED_YET + INT_FLAG_ZERO; | |
| 2033 | |
| 2034 | |
| 2035 // initialize ascent / return times | |
| 623 | 2036 int_O_TTS_norm = 0; |
| 2037 int_O_TTS_alt = 0 + INT_FLAG_INVALID + INT_FLAG_NOT_COMPUTED_YET; | |
| 2038 | |
| 2039 #ifdef _rx_functions | |
| 2040 // clear TR values | |
| 2041 int_O_SAC_measured = 0 + INT_FLAG_NOT_AVAIL; // SAC rate | |
| 2042 int_O_pressure_need[0] = 0 + INT_FLAG_NOT_AVAIL; // pressure need to reading 1 | |
| 2043 int_O_pressure_need[1] = 0 + INT_FLAG_NOT_AVAIL; // pressure need to reading 2 | |
| 628 | 2044 int_O_tank_pressure = 0; // tank pressure for logging |
| 623 | 2045 #endif |
| 2046 | |
| 628 | 2047 #ifdef _helium |
| 2048 int_O_gas_density = 0; | |
| 2049 #endif | |
| 2050 | |
| 2051 // also clear any time++ request | |
| 2052 char_I_sim_advance_time = 0; | |
| 623 | 2053 } |
| 2054 | |
| 656 | 2055 ////////////////////////////////////////////////////////////////////////////// |
| 2056 // calc_GF_surface | |
| 2057 // | |
| 2058 // Calculates the instantaneous surface GF (GF_surf) for the leading compartment. | |
| 2059 // | |
| 2060 // Definition: | |
| 2061 // GF_surf = (P_tissue - pres_surface) / (M_surf - pres_surface) | |
| 2062 // | |
| 2063 // - P_tissue : current inert gas tissue pressure (N2 + He) in bar | |
| 2064 // - pres_surface : pres_surface (Ambient pressure at the surface, in bar) | |
| 2065 // - M_surf : M-value on the surface (per calc_M_value_at_pressure) | |
| 2066 // | |
| 2067 // The maximum across all compartments is taken as GF_surf. | |
| 2068 // The result is written in percent (0..250) in int_O_GF_surface. | |
| 2069 // | |
| 2070 static float calc_M_value_at_pressure(float pres_surface) | |
| 2071 { | |
| 2072 float a, b; | |
| 2073 read_Buhlmann_coefficients(); | |
| 2074 #ifdef _helium | |
| 2075 adopt_Buhlmann_coefficients(); | |
| 2076 a = var_a; | |
| 2077 b = var_b; | |
| 2078 #else | |
| 2079 a = var_N2_a; | |
| 2080 b = var_N2_b; | |
| 2081 #endif | |
| 2082 return a + b * pres_surface; | |
| 2083 } | |
| 2084 | |
| 2085 static void calc_GF_surface(void) | |
| 2086 { | |
| 2087 overlay float P_tissue; | |
| 2088 overlay float M_surf; | |
| 2089 overlay float gf_surf; | |
| 2090 overlay float gf_surf_max = 0.0f; | |
| 2091 | |
| 2092 // Pass through all compartments | |
| 2093 for (ci = 0; ci < NUM_COMP; ci++) | |
| 2094 { | |
| 2095 #ifdef _helium | |
| 2096 P_tissue = real_pres_tissue_N2[ci] + real_pres_tissue_He[ci]; | |
| 2097 #else | |
| 2098 P_tissue = real_pres_tissue_N2[ci]; | |
| 2099 #endif | |
| 2100 | |
| 2101 // M-value on the surface for this compartment | |
| 2102 M_surf = calc_M_value_at_pressure(pres_surface); | |
| 2103 | |
| 2104 // Filtering out unnecessary cases | |
| 2105 if (M_surf <= pres_surface) | |
| 2106 continue; | |
| 2107 | |
| 2108 // Surface-GF of this compartment | |
| 2109 gf_surf = (P_tissue - pres_surface) / (M_surf - pres_surface); | |
| 2110 | |
| 2111 // Keep biggest result in gf_surf | |
| 2112 if (gf_surf > gf_surf_max) | |
| 2113 gf_surf_max = gf_surf; | |
| 2114 } | |
| 2115 | |
| 2116 // Convert to % | |
| 2117 if (gf_surf_max <= 0.0f) | |
| 2118 { | |
| 2119 int_O_GF_surface = 0; | |
| 2120 } | |
| 2121 else | |
| 2122 { | |
| 2123 unsigned int tmp; | |
| 2124 | |
| 2125 tmp = (unsigned int)(gf_surf_max * 100.0f + 0.5f); | |
| 2126 | |
| 2127 if (tmp > 999) | |
| 2128 tmp = 999; | |
| 2129 | |
| 2130 int_O_GF_surface = tmp; | |
| 2131 } | |
| 2132 } | |
| 623 | 2133 |
| 2134 ////////////////////////////////////////////////////////////////////////////// | |
| 2135 // Reset all tissues to surface pressure equilibrium state | |
| 2136 // | |
| 2137 // Input: int_I_pres_surface current surface pressure in hPa (mbar) | |
| 2138 // | |
| 2139 // Output: real_pres_tissue_N2[] partial pressure of N2 in real tissues | |
| 2140 // real_pres_tissue_He[] partial pressure of He in real tissues | |
| 2141 // char_O_tissue_pres_N2[] partial pressure of N2 for tissue graphics | |
| 2142 // char_O_tissue_pres_He[] partial pressure of He for tissue graphics | |
| 2143 // char_O_tissue_pressure[] total pressure for tissue graphics | |
| 2144 // CNS_fraction_real internal CNS value | |
| 2145 // int_O_CNS_current current CNS value | |
| 2146 // int_O_CNS_norm CNS value at end of normal dive plan | |
| 2147 // int_O_CNS_alt CNS value at end of alternative dive plan | |
| 2148 // char_O_deco_warnings deco warnings vector | |
| 631 | 2149 // int_O_NDL_norm remaining NDL time in normal dive plan |
| 2150 // int_O_NDL_alt remaining NDL time in alternative dive plan | |
| 623 | 2151 // int_O_TTS_norm ascent time (TTS) in normal dive plan |
| 2152 // int_O_TTS_alt ascent time (TTS) in alternative dive plan | |
| 2153 // int_O_lead_supersat supersaturation of the leading tissue | |
| 0 | 2154 // |
| 2155 static void clear_tissue(void) | |
| 2156 { | |
| 623 | 2157 // safeguard and convert the surface pressure (mbar -> bar) (*) |
| 2158 if( int_I_pres_surface < 500 ) pres_surface = 0.500; | |
| 2159 else pres_surface = 0.001 * int_I_pres_surface; | |
| 2160 | |
| 2161 // calculate partial pressure of N2 in respired air at surface pressure | |
| 2162 calc_N2_equilibrium(); | |
| 2163 | |
| 2164 // cycle through the 16 Buhlmann tissues | |
| 604 | 2165 for( ci = 0; ci < NUM_COMP; ci++ ) |
| 582 | 2166 { |
| 623 | 2167 // reset tissue pressures |
| 2168 real_pres_tissue_He[ci] = 0.0; // He | |
| 2169 real_pres_tissue_N2[ci] = N2_equilibrium; // N2 | |
| 2170 | |
| 650 | 2171 real_pres_delta_He[ci] = 0.0; // He |
| 2172 real_pres_delta_N2[ci] = 0.0; // N2 | |
| 2173 | |
| 629 | 2174 // reset tissue pressures for scaled tissue graphics |
| 631 | 2175 char_O_tissue_pres_He[ci] = 0; // He |
| 623 | 2176 char_O_tissue_pres_N2[ci] = 10; // N2 |
| 2177 char_O_tissue_pressure[ci] = 10; // combined | |
| 582 | 2178 } |
| 2179 | |
| 2180 // reset CNS values | |
| 623 | 2181 CNS_fraction_real = 0.0; |
| 2182 int_O_CNS_current = int_O_CNS_norm = int_O_CNS_alt = 0; | |
| 582 | 2183 |
| 2184 // reset some more vars to their defaults | |
| 631 | 2185 int_O_NDL_norm = 240; |
| 2186 int_O_NDL_alt = 240; | |
| 2187 int_O_TST_norm = 0; | |
| 2188 int_O_TST_alt = 0 + INT_FLAG_ZERO; | |
| 2189 int_O_TTS_norm = 0; | |
| 2190 int_O_TTS_alt = 0; | |
| 2191 int_O_lead_supersat = 0; | |
| 623 | 2192 |
| 2193 // reset all warning and info flags | |
| 631 | 2194 char_O_deco_warnings = 0; |
| 2195 char_O_deco_info = 0; | |
| 0 | 2196 } |
| 2197 | |
| 582 | 2198 |
| 0 | 2199 ////////////////////////////////////////////////////////////////////////////// |
| 628 | 2200 // Deco engine main code |
| 0 | 2201 // |
| 623 | 2202 // This is the major code in dive mode, it calculates the tissue pressures, |
| 2203 // the bottom time, and it calculates the ascend with all deco stops, etc. | |
| 2204 // | |
| 2205 // Input: char_O_main_status deco engine control and real tissues mode | |
| 2206 // char_O_deco_status deco engine control and simulated tissues mode | |
| 2207 // char_I_sim_advance_time mailbox for bottom time incrementing | |
| 2208 // | |
| 2209 // char_I_SAC_work gas usage rate during working phase in l/min | |
| 2210 // char_I_SAC_deco gas usage rate during deco stops phase in l/min | |
| 2211 // | |
| 631 | 2212 // char_I_model selector for GF extension |
| 623 | 2213 // char_I_saturation_multiplier safety factor for tissue saturation |
| 2214 // char_I_desaturation_multiplier safety factor for tissue desaturation | |
| 2215 // | |
| 631 | 2216 // char_I_pressure_gas[] amount of gas available for ascent / cave return in bar |
| 623 | 2217 // int_I_pressure_drop[] pressure drop used to calculate SAC rate |
| 2218 // char_I_gas_avail_size[] size of the tanks in liters | |
| 2219 // | |
| 2220 // Output: int_O_O2_ppO2 partial pressure of pure O2 at current depth | |
| 2221 // int_O_pure_ppO2 partial pressure of O2 in gas at current depth | |
| 2222 // int_O_pSCR_ppO2 partial pressure of O2 in gas at current depth, corrected for pSCR mode | |
| 2223 // int_O_breathed_ppO2 partial pressure of O2 currently breathed | |
| 2224 // | |
| 2225 // char_O_deco_status deco engine computations status | |
| 2226 // char_O_deco_info deco engine information vector | |
| 2227 // char_O_deco_warnings deco engine warnings vector | |
| 2228 // | |
| 631 | 2229 // int_O_NDL_norm remaining NDL time in normal dive plan |
| 2230 // int_O_NDL_alt remaining NDL time in alternative dive plan | |
| 623 | 2231 // int_O_TTS_norm ascent time (TTS) in normal dive plan |
| 2232 // int_O_TTS_alt ascent time (TTS) in alternative dive plan | |
| 2233 // int_O_CNS_norm CNS value at end of normal dive plan | |
| 2234 // int_O_CNS_alt CNS value at end of alternative dive plan | |
| 2235 // | |
| 631 | 2236 // int_O_gas_need_vol[] calculated gas volumes needed for ascent / cave return |
| 2237 // int_O_gas_need_pres[] calculated gas pressures needed for ascent / cave return | |
| 623 | 2238 // |
| 2239 // int_O_SAC_measured measured surface air consumption (SAC) rate in l/min | |
| 2240 // | |
| 2241 // Modified: int_IO_pressure_value[] warning flags added to pressure reading 1 & 2 | |
| 2242 // int_IO_pressure_need[] pressure needs to pressure reading 1 & 2 | |
| 0 | 2243 // |
| 2244 static void calc_hauptroutine(void) | |
| 2245 { | |
| 623 | 2246 overlay unsigned short int_ppO2_min; |
| 628 | 2247 overlay unsigned short int_ppO2_max_warn; |
| 2248 overlay unsigned short int_ppO2_max_att; | |
| 623 | 2249 overlay unsigned short int_ppO2_max_dil; |
| 631 | 2250 overlay float EAD_pres; |
| 2251 overlay float END_pres; | |
| 623 | 2252 |
| 2253 //============================================================================================= | |
| 2254 | |
| 2255 // | |
| 2256 //--- Setup Part --------------------------------------------------------------------------------- | |
| 2257 // | |
| 2258 | |
| 2259 // set time limit for preempting deco calculations, timer is 16 bit and increments every 1/32 ms | |
| 2260 tmr5_value = 65535 - (32 * BUDGET_PER_SECOND / INVOKES_PER_SECOND); | |
| 2261 | |
| 2262 // load timer | |
| 2263 load_tmr5(); | |
| 2264 | |
| 2265 // read command flags and set up what to do | |
| 2266 switch( char_O_deco_status & COMMAND_MASK ) | |
| 560 | 2267 { |
| 623 | 2268 |
| 2269 case INITIALIZE: | |
| 2270 case INITIALIZE_START_NORM: | |
| 2271 case INITIALIZE_START_ALT: | |
| 2272 | |
| 2273 // copy master modes to shadow registers | |
| 2274 main_status = char_O_main_status; | |
| 2275 deco_status = char_O_deco_status; | |
| 2276 | |
| 650 | 2277 // clear all command flags on the master mode to signal that the command is read and in processing |
| 623 | 2278 char_O_deco_status &= ~COMMAND_MASK; |
| 2279 | |
| 2280 // clear the initialization flag on the shadow copy | |
| 2281 deco_status &= ~INITIALIZE; | |
| 2282 | |
| 2283 // initialize the sequence timer | |
| 2284 sequence_timer = 0; | |
| 2285 | |
| 2286 // set the calculation phase to start with to doing the once-per-dive initialization | |
| 2287 next_planning_phase = PHASE_10_DIVE_INIT; | |
| 2288 | |
| 2289 break; | |
| 2290 | |
| 2291 | |
| 2292 case START_NORM: | |
| 2293 case START_ALT: | |
| 2294 | |
| 2295 // copy master modes to shadow registers | |
| 2296 main_status = char_O_main_status; | |
| 2297 deco_status = char_O_deco_status; | |
| 2298 | |
| 650 | 2299 // clear all command flags on the master mode to signal that the command is read and in processing |
| 623 | 2300 char_O_deco_status &= ~COMMAND_MASK; |
| 2301 | |
| 2302 // set the calculation phase to start with to doing the cyclic initialization | |
| 628 | 2303 next_planning_phase = PHASE_20_CYCLIC_INIT; |
| 623 | 2304 |
| 2305 // continue in CALCULATING | |
| 2306 | |
| 2307 | |
| 2308 case CALCULATING: | |
| 2309 | |
| 2310 // keep current calculation phase | |
| 2311 | |
| 2312 // step the sequence timer | |
| 2313 sequence_timer = (sequence_timer < INVOKES_PER_SECOND * 2 - 1) ? sequence_timer + 1 : 0; | |
| 2314 | |
| 2315 break; | |
| 560 | 2316 } |
| 2317 | |
| 582 | 2318 |
| 623 | 2319 // |
| 2320 //--- End of Setup Part ----------------------------------------------------------------------- | |
| 2321 // | |
| 2322 | |
| 2323 //============================================================================================= | |
| 2324 | |
| 2325 // | |
| 2326 //---- Calculations Part (real Tissues) ------------------------------------------------------- | |
| 2327 // | |
| 2328 | |
| 582 | 2329 |
| 604 | 2330 // target the real tissues with 2 second increments by default |
| 623 | 2331 tissue_increment = TISSUE_SELECTOR | 0; |
| 2332 | |
| 2333 | |
| 2334 // Tasks every second, if more than 1 invocation per second: on the first section of the second. | |
| 2335 // Requests for tissue "fast forward" are executed immediately. | |
| 2336 #if (INVOKES_PER_SECOND > 1) | |
| 2337 if( ( sequence_timer == 0 ) | |
| 2338 || ( sequence_timer == INVOKES_PER_SECOND ) | |
| 2339 || ( char_I_sim_advance_time > 0 ) | |
| 2340 ) | |
| 2341 #endif | |
| 584 | 2342 { |
| 623 | 2343 // acquire current environmental data |
| 2344 calc_hauptroutine_data_input(); | |
| 2345 | |
| 628 | 2346 // calculate ppO2, ppN2 and ppHe for real tissues |
| 623 | 2347 calc_alveolar_pressures(); |
| 2348 | |
| 2349 // add decent calculation here and include trigger in above if-statement | |
| 2350 // TODO | |
| 2351 | |
| 2352 } // tasks every second, on the first section of the second | |
| 2353 | |
| 2354 | |
| 2355 // Tasks every 2 seconds, on the first section of the respective second. | |
| 2356 // Requests for tissue "fast forward" are executed immediately. | |
| 2357 if( ( sequence_timer == 0 ) | |
| 2358 || ( char_I_sim_advance_time > 0 ) | |
| 2359 ) | |
| 2360 { | |
| 2361 // Tissue and CNS updates are based on 2 seconds periods! | |
| 2362 | |
| 604 | 2363 // Set up normal tissue updating or "fast forward" updating for simulator |
| 2364 // sim+5' function and deco calculator bottom time calculation. | |
| 2365 if( char_I_sim_advance_time > 0 ) | |
| 2366 { | |
| 2367 // configure "fast forward" tissue updating | |
| 623 | 2368 tissue_increment = TISSUE_SELECTOR | char_I_sim_advance_time; |
| 2369 | |
| 2370 // clear the request | |
| 604 | 2371 char_I_sim_advance_time = 0; |
| 2372 } | |
| 2373 | |
| 584 | 2374 // calculate the real tissues |
| 2375 calc_tissues(); | |
| 2376 | |
| 604 | 2377 // update the CNS value for the real tissues |
| 2378 calc_CNS(); | |
| 2379 | |
| 2380 // calculate ceiling (at GF_high or 100%) and leading tissue supersaturation | |
| 631 | 2381 if ( char_I_model ) calc_limit(GF_high); // GF factors enabled |
| 2382 else calc_limit( 1.0 ); // classic Buhlmann | |
| 623 | 2383 |
| 2384 // convert the ceiling value to integer | |
| 604 | 2385 convert_ceiling_for_display(); |
| 2386 | |
| 623 | 2387 // convert the saturation value of the leading tissue to integer |
| 2388 convert_sat_for_display(); | |
| 2389 | |
| 2390 // convert the CNS value to integer | |
| 2391 convert_cur_CNS_for_display(); | |
| 656 | 2392 |
| 2393 // compute the surface GF | |
| 2394 calc_GF_surface(); | |
| 623 | 2395 |
| 2396 } // tasks every 2 seconds | |
| 2397 | |
| 2398 | |
| 2399 // Tasks every second, if more than 1 invocation per second: on the first section of the second. | |
| 2400 #if (INVOKES_PER_SECOND > 1) | |
| 2401 if( ( sequence_timer == 0 ) | |
| 2402 || ( sequence_timer == INVOKES_PER_SECOND ) | |
| 2403 ) | |
| 2404 #endif | |
| 582 | 2405 { |
| 623 | 2406 //---- Calculate and Export EAD and END ------------------------------------------------------ |
| 2407 | |
| 631 | 2408 // calculate EAD (Equivalent Air Depth) as pressure in [bar]: |
| 2409 // equivalent depth for the same N2 level with plain air | |
| 2410 EAD_pres = ppN2 / 0.7902 + ppWater - pres_surface; | |
| 2411 | |
| 2412 // calculate END (Equivalent Narcotic Depth) as pressure in [bar]: | |
| 2413 // as before, but with O2 treated as narcotic, too | |
| 2414 // Source: The Physiology and Medicine of Diving by Peter Bennett and David Elliott, | |
| 2415 // 4th edition, 1993, W.B.Saunders Company Ltd, London. | |
| 2416 END_pres = real_pres_respiration - ppHe - pres_surface; | |
| 2417 | |
| 2418 | |
| 2419 // export EAD, factor 10 is because conversion delivers in [cbar] but we need [mbar] | |
| 2420 float_value = EAD_pres; convert_float_to_int(); int_O_EAD_pres = int_value * 10; | |
| 2421 | |
| 2422 // export END, factor 10 is because conversion delivers in [cbar] but we need [mbar] | |
| 2423 float_value = END_pres; convert_float_to_int(); int_O_END_pres = int_value * 10; | |
| 623 | 2424 |
| 2425 | |
| 2426 //---- Compute ppO2 Values in [cbar] --------------------------------------------------------- | |
| 2427 | |
| 2428 float_value = ppO2; convert_float_to_int(); int_O_breathed_ppO2 = int_value; // breathed gas | |
| 2429 #ifdef _ccr_pscr | |
| 631 | 2430 float_value = ppO2_O2; convert_float_to_int(); int_O_O2_ppO2 = int_value; // pure oxygen |
| 2431 float_value = ppO2_OC; convert_float_to_int(); int_O_pure_ppO2 = int_value; // pure gas | |
| 2432 float_value = ppO2_pSCR; convert_float_to_int(); int_O_pSCR_ppO2 = int_value; // pSCR calculated | |
| 623 | 2433 #endif |
| 2434 | |
| 2435 | |
| 628 | 2436 //---- Set/Clear Deco Mode ------------------------------------------------------------------ |
| 2437 | |
| 2438 // Clear the deco mode flag if: | |
| 2439 // deco mode is set | |
| 2440 // AND we are deeper than 7 meters below the deepest deco stop | |
| 2441 // (7 meters chosen as to be 2 stop depth intervals plus 1 additional meter below) | |
| 2442 if ( ( deco_info & DECO_MODE ) > 0 ) | |
| 2443 if ( ( char_depth_real ) > char_O_deco_depth[0] + 7 ) | |
| 650 | 2444 deco_info &= ~DECO_MODE; |
| 623 | 2445 |
| 2446 // Set the deco mode flag if: | |
| 2447 // deco mode is not set | |
| 2448 // AND breathing an OC deco gas (gas type 3) | |
| 2449 // OR breathing a gas or diluent that officially is disabled (type 0) | |
| 628 | 2450 // OR there is a deco stop |
| 631 | 2451 // |
| 2452 // Remark: when breathing a gas, its lost & staged flags are cleared | |
| 2453 // | |
| 628 | 2454 if ( ( deco_info & DECO_MODE ) == 0 ) |
| 2455 if ( ( char_I_current_gas_type == 3 ) | |
| 2456 || ( char_I_current_gas_type == 0 ) | |
| 2457 || ( char_O_deco_depth[0] > 0 ) | |
| 623 | 2458 ) |
| 650 | 2459 deco_info |= DECO_MODE; |
| 623 | 2460 |
| 2461 | |
| 2462 //---- Compute ppO2 Warnings ------------------------------------------------------------------ | |
| 2463 | |
| 628 | 2464 // compute conditional min value |
| 623 | 2465 #ifdef _ccr_pscr |
| 2466 int_ppO2_min = ( main_status & MODE_LOOP ) ? (unsigned short)char_I_ppO2_min_loop : (unsigned short)char_I_ppO2_min; | |
| 2467 #else | |
| 2468 int_ppO2_min = (unsigned short)char_I_ppO2_min; | |
| 2469 #endif | |
| 2470 | |
| 628 | 2471 // determine the absolute max value (should be the deco one, but who knows...) |
| 2472 int_ppO2_max_warn = ( char_I_ppO2_max_work > char_I_ppO2_max_deco) ? char_I_ppO2_max_work : char_I_ppO2_max_deco; | |
| 2473 | |
| 2474 // add some margin to compensate for surface pressures > 1.000 mbar | |
| 2475 int_ppO2_max_warn += ppO2_MARGIN_ON_MAX; | |
| 2476 | |
| 2477 // determine the normal max value | |
| 2478 int_ppO2_max_att = ( deco_info & DECO_MODE ) ? (unsigned short)char_I_ppO2_max_deco : (unsigned short)char_I_ppO2_max_work; | |
| 2479 | |
| 2480 // add some margin to compensate for surface pressures > 1.000 mbar | |
| 2481 int_ppO2_max_att += ppO2_MARGIN_ON_MAX; | |
| 623 | 2482 |
| 2483 #ifdef _ccr_pscr | |
| 628 | 2484 // default value for the upper diluent ppO2 warning threshold is the upper warning threshold |
| 2485 int_ppO2_max_dil = int_ppO2_max_warn; | |
| 2486 | |
| 2487 // when enabled and in CCR mode, the upper diluent warning threshold gets adjust according to the current setpoint | |
| 631 | 2488 if( char_I_dil_check ) |
| 623 | 2489 if( (main_status & MODE_MASK) == MODE_CCR ) |
| 2490 { | |
| 2491 overlay unsigned short max_dil; | |
| 2492 | |
| 2493 // The upper diluent ppO2 threshold is ppO2_GAP_TO_SETPOINT below the setpoint... | |
| 2494 // (the condition protects from negative numbers which would cause a wrap-around in unsigned integers) | |
| 2495 max_dil = (char_I_const_ppO2 > ppO2_GAP_TO_SETPOINT) ? (unsigned short)(char_I_const_ppO2 - ppO2_GAP_TO_SETPOINT) : 0; | |
| 2496 | |
| 628 | 2497 // ...but never above int_ppO2_max_warn |
| 2498 if( max_dil < int_ppO2_max_warn ) int_ppO2_max_dil = max_dil; | |
| 623 | 2499 |
| 2500 // We do not need to guard int_ppO2_max_dil against becoming lower than char_I_ppO2_min because the check | |
| 2501 // against char_I_ppO2_min is done first and will then raise a low warning and inhibit further checks. | |
| 2502 } | |
| 2503 #endif | |
| 2504 | |
| 2505 // check for safe range of breathed gas | |
| 628 | 2506 if ( int_O_breathed_ppO2 <= int_ppO2_min ) int_O_breathed_ppO2 |= INT_FLAG_WARNING + INT_FLAG_LOW; |
| 2507 else if ( int_O_breathed_ppO2 >= int_ppO2_max_warn ) int_O_breathed_ppO2 |= INT_FLAG_WARNING + INT_FLAG_HIGH; | |
| 2508 else if ( deco_info & DECO_MODE ) ; // no attention generated in deco mode | |
| 2509 else if ( main_status & MODE_LOOP ) ; // no attention generated in loop modes | |
| 2510 else if ( int_O_breathed_ppO2 >= int_ppO2_max_att ) int_O_breathed_ppO2 |= INT_FLAG_ATTENTION; | |
| 623 | 2511 |
| 2512 #ifdef _ccr_pscr | |
| 2513 // check for safe range of pure oxygen | |
| 628 | 2514 if ( int_O_O2_ppO2 >= int_ppO2_max_warn ) int_O_O2_ppO2 |= INT_FLAG_WARNING + INT_FLAG_HIGH; |
| 623 | 2515 |
| 2516 // check for safe range of pure diluent | |
| 628 | 2517 if ( int_O_pure_ppO2 <= (unsigned short)char_I_ppO2_min ) int_O_pure_ppO2 |= INT_FLAG_WARNING + INT_FLAG_LOW; |
| 2518 else if ( int_O_pure_ppO2 >= int_ppO2_max_warn ) int_O_pure_ppO2 |= INT_FLAG_WARNING + INT_FLAG_HIGH; | |
| 2519 else if ( int_O_pure_ppO2 >= int_ppO2_max_dil ) int_O_pure_ppO2 |= INT_FLAG_ATTENTION; | |
| 623 | 2520 |
| 2521 // check for safe range of calculated pSCR loop gas | |
| 628 | 2522 if ( int_O_pSCR_ppO2 <= int_ppO2_min ) int_O_pSCR_ppO2 |= INT_FLAG_WARNING + INT_FLAG_LOW; |
| 2523 else if ( int_O_pSCR_ppO2 >= int_ppO2_max_warn ) int_O_pSCR_ppO2 |= INT_FLAG_WARNING + INT_FLAG_HIGH; | |
| 623 | 2524 #endif |
| 2525 | |
| 2526 } // tasks every second / on the first section of the second | |
| 604 | 2527 |
| 2528 | |
| 2529 #ifdef _rx_functions | |
| 2530 | |
| 623 | 2531 // only when TR functions are enabled |
| 2532 if( main_status & TR_FUNCTIONS ) | |
| 2533 | |
| 2534 // Tasks every second, if more than 1 invocation per second: on the second section of the second. | |
| 2535 #if (INVOKES_PER_SECOND > 1) | |
| 2536 if( ( sequence_timer == 1 ) | |
| 2537 || ( sequence_timer == INVOKES_PER_SECOND + 1 ) | |
| 2538 ) | |
| 2539 #endif | |
| 604 | 2540 { |
| 623 | 2541 calc_TR_functions(); |
| 2542 } | |
| 2543 | |
| 2544 #endif // _rx_functions | |
| 2545 | |
| 2546 | |
| 2547 // | |
| 604 | 2548 //---- End of Computations for the real Tissues ----------------------------------------------- |
| 2549 // | |
| 623 | 2550 |
| 604 | 2551 //============================================================================================= |
| 623 | 2552 |
| 604 | 2553 // |
| 623 | 2554 //---- Begin of Computations for Ascent and Decompression (simulated Tissues) ----------------- |
| 2555 // | |
| 2556 | |
| 2557 // Dispatcher: select what to do based on the current calculation phase | |
| 2558 do | |
| 582 | 2559 { |
| 623 | 2560 |
| 2561 #ifdef _profiling | |
| 2562 profiling_phase = next_planning_phase; | |
| 2563 #endif | |
| 2564 | |
| 2565 switch( next_planning_phase ) | |
| 560 | 2566 { |
| 623 | 2567 |
| 2568 // | |
| 2569 //---- once-per-dive Initialization of the Deco Engine ------------------------------------ | |
| 2570 // | |
| 2571 case PHASE_10_DIVE_INIT: | |
| 2572 | |
| 2573 // initialize all output variables to defaults | |
| 2574 init_output_vars(); | |
| 2575 | |
| 582 | 2576 // initialize values that will be recalculated later on periodically |
| 631 | 2577 deco_warnings = 0; // reset all deco warnings |
| 2578 deco_info = 0; // reset all deco infos | |
| 2579 IBCD_tissue_vector = 0; // reset tissue IBCD vector | |
| 2580 NDL_tissue_start_norm = 0; // initialize the tissue to start with when calculating normal NDL time | |
| 2581 NDL_tissue_start_alt = 0; // initialize the tissue to start with when calculating alternative NDL time | |
| 623 | 2582 |
| 2583 // enforce initialization of GF data on first cyclic initialization | |
| 631 | 2584 GF_high_last = 255; |
| 2585 GF_low_last = 255; | |
| 2586 | |
| 2587 | |
| 2588 // calculate volumes available for each gas | |
| 2589 for( i = 0; i < NUM_GAS; i++ ) | |
| 628 | 2590 { |
| 631 | 2591 // total available volume = tank size * fill press, char_I_gas_avail_pres is in multiples of 10 bar |
| 2592 gas_volume_avail[i] = (float)char_I_gas_avail_size[i] * (float)char_I_gas_avail_pres[i] * 10.0; | |
| 2593 | |
| 2594 // attention threshold | |
| 2595 gas_volume_atten[i] = gas_volume_avail[i] * GAS_NEEDS_ATTENTION; | |
| 628 | 2596 } |
| 631 | 2597 |
| 623 | 2598 |
| 2599 #ifdef _profiling | |
| 2600 int_O_profiling_overrun_max = 0; | |
| 2601 char_O_profiling_runs_norm = 0; | |
| 2602 char_O_profiling_runs_alt = 0; | |
| 2603 #endif | |
| 2604 | |
| 2605 | |
| 2606 // the next calculation phase will do the cyclic initialization of the deco engine if a | |
| 2607 // normal or alternative plan shall be calculated, else the calculation cycle is done. | |
| 628 | 2608 if( deco_status & PLAN_MASK ) next_planning_phase = PHASE_20_CYCLIC_INIT; |
| 623 | 2609 else next_planning_phase = PHASE_00_DONE; |
| 2610 | |
| 2611 break; | |
| 2612 | |
| 582 | 2613 |
| 2614 // | |
| 628 | 2615 //---- once-per-cycle Initialization of the Deco Engine ----------------------------------- |
| 582 | 2616 // |
| 628 | 2617 case PHASE_20_CYCLIC_INIT: |
| 623 | 2618 |
| 2619 // target the simulated tissues (flag bit 7 = 0) | |
| 2620 tissue_increment = 0; | |
| 2621 | |
| 2622 // clear the internal stops table | |
| 582 | 2623 clear_deco_table(); |
| 604 | 2624 |
| 631 | 2625 // clear deco stops info |
| 2626 if( deco_status & CALC_NORM ) deco_info &= ~DECO_STOPS_NORM; | |
| 2627 else deco_info &= ~DECO_STOPS_ALT; | |
| 2628 | |
| 560 | 2629 // initialize the simulated tissues with the current state of the real tissues |
| 650 | 2630 memcpy(sim_pres_tissue_N2, real_pres_tissue_N2, 256); |
| 582 | 2631 |
| 631 | 2632 // initialize the gas types |
| 2633 for( i = 0; i < NUM_GAS; i++ ) | |
| 2634 { | |
| 2635 deco_gas_type[i] = char_I_deco_gas_type[i]; | |
| 2636 } | |
| 2637 | |
| 2638 #ifdef _gas_contingency | |
| 2639 // if in gas contingency mode, | |
| 2640 // check if there are multiple tanks with the same gas | |
| 2641 // (or at least with the same change depth...) | |
| 2642 if( main_status & GAS_CONTINGENCY ) | |
| 2643 { | |
| 2644 for( i = 0; i < NUM_GAS; i++ ) | |
| 2645 { | |
| 2646 // default to no peer tanks existing | |
| 2647 peer_tank[i] = 0; | |
| 2648 | |
| 2649 // tank enabled? | |
| 2650 if( char_I_deco_gas_type[i] ) | |
| 2651 { | |
| 2652 // YES - check for peer tanks | |
| 2653 for( j = 0; j < NUM_GAS; j++ ) | |
| 2654 { | |
| 2655 // do not check a tank against itself | |
| 2656 if( i == j ) continue; | |
| 2657 | |
| 2658 // is the other tank also enabled and does it have the same change depth? | |
| 2659 if( char_I_deco_gas_type[j] & GAS_TYPE_MASK ) | |
| 2660 if( char_I_deco_gas_change[i] == char_I_deco_gas_change[j] ) | |
| 2661 { | |
| 2662 // YES - memorize it as a peer tank | |
| 2663 peer_tank[i] |= (1 << j); | |
| 2664 } | |
| 2665 } | |
| 2666 } | |
| 2667 } | |
| 2668 } | |
| 2669 #endif | |
| 2670 | |
| 623 | 2671 // initialize GF parameters if using GF model |
| 631 | 2672 if( char_I_model != 0 ) |
| 604 | 2673 { |
| 623 | 2674 // update GF parameters (GFs may have been switched between GF and aGF) |
| 628 | 2675 if( (char_I_GF_Low_percentage != GF_low_last) || (char_I_GF_High_percentage != GF_high_last) ) |
| 604 | 2676 { |
| 623 | 2677 // store new values in integer format |
| 628 | 2678 GF_low_last = char_I_GF_Low_percentage; |
| 623 | 2679 GF_high_last = char_I_GF_High_percentage; |
| 628 | 2680 |
| 2681 // safeguard and store new values in float format | |
| 2682 GF_low = ( GF_low_last > 10 ) ? 0.01 * GF_low_last : 0.10 ; | |
| 2683 GF_high = ( GF_high_last > GF_low_last ) ? 0.01 * GF_high_last : GF_low; | |
| 2684 | |
| 623 | 2685 |
| 2686 // reset low depth references and slopes | |
| 631 | 2687 GF_depth_norm = 0; |
| 2688 GF_depth_alt = 0; | |
| 2689 GF_slope_norm = 0.0; | |
| 2690 GF_slope_alt = 0.0; | |
| 604 | 2691 } |
| 623 | 2692 |
| 2693 // retrieve GF parameters for current calculation cycle | |
| 2694 if( deco_status & CALC_NORM ) | |
| 560 | 2695 { |
| 631 | 2696 GF_depth = GF_depth_norm; |
| 2697 GF_slope = GF_slope_norm; | |
| 560 | 2698 } |
| 2699 else | |
| 2700 { | |
| 631 | 2701 GF_depth = GF_depth_alt; |
| 2702 GF_slope = GF_slope_alt; | |
| 560 | 2703 } |
| 604 | 2704 } |
| 2705 | |
| 623 | 2706 // initialize the simulated CNS value with the current CNS value of the real tissues |
| 2707 CNS_fraction_sim = CNS_fraction_real; | |
| 2708 | |
| 631 | 2709 // initialize the simulated pressure with the current real pressure |
| 623 | 2710 sim_pres_respiration = real_pres_respiration; |
| 631 | 2711 |
| 2712 // initialize the simulated depth with the current real depth | |
| 2713 char_depth_sim = char_depth_start = char_depth_real; | |
| 2714 | |
| 2715 // cache the gas/dil to start calculations with | |
| 2716 start_gas_num = char_I_current_gas_num; | |
| 2717 | |
| 623 | 2718 |
| 2719 // Lookup the gas that is currently breathed with the real tissues and set it as | |
| 2720 // the gas to be used with the simulated tissues, too. This gas will be used until | |
| 628 | 2721 // gas_find_best()/gas_take_best() is invoked and switches to a better gas. |
| 2722 gas_take_current(); | |
| 2723 | |
| 2724 // Setup the calculation ratio's for N2, He and O2 (sim_N2/He/_O2_ratio). | |
| 2725 // These ratios can be kept until a gas switch is done. Thus, if a call to | |
| 2726 // gas_find_best() has found a better gas and this gas is taken by a call | |
| 2727 // to gas_take_best(), gas_set_ratios() needs to be called again. | |
| 623 | 2728 gas_set_ratios(); |
| 2729 | |
| 2730 // compute ppO2, ppN2 and ppHe for current depth from sim_pres_respiration | |
| 2731 calc_alveolar_pressures(); | |
| 2732 | |
| 2733 // initialize the no decompression limit (NDL) time to 240 minutes | |
| 2734 NDL_time = 240; | |
| 2735 | |
| 631 | 2736 // clear the Total Time to Surface (TTS) and the Total Stops Time (TST) |
| 2737 TTS_time = 0; | |
| 2738 TST_time = 0; | |
| 628 | 2739 |
| 623 | 2740 // retrieve the tissue that had the shortest NDL time during last calculation |
| 2741 NDL_tissue_start = ( deco_status & CALC_NORM ) ? NDL_tissue_start_norm : NDL_tissue_start_alt; | |
| 2742 | |
| 2743 // start calculating NDL time with the tissue that had the shortest NDL last time | |
| 2744 NDL_tissue = NDL_tissue_start; | |
| 2745 NDL_tissue_lead = NDL_tissue_start; | |
| 2746 | |
| 631 | 2747 // tag gas needs as not calculated in fTTS mode by default |
| 2748 deco_info &= ~GAS_NEEDS_fTTS; | |
| 623 | 2749 |
| 628 | 2750 // shall calculate gas needs? |
| 2751 if( main_status & CALC_VOLUME ) | |
| 2752 { | |
| 2753 // set the usage rate (SAC rate), starting with working part of the dive | |
| 2754 gas_needs_usage_rate = char_I_SAC_work; | |
| 2755 | |
| 2756 // clear the gas volume needs for gases 1-5 | |
| 2757 for( i = 0; i < NUM_GAS; ++i ) gas_volume_need[i] = 0.0; | |
| 2758 | |
| 2759 #ifdef _rx_functions | |
| 2760 // only for OSTC TR model with TR functions enabled | |
| 2761 if( main_status & TR_FUNCTIONS ) | |
| 2762 { | |
| 2763 // invalidate pressure needs to pressure readings | |
| 2764 int_O_pressure_need[0] = 0 + INT_FLAG_NOT_AVAIL; | |
| 2765 int_O_pressure_need[1] = 0 + INT_FLAG_NOT_AVAIL; | |
| 2766 } | |
| 2767 #endif | |
| 2768 } | |
| 623 | 2769 |
| 631 | 2770 |
| 2771 #ifdef _cave_mode | |
| 2772 if( main_status & CAVE_MODE ) | |
| 2773 { | |
| 2774 // get the position of the first data set to start the backtracking from | |
| 2775 backtrack_index = char_I_backtrack_index; | |
| 2776 | |
| 2777 // get the first backtracking data set | |
| 2778 read_backtrack_data(); | |
| 2779 } | |
| 2780 #endif | |
| 2781 | |
| 2782 | |
| 623 | 2783 #ifdef _profiling |
| 2784 profiling_runs = 0; | |
| 2785 #endif | |
| 2786 | |
| 2787 // The next calculation phase will | |
| 628 | 2788 // - calculate the extended bottom segment if extended bottom time is configured (fTTS), |
| 2789 // - calculate the bottom segment gas need if gas needs calculation is configured, | |
| 623 | 2790 // - proceed with calculating the NDL time else. |
| 628 | 2791 if ( deco_status & DELAYED_ASCENT ) next_planning_phase = PHASE_30_EXTENDED_BOTTOM_TIME; |
| 2792 else if ( main_status & CALC_VOLUME ) next_planning_phase = PHASE_40_BOTTOM_GAS_NEED; | |
| 2793 else next_planning_phase = PHASE_50_NDL_TIME; | |
| 623 | 2794 |
| 2795 break; | |
| 2796 | |
| 2797 | |
| 2798 // | |
| 2799 //---- extended Bottom Time --------------------------------------------------------------- | |
| 2800 // | |
| 628 | 2801 case PHASE_30_EXTENDED_BOTTOM_TIME: |
| 623 | 2802 |
| 631 | 2803 // tag gas needs as calculated in fTTS mode |
| 2804 deco_info |= GAS_NEEDS_fTTS; | |
| 2805 | |
| 623 | 2806 // program interval on simulated tissues (flag bit 7 = 0) |
| 2807 tissue_increment = char_I_extra_time; | |
| 2808 | |
| 628 | 2809 // update the simulated tissues for tissue_increment (char_I_extra_time) minutes at depth, |
| 2810 // calc_alveolar_pressures() has already been called in cyclic initialization | |
| 623 | 2811 calc_tissues(); |
| 2812 | |
| 628 | 2813 // update the CNS value for tissue_increment (char_I_extra_time) minutes at depth |
| 623 | 2814 calc_CNS(); |
| 2815 | |
| 628 | 2816 // the next calculation phase will |
| 2817 // - calculate the extended bottom segment gas needs if gas needs calculation is configured, | |
| 2818 // - proceed with calculating the NDL time else. | |
| 2819 if ( main_status & CALC_VOLUME ) next_planning_phase = PHASE_40_BOTTOM_GAS_NEED; | |
| 2820 else next_planning_phase = PHASE_50_NDL_TIME; | |
| 2821 | |
| 2822 break; | |
| 2823 | |
| 2824 | |
| 2825 // | |
| 2826 //---- Bottom Segment Gas Need ------------------------------------------------------------ | |
| 2827 // | |
| 2828 case PHASE_40_BOTTOM_GAS_NEED: | |
| 2829 | |
| 2830 // on gas 1-5 ? | |
| 2831 if( sim_gas_current_num ) | |
| 2832 { | |
| 631 | 2833 // YES - take either the whole bottom time or just the fTTS/bailout extra time |
| 628 | 2834 gas_needs_time = ( main_status & CALCULATE_BOTTOM ) ? char_I_bottom_time : char_I_extra_time; |
| 2835 | |
| 631 | 2836 // any time to do? |
| 2837 if( gas_needs_time ) | |
| 2838 { | |
| 2839 // YES - set the bottom depth | |
| 2840 gas_needs_depth = char_depth_start; | |
| 2841 | |
| 2842 // calculate required gas volume | |
| 2843 calc_required_volume(); | |
| 2844 | |
| 2845 // take the result | |
| 2846 gas_volume_need[sim_gas_current_num-1] = gas_needs_volume_due; | |
| 2847 } | |
| 628 | 2848 } |
| 2849 | |
| 2850 | |
| 623 | 2851 // the next calculation phase will calculate the NDL time |
| 628 | 2852 next_planning_phase = PHASE_50_NDL_TIME; |
| 604 | 2853 |
| 582 | 2854 break; |
| 2855 | |
| 623 | 2856 |
| 2857 // | |
| 2858 //---- NDL Time --------------------------------------------------------------------------- | |
| 2859 // | |
| 628 | 2860 case PHASE_50_NDL_TIME: |
| 623 | 2861 |
| 2862 // Calculate the remaining no decompression limit (NDL) time for the tissue NDL_tissue. | |
| 2863 // NDL_time will be updated if the NDL time found is shorter than the current NDL_time. | |
| 2864 // | |
| 2865 // In the initialization phase of the calculation cycle: | |
| 2866 // - NDL_time had been initialized to 240 (minutes), | |
| 2867 // - NDL_tissue had been initialized to the tissue with | |
| 2868 // the shortest NDL time in the last cycle. | |
| 2869 // | |
| 2870 calc_NDL_time_tissue(); | |
| 2871 | |
| 2872 // advance to next tissue, wrapping around after last tissue | |
| 2873 NDL_tissue = (NDL_tissue + 1) & (NUM_COMP - 1); | |
| 2874 | |
| 2875 // did we run out of NDL time or did we have probed all tissues? | |
| 2876 if( (NDL_time == 0) || (NDL_tissue == NDL_tissue_start) ) | |
| 2877 { | |
| 2878 // YES | |
| 2879 | |
| 2880 // set the tissue with the shortest NDL time found as | |
| 2881 // the one to start with in the next calculation cycle | |
| 2882 if( deco_status & CALC_NORM ) NDL_tissue_start_norm = NDL_tissue_lead; | |
| 2883 else NDL_tissue_start_alt = NDL_tissue_lead; | |
| 2884 | |
| 631 | 2885 // done with calculating NDL time, next phase will calculate the ascent / cave return |
| 2886 next_planning_phase = PHASE_70_ASCENT_OR_RETURN; | |
| 623 | 2887 } |
| 2888 | |
| 2889 break; | |
| 2890 | |
| 2891 | |
| 560 | 2892 // |
| 631 | 2893 //---- Ascent or Return (cave mode) ------------------------------------------------------- |
| 560 | 2894 // |
| 631 | 2895 case PHASE_70_ASCENT_OR_RETURN: |
| 2896 | |
| 560 | 2897 { |
| 631 | 2898 overlay unsigned char doing_deco_stop = 0; |
| 2899 overlay unsigned char doing_gas_change = 0; | |
| 2900 | |
| 2901 | |
| 2902 // target simulated tissues, default is 1 minute interval | |
| 2903 tissue_increment = 1; | |
| 2904 | |
| 2905 // check if a deco stop is required: | |
| 2906 // - stays at the current depth if a stop is required, | |
| 2907 // - ascents to the next stop if possible, else | |
| 2908 // - ascents by 1 meter | |
| 2909 if( find_next_stop() ) | |
| 2910 { | |
| 2911 //---- stop required -------------------- | |
| 2912 | |
| 2913 // memorize doing a deco stop | |
| 2914 doing_deco_stop = 1; | |
| 2915 | |
| 2916 // set flag for deco stops found | |
| 2917 if( deco_status & CALC_NORM ) deco_info |= DECO_STOPS_NORM; | |
| 2918 else deco_info |= DECO_STOPS_ALT; | |
| 2919 | |
| 2920 // encountered a deco stop, so switch to deco usage rate (SAC deco) | |
| 2921 gas_needs_usage_rate = char_I_SAC_deco; | |
| 2922 | |
| 2923 // check if there is a better gas to switch to | |
| 2924 if( gas_find_best() ) | |
| 2925 { | |
| 2926 // YES - memorize doing a gas change | |
| 2927 doing_gas_change = 1; | |
| 2928 | |
| 2929 // take the gas | |
| 2930 gas_take_best(); | |
| 2931 | |
| 2932 // set the new calculation ratios for N2, He and O2 | |
| 2933 gas_set_ratios(); | |
| 2934 | |
| 2935 // add the gas change time to the stop time | |
| 2936 tissue_increment += char_I_gas_change_time; | |
| 2937 } | |
| 2938 | |
| 2939 // add the stop to an existing stop or add a new stop | |
| 2940 update_deco_table(tissue_increment); | |
| 2941 } | |
| 2942 else | |
| 623 | 2943 { |
| 631 | 2944 //---- no stop required ----------------- |
| 2945 | |
| 2946 // switch to 1/10 minute interval | |
| 2947 tissue_increment = 0; | |
| 2948 | |
| 2949 // memorize not doing a deco stop | |
| 2950 doing_deco_stop = 0; | |
| 2951 | |
| 2952 // check if there is a better gas to switch to, but only: | |
| 2953 // | |
| 2954 // if extended stops are activated OR if cave mode is enabled OR if in bailout | |
| 2955 // AND if the actual depth (char_depth_start) is deeper or at the change | |
| 2956 // depth of the better gas (change depth has not been passed yet) *) | |
| 2957 // AND if the depth of the last stop is above (shallower) or at the change | |
| 2958 // depth of the better gas (do not switch on final ascent) *) | |
| 2959 // | |
| 2960 // *) skipped when calculating in cave mode | |
| 2961 // | |
| 2962 // Attention: do not use a && formula over all 'if' terms, the | |
| 2963 // conditions need to be evaluated in the given order! | |
| 2964 // | |
| 2965 if( ( main_status & EXTENDED_STOPS ) | |
| 2966 || ( main_status & CAVE_MODE ) | |
| 2967 || ( deco_status & BAILOUT_MODE ) | |
| 2968 ) | |
| 2969 if( gas_find_best() ) | |
| 2970 #ifdef _cave_mode | |
| 2971 if( ( char_depth_start >= sim_gas_best_depth ) || ( main_status & CAVE_MODE ) ) | |
| 2972 if( ( char_I_last_stop_depth <= sim_gas_best_depth ) || ( main_status & CAVE_MODE ) ) | |
| 2973 #else | |
| 2974 if( ( char_depth_start >= sim_gas_best_depth ) ) | |
| 2975 if( ( char_I_last_stop_depth <= sim_gas_best_depth ) ) | |
| 2976 #endif | |
| 623 | 2977 { |
| 631 | 2978 // YES - memorize doing a gas change |
| 2979 doing_gas_change = 1; | |
| 2980 | |
| 2981 // take the gas | |
| 2982 gas_take_best(); | |
| 2983 | |
| 2984 // set the new calculation values for N2, He and O2 | |
| 2985 gas_set_ratios(); | |
| 2986 | |
| 2987 // create a stop lasting the gas change time | |
| 628 | 2988 tissue_increment = char_I_gas_change_time; |
| 2989 | |
| 631 | 2990 // if in deco and run from the deco calculator: |
| 2991 // create a stop for the gas change in the stops table | |
| 2992 if( !NDL_time && (deco_status & DECO_CALCULATOR_MODE) ) | |
| 2993 update_deco_table(tissue_increment); | |
| 2994 } | |
| 2995 | |
| 2996 } // stop / no stop | |
| 2997 | |
| 2998 | |
| 2999 #ifdef _cave_mode | |
| 3000 // cave mode actions | |
| 3001 if( main_status & CAVE_MODE ) | |
| 3002 { | |
| 3003 // doing a deco stop? | |
| 3004 if( doing_deco_stop ) | |
| 3005 { | |
| 3006 // YES - not moving, reset the 1/10 minute steps counter | |
| 3007 backtrack_step_counter = 10; | |
| 623 | 3008 } |
| 631 | 3009 else |
| 3010 { | |
| 3011 // NO - on the move, switch back to SAC work | |
| 3012 gas_needs_usage_rate = char_I_SAC_work; | |
| 3013 | |
| 3014 // - decrement the 1/10 minute steps counter if not already zero | |
| 3015 if( backtrack_step_counter ) backtrack_step_counter--; | |
| 3016 | |
| 3017 // - target backtracking depth reached? | |
| 3018 if( char_depth_sim == backtrack_target_depth ) | |
| 3019 { | |
| 3020 // YES - on target depth | |
| 3021 | |
| 3022 // target depth reached within first 1/10 minute? | |
| 3023 if( backtrack_step_counter == 9 ) | |
| 3024 { | |
| 3025 // YES - will not change depth any more while remaining 9/10 of | |
| 3026 // the minute, so can do the full full minute in one step | |
| 3027 tissue_increment = 1; | |
| 3028 backtrack_step_counter = 0; | |
| 3029 } | |
| 3030 | |
| 3031 // on the move for a minute or more now? | |
| 3032 // (incl. doing full minute in one step) | |
| 3033 if( backtrack_step_counter == 0 ) | |
| 3034 { | |
| 3035 // YES - get the data of the next backtracking data set | |
| 3036 read_backtrack_data(); | |
| 3037 } | |
| 3038 } | |
| 3039 } | |
| 3040 } // cave mode | |
| 3041 #endif | |
| 628 | 3042 |
| 3043 // shall calculate gas needs? | |
| 3044 if( main_status & CALC_VOLUME ) | |
| 3045 { | |
| 650 | 3046 overlay unsigned char index_last_gas = sim_gas_last_num - 1; |
| 3047 overlay unsigned char index_curr_gas = sim_gas_current_num - 1; | |
| 631 | 3048 |
| 3049 #ifdef _cave_mode | |
| 3050 // in cave mode? | |
| 3051 if( main_status & CAVE_MODE ) | |
| 3052 { | |
| 3053 // YES - set the depth for the gas needs calculation to the | |
| 3054 // simulated stop / on-the-move depth | |
| 3055 gas_needs_depth = char_depth_sim; | |
| 3056 } | |
| 3057 else | |
| 3058 #endif | |
| 3059 { | |
| 3060 // NO - set the depth for the gas needs calculation to the shallower | |
| 3061 // one of the actual depth (char_depth_start, current real depth) | |
| 3062 // and the simulated (stop) depth, as we may on purpose dive | |
| 3063 // shallower than we should to conserve on a low running gas supply | |
| 3064 gas_needs_depth = ( char_depth_start < char_depth_sim ) ? | |
| 3065 char_depth_start : char_depth_sim; | |
| 3066 } | |
| 3067 | |
| 3068 // doing a gas change and a gas change time is set? | |
| 3069 if( doing_gas_change && char_I_gas_change_time ) | |
| 628 | 3070 { |
| 3071 // YES - set time it takes for switching the gas | |
| 3072 gas_needs_time = char_I_gas_change_time; | |
| 3073 | |
| 631 | 3074 // - calculate required gas volume for the gas change |
| 3075 calc_required_volume(); | |
| 3076 | |
| 3077 // - add gas change demand to overall demand on the last gas | |
| 3078 if( sim_gas_last_num ) gas_volume_need[index_last_gas] += gas_needs_volume_due; | |
| 3079 | |
| 3080 // - add gas change demand to overall demand on the current gas | |
| 3081 if( sim_gas_current_num ) gas_volume_need[index_curr_gas] += gas_needs_volume_due; | |
| 628 | 3082 } |
| 3083 | |
| 631 | 3084 // current gas is 1-5 ? (i.e. not 0 aka gas 6) |
| 628 | 3085 if( sim_gas_current_num ) |
| 3086 { | |
| 631 | 3087 // set time: doing a deco stop -> 1 minute, encoded by tissue_increment = 1 |
| 3088 // no deco stop -> 1/10 minute, encoded by tissue_increment = 0 | |
| 628 | 3089 gas_needs_time = tissue_increment; |
| 3090 | |
| 631 | 3091 // calculate required gas volume for the stop, ascent or travel |
| 3092 calc_required_volume(); | |
| 628 | 3093 |
| 3094 // add the demand to the overall demand on the current gas | |
| 631 | 3095 gas_volume_need[index_curr_gas] += gas_needs_volume_due; |
| 628 | 3096 } |
| 631 | 3097 |
| 3098 #ifdef _gas_contingency | |
| 3099 // in gas contingency mode? | |
| 3100 if( main_status & GAS_CONTINGENCY ) | |
| 628 | 3101 { |
| 631 | 3102 overlay unsigned char all_peer_tanks_used_up = 1; |
| 3103 | |
| 3104 | |
| 3105 // when doing a gas change and the there is an overdraw on the last gas, | |
| 3106 // then transfer the overdraw to the current gas if the current gas has | |
| 3107 // an equal or deeper change depth than the overdrawn gas | |
| 3108 if( doing_gas_change ) | |
| 3109 if( gas_volume_need[index_last_gas] >= gas_volume_avail[index_last_gas] ) | |
| 3110 if( char_I_deco_gas_change[index_last_gas] <= char_I_deco_gas_change[index_curr_gas] ) | |
| 3111 { | |
| 3112 overlay float overdraw; | |
| 3113 | |
| 3114 // calculate overdraw | |
| 3115 overdraw = gas_volume_need[index_last_gas] - gas_volume_avail[index_last_gas]; | |
| 3116 | |
| 3117 // transfer overdraw | |
| 3118 gas_volume_need[index_last_gas] -= overdraw; | |
| 3119 gas_volume_need[index_curr_gas] += overdraw; | |
| 3120 | |
| 3121 // tag last gas as fully used up | |
| 3122 deco_gas_type[index_last_gas] |= GAS_FULLY_USED_UP; | |
| 3123 } | |
| 3124 | |
| 3125 // if there are peer tanks with the current gas (i.e. other tanks that have the same | |
| 3126 // change depth), check if there is at least one tank that is not yet fully used up | |
| 3127 if( peer_tank[index_curr_gas] ) | |
| 628 | 3128 { |
| 631 | 3129 // scan all tanks |
| 3130 for( i = 0; i < NUM_GAS; i++ ) | |
| 3131 { | |
| 3132 // check if | |
| 3133 // - tank is a peer tank | |
| 3134 // - tank is currently neither staged nor lost | |
| 3135 // - tank is not fully used up yet | |
| 3136 if( (peer_tank[index_curr_gas] & (1 << i) ) ) | |
| 3137 if( !(deco_gas_type[i] & GAS_AVAIL_MASK ) ) | |
| 3138 if( !(deco_gas_type[i] & GAS_FULLY_USED_UP) ) | |
| 3139 { | |
| 3140 // found a peer tank that is available and not fully used up yet | |
| 3141 all_peer_tanks_used_up = 0; | |
| 3142 } | |
| 3143 } | |
| 3144 } | |
| 3145 | |
| 3146 // select which threshold is sensible to check for | |
| 3147 if ( deco_gas_type[index_curr_gas] & GAS_FULLY_USED_UP ) | |
| 3148 { | |
| 3149 // already found as fully used up, nothing to do any more | |
| 628 | 3150 } |
| 631 | 3151 else if( deco_gas_type[index_curr_gas] & GAS_NEARLY_USED_UP ) |
| 3152 { | |
| 3153 // check for fully used up threshold | |
| 3154 if( gas_volume_need[index_curr_gas] >= gas_volume_avail[index_curr_gas] ) | |
| 3155 { | |
| 3156 // tag the gas as fully used up | |
| 3157 deco_gas_type[index_curr_gas] |= GAS_FULLY_USED_UP; | |
| 3158 | |
| 3159 // set warning if all peer tanks are fully used up, too | |
| 3160 if( all_peer_tanks_used_up ) deco_gas_type[index_curr_gas] |= GAS_NEED_WARNING; | |
| 3161 } | |
| 3162 } | |
| 3163 else | |
| 3164 { | |
| 3165 // check for nearly used up threshold | |
| 3166 if( gas_volume_need[index_curr_gas] >= gas_volume_atten[index_curr_gas] ) | |
| 3167 { | |
| 3168 // tag the gas as nearly used up | |
| 3169 deco_gas_type[index_curr_gas] |= GAS_NEARLY_USED_UP; | |
| 3170 | |
| 3171 // set attention if all peer tanks are already fully used up | |
| 3172 if( all_peer_tanks_used_up ) deco_gas_type[index_curr_gas] |= GAS_NEED_ATTENTION; | |
| 3173 } | |
| 3174 } | |
| 3175 } | |
| 3176 #endif // _gas_contingency | |
| 3177 | |
| 3178 } // gas needs | |
| 3179 | |
| 3180 // update the total stops time | |
| 3181 if( doing_deco_stop ) | |
| 628 | 3182 { |
| 631 | 3183 // total stops time is counted in full minutes, add 1 minute |
| 3184 TST_time += 1; | |
| 3185 } | |
| 3186 | |
| 3187 // update the total ascent / cave return time | |
| 3188 if( tissue_increment ) | |
| 3189 { | |
| 3190 // total time to surface is counted in 1/10 minutes, add 1 minute | |
| 3191 TTS_time += 10 * tissue_increment; | |
| 3192 } | |
| 3193 else | |
| 3194 { | |
| 3195 // total time to surface is counted in 1/10 minutes, add 1/10 minute | |
| 3196 TTS_time += 1; | |
| 3197 } | |
| 3198 | |
| 650 | 3199 } |
| 631 | 3200 |
| 3201 // calculate absolute pressure at the current depth | |
| 3202 sim_pres_respiration = (float)char_depth_sim * METER_TO_BAR + pres_surface; | |
| 582 | 3203 |
| 604 | 3204 // compute current ppO2, ppN2 and ppHe |
| 582 | 3205 calc_alveolar_pressures(); |
| 3206 | |
| 560 | 3207 // update the tissues |
| 582 | 3208 calc_tissues(); |
| 604 | 3209 |
| 631 | 3210 // update the CNS |
| 604 | 3211 calc_CNS(); |
| 623 | 3212 |
| 631 | 3213 // finish stops calculation if the surface is reached or |
| 3214 // if the deco table is full / calculations took too long | |
| 3215 if( (char_depth_sim == 0) || (deco_warnings & DECO_WARNING_INCOMPLETE) ) | |
| 3216 next_planning_phase = PHASE_80_RESULTS; | |
| 623 | 3217 |
| 3218 break; | |
| 3219 | |
| 3220 | |
| 3221 /// | |
| 3222 //--- Results - Initialization ------------------------------------------------------------ | |
| 3223 // | |
| 628 | 3224 case PHASE_80_RESULTS: |
| 3225 | |
| 3226 // convert the CNS value to integer | |
| 3227 convert_sim_CNS_for_display(); | |
| 3228 | |
| 631 | 3229 // limit total time to surface to display max. and rescale to full minutes |
| 3230 if( TTS_time < 9995 ) TTS_time = (TTS_time + 5) / 10; | |
| 3231 else TTS_time = 999 | INT_FLAG_INVALID; | |
| 3232 | |
| 650 | 3233 // limit total stops time to display max. |
| 3234 if( TST_time > 999 ) TST_time = 999 | INT_FLAG_INVALID; | |
| 623 | 3235 |
| 631 | 3236 // normal or alternative plan? |
| 623 | 3237 if( deco_status & CALC_NORM ) |
| 3238 { | |
| 650 | 3239 // normal plan |
| 3240 | |
| 3241 // export the integer CNS value | |
| 3242 int_O_CNS_norm = int_sim_CNS_fraction; | |
| 3243 | |
| 3244 // publish the stops table to the display functions | |
| 3245 publish_deco_table(); | |
| 3246 | |
| 3247 // When in deco and the ceiling depth is > 0 but the deco calculation | |
| 3248 // reveals no distinct deco stop yet because the deco obligation will | |
| 3249 // vanish during the ascent, create an artificial stop to signal that | |
| 3250 // expedite surfacing ("popping up") is not allowed any more. | |
| 3251 if( char_O_deco_depth[0] == 0 ) // simulated ascent reveals no required stops | |
| 3252 if( int_O_ceiling > 0 ) // real tissues have a ceiling | |
| 3253 { | |
| 3254 // set a pro forma stop at the configured last stop depth | |
| 3255 char_O_deco_depth[0] = char_I_last_stop_depth; | |
| 3256 | |
| 3257 // set a stop time of 0 minutes, this will be displayed as "..'" | |
| 3258 char_O_deco_time[0] = 0; | |
| 3259 } | |
| 3260 | |
| 3261 // within NDL? | |
| 3262 if ( NDL_time ) | |
| 3263 { | |
| 3264 // YES - output the NDL and TTS time | |
| 3265 int_O_NDL_norm = NDL_time; | |
| 3266 int_O_TTS_norm = TTS_time; | |
| 3267 | |
| 3268 // clear the stops time | |
| 3269 int_O_TST_norm = 0; | |
| 3270 } | |
| 3271 else | |
| 3272 { | |
| 3273 // NO - clear the normal NDL time | |
| 3274 int_O_NDL_norm = 0; | |
| 3275 | |
| 3276 // export the TTS and total stops time | |
| 3277 int_O_TTS_norm = TTS_time; | |
| 3278 int_O_TST_norm = TST_time; | |
| 3279 } | |
| 623 | 3280 } |
| 3281 else | |
| 3282 { | |
| 650 | 3283 // alternative plan |
| 3284 | |
| 3285 // export the integer CNS value | |
| 3286 int_O_CNS_alt = int_sim_CNS_fraction; | |
| 3287 | |
| 3288 // within NDL? | |
| 3289 if ( NDL_time ) | |
| 3290 { | |
| 3291 // YES - output the NDL time | |
| 3292 int_O_NDL_alt = NDL_time; | |
| 3293 int_O_TTS_alt = TTS_time; | |
| 3294 | |
| 3295 // clear the alternative TTS and stops time | |
| 3296 int_O_TST_alt = 0 + INT_FLAG_ZERO; | |
| 3297 } | |
| 3298 else | |
| 3299 { | |
| 3300 // NO - clear the alternative NDL time | |
| 3301 int_O_NDL_alt = 0; | |
| 3302 | |
| 3303 // export the TTS and total stops time | |
| 3304 int_O_TTS_alt = TTS_time; | |
| 3305 int_O_TST_alt = TST_time; | |
| 3306 } | |
| 623 | 3307 } |
| 3308 | |
| 650 | 3309 // export deco infos and warnings |
| 3310 char_O_deco_info = deco_info; | |
| 3311 char_O_deco_warnings = deco_warnings; | |
| 3312 | |
| 623 | 3313 // The next calculation phase will |
| 631 | 3314 // - convert the gas needs from volume to pressure if gas needs calculation is configured |
| 3315 // - else finish the calculation cycle | |
| 650 | 3316 if ( main_status & CALC_VOLUME ) next_planning_phase = PHASE_81_GAS_NEEDS_PRESSURES; |
| 631 | 3317 else next_planning_phase = PHASE_90_FINISH; |
| 623 | 3318 |
| 3319 break; | |
| 3320 | |
| 3321 | |
| 3322 // | |
| 628 | 3323 //--- Results - convert Gas Needs Volumes to Pressures ------------------------------------ |
| 623 | 3324 // |
| 650 | 3325 case PHASE_81_GAS_NEEDS_PRESSURES: |
| 3326 | |
| 3327 // step through all gases | |
| 3328 for (i = 0; i < NUM_GAS; i++) | |
| 3329 { | |
| 3330 // convert required volume of the gas into the respective pressure and set the flags | |
| 3331 convert_volume_to_pressure(i); | |
| 3332 } | |
| 623 | 3333 |
| 631 | 3334 #ifdef _cave_mode |
| 650 | 3335 // tag gas needs as calculated in cave mode or in open water mode |
| 3336 if( main_status & CAVE_MODE ) deco_info |= GAS_NEEDS_CAVE; | |
| 3337 else deco_info &= ~GAS_NEEDS_CAVE; | |
| 3338 | |
| 3339 // export updated deco info | |
| 3340 char_O_deco_info = deco_info; | |
| 3341 #endif | |
| 631 | 3342 |
| 3343 // advance to next calculation phase | |
| 3344 next_planning_phase = PHASE_90_FINISH; | |
| 3345 | |
| 3346 break; | |
| 3347 | |
| 623 | 3348 // |
| 3349 //--- finish Calculation Cycle ------------------------------------------------------------ | |
| 3350 // | |
| 3351 case PHASE_90_FINISH: | |
| 3352 | |
| 3353 // Check if deco obligation is steady state or decreasing. | |
| 631 | 3354 // Update the result only: |
| 3355 // - if an alternative plan is enabled, and | |
| 3356 // - if a valid alternative plan TTS exists, and | |
| 3357 // - if it is not a bailout plan | |
| 3358 if( (deco_status & CALC_ALT ) ) | |
|
635
9a64914a8fca
3.10 re-release addressing some minor issues and language fixes
heinrichsweikamp
parents:
634
diff
changeset
|
3359 if( !(int_O_TTS_alt & INT_FLAG_INVALID) ) |
| 631 | 3360 if( !(deco_status & BAILOUT_MODE ) ) |
| 623 | 3361 { |
| 650 | 3362 if ( int_O_TTS_alt < int_O_TTS_norm ) deco_info |= DECO_ZONE; |
| 3363 else if( int_O_TTS_alt > int_O_TTS_norm ) deco_info &= ~DECO_ZONE; | |
| 3364 | |
| 3365 // export updated deco info | |
| 3366 char_O_deco_info = deco_info; | |
| 623 | 3367 } |
| 3368 | |
| 650 | 3369 // restore command flag to indicate that the deco calculation cycle has finished |
| 3370 char_O_deco_status = deco_status; | |
| 623 | 3371 |
| 3372 // signal end of deco calculation | |
| 3373 next_planning_phase = PHASE_00_DONE; | |
| 3374 | |
| 3375 break; | |
| 3376 | |
| 3377 } // switch | |
| 3378 | |
| 3379 // read timer 5, result will be stored in tmr5_value (in 1/32 ms) and tmr5_overflow | |
| 3380 read_tmr5(); | |
| 3381 | |
| 3382 } // sequence calculation phases while not timed out and calculation cycle is not finished | |
| 3383 while( (tmr5_overflow == 0) && ( next_planning_phase != PHASE_00_DONE ) ); | |
| 3384 | |
| 3385 // report where we are in terms of depth reached, used in deco calculator to show deco calculation progress | |
| 3386 char_O_depth_sim = char_depth_sim; | |
| 3387 | |
| 3388 | |
| 3389 #ifdef _profiling | |
| 3390 | |
| 3391 //---- Performance Measurement ------------------------------------------- | |
| 3392 | |
| 3393 // convert timer 5 readout into ms | |
| 3394 profiling_runtime = tmr5_value / 32; | |
| 3395 | |
| 3396 // actual runtime longer than target runtime? | |
| 3397 if( tmr5_overflow ) | |
| 3398 { | |
| 3399 // YES - report excess | |
| 3400 int_O_profiling_overrun = profiling_runtime; | |
| 3401 | |
| 3402 // - excess > max we had so far? | |
| 3403 if( int_O_profiling_overrun > int_O_profiling_overrun_max ) | |
| 3404 { | |
| 3405 // YES - update max | |
| 3406 int_O_profiling_overrun_max = int_O_profiling_overrun; | |
| 3407 | |
| 3408 // - store the causing phase | |
| 3409 char_O_profiling_overrun_phase = profiling_phase; | |
| 3410 } | |
| 560 | 3411 } |
| 623 | 3412 else |
| 3413 { | |
| 3414 // NO - calculate unused budget and flag it to be under-run time | |
| 3415 int_O_profiling_overrun = (2048 - profiling_runtime) | 0x8000; | |
| 3416 } | |
| 3417 | |
| 3418 // increment number of runs in current cycle | |
| 3419 profiling_runs += 1; | |
| 3420 | |
| 3421 // planning cycle completed? | |
| 3422 if( next_planning_phase == PHASE_00_DONE ) | |
| 3423 { | |
| 3424 // YES - export number of runs it took | |
| 3425 if( deco_status & COMPLETED_NORM ) char_O_profiling_runs_norm = profiling_runs; | |
| 3426 else char_O_profiling_runs_alt = profiling_runs; | |
| 3427 } | |
| 3428 | |
| 3429 #endif | |
| 3430 | |
| 3431 } | |
| 3432 | |
| 3433 | |
| 3434 ////////////////////////////////////////////////////////////////////////////// | |
| 3435 // calc_hauptroutine_data_input | |
| 3436 // | |
| 3437 // Set all C-code dive parameters from their ASM-code values. | |
| 3438 // | |
| 3439 void calc_hauptroutine_data_input(void) | |
| 3440 { | |
| 3441 overlay float IG_ratio; | |
| 3442 | |
| 3443 // safeguard and convert the surface pressure (mbar -> bar) (*) | |
| 3444 if( int_I_pres_surface < 500 ) pres_surface = 0.500; | |
| 3445 else pres_surface = 0.001 * int_I_pres_surface; | |
| 3446 | |
| 3447 // safeguard and convert the current real pressure | |
| 3448 if( int_I_pres_respiration < 500 ) real_pres_respiration = 0.500; | |
| 3449 else real_pres_respiration = 0.001 * int_I_pres_respiration; | |
| 3450 | |
| 3451 // safeguard further parameters to protect the tissue-flag and the stop table | |
| 3452 if( char_I_sim_advance_time > 127 ) char_I_sim_advance_time = 127; | |
| 3453 if( char_I_extra_time > 127 ) char_I_extra_time = 127; | |
| 3454 if( char_I_gas_change_time > 99 ) char_I_gas_change_time = 99; | |
| 3455 | |
| 628 | 3456 |
| 3457 // compute the depth in meters where we are now | |
| 3458 float_depth_real = (real_pres_respiration - pres_surface) * BAR_TO_METER; | |
| 3459 | |
| 3460 // convert to integer and round up to next full meter | |
| 3461 char_depth_real = (unsigned char)(float_depth_real + 0.99); | |
| 3462 | |
| 3463 | |
| 623 | 3464 // calculate partial pressure of N2 in respired air at surface pressure |
| 3465 calc_N2_equilibrium(); | |
| 3466 | |
| 3467 // get, safeguard and convert the saturation and desaturation factors | |
| 3468 get_saturation_factors(); | |
| 3469 | |
| 3470 #ifdef _ccr_pscr | |
| 3471 // compute a factor that will be used later on for pSCR ppO2 drop calculation (*) | |
| 3472 float_pSCR_factor = 0.01 * char_I_PSCR_drop * char_I_PSCR_lungratio; | |
| 3473 #endif | |
| 3474 | |
| 3475 #ifdef _helium | |
| 3476 // get the currently breathed gas ratios | |
| 3477 real_O2_ratio = 0.01 * char_I_O2_ratio; | |
| 3478 real_He_ratio = 0.01 * char_I_He_ratio; | |
| 3479 | |
| 3480 // calculate the inert gas ratio (local helper variable) | |
| 3481 IG_ratio = 1.00 - real_O2_ratio; | |
| 3482 | |
| 3483 // calculate the N2 ratio | |
| 3484 real_N2_ratio = IG_ratio - real_He_ratio; | |
| 3485 #else | |
| 3486 // get the currently breathed O2 ratio | |
| 3487 real_O2_ratio = 0.01 * char_I_O2_ratio; | |
| 3488 | |
| 3489 // set the He ratio to zero | |
| 3490 real_He_ratio = 0.0; | |
| 3491 | |
| 3492 // calculate the N2 / inert gas ratio | |
| 3493 real_N2_ratio = IG_ratio = 1.00 - real_O2_ratio; | |
| 3494 #endif // _helium | |
| 3495 | |
| 3496 #ifdef _ccr_pscr | |
| 3497 // calculate ppO2 drop in pSCR loop for real tissues | |
| 3498 real_pSCR_drop = IG_ratio * float_pSCR_factor; | |
| 3499 #endif | |
| 3500 | |
| 0 | 3501 } |
| 3502 | |
| 3503 | |
| 3504 ////////////////////////////////////////////////////////////////////////////// | |
| 582 | 3505 // calc_tissues |
| 0 | 3506 // |
| 623 | 3507 // INPUT: ppN2 partial pressure of inspired N2 |
| 3508 // ppHe partial pressure of inspired He | |
| 631 | 3509 // tissue_increment tissue selector (real or simulated) and interval time |
| 623 | 3510 // |
| 3511 // MODIFIED: real_pres_tissue_N2[] tissue N2 pressures (in real tissues context) | |
| 3512 // real_pres_tissue_He[] tissue He pressures (in real tissues context) | |
| 3513 // sim_pres_tissue_N2[] tissue N2 pressures (in simulated tissues context) | |
| 3514 // sim_pres_tissue_He[] tissue He pressures (in simulated tissues context) | |
| 3515 // | |
| 3516 // OUTPUT: char_O_tissue_pres_N2[] tissue N2 pressures scaled for display purpose (in real tissues context) | |
| 3517 // char_O_tissue_pres_He[] tissue He pressures scaled for display purpose (in real tissues context) | |
| 3518 // char_O_tissue_pressure[] combined tissue pressures scaled for display purpose (in real tissue context) | |
| 582 | 3519 // |
| 3520 static void calc_tissues() | |
| 0 | 3521 { |
| 623 | 3522 overlay unsigned char period; |
| 650 | 3523 overlay float last_press_tissue; |
| 3524 overlay float delta_press_N2; | |
| 623 | 3525 |
| 3526 #ifdef _helium | |
| 650 | 3527 overlay float delta_press_He; |
| 623 | 3528 #endif |
| 582 | 3529 |
| 3530 assert( 0.00 <= ppN2 && ppN2 < 11.2 ); // 80% N2 at 130m | |
| 3531 assert( 0.00 <= ppHe && ppHe < 12.6 ); // 90% He at 130m | |
| 3532 | |
| 3533 | |
| 604 | 3534 for( ci=0; ci < NUM_COMP; ci++ ) // iterate through all compartments |
| 582 | 3535 { |
| 631 | 3536 i = tissue_increment & TIME_MASK; // i > 0: do a number of i full minutes |
| 3537 // I = 0: do 2 (real tissues) or 6 (simulated tissues) seconds | |
| 3538 | |
| 3539 if( i == 0 ) // check if we shall do one 2 or 6 seconds interval | |
| 560 | 3540 { |
| 631 | 3541 read_Buhlmann_times(0); // YES - program coefficients for a 2 or 6 seconds period |
| 623 | 3542 period = 1; // - set period length (in cycles) |
| 3543 i = 1; // - and one cycle to do | |
| 560 | 3544 } |
| 582 | 3545 else if( i > 9 ) // check if we can start with 10 minutes periods |
| 560 | 3546 { |
| 623 | 3547 read_Buhlmann_times(2); // YES - program coefficients for 10 minutes periods |
| 3548 period = 10; // set period length (in cycles) to ten | |
| 560 | 3549 } |
| 623 | 3550 else // last but not lease, do 1 to 9 minutes |
| 560 | 3551 { |
| 623 | 3552 read_Buhlmann_times(1); // NO - program coefficients for 1 minute periods |
| 3553 period = 1; // - set period length (in cycles) to one | |
| 560 | 3554 } |
| 3555 | |
| 3556 do | |
| 3557 { | |
| 650 | 3558 if (tissue_increment & TISSUE_SELECTOR) |
| 560 | 3559 { |
| 650 | 3560 //---- real N2 -------------------------------------- |
| 3561 | |
| 3562 // get the real tissue pressure | |
| 3563 last_press_tissue = real_pres_tissue_N2[ci]; | |
| 3564 | |
| 3565 // calculate the pressure change | |
| 3566 temp_tissue = (ppN2 - last_press_tissue) * var_N2_e; | |
| 3567 | |
| 3568 // apply the saturation / desaturation factor on temp_tissue | |
| 3569 apply_saturation_factors(); | |
| 3570 | |
| 3571 // store the pressure change for IBCD check and tissue graphics | |
| 3572 delta_press_N2 = temp_tissue; | |
| 3573 | |
| 3574 // update the delta accumulator | |
| 3575 real_pres_delta_N2[ci] += temp_tissue; | |
| 3576 | |
| 3577 // update the real tissue pressure | |
| 3578 real_pres_tissue_N2[ci] += real_pres_delta_N2[ci]; | |
| 3579 | |
| 3580 // reduce the delta accumulator | |
| 3581 real_pres_delta_N2[ci] -= real_pres_tissue_N2[ci] - last_press_tissue; | |
| 3582 | |
| 3583 #ifdef _helium | |
| 3584 //---- real He -------------------------------------- | |
| 3585 | |
| 3586 // get the real tissue pressure | |
| 3587 last_press_tissue = real_pres_tissue_He[ci]; | |
| 3588 | |
| 3589 // calculate the pressure change | |
| 3590 temp_tissue = (ppHe - last_press_tissue) * var_He_e; | |
| 3591 | |
| 3592 // apply the saturation / desaturation factor on temp_tissue | |
| 3593 apply_saturation_factors(); | |
| 3594 | |
| 3595 // store the pressure change for IBCD check and tissue graphics | |
| 3596 delta_press_He = temp_tissue; | |
| 3597 | |
| 3598 // update the delta accumulator | |
| 3599 real_pres_delta_He[ci] += temp_tissue; | |
| 3600 | |
| 3601 // update the real tissue pressure | |
| 3602 real_pres_tissue_He[ci] += real_pres_delta_He[ci]; | |
| 3603 | |
| 3604 // reduce the delta accumulator | |
| 3605 real_pres_delta_He[ci] -= real_pres_tissue_He[ci] - last_press_tissue; | |
| 3606 #endif | |
| 560 | 3607 } |
| 3608 else | |
| 3609 { | |
| 650 | 3610 //---- simulated N2 --------------------------------- |
| 3611 | |
| 3612 // get the simulated tissue pressure | |
| 3613 last_press_tissue = sim_pres_tissue_N2[ci]; | |
| 3614 | |
| 3615 // calculate the pressure change | |
| 3616 temp_tissue = (ppN2 - last_press_tissue) * var_N2_e; | |
| 3617 | |
| 3618 // apply the saturation / desaturation factor on temp_tissue | |
| 3619 apply_saturation_factors(); | |
| 3620 | |
| 3621 // update the delta accumulator | |
| 3622 sim_pres_delta_N2[ci] += temp_tissue; | |
| 3623 | |
| 3624 // update the simulated tissue pressure | |
| 3625 sim_pres_tissue_N2[ci] += sim_pres_delta_N2[ci]; | |
| 3626 | |
| 3627 // reduce the delta accumulator | |
| 3628 sim_pres_delta_N2[ci] -= sim_pres_tissue_N2[ci] - last_press_tissue; | |
| 560 | 3629 |
| 623 | 3630 #ifdef _helium |
| 650 | 3631 //---- simulated He --------------------------------- |
| 3632 | |
| 3633 // get the simulated tissue pressure | |
| 3634 last_press_tissue = sim_pres_tissue_He[ci]; | |
| 3635 | |
| 3636 // calculate the pressure change | |
| 3637 temp_tissue = (ppHe - last_press_tissue) * var_He_e; | |
| 3638 | |
| 3639 // apply the saturation / desaturation factor on temp_tissue | |
| 3640 apply_saturation_factors(); | |
| 3641 | |
| 3642 // update the delta accumulator | |
| 3643 sim_pres_delta_He[ci] += temp_tissue; | |
| 3644 | |
| 3645 // update the simulated tissue pressure | |
| 3646 sim_pres_tissue_He[ci] += sim_pres_delta_He[ci]; | |
| 3647 | |
| 3648 // reduce the delta accumulator | |
| 3649 sim_pres_delta_He[ci] -= sim_pres_tissue_He[ci] - last_press_tissue; | |
| 3650 #endif | |
| 560 | 3651 } |
| 623 | 3652 |
| 3653 //---- decrement loop counter and adjust step size --------------- | |
| 560 | 3654 |
| 3655 // decrement loop counter | |
| 3656 i -= period; | |
| 582 | 3657 |
| 560 | 3658 // check if we need to switch from 10 minute periods to 1 minute periods |
| 3659 if( (i > 0) && (period = 10) && (i < 10) ) | |
| 3660 { | |
| 582 | 3661 read_Buhlmann_times(1); // program coefficients for 1 minute periods |
| 560 | 3662 period = 1; // set period length (in cycles) to one |
| 3663 } | |
| 3664 } | |
| 3665 while( i ); | |
| 582 | 3666 |
| 3667 | |
| 560 | 3668 // have the computations been done for the "real" tissues? |
| 623 | 3669 if( tissue_increment & TISSUE_SELECTOR ) |
| 560 | 3670 { |
| 623 | 3671 |
| 3672 #ifdef _helium | |
| 560 | 3673 // net tissue balance |
| 650 | 3674 temp_tissue = delta_press_N2 + delta_press_He; |
| 623 | 3675 |
| 560 | 3676 // check tissue on-/off-gassing and IBCD with applying a threshold of +/-HYST |
| 3677 // | |
| 584 | 3678 if ( temp_tissue < -HYST ) // check if the tissue is off-gassing |
| 560 | 3679 { |
| 623 | 3680 // tag tissue as not experiencing mentionable IBCD |
| 3681 IBCD_tissue_vector &= ~(1 << ci); | |
| 560 | 3682 } |
| 3683 else if ( temp_tissue > +HYST ) // check if the tissue in on-gassing | |
| 3684 { | |
| 623 | 3685 // check for counter diffusion |
| 650 | 3686 if( ((delta_press_N2 > 0.0) && (delta_press_He < 0.0)) |
| 3687 || ((delta_press_N2 < 0.0) && (delta_press_He > 0.0)) ) | |
| 560 | 3688 { |
| 623 | 3689 // tag tissue as experiencing mentionable IBCD |
| 3690 IBCD_tissue_vector |= (1 << ci); | |
| 560 | 3691 } |
| 3692 } | |
| 623 | 3693 #endif |
| 3694 | |
| 3695 // For N2 tissue pressure display purpose: | |
| 3696 | |
| 3697 // basically keep the on-gassing / off-gassing flag from last invocation, but flip | |
| 3698 // it in case the rate exceeds a set hysteresis (actual value: see #define of HYST) | |
| 3699 char_O_tissue_pres_N2[ci] &= 128; | |
| 650 | 3700 if ( delta_press_N2 > +HYST ) char_O_tissue_pres_N2[ci] = 128; // set flag for tissue pressure is increasing |
| 3701 else if ( delta_press_N2 < -HYST ) char_O_tissue_pres_N2[ci] = 0; // clear flag (-> tissue pressure is decreasing) | |
| 623 | 3702 |
| 3703 // scale N2 tissue pressure such that the surface steady-state tissue loading | |
| 3704 // of [0.7902 * (1013 hPa - ppWater)] bar will give a 8, which aligns with | |
| 3705 // the 2nd scale line. | |
| 650 | 3706 delta_press_N2 = (8 / (0.7902 * (1.013 - ppWater))) * real_pres_tissue_N2[ci]; |
| 623 | 3707 |
| 3708 // limit to 127 to protect the uppermost bit which holds the sat/desat flag | |
| 650 | 3709 if (delta_press_N2 > 127) delta_press_N2 = 127; |
| 623 | 3710 |
| 3711 // convert to integer and combine with sat/desat flag | |
| 650 | 3712 char_O_tissue_pres_N2[ci] += (unsigned char)delta_press_N2; |
| 623 | 3713 |
| 3714 #ifdef _helium | |
| 3715 | |
| 3716 // For He tissue pressure display purpose: | |
| 3717 | |
| 3718 // basically keep the on-gassing / off-gassing flag from last invocation, but flip | |
| 3719 // it in case the rate exceeds a set hysteresis (actual value: see #define of HYST) | |
| 3720 char_O_tissue_pres_He[ci] &= 128; | |
| 650 | 3721 if ( delta_press_He > +HYST ) char_O_tissue_pres_He[ci] = 128; // set flag for tissue pressure is increasing |
| 3722 else if ( delta_press_He < -HYST ) char_O_tissue_pres_He[ci] = 0; // clear flag (-> tissue pressure is decreasing) | |
| 623 | 3723 |
| 3724 // scale He tissue pressure alike it is done for N2. | |
| 3725 // With no He in a tissue, the result will be 0. | |
| 650 | 3726 delta_press_He = (8 / (0.7902 * (1.013 - ppWater))) * real_pres_tissue_He[ci]; |
| 623 | 3727 |
| 3728 // limit to 127 to protect the uppermost bit which holds the sat/desat flag | |
| 650 | 3729 if (delta_press_He > 127) delta_press_He = 127; |
| 623 | 3730 |
| 3731 // convert to integer and combine with sat/desat flag | |
| 650 | 3732 char_O_tissue_pres_He[ci] += (unsigned char)delta_press_He; |
| 623 | 3733 |
| 3734 // For combined tissue pressure display purpose: | |
| 3735 | |
| 3736 // basically keep the on-gassing / off-gassing flag from last invocation, but flip | |
| 3737 // it in case the rate exceeds a set hysteresis (actual value: see #define of HYST) | |
| 3738 char_O_tissue_pressure[ci] &= 128; | |
| 3739 if ( temp_tissue > +HYST ) char_O_tissue_pressure[ci] = 128; // set flag for tissue pressure is increasing | |
| 3740 else if ( temp_tissue < -HYST ) char_O_tissue_pressure[ci] = 0; // clear flag (-> tissue pressure is decreasing) | |
| 3741 | |
| 3742 // add the two scaled pressures. | |
| 650 | 3743 temp_tissue = delta_press_N2 + delta_press_He; |
| 623 | 3744 |
| 3745 // limit to 127 to protect the uppermost bit which holds the sat/desat flag | |
| 560 | 3746 if (temp_tissue > 127) temp_tissue = 127; |
| 3747 | |
| 623 | 3748 // convert to integer and combine with sat/desat flag |
| 3749 char_O_tissue_pressure[ci] += (unsigned char)temp_tissue; | |
| 3750 | |
| 3751 #else | |
| 3752 | |
| 3753 // He tissue pressure is zero | |
| 3754 char_O_tissue_pres_He[ci] = 0; | |
| 3755 | |
| 3756 // combined tissue pressure equals N2 tissue pressure | |
| 3757 char_O_tissue_pressure[ci] = char_O_tissue_pres_N2[ci]; | |
| 3758 | |
| 3759 #endif | |
| 3760 | |
| 582 | 3761 } //if |
| 3762 | |
| 3763 } // for | |
| 0 | 3764 } |
| 3765 | |
| 623 | 3766 |
| 0 | 3767 ////////////////////////////////////////////////////////////////////////////// |
| 3768 // calc_limit | |
| 3769 // | |
| 623 | 3770 // Input: GF_parameter gradient factor to be used, negative values activate surface mode |
| 3771 // tissue_increment selector for context: real or simulated tissues | |
| 3772 // sim_pres_tissue_N2/_He tissue pressures (used in simulated tissues context) | |
| 3773 // real_pres_tissue_N2/_He tissue pressures (used in real tissues context) | |
| 3774 // | |
| 3775 // Output: lead_supersat highest supersaturation found among all tissues, 1.0 = 100% | |
| 3776 // lead_tissue number of the leading tissue (0-15) | |
| 3777 // ceiling ceiling in bar relative pressure | |
| 3778 // | |
| 3779 // Modified: deco_warnings for IBCD, micro bubbles and outside warning (only in real tissues context) | |
| 582 | 3780 // |
| 3781 static void calc_limit(PARAMETER float GF_parameter) | |
| 0 | 3782 { |
| 631 | 3783 overlay float pres_ambient_min_overall = 0.0; |
| 3784 overlay unsigned char surface_mode = 0; // 0: off, 1: on | |
| 623 | 3785 |
| 3786 | |
| 3787 // check mode | |
| 3788 if( GF_parameter < 0 ) | |
| 3789 { | |
| 3790 // activate surface mode | |
| 3791 surface_mode = 1; | |
| 3792 | |
| 3793 // normalize parameter | |
| 3794 GF_parameter = -GF_parameter; | |
| 3795 } | |
| 3796 | |
| 3797 // set leading tissue number to tissue 1 (it has the index 0) | |
| 3798 lead_tissue = 0; | |
| 604 | 3799 |
| 3800 // initialize leading tissue supersaturation value to null | |
| 623 | 3801 lead_supersat = 0.0; |
| 3802 | |
| 3803 // next code section is relevant only when invoked on the real tissues | |
| 3804 if( tissue_increment & TISSUE_SELECTOR ) | |
| 582 | 3805 { |
| 623 | 3806 // clear IBCD, micro-bubbles and outside warning flags (locked warnings will be preserved) |
| 3807 deco_warnings &= ~( DECO_WARNING_IBCD + DECO_WARNING_MBUBBLES + DECO_WARNING_OUTSIDE + DECO_ATTENTION_OUTSIDE ); | |
| 582 | 3808 } |
| 3809 | |
| 3810 // loop over all tissues | |
| 604 | 3811 for( ci = 0; ci < NUM_COMP; ci++ ) |
| 582 | 3812 { |
| 631 | 3813 overlay float pres_ambient_min_tissue; |
| 623 | 3814 |
| 3815 | |
| 3816 // get the coefficients for tissue ci | |
| 3817 read_Buhlmann_coefficients(); | |
| 3818 | |
| 3819 #ifdef _helium | |
| 582 | 3820 |
| 3821 // get the tissue pressures | |
| 623 | 3822 // adopt_Buhlmann_coefficients needs calc_pres_tissue_N2/He when compiled for helium |
| 3823 if( tissue_increment & TISSUE_SELECTOR ) | |
| 560 | 3824 { |
| 582 | 3825 // context is real tissues |
| 623 | 3826 calc_pres_tissue_N2 = real_pres_tissue_N2[ci]; |
| 3827 calc_pres_tissue_He = real_pres_tissue_He[ci]; | |
| 560 | 3828 } |
| 3829 else | |
| 3830 { | |
| 582 | 3831 // context is simulated tissues |
| 3832 calc_pres_tissue_N2 = sim_pres_tissue_N2[ci]; | |
| 3833 calc_pres_tissue_He = sim_pres_tissue_He[ci]; | |
| 3834 } | |
| 3835 | |
| 3836 // overall tissue pressure | |
| 650 | 3837 calc_pres_tissue = calc_pres_tissue_N2 + calc_pres_tissue_He; |
| 582 | 3838 |
| 623 | 3839 #else |
| 3840 | |
| 3841 // get the tissue pressure | |
| 650 | 3842 calc_pres_tissue = ( tissue_increment & TISSUE_SELECTOR ) ? real_pres_tissue_N2[ci] : sim_pres_tissue_N2[ci]; |
| 623 | 3843 |
| 3844 #endif | |
| 582 | 3845 |
| 604 | 3846 // adopt a and b coefficients to current N2/He ratio inside the tissue |
| 3847 adopt_Buhlmann_coefficients(); | |
| 582 | 3848 |
| 3849 // next calculations are only relevant when invoked on the real tissues | |
| 623 | 3850 if( tissue_increment & TISSUE_SELECTOR ) |
| 582 | 3851 { |
| 618 | 3852 overlay float pres_tissue_max; |
| 582 | 3853 overlay float supersat; |
| 608 | 3854 overlay float baseline_threshold; |
| 604 | 3855 |
| 623 | 3856 |
| 582 | 3857 // check if tissue is in supersaturation |
| 650 | 3858 if( calc_pres_tissue > real_pres_respiration ) |
| 582 | 3859 { |
| 618 | 3860 // calculate maximum allowed tissue pressure at current ambient pressure |
| 623 | 3861 pres_tissue_max = real_pres_respiration / var_b + var_a; |
| 618 | 3862 |
| 3863 // calculate current supersaturation value (1.0 = 100%) of this tissue according to straight Buhlmann | |
| 650 | 3864 supersat = ( calc_pres_tissue - real_pres_respiration ) |
| 3865 / ( pres_tissue_max - real_pres_respiration ); | |
| 618 | 3866 |
| 623 | 3867 // calculate supersaturation value for display purpose: 1.35 = 135% = 86 pixel |
| 3868 if( supersat <= 1.35 ) char_O_tissue_saturation[ci] = (unsigned char)(supersat * 64); | |
| 3869 else char_O_tissue_saturation[ci] = 86; | |
| 3870 | |
| 582 | 3871 // memorize highest supersaturation found |
| 3872 if( supersat > lead_supersat ) lead_supersat = supersat; | |
| 3873 | |
| 608 | 3874 // tissue-dependent baseline threshold for micro bubbles and outside warnings |
| 3875 baseline_threshold = 0.02 * ci + 1.0; | |
| 3876 | |
| 3877 // micro bubbles warning: supersaturation > baseline threshold | |
| 3878 if( supersat > baseline_threshold ) | |
| 623 | 3879 deco_warnings |= (DECO_WARNING_MBUBBLES + DECO_WARNING_MBUBBLES_lock); |
| 604 | 3880 |
| 608 | 3881 // outside warning: supersaturation > baseline threshold + additional 5% margin |
| 623 | 3882 if( supersat > (baseline_threshold + 0.05) ) |
| 3883 deco_warnings |= (DECO_WARNING_OUTSIDE + DECO_WARNING_OUTSIDE_lock ); | |
| 3884 } | |
| 3885 else | |
| 3886 { | |
| 3887 // supersaturation is defined as zero while tissue pressure <= ambient pressure | |
| 3888 supersat = 0.0; | |
| 3889 char_O_tissue_saturation[ci] = 0; | |
| 560 | 3890 } |
| 623 | 3891 |
| 3892 // next only when in surface mode | |
| 3893 if( surface_mode ) | |
| 3894 { | |
| 3895 // tag tissue whether it is beyond the M-line limit or not | |
| 3896 if( supersat > 1.0 ) | |
| 3897 { | |
| 3898 char_O_tissue_pres_N2[ci] |= 128; | |
| 3899 #ifdef _helium | |
| 3900 char_O_tissue_pres_He[ci] |= 128; | |
| 3901 #endif | |
| 3902 char_O_tissue_pressure[ci] |= 128; | |
| 3903 } | |
| 3904 else | |
| 3905 { | |
| 3906 char_O_tissue_pres_N2[ci] &= ~128; | |
| 3907 #ifdef _helium | |
| 3908 char_O_tissue_pres_He[ci] &= ~128; | |
| 3909 #endif | |
| 3910 char_O_tissue_pressure[ci] &= ~128; | |
| 3911 } | |
| 3912 } | |
| 3913 } // real tissues | |
| 582 | 3914 |
| 618 | 3915 // calculate the minimum ambient pressure that the tissue can withstand |
| 631 | 3916 if( char_I_model == 0 ) |
| 618 | 3917 { |
| 3918 // straight Buhlmann | |
| 650 | 3919 pres_ambient_min_tissue = (calc_pres_tissue - var_a) * var_b; |
| 618 | 3920 } |
| 3921 else | |
| 3922 { | |
| 3923 // Buhlmann with Eric Baker's varying gradient factor correction | |
| 3924 // note: this equation [1] is the inverse of equation [2] | |
| 650 | 3925 pres_ambient_min_tissue = ( calc_pres_tissue - (var_a * GF_parameter) ) |
| 631 | 3926 / ( 1.0 - GF_parameter + (GF_parameter / var_b ) ); |
| 618 | 3927 } |
| 560 | 3928 |
| 3929 // check if this tissue requires a higher ambient pressure than was found to be needed up to now | |
| 631 | 3930 if( pres_ambient_min_tissue > pres_ambient_min_overall ) |
| 582 | 3931 { |
| 631 | 3932 pres_ambient_min_overall = pres_ambient_min_tissue; |
| 3933 lead_tissue = ci; | |
| 582 | 3934 } |
| 3935 } // for | |
| 3936 | |
| 631 | 3937 // compute ceiling in bar relative pressure |
| 3938 ceiling = pres_ambient_min_overall - pres_surface; | |
| 3939 | |
| 3940 // limit ceiling to positive values | |
| 3941 if( ceiling < 0.0 ) ceiling = 0.0; | |
| 623 | 3942 |
| 3943 #ifdef _helium | |
| 3944 // IBCD is checked for real tissues only | |
| 3945 if( tissue_increment & TISSUE_SELECTOR ) | |
| 560 | 3946 { |
| 604 | 3947 // check if the leading tissue is in IBCD condition |
| 623 | 3948 if( (IBCD_tissue_vector & (1 << lead_tissue)) |
| 3949 && ((real_pres_tissue_N2[lead_tissue] + real_pres_tissue_He[lead_tissue]) > real_pres_respiration) ) | |
| 582 | 3950 { |
| 623 | 3951 // leading tissue is in IBCD condition and in super-saturation, so issue a warning. |
| 3952 deco_warnings |= (DECO_WARNING_IBCD + DECO_WARNING_IBCD_lock); | |
| 582 | 3953 } |
| 560 | 3954 } |
| 623 | 3955 #endif |
| 3956 | |
| 0 | 3957 } |
| 623 | 3958 |
| 3959 | |
| 0 | 3960 ////////////////////////////////////////////////////////////////////////////// |
| 623 | 3961 // calc_NDL_time_tissue |
| 3962 // | |
| 3963 // calculation of the remaining no decompression limit (NDL) time for a tissue | |
| 0 | 3964 // |
| 604 | 3965 // NOTE: Erik Baker's closed formula works for Nitrox. Trimix adds a second |
| 0 | 3966 // exponential term to the M-value equation, making it impossible to |
| 623 | 3967 // invert. So we have to solve the problem with a search approach. |
| 3968 // | |
| 3969 // Input: NDL_tissue tissue for which to calculate remaining NDL time | |
| 3970 // GF_high gradient factor used when GF factors are enabled | |
| 3971 // ppN2, ppHe partial pressures of N2 and He breathed | |
| 3972 // | |
| 3973 // Modified: NDL_time shortest NDL time found so far | |
| 3974 // NDL_tissue_lead leading tissue, i.e. tissue with the shortest NDL | |
| 3975 // | |
| 3976 static void calc_NDL_time_tissue(void) | |
| 0 | 3977 { |
| 623 | 3978 overlay unsigned char NDL_time_tissue = 0; // NDL time of this tissue, starting with 0 minutes |
| 3979 overlay unsigned char step_size = 10; // step size in searching, starting with 10 minutes | |
| 3980 overlay float pres_limit; // max. tissue pressure allowed | |
| 3981 | |
| 3982 #ifdef _helium | |
| 3983 overlay float last_pres_tissue_N2; // last tissue pressure for N2 | |
| 3984 overlay float last_pres_tissue_He; // last tissue pressure for He | |
| 3985 #else | |
| 3986 overlay float last_pres_tissue; // last tissue pressure | |
| 3987 #endif | |
| 3988 | |
| 3989 | |
| 3990 // set the compartment index ci for reading the Buhlmann increments and coefficients | |
| 3991 ci = NDL_tissue; | |
| 3992 | |
| 3993 // read the tissue increments for a step size of 10 minutes | |
| 3994 read_Buhlmann_times(2); | |
| 3995 | |
| 3996 // read Buhlmann a and b coefficients for tissue ci | |
| 3997 read_Buhlmann_coefficients(); | |
| 3998 | |
| 3999 #ifdef _helium | |
| 4000 | |
| 4001 // get the current simulated tissue pressures | |
| 4002 calc_pres_tissue_N2 = last_pres_tissue_N2 = sim_pres_tissue_N2[ci]; | |
| 4003 calc_pres_tissue_He = last_pres_tissue_He = sim_pres_tissue_He[ci]; | |
| 4004 | |
| 4005 #else | |
| 4006 | |
| 4007 // get the current simulated tissue pressure | |
| 650 | 4008 calc_pres_tissue = last_pres_tissue = sim_pres_tissue_N2[ci]; |
| 623 | 4009 |
| 4010 // set the a and b coefficients | |
| 4011 adopt_Buhlmann_coefficients(); | |
| 4012 | |
| 4013 #endif | |
| 4014 | |
| 4015 // simulate an increasing bottom time and check when the NDL is hit | |
| 4016 for(;;) | |
| 582 | 4017 { |
| 623 | 4018 |
| 4019 #ifdef _helium | |
| 4020 | |
| 4021 // calculate the total tissue pressure | |
| 650 | 4022 calc_pres_tissue = calc_pres_tissue_N2 + calc_pres_tissue_He; |
| 582 | 4023 |
| 623 | 4024 // adopt a and b coefficients to current N2/He ratio inside the tissue |
| 4025 adopt_Buhlmann_coefficients(); | |
| 4026 | |
| 4027 #endif | |
| 4028 | |
| 4029 // compute the maximum tissue pressure allowed to be exposed to an | |
| 4030 // ambient pressure equaling the surface pressure | |
| 631 | 4031 if( char_I_model != 0 ) |
| 623 | 4032 { |
| 4033 // GF model enabled, this equation [2] is the inverse of equation [1] | |
| 4034 pres_limit = (1.0 - GF_high + GF_high / var_b) * pres_surface + GF_high * var_a; | |
| 4035 } | |
| 4036 else | |
| 582 | 4037 { |
| 623 | 4038 // straight Buhlmann |
| 4039 pres_limit = pres_surface / var_b + var_a; | |
| 4040 } | |
| 4041 | |
| 4042 // is the tissue pressure higher than the maximum tissue pressure allowed? | |
| 650 | 4043 if( calc_pres_tissue > pres_limit) |
| 623 | 4044 { |
| 4045 // YES - tissue is outside NDL | |
| 4046 | |
| 4047 // was the tissue outside NDL right from the start? | |
| 4048 if( NDL_time_tissue == 0 ) | |
| 582 | 4049 { |
| 623 | 4050 // YES - search can be aborted |
| 4051 | |
| 4052 // at least one tissue is outside NDL, so overall NDL time is zero | |
| 4053 NDL_time = 0; | |
| 4054 | |
| 4055 // store the number of this tissue as being the leading one | |
| 4056 NDL_tissue_lead = NDL_tissue; | |
| 4057 | |
| 4058 // done | |
| 582 | 4059 break; |
| 4060 } | |
| 4061 | |
| 623 | 4062 // when code execution passes here, the tissue has become |
| 4063 // being outside NDL after doing one or more search steps | |
| 4064 | |
| 4065 // still searching with a step size of 10 minutes? | |
| 4066 if( step_size == 10 ) | |
| 582 | 4067 { |
| 623 | 4068 // YES - retry with smaller step size |
| 4069 | |
| 4070 // go back to last NDL time | |
| 4071 NDL_time_tissue -= 10; | |
| 4072 | |
| 4073 #ifdef _helium | |
| 4074 | |
| 4075 // go back to last pressures | |
| 4076 calc_pres_tissue_N2 = last_pres_tissue_N2; | |
| 4077 calc_pres_tissue_He = last_pres_tissue_He; | |
| 4078 | |
| 4079 #else | |
| 4080 | |
| 4081 // go back to last pressure | |
| 650 | 4082 calc_pres_tissue = last_pres_tissue; |
| 623 | 4083 |
| 4084 #endif | |
| 4085 | |
| 4086 // reduce step size to 1 minute | |
| 4087 step_size = 1; | |
| 4088 | |
| 4089 // read the tissue increments for a step size of 1 minute | |
| 4090 read_Buhlmann_times(1); | |
| 4091 | |
| 4092 // redo search from last pressure & time within NDL with smaller step size | |
| 582 | 4093 continue; |
| 4094 } | |
| 623 | 4095 else |
| 582 | 4096 { |
| 623 | 4097 // NO - already tried with a step size of 1 minute |
| 4098 | |
| 4099 // go back to last NDL time that was within NDL | |
| 4100 NDL_time_tissue -= 1; | |
| 4101 | |
| 4102 // is the NDL time of this tissue shorter than the overall NDL time found so far? | |
| 4103 if( NDL_time_tissue < NDL_time ) | |
| 4104 { | |
| 4105 // YES - set this tissue's NDL time as the new overall NDL time | |
| 4106 NDL_time = NDL_time_tissue; | |
| 4107 | |
| 4108 // - store the number of this tissue as being the leading one | |
| 4109 NDL_tissue_lead = NDL_tissue; | |
| 4110 } | |
| 4111 | |
| 4112 // done | |
| 4113 break; | |
| 582 | 4114 } |
| 623 | 4115 } |
| 4116 else | |
| 582 | 4117 { |
| 623 | 4118 // NO - tissue is still within NDL |
| 4119 | |
| 4120 // The search can be terminated when the NDL time of this tissue | |
| 4121 // exceeds the overall NDL time, thus when a shorter NDL time has | |
| 4122 // already been found with another tissue. | |
| 4123 if( NDL_time_tissue >= NDL_time ) break; | |
| 4124 | |
| 4125 #ifdef _helium | |
| 4126 | |
| 4127 // back-up current tissue pressures | |
| 4128 last_pres_tissue_N2 = calc_pres_tissue_N2; | |
| 4129 last_pres_tissue_He = calc_pres_tissue_He; | |
| 4130 | |
| 4131 #else | |
| 4132 | |
| 4133 // back-up current tissue pressure | |
| 650 | 4134 last_pres_tissue = calc_pres_tissue; |
| 623 | 4135 |
| 4136 #endif | |
| 4137 | |
| 4138 // step forward NDL time of current tissue | |
| 4139 NDL_time_tissue += step_size; | |
| 4140 | |
| 4141 #ifdef _helium | |
| 4142 | |
| 4143 // step forward tissue pressure - N2 | |
| 4144 temp_tissue = (ppN2 - calc_pres_tissue_N2) * var_N2_e; // pressure delta breathed - tissue | |
| 4145 apply_saturation_factors(); // apply safety factor | |
| 4146 calc_pres_tissue_N2 += temp_tissue; // add pressure delta to tissue | |
| 4147 | |
| 4148 // step forward tissue pressure - He | |
| 4149 temp_tissue = (ppHe - calc_pres_tissue_He) * var_He_e; // pressure delta breathed - tissue | |
| 4150 apply_saturation_factors(); // apply safety factor | |
| 4151 calc_pres_tissue_He += temp_tissue; // add pressure delta to tissue | |
| 4152 | |
| 4153 #else | |
| 4154 | |
| 4155 // step forward tissue pressure | |
| 650 | 4156 temp_tissue = (ppN2 - calc_pres_tissue ) * var_N2_e; // pressure delta breathed - tissue |
| 623 | 4157 apply_saturation_factors(); // apply safety factor |
| 650 | 4158 calc_pres_tissue += temp_tissue; // add pressure delta to tissue |
| 623 | 4159 |
| 4160 #endif | |
| 4161 | |
| 582 | 4162 } |
| 623 | 4163 } |
| 0 | 4164 } |
| 4165 | |
| 604 | 4166 |
| 0 | 4167 ////////////////////////////////////////////////////////////////////////////// |
| 4168 // clear_deco_table | |
| 4169 // | |
| 623 | 4170 // Modified: internal_deco_time[] stop durations |
| 4171 // internal_deco_depth[] stop depths | |
| 4172 // internal_deco_gas[] gases used at stops | |
| 0 | 4173 // |
| 4174 static void clear_deco_table(void) | |
| 4175 { | |
| 623 | 4176 for( i = 0; i < NUM_STOPS; ++i ) |
| 582 | 4177 { |
| 623 | 4178 internal_deco_time [i] = 0; |
| 4179 internal_deco_depth[i] = 0; | |
| 4180 internal_deco_gas[i] = 0; | |
| 582 | 4181 } |
| 4182 | |
| 628 | 4183 // reset stop table index and chained stops counter |
| 4184 stop_index = 0; | |
| 4185 chained_stops = 0; | |
| 4186 | |
| 560 | 4187 // clear stop table overflow warning |
| 628 | 4188 deco_warnings &= ~DECO_WARNING_INCOMPLETE; |
| 0 | 4189 } |
| 4190 | |
| 623 | 4191 |
| 0 | 4192 ////////////////////////////////////////////////////////////////////////////// |
| 4193 // update_deco_table | |
| 4194 // | |
| 623 | 4195 // Add time to a stop at char_depth_sim |
| 560 | 4196 // |
| 4197 // It is possible to create stops with a duration of 0 minutes, e.g. to | |
| 4198 // note a gas change "on the fly" while ascending. Therefore the criteria | |
| 604 | 4199 // to have reached the end of the list is depth == 0. |
| 0 | 4200 // |
| 623 | 4201 // Input: char_depth_sim stop's depth, in meters |
| 4202 // sim_gas_current_num gas used at stop, as index 1..5 or 0 for gas 6 | |
| 4203 // time_increment number of minutes to add to the stop | |
| 4204 // | |
| 4205 // Updated: internal_deco_depth[] depth (in meters) of each stop | |
| 4206 // internal_deco_time [] time (in minutes) of each stop | |
| 4207 // internal_deco_gas [] gas used (index 1-5) at each stop | |
| 560 | 4208 // |
| 631 | 4209 static void update_deco_table(PARAMETER unsigned char time_increment) |
| 0 | 4210 { |
| 623 | 4211 assert( char_depth_sim > 0 ); // no stop at surface |
| 560 | 4212 |
| 628 | 4213 |
| 4214 // is there already a stop entry matching with the current depth and gas? | |
| 4215 if( internal_deco_depth[stop_index] == char_depth_sim ) | |
| 4216 if( internal_deco_gas [stop_index] == sim_gas_current_num ) | |
| 560 | 4217 { |
| 628 | 4218 // YES - increment stop time if possible, stop time entries are |
| 4219 // limited to 99 minutes because of display constraints | |
| 4220 if( internal_deco_time[stop_index] < (100 - time_increment) ) | |
| 560 | 4221 { |
| 628 | 4222 // YES - time increment fits into current stop entry, |
| 631 | 4223 // increment stop time |
| 628 | 4224 internal_deco_time[stop_index] += time_increment; |
| 631 | 4225 |
| 4226 // done | |
| 4227 return; | |
| 628 | 4228 } |
| 4229 else | |
| 4230 { | |
| 4231 // NO - A chained stop entry will be created further down in the | |
| 4232 // code to continue the stop, but we will limit the number | |
| 4233 // of chained stop table entries in order to abort an ever- | |
| 4234 // running deco calculation. Too many chained entries? | |
| 4235 if( ++chained_stops >= STOP_CHAINING_LIMIT ) | |
| 560 | 4236 { |
| 631 | 4237 // YES - set warning that calculations took too long |
| 628 | 4238 deco_warnings |= DECO_WARNING_INCOMPLETE; |
| 631 | 4239 |
| 4240 // done | |
| 4241 return; | |
| 560 | 4242 } |
| 4243 } | |
| 628 | 4244 } |
| 4245 | |
| 4246 // the current stop entry does not match the current depth and gas, | |
| 4247 // or hasn't enough room left for the time increment | |
| 4248 | |
| 4249 // is the current stop entry in use? | |
| 4250 if( internal_deco_depth[stop_index] > 0 ) | |
| 4251 { | |
| 4252 // YES - current entry is in use, need to move on | |
| 4253 // to next entry position if possible | |
| 4254 | |
| 4255 // have all entry positions been used up? | |
| 4256 if( stop_index < (NUM_STOPS - 1) ) | |
| 560 | 4257 { |
| 628 | 4258 // NO - move on to next entry position |
| 4259 stop_index += 1; | |
| 4260 } | |
| 4261 else | |
| 4262 { | |
| 631 | 4263 // YES - if run in deco calculator mode, set a warning that there is an overflow in the stops table |
| 4264 if( main_status & CALCULATE_BOTTOM ) deco_warnings |= DECO_WARNING_INCOMPLETE; | |
| 4265 | |
| 4266 // limit runtime via reached TTS (scaled in 1/10 minutes here) | |
| 4267 if( TTS_time > 9999 ) deco_warnings |= DECO_WARNING_INCOMPLETE; | |
| 4268 | |
| 4269 // done | |
| 4270 return; | |
| 560 | 4271 } |
| 4272 } | |
| 4273 | |
| 631 | 4274 // initial use of a new (or the very first) stop entry, store all stop data |
| 628 | 4275 internal_deco_time [stop_index] = time_increment; |
| 4276 internal_deco_depth[stop_index] = char_depth_sim; | |
| 4277 internal_deco_gas [stop_index] = sim_gas_current_num; | |
| 631 | 4278 |
| 4279 // done | |
| 4280 return; | |
| 628 | 4281 } |
| 4282 | |
| 4283 | |
| 4284 ////////////////////////////////////////////////////////////////////////////// | |
| 4285 // publish_deco_table | |
| 4286 // | |
| 4287 // Input: internal_deco_depth[] depth in internal stops table | |
| 4288 // internal_deco_time[] times ... | |
| 4289 // internal_deco_gas[] gases ... | |
| 4290 // | |
| 4291 // Output: char_O_deco_depth[] depth in the external stops table | |
| 4292 // char_O_deco_time[] times ... | |
| 4293 // char_O_deco_gas[] gases ... | |
| 4294 // char_O_deco_time_for_log times in reverse order | |
| 4295 // | |
| 4296 static void publish_deco_table(void) | |
| 4297 { | |
| 4298 overlay unsigned char x = stop_index; | |
| 4299 overlay unsigned char y; | |
| 4300 | |
| 4301 | |
| 4302 // copy depth, time and gas from internal to external stops table | |
| 4303 for( y = 0; y < NUM_STOPS; y++ ) | |
| 4304 { | |
| 4305 char_O_deco_depth[y] = internal_deco_depth[y]; | |
| 4306 char_O_deco_time [y] = internal_deco_time [y]; | |
| 4307 char_O_deco_gas [y] = internal_deco_gas [y]; | |
| 4308 } | |
| 4309 | |
| 4310 // copy times of shallowest stops to logging table | |
| 4311 for(y = 0; y < NUM_STOPS_LOG; x-- ) | |
| 4312 { | |
| 4313 // copy all stops that have a non-null stop time | |
| 4314 if( internal_deco_time[x] ) | |
| 4315 char_O_deco_time_for_log[y++] = internal_deco_time[x]; | |
| 4316 | |
| 4317 // abort if all stops are copied | |
| 4318 if( x == 0) break; | |
| 4319 } | |
| 4320 | |
| 4321 // fill the remainder of the logging table with null | |
| 4322 // if it is not completely filled already | |
| 4323 while( y < NUM_STOPS_LOG ) | |
| 4324 { | |
| 4325 char_O_deco_time_for_log[y++] = 0; | |
| 4326 } | |
| 0 | 4327 } |
| 4328 | |
| 4329 | |
| 4330 ////////////////////////////////////////////////////////////////////////////// | |
| 623 | 4331 // calc_desaturation_time_helper |
| 0 | 4332 // |
| 560 | 4333 // Helper function |
| 4334 // | |
| 623 | 4335 // Input: pres_actual current tissue pressure |
| 4336 // pres_target target tissue pressure | |
| 4337 // var_ht half-time of the tissue | |
| 4338 // desat_factor desaturation factor | |
| 4339 // | |
| 4340 // Output: int_time time needed by tissue to reach target pressure | |
| 4341 // | |
| 604 | 4342 static void calc_desaturation_time_helper(void) |
| 0 | 4343 { |
| 623 | 4344 // check if actual pressure is higher then target pressure |
| 4345 if( pres_actual > pres_target ) | |
| 4346 { | |
| 4347 // YES - compute remaining time | |
| 4348 | |
| 604 | 4349 overlay float pres_ratio; |
| 582 | 4350 |
| 623 | 4351 // compute pressure ratio to archive |
| 560 | 4352 pres_ratio = pres_actual / pres_target; |
| 4353 | |
| 4354 // Compute desaturation time with result rounded up to multiples of 10 minutes. | |
| 582 | 4355 // Main purpose is to avoid confusion, because the times do not clock down in |
| 4356 // one minute steps any more but get constantly re-computed according to current | |
| 4357 // ambient pressure and may therefor make steps of several minutes forwards and | |
| 604 | 4358 // backwards as ambient pressure rises/falls and N2/He ratio is being adjusted. |
| 623 | 4359 int_time = (unsigned short)( (var_ht * log(pres_ratio) / desat_factor) + 0.9 ); |
| 560 | 4360 } |
| 4361 else | |
| 623 | 4362 { |
| 4363 // NO - desaturation state reached, no remaining time | |
| 582 | 4364 int_time = 0; |
| 560 | 4365 } |
| 4366 } | |
| 4367 | |
| 623 | 4368 |
| 560 | 4369 ///////////////////////////////////////////////////////////////////////////// |
| 604 | 4370 // calc_desaturation_time |
| 4371 // | |
| 623 | 4372 // Calculates the time needed for the tissues to equilibrate with |
| 4373 // surface pressure and the no-fly / no-altitude time. | |
| 4374 // | |
| 4375 // Input: int_I_pres_surface | |
| 4376 // char_I_desaturation_multiplier | |
| 4377 // | |
| 4378 // Output: int_O_desaturation_time | |
| 4379 // int_O_nofly_time | |
| 560 | 4380 // |
| 4381 void calc_desaturation_time(void) | |
| 4382 { | |
| 623 | 4383 overlay float P_ambient_altitude; |
| 4384 | |
| 604 | 4385 assert( 800 < int_I_pres_surface && int_I_pres_surface < 1100 ); |
| 4386 assert( 0 < char_I_desaturation_multiplier && char_I_desaturation_multiplier <= 100 ); | |
| 4387 | |
| 4388 | |
| 623 | 4389 // safeguard and convert surface pressure |
| 4390 if( int_I_pres_surface < 500) pres_surface = 0.5; | |
| 4391 else pres_surface = 0.001 * int_I_pres_surface; | |
| 4392 | |
| 4393 // calculate partial pressure of N2 in respired air at surface pressure | |
| 4394 calc_N2_equilibrium(); | |
| 4395 | |
| 4396 // get, safeguard and convert the saturation and desaturation factors | |
| 4397 get_saturation_factors(); | |
| 4398 | |
| 4399 // pre-computed term for later use: 10 [Min] * 0.6931 [=log(2)] * 1 [Desat Factor] * ... | |
| 4400 desat_factor = (6.931 * SURFACE_DESAT_FACTOR) * float_desaturation_multiplier; | |
| 582 | 4401 |
| 4402 // initialize vars | |
| 560 | 4403 int_O_desaturation_time = 0; |
| 582 | 4404 int_O_nofly_time = 0; |
| 4405 | |
| 623 | 4406 // get selected target altitude |
| 4407 switch( char_I_altitude_wait ) | |
| 4408 { | |
| 4409 case 1: P_ambient_altitude = P_ambient_1000m; break; | |
| 4410 case 2: P_ambient_altitude = P_ambient_2000m; break; | |
| 4411 case 3: P_ambient_altitude = P_ambient_3000m; break; | |
| 4412 default: P_ambient_altitude = P_ambient_fly; break; | |
| 4413 } | |
| 4414 | |
| 4415 // loop over all compartments in order slowest to fastest | |
| 604 | 4416 for( ci = NUM_COMP; ci > 0; ) |
| 582 | 4417 { |
| 623 | 4418 overlay float pres_tissue_max; |
| 4419 overlay unsigned short nofly_last = ~0; | |
| 4420 overlay unsigned short nofly_N2 = 0; | |
| 4421 | |
| 4422 #ifdef _helium | |
| 4423 overlay signed char search_direction; | |
| 4424 overlay unsigned short nofly_He = 0; | |
| 4425 #endif | |
| 4426 | |
| 4427 | |
| 4428 // decrement compartment index | |
| 560 | 4429 ci -= 1; |
| 582 | 4430 |
| 623 | 4431 // get the Buhlmann halftimes and coefficients |
| 582 | 4432 read_Buhlmann_ht(); |
| 560 | 4433 read_Buhlmann_coefficients(); |
| 582 | 4434 |
| 623 | 4435 |
| 4436 // | |
| 4437 // Desaturation time | |
| 4438 // | |
| 4439 | |
| 4440 // calculate desaturation time for N2 in tissue, | |
| 4441 // desaturated state is defined as residual tissue pressure <= 1.05 x ppN2 respired | |
| 4442 | |
| 4443 // current tissue pressure above equilibrium pressure | |
| 4444 pres_actual = real_pres_tissue_N2[ci] - N2_equilibrium; | |
| 4445 | |
| 4446 // target pressure above equilibrium pressure | |
| 4447 pres_target = 0.05 * N2_equilibrium; | |
| 4448 | |
| 4449 // half-time of the current tissue | |
| 4450 var_ht = var_N2_ht; | |
| 4451 | |
| 4452 // calculate desaturation time | |
| 4453 calc_desaturation_time_helper(); | |
| 4454 | |
| 4455 // store desaturation time if it is longer than longest found so far | |
| 4456 if( int_time > int_O_desaturation_time) int_O_desaturation_time = int_time; | |
| 4457 | |
| 4458 | |
| 4459 #ifdef _helium | |
| 4460 | |
| 4461 // calculate desaturation time for He in the tissue, | |
| 4462 // desaturated state is defined as residual tissue pressure <= 0.05 x ppN2 respired | |
| 4463 | |
| 4464 // actual tissue pressure above equilibrium: equilibrium for He is 0 bar | |
| 4465 pres_actual = real_pres_tissue_He[ci]; | |
| 4466 | |
| 4467 // target pressure above equilibrium pressure: use same target pressure as for N2 | |
| 4468 pres_target = 0.05 * N2_equilibrium; | |
| 4469 | |
| 4470 // half-time of the current tissue | |
| 4471 var_ht = var_He_ht; | |
| 4472 | |
| 4473 // calculate desaturation time | |
| 4474 calc_desaturation_time_helper(); | |
| 4475 | |
| 4476 // store desaturation time if it is longer than longest found so far | |
| 4477 if( int_time > int_O_desaturation_time) int_O_desaturation_time = int_time; | |
| 4478 | |
| 4479 #endif | |
| 4480 | |
| 4481 // | |
| 4482 // no-fly time | |
| 4483 // | |
| 582 | 4484 |
| 560 | 4485 // Target pressure for the tissue is the Buhlmann limit. We use the Buhlmann |
| 4486 // coefficients for N2 also for He because it is easier to calculate and the | |
| 4487 // N2 coefficients are more conservative than those for He, so we are on the | |
| 4488 // safe side, too. | |
| 582 | 4489 pres_tissue_max = (P_ambient_altitude/var_N2_b + var_N2_a); |
| 4490 | |
| 623 | 4491 // adjust target pressure by GF-high in case the GF model is in use, but not |
| 4492 // for the no-fly time as it's target pressure is hard to reach anyhow | |
| 631 | 4493 if( char_I_model && char_I_altitude_wait ) |
| 623 | 4494 pres_tissue_max = P_ambient_altitude + |
| 4495 0.01 * char_I_GF_High_percentage * (pres_tissue_max - P_ambient_altitude); | |
| 4496 | |
| 4497 | |
| 4498 #ifdef _helium | |
| 4499 | |
| 4500 //---- Variant with Helium ------------------------------------------- | |
| 4501 | |
| 4502 // initialize split_N2_He in case there was a hard reboot / memory clear | |
| 604 | 4503 if( split_N2_He[ci] == 0 ) split_N2_He[ci] = 90; |
| 4504 | |
| 560 | 4505 // initialize search direction |
| 4506 search_direction = 0; | |
| 582 | 4507 |
| 560 | 4508 for(;;) |
| 4509 { | |
| 4510 // Calculate no-fly time for N2 in the tissue. | |
| 4511 // Flying is permitted when the N2 pressure fits into the assigned fraction above equilibrium. | |
| 4512 | |
| 623 | 4513 // current tissue pressure above equilibrium |
| 4514 pres_actual = real_pres_tissue_N2[ci] - N2_equilibrium; | |
| 4515 | |
| 4516 // target pressure above equilibrium pressure, weighted by N2/He split | |
| 560 | 4517 pres_target = (split_N2_He[ci] * 0.01) * (pres_tissue_max - N2_equilibrium); |
| 4518 | |
| 623 | 4519 // half-time of the current tissue |
| 4520 var_ht = var_N2_ht; | |
| 4521 | |
| 4522 // check if desaturation to target pressure is possible at all | |
| 4523 if( pres_target < 0.0 ) | |
| 560 | 4524 { |
| 623 | 4525 // NO - set no-fly time to 288 * 10 min = 48 h |
| 4526 int_O_nofly_time = 288; | |
| 4527 break; | |
| 560 | 4528 } |
| 604 | 4529 else |
| 560 | 4530 { |
| 623 | 4531 // YES - calculate desaturation time |
| 560 | 4532 calc_desaturation_time_helper(); |
| 623 | 4533 |
| 4534 // store time found | |
| 582 | 4535 nofly_N2 = int_time; |
| 560 | 4536 } |
| 582 | 4537 |
| 623 | 4538 // calculate no-fly time for He in the tissue, |
| 4539 // flying is permitted when the He pressure fits into the assigned fraction | |
| 4540 | |
| 4541 // current tissue pressure above equilibrium: equilibrium for He is 0 bar | |
| 4542 pres_actual = real_pres_tissue_He[ci]; | |
| 4543 | |
| 4544 // target pressure above equilibrium pressure, weighted by N2/He split | |
| 4545 pres_target = ((100 - split_N2_He[ci]) * 0.01) * (pres_tissue_max - N2_equilibrium); | |
| 4546 | |
| 4547 // half-time of the current tissue | |
| 582 | 4548 var_ht = var_He_ht; |
| 4549 | |
| 623 | 4550 // calculate desaturation time |
| 560 | 4551 calc_desaturation_time_helper(); |
| 623 | 4552 |
| 4553 // store time found | |
| 582 | 4554 nofly_He = int_time; |
| 560 | 4555 |
| 4556 | |
| 4557 // Because the sum of N2 and He tissue pressures needs to fit into the Buhlmann limit for | |
| 4558 // no-fly time calculation, each gas gets assigned a fraction of the available total pressure | |
| 623 | 4559 // limit. The optimum split between the two gases can not be computed by a single formula, |
| 560 | 4560 // because this would require the inversion of a function with two exponential terms, which is |
| 4561 // not possible. We do not want to do a computational complex simulation here like it is done | |
| 4562 // in the deco calculation code (although we tackle the same base problem here), so we just let | |
| 4563 // the computer try out which split will balance the no-fly times induced by the N2 and the He | |
| 4564 // at best. | |
| 582 | 4565 |
| 560 | 4566 // first of all, skip any optimization in case the current compartment is not the leading one |
| 4567 if( (nofly_N2 <= int_O_nofly_time) && (nofly_He <= int_O_nofly_time) ) break; | |
| 4568 | |
| 4569 // check if the N2 requires more waiting time than the He | |
| 582 | 4570 if( nofly_N2 >= nofly_He ) |
| 560 | 4571 { |
| 4572 // check if the search direction has changed, which means we are beyond the | |
| 4573 // optimum now, or if we are at the upper stop limit of split_N2_He | |
| 4574 if( (search_direction < 0) || (split_N2_He[ci] == 99) ) | |
| 604 | 4575 { |
| 623 | 4576 // either the just completed iteration was more close to the optimum or the one before |
| 4577 // was, so we take the best (i.e. shortest) time of both as the final no-fly time | |
| 560 | 4578 int_O_nofly_time = (nofly_N2 < nofly_last) ? nofly_N2 : nofly_last; |
| 623 | 4579 |
| 4580 // done | |
| 560 | 4581 break; |
| 4582 } | |
| 4583 | |
| 4584 // store the no-fly time found in this iteration | |
| 582 | 4585 nofly_last = nofly_N2; |
| 4586 | |
| 623 | 4587 // increase the N2 fraction of the split |
| 560 | 4588 split_N2_He[ci] += 1; |
| 623 | 4589 |
| 4590 // set search direction towards more N2 | |
| 560 | 4591 search_direction = +1; |
| 4592 } | |
| 4593 else | |
| 4594 { | |
| 4595 // check if the search direction has changed, which means we are beyond the | |
| 4596 // optimum now, or if we are at the lower stop limit of split_N2_He | |
| 4597 if( (search_direction > 0) || (split_N2_He[ci] == 1) ) | |
| 582 | 4598 { |
| 623 | 4599 // either the just completed iteration was more close to the optimum or the one before |
| 4600 // was, so we take the best (i.e. shortest) time of both as the final no-fly time | |
| 560 | 4601 int_O_nofly_time = (nofly_He < nofly_last) ? nofly_He : nofly_last; |
| 623 | 4602 |
| 4603 // done | |
| 560 | 4604 break; |
| 4605 } | |
| 4606 | |
| 4607 // store the no-fly time found in this iteration | |
| 582 | 4608 nofly_last = nofly_He; |
| 4609 | |
| 623 | 4610 // decrease the N2 fraction of the split |
| 560 | 4611 split_N2_He[ci] -= 1; |
| 623 | 4612 |
| 4613 // set search direction towards less N2 | |
| 604 | 4614 search_direction = -1; |
| 560 | 4615 } |
| 582 | 4616 |
| 560 | 4617 } // for(;;) |
| 4618 | |
| 623 | 4619 #else |
| 4620 | |
| 4621 //---- Variant without Helium ---------------------------------------- | |
| 4622 | |
| 4623 // current tissue pressure above equilibrium | |
| 4624 pres_actual = real_pres_tissue_N2[ci] - N2_equilibrium; | |
| 4625 | |
| 4626 // target pressure above equilibrium pressure | |
| 4627 pres_target = pres_tissue_max - N2_equilibrium; | |
| 4628 | |
| 4629 // half-time of the current tissue | |
| 4630 var_ht = var_N2_ht; | |
| 4631 | |
| 4632 // check if desaturation to target pressure is possible at all | |
| 4633 if( pres_target < 0.0 ) | |
| 4634 { | |
| 4635 // NO - set no-fly time to 288 * 10 min = 48 h | |
| 4636 int_O_nofly_time = 288; | |
| 4637 } | |
| 4638 else | |
| 4639 { | |
| 4640 // YES - calculate desaturation time | |
| 4641 calc_desaturation_time_helper(); | |
| 4642 | |
| 4643 // - extend desaturation time if this tissue needs | |
| 4644 // more time than already found to be needed | |
| 4645 if( int_time > int_O_nofly_time ) int_O_nofly_time = int_time; | |
| 4646 } | |
| 4647 | |
| 4648 #endif | |
| 4649 | |
| 560 | 4650 } // for(compartments) |
| 4651 | |
| 582 | 4652 |
| 623 | 4653 // rescale int_O_desaturation_time and int_O_nofly_time to full minutes for display purpose |
| 560 | 4654 int_O_desaturation_time *= 10; |
| 4655 int_O_nofly_time *= 10; | |
| 582 | 4656 |
| 623 | 4657 // limit int_O_desaturation_time and int_O_nofly_time to 5999 = 99 hours + 59 minutes |
| 4658 // because of display space constraints and rounding done above | |
| 560 | 4659 if( int_O_desaturation_time > 5999 ) int_O_desaturation_time = 5999; |
| 4660 if( int_O_nofly_time > 5999 ) int_O_nofly_time = 5999; | |
| 4661 | |
| 4662 | |
| 623 | 4663 // Clear the micro bubbles warning when the current gradient factor is < 100%. |
| 604 | 4664 // The current gradient factor is calculated by calc_interval() while not in diving mode. |
| 4665 // As the locked warning will stay set, this will cause the warning be be displayed in | |
| 4666 // attention color instead of warning color. | |
| 623 | 4667 if( int_O_lead_supersat < 100 ) |
| 4668 deco_warnings &= ~DECO_WARNING_MBUBBLES; | |
| 582 | 4669 |
| 560 | 4670 // clear some warnings when the desaturation time has become zero |
| 582 | 4671 if( int_O_desaturation_time == 0 ) |
| 623 | 4672 deco_warnings &= ~( DECO_WARNING_IBCD + DECO_WARNING_IBCD_lock |
| 4673 + DECO_WARNING_MBUBBLES + DECO_WARNING_MBUBBLES_lock | |
| 4674 + DECO_WARNING_OUTSIDE + DECO_WARNING_OUTSIDE_lock | |
| 4675 + DECO_ATTENTION_OUTSIDE ); | |
| 0 | 4676 } |
| 4677 | |
| 623 | 4678 |
| 0 | 4679 ////////////////////////////////////////////////////////////////////////////// |
| 582 | 4680 // Calculate desaturation of the real tissues for a given time interval |
| 0 | 4681 // |
| 582 | 4682 // Caution: Works on the real tissues! |
| 4683 // If in doubt, use this function only inside a context surrounded with | |
| 4684 // push_tissues_to_vault() / pull_tissues_from_vault() ! | |
| 4685 // | |
| 623 | 4686 // Input: int_I_pres_surface surface pressure in mbar |
| 4687 // time_interval time interval in minutes, must be limited to 254 at max | |
| 4688 // | |
| 4689 // Modified: tissue pressures N2 and He pressures of the tissues | |
| 4690 // CNS_fraction_real current real CNS value | |
| 631 | 4691 // ceiling minimum allowed depth in bar relative pressure |
| 623 | 4692 // lead_supersat supersaturation of the leading tissue (float) |
| 4693 // int_O_lead_supersat supersaturation of the leading tissue (integer) | |
| 631 | 4694 // char_O_lead_tissue number of the leading tissue |
| 560 | 4695 // |
| 582 | 4696 static void calc_interval(PARAMETER unsigned char time_interval) |
| 0 | 4697 { |
| 582 | 4698 overlay unsigned char time; |
| 4699 | |
| 4700 assert( 800 < int_I_pres_surface && int_I_pres_surface < 1100 ); | |
| 604 | 4701 assert( 100 <= char_I_saturation_multiplier && char_I_saturation_multiplier < 200 ); |
| 4702 assert( 0 < char_I_desaturation_multiplier && char_I_desaturation_multiplier <= 100 ); | |
| 4703 | |
| 4704 | |
| 623 | 4705 // safeguard and convert surface pressure |
| 4706 if( int_I_pres_surface < 500) pres_surface = 0.500; | |
| 4707 else pres_surface = 0.001 * int_I_pres_surface; | |
| 4708 | |
| 631 | 4709 // safeguard time interval |
| 4710 if( time_interval > 254 ) time_interval = 254; | |
| 4711 | |
| 623 | 4712 // set breathed pressure to surface pressure |
| 4713 real_pres_respiration = pres_surface; | |
| 4714 | |
| 4715 // calculate partial pressure of N2 in respired air at surface pressure | |
| 4716 calc_N2_equilibrium(); | |
| 4717 | |
| 4718 // calculate partial pressures (0.7902 is fraction of N2 in atmosphere as of Buhlmann) | |
| 4719 ppN2 = N2_equilibrium; | |
| 4720 ppHe = 0.0; | |
| 4721 | |
| 4722 // get, safeguard and convert the saturation and desaturation factors | |
| 4723 get_saturation_factors(); | |
| 4724 | |
| 4725 // adjust desaturation factor to surface mode | |
| 4726 float_desaturation_multiplier *= SURFACE_DESAT_FACTOR; | |
| 582 | 4727 |
| 4728 // Calculate the tissues: | |
| 4729 // Because calc_tissues() can calculate for 127 minutes at max, | |
| 4730 // the tissue updating may need to be done in two chunks. | |
| 4731 | |
| 4732 time = time_interval; | |
| 4733 | |
| 4734 // first chunk for the part exceeding 127 minutes | |
| 4735 if( time > 127) | |
| 560 | 4736 { |
| 582 | 4737 // do a full 127 minutes on the real tissues |
| 623 | 4738 tissue_increment = TISSUE_SELECTOR | 127; |
| 582 | 4739 calc_tissues(); |
| 4740 | |
| 623 | 4741 // determine the remaining time |
| 582 | 4742 time -= 127; |
| 560 | 4743 } |
| 4744 | |
| 623 | 4745 // program the remaining time (or full time if not exceeding 127 minutes) on the real tissues |
| 4746 tissue_increment = TISSUE_SELECTOR | time; | |
| 604 | 4747 |
| 4748 // update the N2 and He pressures in the tissues | |
| 582 | 4749 calc_tissues(); |
| 4750 | |
| 560 | 4751 |
| 4752 // Calculate CNS: | |
| 4753 // To speed up things and because on most invocations of this code char_I_dive_interval | |
| 4754 // is a multiple of 10 minutes, we loop the loop-counter down using two speeds. | |
| 4755 | |
| 582 | 4756 time = time_interval; |
| 4757 | |
| 604 | 4758 while( time ) |
| 560 | 4759 { |
| 582 | 4760 if( time > 9 ) |
| 560 | 4761 { |
| 623 | 4762 CNS_fraction_real *= 0.925874712; // half-time = 90 min -> 10 min: (1/2)^(1/9) |
| 4763 time -= 10; // fast speed looping | |
| 560 | 4764 } |
| 4765 else | |
| 4766 { | |
| 623 | 4767 CNS_fraction_real *= 0.992327946; // half-time = 90 min -> 1 min: (1/2)^(1/90) |
| 4768 time -= 1; // slow speed looping | |
| 560 | 4769 } |
| 582 | 4770 } |
| 560 | 4771 |
| 623 | 4772 // convert the CNS value to integer |
| 4773 convert_cur_CNS_for_display(); | |
| 4774 | |
| 4775 // calculate the supersaturation of the leading tissue, the | |
| 4776 // negative argument puts calc_limit() into surface mode | |
| 4777 // Attention: do not pass char_I_GF_High_percentage as an argument | |
| 4778 // here because it is not configured outside dive mode | |
| 4779 calc_limit(-1.0); | |
| 4780 | |
| 4781 // convert the saturation value of the leading tissue to integer | |
| 4782 convert_sat_for_display(); | |
| 0 | 4783 } |
| 4784 | |
| 560 | 4785 |
| 4786 ////////////////////////////////////////////////////////////////////////////// | |
| 604 | 4787 // calc_CNS |
| 560 | 4788 // |
| 623 | 4789 // Input: char_ppO2 current ppO2 [in 0.1 bars] |
| 631 | 4790 // tissue_increment tissue selector and time interval |
| 623 | 4791 // |
| 4792 // Modified: CNS_fraction_real accumulated CNS (real tissue context) | |
| 4793 // CNS_fraction_sim accumulated CNS (simulated tissue context) | |
| 560 | 4794 // |
| 604 | 4795 static void calc_CNS(void) |
| 560 | 4796 { |
| 604 | 4797 overlay float CNS_fraction_inc; // increment of CNS load, 0.01 = 1% |
| 623 | 4798 |
| 4799 | |
| 631 | 4800 // calculate CNS increment for a 2 seconds interval |
| 623 | 4801 if( char_ppO2 > 160 ) |
| 4802 { | |
| 4803 // step-wise CNS increment | |
| 4804 | |
| 631 | 4805 // calculate index for increment look-up (uses integer division) |
| 4806 cns_i = (char_ppO2 - 161) / 5; | |
| 623 | 4807 |
| 628 | 4808 // indexes > 17 use increment of index 17 |
| 4809 if( cns_i > 17 ) cns_i = 17; | |
| 4810 | |
| 623 | 4811 // read coefficient (increment) |
| 4812 read_CNS_c_coefficient(); | |
| 4813 | |
| 4814 // re-scale coefficient from storage format in [1/100000] to productive value | |
| 631 | 4815 CNS_fraction_inc = (float)var_cns_value / 100000.0; |
| 623 | 4816 } |
| 4817 else if( char_ppO2 > 50 ) | |
| 4818 { | |
| 4819 // range wise CNS increment approximation | |
| 4820 | |
| 631 | 4821 // calculate index for approximation coefficients look-up (uses integer division) |
| 4822 cns_i = (char_ppO2 - 51) / 10; | |
| 623 | 4823 |
| 4824 // read coefficients | |
| 4825 read_CNS_ab_coefficient(); | |
| 4826 | |
| 4827 // calculate the CNS increment | |
| 631 | 4828 CNS_fraction_inc = 1.0 / (var_cns_gain * char_ppO2 + var_cns_offset ); |
| 623 | 4829 } |
| 4830 else | |
| 631 | 4831 { // no increment for a ppO2 of up to 0.5 bar |
| 623 | 4832 CNS_fraction_inc = 0.0; |
| 4833 } | |
| 4834 | |
| 631 | 4835 // apply a time factor in case of: |
| 4836 // - simulated tissues and interval = 0 (i.e. 6 seconds to do) -> factor 3 | |
| 4837 // - any tissues and interval > 0 (i.e. i minutes to do) -> factor 30 * i | |
| 4838 if( tissue_increment == 0 ) CNS_fraction_inc *= 3.0; | |
| 4839 else if( tissue_increment & TIME_MASK ) CNS_fraction_inc *= 30.0 * (float)(tissue_increment & TIME_MASK); | |
| 4840 | |
| 604 | 4841 |
| 4842 // update the CNS accumulator | |
| 623 | 4843 if ( tissue_increment & TISSUE_SELECTOR ) CNS_fraction_real += CNS_fraction_inc; // real tissues |
| 4844 else CNS_fraction_sim += CNS_fraction_inc; // simulated tissues | |
| 4845 } | |
| 4846 | |
| 4847 | |
| 4848 ////////////////////////////////////////////////////////////////////////////// | |
| 631 | 4849 // calc_required_volume |
| 623 | 4850 // |
| 4851 // Calculates the gas volume required for a given depth, time and usage (SAC) | |
| 4852 // rate. It uses a fixed surface pressure of 1.0 bar to deliver stable results | |
| 4853 // when used through the deco calculator. | |
| 4854 // | |
| 628 | 4855 // Input: gas_needs_depth depth in meters |
| 631 | 4856 // gas_needs_time time in minutes (0 encodes 1/10 minute) |
| 628 | 4857 // gas_needs_usage_rate gas usage in liters per minute at surface pressure |
| 623 | 4858 // |
| 628 | 4859 // Output: gas_needs_volume_due required gas volume in liters |
| 623 | 4860 // |
| 631 | 4861 static void calc_required_volume(void) |
| 623 | 4862 { |
| 631 | 4863 // calculate volume for 1 minute |
| 4864 gas_needs_volume_due = ((float)gas_needs_depth * METER_TO_BAR + pres_surface) * gas_needs_usage_rate; | |
| 4865 | |
| 4866 // multiply 1-minute-volume with time factor if time factor <> 1 | |
| 4867 if ( gas_needs_time == 0 ) gas_needs_volume_due *= 0.1; // 1/10 minute | |
| 4868 else if ( gas_needs_time > 1 ) gas_needs_volume_due *= gas_needs_time; // multiple minutes | |
| 0 | 4869 } |
| 4870 | |
| 560 | 4871 |
| 631 | 4872 |
| 4873 #ifdef _rx_functions | |
| 4874 | |
| 0 | 4875 ////////////////////////////////////////////////////////////////////////////// |
| 623 | 4876 // calc_TR_functions |
| 4877 // | |
| 4878 // Process Pressure Readings (OSTC TR only) | |
| 4879 // | |
| 4880 // Input: todo | |
| 4881 // | |
| 4882 // Output: todo | |
| 4883 // | |
| 4884 static void calc_TR_functions(void) | |
| 560 | 4885 { |
| 623 | 4886 // pressure warnings for reading 1, but only if enabled and pressure value available |
| 4887 if( (char_I_pressure_gas[0] > 0) && !(int_IO_pressure_value[0] & INT_FLAG_NOT_AVAIL) ) | |
| 604 | 4888 { |
| 623 | 4889 overlay unsigned short pressure_value = int_IO_pressure_value[0] & ~INT_FLAG_OUTDATED; |
| 4890 | |
| 4891 if( (char_I_pressure_gas[0] < 6 ) && !(int_O_pressure_need[0] & INT_FLAG_NOT_AVAIL) ) | |
| 4892 { | |
| 4893 // not a diluent and need available: warning & attention by need | |
| 4894 if ( pressure_value <= int_O_pressure_need[0]) | |
| 4895 int_IO_pressure_value[0] |= INT_FLAG_WARNING; | |
| 4896 else if( pressure_value <= int_O_pressure_need[0] + int_O_pressure_need[0] / 2 ) | |
| 4897 int_IO_pressure_value[0] |= INT_FLAG_ATTENTION; | |
| 4898 } | |
| 4899 else | |
| 4900 { | |
| 4901 // a diluent or need not available: warning & attention by fixed thresholds | |
| 4902 if ( pressure_value <= PRESSURE_LIMIT_WARNING ) int_IO_pressure_value[0] |= INT_FLAG_WARNING; | |
| 4903 else if ( pressure_value <= PRESSURE_LIMIT_ATTENTION ) int_IO_pressure_value[0] |= INT_FLAG_ATTENTION; | |
| 4904 } | |
| 604 | 4905 } |
| 4906 | |
| 623 | 4907 // pressure warnings for reading 2, but only if enabled and pressure value available |
| 4908 if( (char_I_pressure_gas[1] > 0) && !(int_IO_pressure_value[1] & INT_FLAG_NOT_AVAIL) ) | |
| 560 | 4909 { |
| 623 | 4910 overlay unsigned short pressure_value = int_IO_pressure_value[1] & ~INT_FLAG_OUTDATED; |
| 4911 | |
| 4912 if( (char_I_pressure_gas[1] < 6 ) && !(int_O_pressure_need[1] & INT_FLAG_NOT_AVAIL) ) | |
| 560 | 4913 { |
| 623 | 4914 // not a diluent and need available: warning & attention by need |
| 4915 if ( pressure_value <= int_O_pressure_need[1]) | |
| 4916 int_IO_pressure_value[1] |= INT_FLAG_WARNING; | |
| 4917 else if ( pressure_value <= int_O_pressure_need[1] + int_O_pressure_need[1] / 2 ) | |
| 4918 int_IO_pressure_value[1] |= INT_FLAG_ATTENTION; | |
| 560 | 4919 } |
| 4920 else | |
| 4921 { | |
| 623 | 4922 // a diluent or need not available: warning & attention by fixed thresholds |
| 4923 if ( pressure_value <= PRESSURE_LIMIT_WARNING ) int_IO_pressure_value[1] |= INT_FLAG_WARNING; | |
| 4924 else if ( pressure_value <= PRESSURE_LIMIT_ATTENTION ) int_IO_pressure_value[1] |= INT_FLAG_ATTENTION; | |
| 560 | 4925 } |
| 4926 } | |
| 582 | 4927 |
| 623 | 4928 //--- SAC Calculation --------------------------------------------------------------------- |
| 4929 // | |
| 4930 // char_I_SAC_mode =0: disabled | |
| 4931 // =1: SAC from 1st reading | |
| 4932 // =2: SAC from 2nd reading | |
| 4933 // =3: SAC from higher one of both pressure drops (independent double mode) | |
| 4934 // =4: SAC (O2 usage) from 2nd reading without real_pres_respiration term | |
| 4935 | |
| 4936 // set SAC rate to not available by default | |
| 4937 int_O_SAC_measured = 0 + INT_FLAG_NOT_AVAIL; | |
| 4938 | |
| 4939 // get a copy of the current absolute pressure | |
| 4940 pres_respiration_sac = real_pres_respiration; | |
| 4941 | |
| 4942 // set threshold for SAC rate attention | |
| 628 | 4943 max_sac_rate = (deco_info & DECO_MODE) ? char_I_SAC_deco : char_I_SAC_work; |
| 623 | 4944 |
| 4945 // char_I_SAC_deco / char_I_SAC_work are in l/min, max_sac_rate is in 0.1 l/min | |
| 4946 max_sac_rate *= 10; | |
| 4947 | |
| 4948 | |
| 4949 // pre-process SAC mode 3 (independent double) | |
| 4950 if( char_I_SAC_mode == 3 ) | |
| 560 | 4951 { |
| 623 | 4952 overlay unsigned char reading1_gas; |
| 4953 overlay unsigned char reading2_gas; | |
| 4954 overlay unsigned char reading1_tanksize; | |
| 4955 overlay unsigned char reading2_tanksize; | |
| 4956 overlay unsigned short reading1_press; | |
| 4957 overlay unsigned short reading2_press; | |
| 4958 overlay unsigned short reading1_drop; | |
| 4959 overlay unsigned short reading2_drop; | |
| 4960 | |
| 4961 // get gas numbers (1-10) of both readings | |
| 4962 reading1_gas = char_I_pressure_gas[0]; | |
| 4963 reading2_gas = char_I_pressure_gas[1]; | |
| 4964 | |
| 4965 // default to no SAC calculation | |
| 4966 char_I_SAC_mode = 0; | |
| 4967 | |
| 4968 // clear switch advice by default | |
| 4969 deco_info &= ~IND_DOUBLE_SWITCH_FLAG; | |
| 4970 | |
| 4971 // check if both readings are configured and available | |
| 4972 if( reading1_gas ) | |
| 4973 if( reading2_gas ) | |
| 4974 if( !(int_IO_pressure_value[0] & INT_FLAG_NOT_AVAIL) ) | |
| 4975 if( !(int_IO_pressure_value[1] & INT_FLAG_NOT_AVAIL) ) | |
| 4976 if( !(int_I_pressure_drop[0] & INT_FLAG_NOT_AVAIL) ) | |
| 4977 if( !(int_I_pressure_drop[1] & INT_FLAG_NOT_AVAIL) ) | |
| 560 | 4978 { |
| 623 | 4979 // get tank pressures, stripping flags |
| 4980 reading1_press = int_IO_pressure_value[0] & 0x0FFF; // in 0.1 bar | |
| 4981 reading2_press = int_IO_pressure_value[1] & 0x0FFF; // in 0.1 bar | |
| 4982 | |
| 4983 // get pressure drops as integers, stripping flags and shifting right | |
| 4984 // to avoid an overflow when multiplying with the tank size later on | |
| 4985 reading1_drop = (int_I_pressure_drop[0] & 0x0FFF) >> 2; | |
| 4986 reading2_drop = (int_I_pressure_drop[1] & 0x0FFF) >> 2; | |
| 4987 | |
| 4988 // get tank sizes | |
| 4989 reading1_tanksize = char_I_gas_avail_size[reading1_gas-1]; | |
| 4990 reading2_tanksize = char_I_gas_avail_size[reading2_gas-1]; | |
| 4991 | |
| 4992 // set mode to calculate SAC on the reading with the higher absolute drop | |
| 4993 char_I_SAC_mode = (reading1_drop * reading1_tanksize > reading2_drop * reading2_tanksize) ? 1 : 2; | |
| 4994 | |
| 4995 // compute switch advice if pressure (in 0.1 bar) of tank breathed from is | |
| 4996 // more than char_I_max_pres_diff (in bar) below pressure of the other tank. | |
| 4997 if( char_I_SAC_mode == 1 ) | |
| 4998 { | |
| 4999 // breathing from reading 1, switch advice if pressure on reading 1 lower than on 2 | |
| 5000 if( (reading1_press + 10*char_I_max_pres_diff) <= reading2_press ) | |
| 5001 deco_info |= IND_DOUBLE_SWITCH_FLAG; | |
| 5002 } | |
| 5003 else | |
| 5004 { | |
| 5005 // breathing from reading 2, switch advice if pressure on reading 2 lower than on 1 | |
| 5006 if( (reading2_press + 10*char_I_max_pres_diff) <= reading1_press ) | |
| 5007 deco_info |= IND_DOUBLE_SWITCH_FLAG; | |
| 5008 } | |
| 560 | 5009 } |
| 5010 } | |
| 5011 | |
| 623 | 5012 |
| 5013 // pre-process SAC mode 4 (O2 usage by reading 2) | |
| 5014 if( char_I_SAC_mode == 4 ) | |
| 5015 { | |
| 5016 // O2 usage on CCR is independent from absolute pressure | |
| 5017 pres_respiration_sac = 1.0; | |
| 5018 | |
| 5019 // O2 pressure drop is measured via reading 2 | |
| 5020 char_I_SAC_mode = 2; | |
| 5021 | |
| 5022 // reconfigure max SAC rate to O2 consumption attention threshold | |
| 5023 max_sac_rate = O2_CONSUMPTION_LIMIT_ATTENTION; | |
| 5024 } | |
| 5025 | |
| 5026 | |
| 628 | 5027 // select which pressure reading to log |
| 5028 if( char_I_SAC_mode == 1 ) int_O_tank_pressure = int_IO_pressure_value[0]; | |
| 5029 else if( char_I_SAC_mode == 2 ) int_O_tank_pressure = int_IO_pressure_value[1]; | |
| 5030 else int_O_tank_pressure = 0; | |
| 5031 | |
| 5032 // strip flags | |
| 5033 int_O_tank_pressure &= 0x0FFF; | |
| 5034 | |
| 631 | 5035 // scale to recording format of full bar only |
| 628 | 5036 int_O_tank_pressure /= 10; |
| 5037 | |
| 5038 | |
| 623 | 5039 // calculate SAC - modes 1 & 2 |
| 5040 if( (char_I_SAC_mode == 1) || (char_I_SAC_mode == 2) ) | |
| 560 | 5041 { |
| 623 | 5042 overlay unsigned char reading_index; |
| 5043 overlay unsigned char reading_gas; | |
| 5044 overlay unsigned char reading_tanksize; | |
| 5045 overlay float reading_drop; | |
| 5046 | |
| 5047 // set index: char_I_SAC_mode = 1 -> reading one, index 0 | |
| 5048 // = 2 -> two, 1 | |
| 5049 reading_index = char_I_SAC_mode - 1; | |
| 5050 | |
| 5051 // get gas number (1-10) | |
| 5052 reading_gas = char_I_pressure_gas[reading_index]; | |
| 5053 | |
| 5054 // check if reading is configured and available | |
| 5055 if( reading_gas ) | |
| 5056 if( !(int_I_pressure_drop[reading_index] & INT_FLAG_NOT_AVAIL) ) | |
| 5057 { | |
| 5058 // get tank size (in liter) | |
| 5059 reading_tanksize = char_I_gas_avail_size[reading_gas-1]; | |
| 5060 | |
| 5061 // get pressure drop as float, stripping flags (in 1/5120 bar/sec) | |
| 5062 reading_drop = (float)(int_I_pressure_drop[reading_index] & 0x0FFF); | |
| 5063 | |
| 5064 // check if pressure drop is within range | |
| 5065 if( !(int_I_pressure_drop[reading_index] & INT_FLAG_OUT_OF_RANGE) ) | |
| 5066 { | |
| 5067 // calculate SAC, 10 is factor to have result in 0.1 liter/min | |
| 5068 // 60 is factor for 60 seconds per 1 minute, | |
| 5069 // 5120 accounts for reading_drop being in 1/5120 bar/sec | |
| 5070 // 10*60/5120 = 60/512 = 15/128 | |
| 5071 float_sac = reading_drop * 15/128 * reading_tanksize / pres_respiration_sac; | |
| 5072 | |
| 5073 // limit result to 999 (99.9 liter/min) | |
| 5074 if ( float_sac >= 998.5 ) | |
| 5075 { | |
| 5076 int_O_SAC_measured = 999 + INT_FLAG_ATTENTION; | |
| 5077 } | |
| 5078 else | |
| 5079 { | |
| 5080 // convert float to integer | |
| 5081 int_O_SAC_measured = (unsigned short)(float_sac + 0.5); | |
| 5082 | |
| 5083 // set attention flag if exceeding SAC threshold, but only if pressure drop is not outdated | |
| 5084 if( !(int_I_pressure_drop[reading_index] & INT_FLAG_OUTDATED) ) | |
| 5085 if( int_O_SAC_measured >= max_sac_rate ) | |
| 5086 { | |
| 5087 int_O_SAC_measured |= INT_FLAG_ATTENTION; | |
| 5088 } | |
| 5089 } | |
| 5090 } | |
| 5091 else | |
| 5092 { | |
| 5093 // pressure drop is out of range, so SAC will be set out of range, too | |
| 5094 int_O_SAC_measured = 999 + INT_FLAG_ATTENTION; | |
| 5095 } | |
| 5096 | |
| 5097 // copy outdated flag from int_I_pressure_drop to int_O_SAC_measured | |
| 5098 if( int_I_pressure_drop[reading_index] & INT_FLAG_OUTDATED ) | |
| 5099 { | |
| 5100 int_O_SAC_measured |= INT_FLAG_OUTDATED; | |
| 5101 } | |
| 5102 } | |
| 5103 } | |
| 5104 } | |
| 631 | 5105 #endif // _rx_functions |
| 5106 | |
| 5107 | |
| 5108 #ifdef _cave_mode | |
| 5109 | |
| 5110 ////////////////////////////////////////////////////////////////////////////// | |
| 5111 // read_backtrack_data | |
| 5112 // | |
| 5113 // Gets the data of the next backtracking data set | |
| 5114 // | |
| 5115 // Modified: backtrack_index last current position in backtracking storage | |
| 5116 // | |
| 5117 // Output: backtrack_step_counter number of 1/10 min calculation steps to do on next target depth | |
| 5118 // backtrack_target_depth next target depth | |
| 5119 // | |
| 5120 static void read_backtrack_data(void) | |
| 5121 { | |
| 5122 overlay unsigned char firstround = 1; | |
| 5123 | |
| 5124 | |
| 5125 // load the step counter with the default of ten 1/10 minute steps to go between two depth samples | |
| 5126 backtrack_step_counter = 10; | |
| 5127 | |
| 5128 // repeat until having read the whole data set or having reached the first storage position | |
| 5129 while(backtrack_index) | |
| 5130 { | |
| 5131 // backtracking data recording format: | |
| 5132 // --------------------------------------------------------------------------- | |
| 5133 // 0ddddddd -> datum is a depth recording with d = 0..127 depth in meters | |
| 5134 // 10tttttt -> delta time with t = 0.. 59 time in seconds | |
| 5135 // 110ggggg -> gas staging status with g = 1 gas staged | |
| 5136 // 111nnnnn -> waypoint marker with n = 1.. 31 waypoint number | |
| 5137 // | |
| 5138 // gas availability vector: LSB : gas 1 (and so on) | |
| 5139 // waypoint marker : 1 - 30: user waypoints, last waypoint is turn point | |
| 5140 // : 31: spare for turn point if all 30 waypoints used up | |
| 5141 // : 0: reserved | |
| 5142 | |
| 5143 // read recording entry and then advance (backward direction!) the reading index | |
| 5144 overlay unsigned char datum = char_I_backtrack_storage[backtrack_index--]; | |
| 5145 | |
| 5146 // is it a target depth? | |
| 5147 if( datum < 128 ) | |
| 5148 { | |
| 5149 // YES - assign the target depth | |
| 5150 backtrack_target_depth = datum; | |
| 5151 | |
| 5152 // a depth entry closes a data set, so done with this data set | |
| 5153 return; | |
| 5154 } | |
| 5155 | |
| 5156 // is it a delta time entry? | |
| 5157 else if( datum < 128 + 64 ) | |
| 5158 { | |
| 5159 // YES - When a delta time entry is contained in a data set, the time | |
| 5160 // stored in the delta time entry is the time that has elapsed | |
| 5161 // between storage of the depth that is part of this data set | |
| 5162 // and the previous depth recording. | |
| 5163 // This entry is stored whenever the delta time is shorter than | |
| 5164 // the default 1 minute depth sampling interval time. | |
| 5165 | |
| 5166 // assign the delta time to the step counter: remove entry tag and | |
| 5167 // convert datum from 0..59 seconds to 0..9 steps by integer division | |
| 5168 backtrack_step_counter = (datum - 128) / 6; | |
| 5169 } | |
| 5170 | |
| 5171 // is it a gas staging status entry? | |
| 5172 else if( datum < 128 + 64 + 32 ) | |
| 5173 { | |
| 5174 // YES - extract gas staging status | |
| 5175 overlay unsigned char gas_status = datum - (128 + 64); | |
| 5176 | |
| 5177 // decode and update gas staging status | |
| 5178 // | |
| 5179 // bit set : gas is staged (not available) during next section of backtracking, | |
| 5180 // bit cleared: gas is not staged ( available) during next section of backtracking. | |
| 5181 // | |
| 5182 // bit 0 corresponds to gas 1, ..., bit 4 corresponds to gas 5 | |
| 5183 // | |
| 5184 for( i = 0; i < NUM_GAS; i++ ) | |
| 5185 { | |
| 5186 if ( gas_status & (1 << i) ) deco_gas_type[i] |= GAS_AVAIL_STAGED; // staged | |
| 5187 else deco_gas_type[i] &= ~GAS_AVAIL_STAGED; // not staged | |
| 5188 } | |
| 5189 } | |
| 5190 | |
| 5191 // must be waypoint marker entry then | |
| 5192 else | |
| 5193 { | |
| 5194 // set step counter to zero if first entry read is a waypoint entry | |
| 5195 if( firstround ) backtrack_step_counter = 0; | |
| 5196 } | |
| 5197 | |
| 5198 // first round done | |
| 5199 firstround = 0; | |
| 5200 | |
| 5201 } // while | |
| 5202 | |
| 5203 // first storage position reached, by convention it contains | |
| 5204 // the final target depth which is zero meters aka the surface | |
| 5205 backtrack_target_depth = 0; | |
| 5206 } | |
| 5207 | |
| 5208 #endif // _cave_mode | |
| 5209 | |
| 623 | 5210 |
| 5211 | |
| 5212 ////////////////////////////////////////////////////////////////////////////// | |
| 631 | 5213 // convert_volume_to_pressure |
| 623 | 5214 // |
| 5215 // Converts gas volumes into pressures and sets respective flags | |
| 5216 // | |
| 650 | 5217 // Input: index index of the gas to convert (0-4) |
| 623 | 5218 // gas_volume_need[] needed gas volume in liters |
| 5219 // char_I_gas_avail_pres[] available gas volume in bar | |
| 5220 // char_I_gas_avail_size[] size of the tanks in liters | |
| 5221 // char_I_pressure_gas[] gas configured on reading 1/2 (TR only) | |
| 5222 // | |
| 631 | 5223 // Output: int_O_gas_need_vol[] required gas amount in liters |
| 5224 // int_O_gas_need_pres[] required gas amount in bar, including flags | |
| 623 | 5225 // int_O_pressure_need[] required gas amount for reading 1/2 (TR only) |
| 5226 // | |
| 650 | 5227 static void convert_volume_to_pressure(PARAMETER unsigned char index) |
| 623 | 5228 { |
| 5229 // just to make the code more readable... | |
| 650 | 5230 i = index; |
| 623 | 5231 |
| 5232 if( gas_volume_need[i] >= 65534.5 ) | |
| 5233 { | |
| 5234 int_O_gas_need_vol[i] = 65535; // clip at 65535 liters | |
| 5235 int_O_gas_need_pres[i] = 999 | INT_FLAG_WARNING | INT_FLAG_HIGH; // 999 bar + warning flag + >999 flag | |
| 560 | 5236 } |
| 5237 else | |
| 5238 { | |
| 631 | 5239 // convert gas volume need from float to integer [in liter] |
| 5240 int_O_gas_need_vol[i] = (unsigned short)( gas_volume_need[i] + 0.5 ); | |
| 623 | 5241 |
| 5242 // compute how much pressure in the tank will be needed [in bar] | |
| 631 | 5243 int_O_gas_need_pres[i] = (unsigned short)( gas_volume_need[i] / char_I_gas_avail_size[i] + 0.5 ); |
| 623 | 5244 |
| 5245 // limit result to 999 bar because of display constraints | |
| 5246 if( int_O_gas_need_pres[i] > 999 ) int_O_gas_need_pres[i] = 999 | INT_FLAG_HIGH; | |
| 5247 | |
| 631 | 5248 // set flags for fast evaluation by output routine |
| 5249 if( int_O_gas_need_pres[i] == 0 ) int_O_gas_need_pres[i] |= INT_FLAG_ZERO; | |
| 5250 else | |
| 5251 #ifdef _gas_contingency | |
| 5252 if( main_status & GAS_CONTINGENCY ) | |
| 5253 { | |
| 5254 // take warning and attention computed en-route | |
| 5255 if ( deco_gas_type[i] & GAS_NEED_WARNING ) | |
| 5256 { | |
| 5257 // tag the tank with a warning | |
| 5258 int_O_gas_need_pres[i] |= INT_FLAG_WARNING; | |
| 5259 | |
| 5260 // tag the peer tanks with a warning, too | |
| 5261 for( j = 0; j < NUM_GAS; j++ ) | |
| 5262 { | |
| 5263 if( peer_tank[i] & (1 << j) ) int_O_gas_need_pres[j] |= INT_FLAG_WARNING; | |
| 5264 } | |
| 5265 } | |
| 5266 else if( deco_gas_type[i] & GAS_NEED_ATTENTION ) | |
| 5267 { | |
| 5268 // tag the tank with an attention | |
| 5269 int_O_gas_need_pres[i] |= INT_FLAG_ATTENTION; | |
| 5270 | |
| 5271 // tag the peer tanks with an attention, too | |
| 5272 for( j = 0; j < NUM_GAS; j++ ) | |
| 5273 { | |
| 5274 if( peer_tank[i] & (1 << j) ) int_O_gas_need_pres[j] |= INT_FLAG_ATTENTION; | |
| 5275 } | |
| 5276 } | |
| 5277 } | |
| 5278 else | |
| 5279 #endif | |
| 5280 { | |
| 5281 // compute warning and attention now | |
| 5282 if ( gas_volume_need[i] >= gas_volume_avail[i] ) int_O_gas_need_pres[i] |= INT_FLAG_WARNING; | |
| 5283 else if( gas_volume_need[i] >= gas_volume_atten[i] ) int_O_gas_need_pres[i] |= INT_FLAG_ATTENTION; | |
| 5284 } | |
| 560 | 5285 } |
| 5286 | |
| 631 | 5287 // set invalid flag if there is an overflow in the stops table or calculation took too long |
| 628 | 5288 if( deco_warnings & DECO_WARNING_INCOMPLETE ) int_O_gas_need_pres[i] |= INT_FLAG_INVALID; |
| 560 | 5289 |
| 604 | 5290 #ifdef _rx_functions |
| 5291 // only for OSTC TR model with TR functions enabled | |
| 623 | 5292 if( main_status & TR_FUNCTIONS ) |
| 560 | 5293 { |
| 623 | 5294 // char_I_pressure_gas[] uses gas indexes from 1-10, loop variable i runs from 0 to 4 |
| 631 | 5295 j = i+1; |
| 623 | 5296 |
| 5297 // check if the current gas is configured on pressure reading 1 or 2 | |
| 5298 if( (char_I_pressure_gas[0] == j) || (char_I_pressure_gas[1] == j) ) | |
| 560 | 5299 { |
| 623 | 5300 // get a copy of the required pressure in full bar |
| 5301 overlay unsigned short int_pres_need = int_O_gas_need_pres[i]; | |
| 5302 | |
| 5303 // strip all flags | |
| 5304 int_pres_need &= 1023; | |
| 5305 | |
| 5306 // limit to 400 bar and multiply by 10 to get required pressure in 0.1 bar | |
| 5307 int_pres_need = (int_pres_need > 400) ? 4000 | INT_FLAG_OUT_OF_RANGE : 10 * int_pres_need; | |
| 5308 | |
| 631 | 5309 // tag as not available if there is an overflow in the stops table or calculation took too long |
| 628 | 5310 if( deco_warnings & DECO_WARNING_INCOMPLETE ) int_pres_need |= INT_FLAG_NOT_AVAIL; |
| 623 | 5311 |
| 5312 // copy to reading data (in both readings the same gas could be configured) | |
| 5313 if( char_I_pressure_gas[0] == j ) int_O_pressure_need[0] = int_pres_need; | |
| 5314 if( char_I_pressure_gas[1] == j ) int_O_pressure_need[1] = int_pres_need; | |
| 5315 } | |
| 5316 } // TR functions | |
| 604 | 5317 #endif |
| 0 | 5318 } |
| 5319 | |
| 623 | 5320 |
| 5321 ////////////////////////////////////////////////////////////////////////////// | |
| 5322 // convert the real CNS value to integer | |
| 5323 // | |
| 5324 // Input CNS_fraction_real current CNS value as float | |
| 5325 // | |
| 5326 // Output: int_O_CNS_current current CNS value as integer including flags | |
| 5327 // | |
| 5328 static void convert_cur_CNS_for_display(void) | |
| 5329 { | |
| 5330 // convert to integer | |
| 5331 float_value = CNS_fraction_real; convert_float_to_int(); int_O_CNS_current = int_value; | |
| 5332 | |
| 5333 // set warning & attention flags | |
| 628 | 5334 if ( int_O_CNS_current >= CNS_LIMIT_WARNING ) int_O_CNS_current |= INT_FLAG_WARNING; |
| 5335 else if ( int_O_CNS_current >= CNS_LIMIT_ATTENTION ) int_O_CNS_current |= INT_FLAG_ATTENTION; | |
| 623 | 5336 } |
| 5337 | |
| 5338 | |
| 0 | 5339 ////////////////////////////////////////////////////////////////////////////// |
| 623 | 5340 // convert the simulated CNS value to integer |
| 5341 // | |
| 5342 // Input: CNS_fraction_sim CNS value after predicted ascent in float | |
| 5343 // | |
| 5344 // Output: int_sim_CNS_fraction CNS value after predicted ascent in integer | |
| 5345 // including flags, will be routed to | |
| 5346 // int_O_{normal,alternative}_CNS_fraction | |
| 5347 // | |
| 604 | 5348 static void convert_sim_CNS_for_display(void) |
| 582 | 5349 { |
| 623 | 5350 // convert to integer |
| 5351 float_value = CNS_fraction_sim; convert_float_to_int(); int_sim_CNS_fraction = int_value; | |
| 5352 | |
| 5353 // set warning & attention flags | |
| 628 | 5354 if ( int_sim_CNS_fraction >= CNS_LIMIT_WARNING ) int_sim_CNS_fraction |= INT_FLAG_WARNING; |
| 5355 else if ( int_sim_CNS_fraction >= CNS_LIMIT_ATTENTION ) int_sim_CNS_fraction |= INT_FLAG_ATTENTION; | |
| 604 | 5356 |
| 631 | 5357 // set invalid flag if there is an overflow in the stops table or calculation took too long |
| 628 | 5358 if ( deco_warnings & DECO_WARNING_INCOMPLETE ) int_sim_CNS_fraction |= INT_FLAG_INVALID; |
| 604 | 5359 } |
| 5360 | |
| 623 | 5361 |
| 604 | 5362 ////////////////////////////////////////////////////////////////////////////// |
| 623 | 5363 // convert the saturation value of the leading tissue to integer |
| 5364 // | |
| 5365 // Input lead_supersat saturation of the leading tissue | |
| 5366 // lead_tissue number of the leading tissue | |
| 5367 // char_I_GF_High_percentage GF high factor | |
| 5368 // | |
| 5369 // Output: int_O_lead_supersat saturation of the leading tissue | |
| 5370 // char_O_lead_tissue number of the leading tissue | |
| 5371 // | |
| 5372 // Modified: deco_warnings deco engine warnings vector | |
| 5373 // | |
| 5374 static void convert_sat_for_display(void) | |
| 604 | 5375 { |
| 623 | 5376 // convert supersaturation of the leading tissue to int_O_lead_supersat in % (1.0 = 100%) |
| 604 | 5377 // limit to 255 because of constraints in ghostwriter code |
| 623 | 5378 if ( lead_supersat <= 0.000 ) int_O_lead_supersat = 0; |
| 5379 else if ( lead_supersat > 2.545 ) int_O_lead_supersat = 255; | |
| 5380 else int_O_lead_supersat = (unsigned short)(100 * lead_supersat + 0.5); | |
| 5381 | |
| 5382 // set warning & attention flags | |
| 5383 if( int_O_lead_supersat > 100 ) | |
| 604 | 5384 { |
| 623 | 5385 int_O_lead_supersat |= INT_FLAG_WARNING; // make GF factor shown in red |
| 5386 deco_warnings |= DECO_WARNING_OUTSIDE; // make depth shown in red | |
| 5387 } | |
| 631 | 5388 else if( (char_I_model != 0) && (int_O_lead_supersat > char_I_GF_High_percentage) |
| 5389 || (char_I_model == 0) && (int_O_lead_supersat > 99 ) ) | |
| 623 | 5390 { |
| 5391 int_O_lead_supersat |= INT_FLAG_ATTENTION; // make GF factor shown in yellow | |
| 5392 deco_warnings |= DECO_ATTENTION_OUTSIDE; // make depth shown in yellow | |
| 604 | 5393 } |
| 5394 | |
| 5395 // export also the number of the leading tissue | |
| 623 | 5396 char_O_lead_tissue = lead_tissue; |
| 604 | 5397 } |
| 5398 | |
| 623 | 5399 |
| 604 | 5400 ////////////////////////////////////////////////////////////////////////////// |
| 623 | 5401 // convert the ceiling value to integer |
| 5402 // | |
| 5403 // Input: ceiling minimum depth permitted in float | |
| 5404 // | |
| 628 | 5405 // Output: int_O_ceiling minimum depth permitted in mbar (cm) |
| 623 | 5406 // |
| 5407 // Modified: deco_info deco engine information vector | |
| 5408 // | |
| 604 | 5409 static void convert_ceiling_for_display(void) |
| 5410 { | |
| 5411 // Convert ceiling to int_O_ceiling in mbar relative pressure. | |
| 5412 // Round up to next 10 cm so that the ceiling disappears only | |
| 5413 // when the ceiling limit is really zero. This will coincident | |
| 5414 // with TTS switching back to NDL time. | |
| 628 | 5415 // The +1.5 term figures in the conversion factor of 10.015 m/bar |
| 5416 // which is used inside the deco engine but not outside of it. | |
| 623 | 5417 if ( ceiling <= 0.0 ) int_O_ceiling = 0; |
| 5418 else if ( ceiling > 16.0 ) int_O_ceiling = 16000; | |
| 628 | 5419 else int_O_ceiling = (unsigned short)(ceiling * (1000+1.5) + 9); |
| 604 | 5420 |
| 5421 // set/reset ceiling flag | |
| 623 | 5422 if ( int_O_ceiling ) deco_info |= DECO_CEILING; |
| 5423 else deco_info &= ~DECO_CEILING; | |
| 0 | 5424 } |
| 5425 | |
| 623 | 5426 |
| 0 | 5427 ////////////////////////////////////////////////////////////////////////////// |
| 582 | 5428 // push_tissues_to_vault & pull_tissues_from_vault |
| 5429 // | |
| 5430 // ATTENTION: Do not use from inside the deco engine! | |
| 5431 // The vault is exclusively reserved to back-up and restore the real | |
| 5432 // tissues and related data when entering / leaving simulation mode! | |
| 5433 // | |
| 623 | 5434 // Input/Output: CNS_fraction_real current real CNS value |
| 5435 // char_O_deco_warnings deco engine warnings vector | |
| 5436 // real_pres_tissue_N2[] partial pressure of N2 in real tissues | |
| 5437 // real_pres_tissue_He[] partial pressure of He in real tissues | |
| 5438 // | |
| 5439 // Output: int_O_CNS_current current CNS value as integer including flags | |
| 5440 // | |
| 604 | 5441 static void push_tissues_to_vault(void) |
| 0 | 5442 { |
| 623 | 5443 // store the current CNS value and deco warnings |
| 5444 vault_CNS_fraction_real = CNS_fraction_real; | |
| 5445 vault_deco_warnings = char_O_deco_warnings; | |
| 5446 vault_deco_info = char_O_deco_info; | |
| 5447 | |
| 650 | 5448 // copy the real pressures to the vault |
| 5449 memcpy(vault_pres_tissue_N2, real_pres_tissue_N2, 256); | |
| 0 | 5450 } |
| 5451 | |
| 604 | 5452 static void pull_tissues_from_vault(void) |
| 0 | 5453 { |
| 623 | 5454 // restore the CNS value and deco warnings |
| 5455 CNS_fraction_real = vault_CNS_fraction_real; | |
| 5456 char_O_deco_warnings = vault_deco_warnings; | |
| 5457 char_O_deco_info = vault_deco_info; | |
| 5458 | |
| 5459 // convert the CNS value to integer | |
| 5460 convert_cur_CNS_for_display(); | |
| 5461 | |
| 650 | 5462 // copy the vault back to the real pressures |
| 5463 memcpy(real_pres_tissue_N2, vault_pres_tissue_N2, 256); | |
| 0 | 5464 } |
| 5465 | |
| 623 | 5466 |
| 0 | 5467 ////////////////////////////////////////////////////////////////////////////// |
| 5468 // | |
| 5469 #ifndef CROSS_COMPILE | |
| 5470 void main() {} | |
| 5471 #endif |
