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