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