comparison src/p2_deco.c @ 623:c40025d8e750

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