Mercurial > public > hwos_code
annotate src/p2_deco.c @ 522:4d70a93b18cb
BUGFIX: Impossible fixed-ppO2 (e.g. 1,6bar in 3m) used for tissue calculations
author | heinrichsweikamp |
---|---|
date | Fri, 04 Aug 2017 16:19:50 +0200 |
parents | 06e9370c6d75 |
children | 015b7fdd90a7 |
rev | line source |
---|---|
0 | 1 // ************************************************************** |
2 // p2_deco.c | |
3 // | |
4 // Created on: 12.05.2009 | |
5 // Author: chsw | |
6 // | |
7 // ************************************************************** | |
8 | |
9 ////////////////////////////////////////////////////////////////////////////// | |
10 // OSTC - diving computer code | |
11 // Copyright (C) 2011 HeinrichsWeikamp GbR | |
12 // | |
13 // This program is free software: you can redistribute it and/or modify | |
14 // it under the terms of the GNU General Public License as published by | |
15 // the Free Software Foundation, either version 3 of the License, or | |
16 // (at your option) any later version. | |
17 // | |
18 // This program is distributed in the hope that it will be useful, | |
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 // GNU General Public License for more details. | |
22 // | |
23 // You should have received a copy of the GNU General Public License | |
24 // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 // | |
26 ////////////////////////////////////////////////////////////////////////////// | |
27 | |
28 // ***************************** | |
29 // ** I N T R O D U C T I O N ** | |
30 // ***************************** | |
31 // | |
32 // OSTC | |
33 // | |
34 // code: | |
35 // p2_deco_main_c_v101.c | |
36 // part2 of the OSTC code | |
37 // code with constant O2 partial pressure routines | |
38 // under construction !! | |
39 // | |
40 // summary: | |
41 // decompression routines | |
42 // for the OSTC experimental project | |
43 // written by Christian Weikamp | |
44 // last revision __________ | |
45 // comments added _________ | |
46 // | |
47 // additional files: | |
48 // p2_tables_v100.romdata (other files) | |
49 // 18f4685_ostc_v100.lkr (linker script) | |
50 // | |
51 // history: | |
52 // 01/03/08 v100: first release candidate | |
53 // 03/13/08 v101: start of programming ppO2 code | |
54 // 03/13/25 v101a: backup of interrim version with ppO2 calculation | |
55 // 03/13/25 v101: open circuit gas change during deco | |
56 // 03/13/25 v101: CNS_fraction calculation | |
57 // 03/13/26 v101: optimization of tissue calc routines | |
58 // 07/xx/08 v102a: debug of bottom time routine | |
59 // 09/xx/08 v102d: Gradient Factor Model implemenation | |
60 // 10/10/08 v104: renamed to build v103 for v118 stable | |
61 // 10/14/08 v104: integration of char_I_depth_last_deco for Gradient Model | |
62 // 03/31/09 v107: integration of FONT Incon24 | |
63 // 05/23/10 v109: 5 gas changes & 1 min timer | |
64 // 07/13/10 v110: cns vault added | |
65 // 12/25/10 v110: split in three files (deco.c, main.c, definitions.h) | |
66 // 2011/01/20: [jDG] Create a common file included in ASM and C code. | |
67 // 2011/01/24: [jDG] Make ascenttime an short. No more overflow! | |
68 // 2011/01/25: [jDG] Fusion deco array for both models. | |
69 // 2011/01/25: [jDG] Use CF(54) to reverse deco order. | |
70 // 2011/02/11: [jDG] Reworked gradient-factor implementation. | |
71 // 2011/02/15: [jDG] Fixed inconsistencies introduced by gas switch delays. | |
72 // 2011/03/21: [jDG] Added gas consumption (CF56 & CF57) evaluation for OCR mode. | |
73 // 2011/04/15: [jDG] Store low_depth in 32bits (w/o rounding), for a better stability. | |
74 // 2011/04/25: [jDG] Added 1mn mode for CNS calculation, to allow it for decoplanning. | |
75 // 2011/04/27: [jDG] Fixed char_O_gradient_factor calculation when model uses gradient-factor. | |
76 // 2011/05/02: [jDG] Added "Future TTS" function (CF58). | |
77 // 2011/05/17: [jDG] Various cleanups. | |
78 // 2011/08/08: [jDG] Computes CNS during deco planning ascent. | |
79 // 2011/11/24: [jDG] Slightly faster and better NDL computation. | |
80 // 2011/12/17: [mH] Remove of the useless debug stuff | |
81 // 2012/02/24: [jDG] Remove missed stop bug. | |
82 // 2012/02/25: [jDG] Looking for a more stable LOW grad factor reference. | |
83 // 2012/09/10: [mH] Fill char_O_deco_time_for_log for logbook write | |
84 // 2012/10/05: [jDG] Better deco_gas_volumes accuracy (average depth, switch between stop). | |
85 // 2013/03/05: [jDG] Should vault low_depth too. | |
86 // 2013/03/05: [jDG] Wrobell remark: ascent_to_first_stop works better with finer steps (2sec). | |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
87 // 2013/05/08: [jDG] A. Salm remark: NOAA tables for CNS are in ATA, not bar. |
115
72259a79eac2
FIX: Gas volume computation when using several travel mix.
heinrichsweikamp
parents:
114
diff
changeset
|
88 // 2013/12/21: [jDG] Fix CNS calculation in decoplan w/o marked gas switch |
72259a79eac2
FIX: Gas volume computation when using several travel mix.
heinrichsweikamp
parents:
114
diff
changeset
|
89 // 2014/06/16: [jDG] Fix Helium diluant. Fix volumes with many travel mix. |
324
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
90 // 2014/06/29: [mH] Compute int_O_ceiling |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
91 // 2015/06/12: [jDG] Fix NDL prediction while desaturating with the Buhlmann model. |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
92 // 2017/08/04: [mH] Switch to absolute GF everywhere and apply safety margin parameters to both models (GF and non-GF), fixes from Ralph Lembcke |
0 | 93 // |
94 // TODO: | |
95 // | |
96 // Literature: | |
313 | 97 // Buhlmann, Albert: Tauchmedizin; 4. Auflage [2002]; |
0 | 98 // Schr"oder, Kai & Reith, Steffen; 2000; S"attigungsvorg"ange beim Tauchen, das Modell ZH-L16, Funktionsweise von Tauchcomputern; http://www.achim-und-kai.de/kai/tausim/saett_faq |
99 // Morrison, Stuart; 2000; DIY DECOMPRESSION; http://www.lizardland.co.uk/DIYDeco.html | |
100 // Balthasar, Steffen; Dekompressionstheorie I: Neo Haldane Modelle; http://www.txfreak.de/dekompressionstheorie_1.pdf | |
101 // Baker, Erik C.; Clearing Up The Confusion About "Deep Stops" | |
102 // Baker, Erik C.; Understanding M-values; http://www.txfreak.de/understanding_m-values.pdf | |
103 // | |
104 // | |
105 | |
106 // ********************* | |
107 // ** I N C L U D E S ** | |
108 // ********************* | |
109 #include <math.h> | |
110 | |
111 // *********************************************** | |
112 // ** V A R I A B L E S D E F I N I T I O N S ** | |
113 // *********************************************** | |
114 | |
115 #include "p2_definitions.h" | |
116 #define TEST_MAIN | |
117 #include "shared_definitions.h" | |
118 | |
519 | 119 // Water vapour partial pressure in the lungs |
0 | 120 #define ppWater 0.0627 |
121 #define METER_TO_BAR 0.09985 | |
122 #define BAR_TO_METER 10.0150 // (1.0/METER_TO_BAR) | |
123 | |
124 // Surface security factor | |
125 #define SURFACE_DESAT_FACTOR 0.7042 | |
126 | |
127 // ************************* | |
128 // ** P R O T O T Y P E S ** | |
129 // ************************* | |
130 | |
131 static void calc_hauptroutine(void); | |
132 static void calc_nullzeit(void); | |
133 | |
134 static void calc_tissue(PARAMETER unsigned char period); | |
135 static void calc_limit(void); | |
136 | |
137 static void clear_tissue(void); | |
138 static void calc_ascenttime(void); | |
139 static void update_startvalues(void); | |
140 static void clear_deco_table(void); | |
141 static unsigned char update_deco_table(void); | |
142 | |
143 static void sim_tissue(PARAMETER unsigned char period); | |
144 static void sim_limit(PARAMETER float GF_current); | |
145 static void sim_extra_time(void); | |
146 static void calc_dive_interval(void); | |
147 | |
148 static void calc_gradient_factor(void); | |
149 static void calc_wo_deco_step_1_min(void); | |
150 | |
151 static void calc_hauptroutine_data_input(void); | |
152 static void calc_hauptroutine_update_tissues(void); | |
153 static void calc_hauptroutine_calc_deco(void); | |
154 static void sim_ascent_to_first_stop(void); | |
155 | |
156 static unsigned char gas_switch_deepest(void); | |
157 static void gas_switch_set(void); | |
158 | |
159 static unsigned char calc_nextdecodepth(void); | |
160 | |
161 //---- Bank 5 parameters ----------------------------------------------------- | |
162 #ifndef UNIX | |
163 # pragma udata bank5=0x500 | |
164 #endif | |
165 | |
166 static float GF_low; | |
167 static float GF_high; | |
168 static float GF_delta; | |
169 static float locked_GF_step; // GF_delta / low_depth | |
170 | |
171 static unsigned char temp_depth_limit; | |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
172 float low_depth; // Depth of deepest stop |
0 | 173 |
174 // Simulation context: used to predict ascent. | |
175 static unsigned char sim_lead_tissue_no; // Leading compatiment number. | |
176 static float sim_lead_tissue_limit; // Buhlmann tolerated pressure. | |
177 | |
178 // Real context: what we are doing now. | |
179 static float calc_lead_tissue_limit; // | |
180 | |
181 static unsigned char internal_deco_time[NUM_STOPS]; | |
182 static unsigned char internal_deco_depth[NUM_STOPS]; | |
183 | |
184 static float cns_vault; | |
185 static float low_depth_vault; | |
186 static float pres_tissue_N2_vault[NUM_COMP]; | |
187 static float pres_tissue_He_vault[NUM_COMP]; | |
188 | |
189 //---- Bank 6 parameters ----------------------------------------------------- | |
190 #ifndef UNIX | |
191 # pragma udata bank6=0x600 | |
192 #endif | |
193 | |
194 static unsigned char ci; | |
324
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
195 static float pres_respiration; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
196 static float pres_surface; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
197 static float temp_deco; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
198 static float ppN2; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
199 static float ppHe; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
200 static float temp_tissue; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
201 static float N2_ratio; // Breathed gas nitrogen ratio. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
202 static float He_ratio; // Breathed gas helium ratio. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
203 static float var_N2_a; // Buhlmann a, for current N2 tissue. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
204 static float var_N2_b; // Buhlmann b, for current N2 tissue. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
205 static float var_He_a; // Buhlmann a, for current He tissue. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
206 static float var_He_b; // Buhlmann b, for current He tissue. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
207 static float var_N2_e; // Exposition, for current N2 tissue. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
208 static float var_He_e; // Exposition, for current He tissue. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
209 static float var_N2_ht; // Half-time for current N2 tissue. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
210 static float var_He_ht; // Half-time for current N2 tissue. |
0 | 211 |
212 static float pres_diluent; // new in v.101 | |
213 static float const_ppO2; // new in v.101 | |
214 | |
215 static unsigned char sim_gas_last_depth; // Depth of last used gas, to detected a gas switch. | |
216 static unsigned char sim_gas_last_used; // Number of last used gas, to detected a gas switch. | |
217 static unsigned short sim_dive_mins; // Simulated dive time. | |
324
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
218 static float calc_N2_ratio; // Simulated (switched) nitrogen ratio. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
219 static float calc_He_ratio; // Simulated (switched) helium ratio. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
220 static float CNS_fraction; // new in v.101 |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
221 static float float_saturation_multiplier; // new in v.101 |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
222 static float float_desaturation_multiplier; // new in v.101 |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
223 static float float_deco_distance; // new in v.101 |
0 | 224 |
225 static unsigned char deco_gas_change[NUM_GAS]; // new in v.109 | |
111 | 226 static unsigned char internal_deco_gas [NUM_STOPS]; |
0 | 227 |
228 //---- Bank 7 parameters ----------------------------------------------------- | |
229 #ifndef UNIX | |
230 # pragma udata bank7=0x700 | |
231 #endif | |
133
939f1e83c4c2
BUGFIX: Surface interval was not displayed correctly in some cases
heinrichsweikamp
parents:
126
diff
changeset
|
232 // Keep order of 0x700 variables |
0 | 233 float pres_tissue_N2[NUM_COMP]; |
234 float pres_tissue_He[NUM_COMP]; | |
235 float sim_pres_tissue_N2[NUM_COMP]; // 16 floats = 64 bytes. | |
236 float sim_pres_tissue_He[NUM_COMP]; // 16 floats = 64 bytes. | |
237 | |
238 //---- Bank 8 parameters ----------------------------------------------------- | |
239 #ifndef UNIX | |
240 # pragma udata overlay bank8=0x800 | |
241 static char md_pi_subst[256]; | |
242 # define C_STACK md_pi_subst // Overlay C-code data stack here, too. | |
243 #endif | |
244 | |
245 // Back to bank6 for further tmp data | |
246 #ifndef UNIX | |
247 # pragma udata bank6 | |
248 #endif | |
249 | |
250 ////////////////////////////////////////////////////////////////////////////// | |
251 ////////////////////////////////////////////////////////////////////////////// | |
252 ///////////////////////////// THE LOOKUP TABLES ////////////////////////////// | |
253 ////////////////////////////////////////////////////////////////////////////// | |
254 ////////////////////////////////////////////////////////////////////////////// | |
255 // | |
256 // End of PROM code is 17F00, So push tables on PROM top... | |
257 // | |
258 #ifndef UNIX | |
259 # pragma romdata buhlmann_tables = 0x1DD00 // Needs to be in UPPER bank. | |
260 #endif | |
261 | |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
262 rom const float buhlmann_ab[4*16] = { |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
263 // Data ZH-L16C, from Bühlmann Tauchmedizin 2002, option 1a (4mn) |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
264 // a for N2 b for N2 a of He b for He |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
265 1.2599, 0.5050, 1.7424, 0.4245, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
266 1.0000, 0.6514, 1.3830, 0.5747, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
267 0.8618, 0.7222, 1.1919, 0.6527, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
268 0.7562, 0.7825, 1.0458, 0.7223, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
269 0.6200, 0.8126, 0.9220, 0.7582, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
270 0.5043, 0.8434, 0.8205, 0.7957, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
271 0.4410, 0.8693, 0.7305, 0.8279, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
272 0.4000, 0.8910, 0.6502, 0.8553, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
273 0.3750, 0.9092, 0.5950, 0.8757, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
274 0.3500, 0.9222, 0.5545, 0.8903, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
275 0.3295, 0.9319, 0.5333, 0.8997, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
276 0.3065, 0.9403, 0.5189, 0.9073, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
277 0.2835, 0.9477, 0.5181, 0.9122, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
278 0.2610, 0.9544, 0.5176, 0.9171, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
279 0.2480, 0.9602, 0.5172, 0.9217, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
280 0.2327, 0.9653, 0.5119, 0.9267 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
281 }; |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
282 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
283 rom const float buhlmann_ht[2*16] = { |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
284 // Compartiment half-life, in minute |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
285 //-- N2 ---- He --------------------------------------------------------------------- |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
286 4.0, 1.51, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
287 8.0, 3.02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
288 12.5, 4.72, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
289 18.5, 6.99, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
290 27.0, 10.21, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
291 38.3, 14.48, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
292 54.3, 20.53, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
293 77.0, 29.11, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
294 109.0, 41.20, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
295 146.0, 55.19, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
296 187.0, 70.69, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
297 239.0, 90.34, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
298 305.0, 115.29, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
299 390.0, 147.42, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
300 498.0, 188.24, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
301 635.0, 240.03 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
302 }; |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
303 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
304 rom const float e2secs[2*16] = { |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
305 // result of 1 - 2^(-1/(30sec*HT)) |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
306 //---- N2 ------------- He ------------ |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
307 5.75958E-03, 1.51848E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
308 2.88395E-03, 7.62144E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
309 1.84669E-03, 4.88315E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
310 1.24813E-03, 3.29997E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
311 8.55371E-04, 2.26041E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
312 6.03079E-04, 1.59437E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
313 4.25414E-04, 1.12479E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
314 3.00019E-04, 7.93395E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
315 2.11949E-04, 5.60641E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
316 1.58240E-04, 4.18555E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
317 1.23548E-04, 3.26795E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
318 9.66686E-05, 2.55722E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
319 7.57509E-05, 2.00387E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
320 5.92416E-05, 1.56716E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
321 4.63943E-05, 1.22734E-04, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
322 3.63850E-05, 9.62538E-05 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
323 //------------------------------------- |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
324 }; |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
325 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
326 rom const float e1min[2*16] = { |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
327 // Integration constant for 1 minute, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
328 // Ie. 1- 2^(-1/HT) |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
329 //----- N2 --------- e 1min He -------- |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
330 1.59104E-01, 3.68109E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
331 8.29960E-02, 2.05084E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
332 5.39424E-02, 1.36579E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
333 3.67742E-02, 9.44046E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
334 2.53454E-02, 6.56359E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
335 1.79351E-02, 4.67416E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
336 1.26840E-02, 3.31991E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
337 8.96152E-03, 2.35301E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
338 6.33897E-03, 1.66832E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
339 4.73633E-03, 1.24808E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
340 3.69981E-03, 9.75753E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
341 2.89600E-03, 7.64329E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
342 2.27003E-03, 5.99417E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
343 1.77572E-03, 4.69082E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
344 1.39089E-03, 3.67548E-03, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
345 1.09097E-03, 2.88359E-03 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
346 //------------------------------------- |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
347 }; |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
348 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
349 rom const float e10min[2*16] = { |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
350 // The 10 min Value in float notation: |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
351 // result of 1 - 2^(-10/ht) |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
352 //---- N2 -------------- He ----------- |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
353 8.23223E-01, 9.89851E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
354 5.79552E-01, 8.99258E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
355 4.25651E-01, 7.69737E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
356 3.12487E-01, 6.29027E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
357 2.26416E-01, 4.92821E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
358 1.65547E-01, 3.80407E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
359 1.19840E-01, 2.86538E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
360 8.60863E-02, 2.11886E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
361 6.16117E-02, 1.54849E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
362 4.63665E-02, 1.18026E-01, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
363 3.63881E-02, 9.34005E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
364 2.85855E-02, 7.38569E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
365 2.24698E-02, 5.83504E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
366 1.76160E-02, 4.59303E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
367 1.38222E-02, 3.61528E-02, |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
368 1.08563E-02, 2.84646E-02 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
369 //------------------------------------- |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
370 }; |
0 | 371 |
372 ////////////////////////////////////////////////////////////////////////////// | |
373 ////////////////////////////////////////////////////////////////////////////// | |
374 ////////////////////////////// THE SUBROUTINES /////////////////////////////// | |
375 ////////////////////////////////////////////////////////////////////////////// | |
376 ////////////////////////////////////////////////////////////////////////////// | |
377 // | |
378 // all new in v.102 | |
379 // moved from 0x0D000 to 0x0C000 in v.108 | |
380 #ifndef UNIX | |
381 # pragma code p2_deco = 0x0C000 | |
382 #endif | |
383 | |
384 ////////////////////////////////////////////////////////////////////////////// | |
385 ////////////////////////////////////////////////////////////////////////////// | |
386 /////////////////////// U T I L I T I E S ///////////////////////////////// | |
387 ////////////////////////////////////////////////////////////////////////////// | |
388 ////////////////////////////////////////////////////////////////////////////// | |
389 | |
390 ////////////////////////////////////////////////////////////////////////////// | |
391 // Bump to blue-screen when an assert is wrong | |
392 #ifdef __DEBUG | |
393 void assert_failed(PARAMETER short int line) | |
394 { | |
395 } | |
396 #endif | |
397 | |
398 ////////////////////////////////////////////////////////////////////////////// | |
399 // When calling C code from ASM context, the data stack pointer and | |
400 // frames should be reset. Bank8 is used by stack | |
401 | |
402 #ifdef CROSS_COMPILE | |
403 # define RESET_C_STACK | |
404 #else | |
405 # ifdef __DEBUG | |
406 # define RESET_C_STACK fillDataStack(); | |
407 void fillDataStack(void) | |
408 { | |
409 _asm | |
410 LFSR 1,C_STACK | |
411 MOVLW 0xCC | |
412 loop: MOVWF POSTINC1,0 | |
413 TSTFSZ FSR1L,0 | |
414 BRA loop | |
415 | |
416 LFSR 1,C_STACK | |
417 LFSR 2,C_STACK | |
418 _endasm | |
419 } | |
420 # else | |
421 # define RESET_C_STACK \ | |
422 _asm \ | |
423 LFSR 1, C_STACK \ | |
424 LFSR 2, C_STACK \ | |
425 _endasm | |
426 # endif | |
427 #endif | |
428 | |
429 ////////////////////////////////////////////////////////////////////////////// | |
430 // Fast subroutine to read timer 5. | |
313 | 431 // Note: result is in 1/32 of msecs (30,51757813 us/bit to be precise) |
0 | 432 static unsigned short tmr5(void) |
433 { | |
434 #ifndef CROSS_COMPILE | |
435 _asm | |
436 movff 0xf7c,PRODL // TMR5L | |
437 movff 0xf7d,PRODH // TMR5H | |
438 _endasm // result in PRODH:PRODL. | |
439 #else | |
440 return 0; | |
441 #endif | |
442 } | |
443 | |
444 | |
445 ////////////////////////////////////////////////////////////////////////////// | |
446 // read buhlmann tables A and B for compatriment ci | |
447 // | |
448 static void read_buhlmann_coefficients(void) | |
449 { | |
450 #ifndef CROSS_COMPILE | |
451 // Note: we don't use far rom pointer, because the | |
452 // 24 bits is too complex, hence we have to set | |
453 // the UPPER page ourself... | |
454 // --> Set zero if tables are moved to lower pages ! | |
455 _asm | |
456 movlw 1 | |
457 movwf TBLPTRU,0 | |
458 _endasm | |
459 #endif | |
460 | |
461 assert( ci < NUM_COMP ); | |
462 | |
463 // Use an interleaved array (AoS) to access coefficients with a | |
464 // single addressing. | |
465 { | |
466 overlay rom const float* ptr = &buhlmann_ab[4*ci]; | |
467 var_N2_a = *ptr++; | |
468 var_N2_b = *ptr++; | |
469 var_He_a = *ptr++; | |
470 var_He_b = *ptr++; | |
471 } | |
472 } | |
473 | |
474 ////////////////////////////////////////////////////////////////////////////// | |
475 // read buhlmann tables for compatriment ci | |
476 // If period == 0 : 2sec interval | |
477 // 1 : 1 min interval | |
478 // 2 : 10 min interval. | |
479 static void read_buhlmann_times(PARAMETER char period) | |
480 { | |
481 #ifndef CROSS_COMPILE | |
482 // Note: we don't use far rom pointer, because the | |
483 // 24 bits is to complex, hence we have to set | |
484 // the UPPER page ourself... | |
485 // --> Set zero if tables are moved to lower pages ! | |
486 _asm | |
487 movlw 1 | |
488 movwf TBLPTRU,0 | |
489 _endasm | |
490 #endif | |
491 | |
492 assert( ci < NUM_COMP ); | |
493 | |
494 // Integration intervals. | |
495 switch(period) | |
496 { | |
497 case 0: //---- 2 sec ----------------------------------------------------- | |
498 { | |
499 overlay rom const float* ptr = &e2secs[2*ci]; | |
500 var_N2_e = *ptr++; | |
501 var_He_e = *ptr++; | |
502 } | |
503 break; | |
504 | |
505 case 1: //---- 1 min ----------------------------------------------------- | |
506 { | |
507 overlay rom const float* ptr = &e1min[2*ci]; | |
508 var_N2_e = *ptr++; | |
509 var_He_e = *ptr++; | |
510 } | |
511 break; | |
512 | |
513 case 2: //---- 10 min ---------------------------------------------------- | |
514 { | |
515 overlay rom const float* ptr = &e10min[2*ci]; | |
516 var_N2_e = *ptr++; | |
517 var_He_e = *ptr++; | |
518 } | |
519 break; | |
520 | |
521 default: | |
522 assert(0); // Never go there... | |
523 } | |
524 } | |
525 | |
526 ////////////////////////////////////////////////////////////////////////////// | |
527 // read buhlmann tables for compatriment ci | |
528 // | |
529 static void read_buhlmann_ht(void) | |
530 { | |
531 | |
532 #ifndef CROSS_COMPILE | |
533 // Note: we don't use far rom pointer, because the | |
534 // 24 bits is to complex, hence we have to set | |
535 // the UPPER page ourself... | |
536 // --> Set zero if tables are moved to lower pages ! | |
537 _asm | |
538 movlw 1 | |
539 movwf TBLPTRU,0 | |
540 _endasm | |
541 #endif | |
542 | |
543 assert( ci < NUM_COMP ); | |
544 { | |
545 overlay rom const float* ptr = &buhlmann_ht[2*ci]; | |
546 var_N2_ht = *ptr++; | |
547 var_He_ht = *ptr++; | |
548 } | |
549 | |
550 assert( 4.0 <= var_N2_ht && var_N2_ht <= 635.0 ); | |
551 assert( 1.5099 <= var_He_ht && var_He_ht <= 240.03 ); | |
552 } | |
553 | |
554 ////////////////////////////////////////////////////////////////////////////// | |
555 // calc_nextdecodepth | |
556 // | |
557 // new in v.102 | |
558 // | |
559 // INPUT, changing during dive: | |
560 // temp_deco | |
561 // low_depth | |
562 // | |
563 // INPUT, fixed during dive: | |
564 // pres_surface | |
565 // GF_delta | |
566 // GF_high | |
567 // GF_low | |
568 // char_I_depth_last_deco | |
569 // float_deco_distance | |
570 // | |
571 // RETURN TRUE iff a stop is needed. | |
572 // | |
573 // OUTPUT | |
574 // locked_GF_step | |
575 // temp_depth_limt | |
576 // low_depth | |
577 // | |
578 static unsigned char calc_nextdecodepth(void) | |
579 { | |
580 //--- Max ascent speed --------------------------------------------------- | |
581 // Recompute leading gas limit, at current depth: | |
582 overlay float depth = (temp_deco - pres_surface) * BAR_TO_METER; | |
583 | |
584 // At most, ascent 1 minute, at 10m/min == 10.0 m. | |
585 overlay float min_depth = (depth > 10.0) ? (depth - 10.0) : 0.0; | |
586 | |
587 // Do we need to stop at current depth ? | |
588 overlay unsigned char need_stop = 0; | |
589 | |
590 assert( depth >= -0.2 ); // Allow for 200mbar of weather change. | |
591 | |
592 //---- ZH-L16 + GRADIENT FACTOR model ------------------------------------ | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
593 if( char_I_deco_model != 0 ) |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
594 { |
0 | 595 overlay unsigned char first_stop = 0; |
596 overlay float p; | |
597 | |
598 sim_limit( GF_low ); | |
599 p = sim_lead_tissue_limit - pres_surface; | |
600 if( p <= 0.0f ) | |
601 goto no_deco_stop; // We can surface directly... | |
602 | |
603 p *= BAR_TO_METER; | |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
604 |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
605 // Store the deepest point needing a deco stop as the LOW reference for GF. |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
606 // NOTE: following stops will be validated using this LOW-HIGH gf scale, |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
607 // so if we want to keep coherency, we should not validate this stop |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
608 // yet, but apply the search to it, as for all the following stops afterward. |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
609 if( p > low_depth ) |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
610 { |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
611 low_depth = p; |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
612 locked_GF_step = GF_delta / low_depth; |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
613 } |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
614 |
0 | 615 if( p < min_depth ) |
616 goto no_deco_stop; // First stop is higher than 1' ascent. | |
617 | |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
618 // Round to multiple of 3m. |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
619 first_stop = 3 * (short)(0.9995f + p*0.333333f); |
0 | 620 assert( first_stop < 128 ); |
621 | |
622 // Apply correction for the shallowest stop. | |
623 if( first_stop == 3 ) // new in v104 | |
624 first_stop = char_I_depth_last_deco; // Use last 3m..6m instead. | |
625 | |
626 // We have a stop candidate. | |
627 // But maybe ascending to the next stop will diminish the constraint, | |
628 // because the GF might decrease more than the preassure gradient... | |
629 while(first_stop > 0) | |
630 { | |
631 overlay unsigned char next_stop; // Next depth (0..90m) | |
632 | |
633 // Check max speed, or reaching surface. | |
634 if( first_stop <= min_depth ) | |
635 goto no_deco_stop; | |
636 | |
637 if( first_stop <= char_I_depth_last_deco ) // new in v104 | |
638 next_stop = 0; | |
639 else if( first_stop == 6 ) | |
640 next_stop = char_I_depth_last_deco; | |
641 else | |
642 next_stop = first_stop - 3; // Index of next (upper) stop. | |
643 | |
644 // Total preassure at the new stop candidate: | |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
645 p = next_stop * METER_TO_BAR |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
646 + pres_surface; |
0 | 647 |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
648 // Recompute limit for this new stop: |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
649 if( !low_depth || next_stop > low_depth ) |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
650 sim_limit( GF_low ); |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
651 else |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
652 sim_limit( GF_high - next_stop * locked_GF_step ); |
0 | 653 |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
654 // Check upper limit (lowest ambiant pressure tolerated): |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
655 if( sim_lead_tissue_limit >= p ) |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
656 goto deco_stop_found; // Ascent to next_stop forbiden. |
0 | 657 |
658 // Else, validate that stop and loop... | |
659 first_stop = next_stop; | |
660 } | |
661 | |
662 no_deco_stop: | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
663 temp_depth_limit = min_depth; |
0 | 664 goto done; |
665 | |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
666 deco_stop_found: |
0 | 667 // next stop is the last validated depth found, aka first_stop |
668 need_stop = 1; // Hit. | |
669 temp_depth_limit = first_stop; // Stop depth, in meter. | |
670 | |
671 done: | |
672 ; | |
673 } | |
674 else //---- ZH-L16 model ------------------------------------------------- | |
675 { | |
676 overlay float pres_gradient; | |
677 | |
678 // Original model | |
679 // optimized in v.101 | |
680 // char_I_depth_last_deco included in v.101 | |
681 | |
682 // Compute sim_lead_tissue_limit too, but just once. | |
683 sim_limit(1.0); | |
684 | |
685 pres_gradient = sim_lead_tissue_limit - pres_surface; | |
686 if (pres_gradient >= 0) | |
687 { | |
379 | 688 pres_gradient *= BAR_TO_METER/3; // bar --> stop number; |
0 | 689 temp_depth_limit = 3 * (short) (pres_gradient + 0.99); // --> metre : depth for deco |
690 need_stop = 1; // Hit. | |
691 | |
692 // Implement last stop at 4m/5m/6m... | |
693 if( temp_depth_limit == 3 ) | |
694 temp_depth_limit = char_I_depth_last_deco; | |
695 } | |
696 else | |
697 temp_depth_limit = 0; | |
698 } | |
699 | |
700 //---- Check gas change -------------------------------------------------- | |
701 need_stop |= gas_switch_deepest(); // Update temp_depth_limit if there is a change, | |
702 | |
703 return need_stop; | |
704 } | |
705 | |
706 ////////////////////////////////////////////////////////////////////////////// | |
707 // copy_deco_table | |
708 // | |
709 // Buffer the stops, once computed, so we can continue to display them | |
710 // while computing the next set. | |
711 // | |
712 static void copy_deco_table(void) | |
713 { | |
714 // Copy depth of the first (deepest) stop, because when reversing | |
715 // order, it will be hard to find... | |
111 | 716 char_O_first_deco_depth = internal_deco_depth[0]; |
0 | 717 char_O_first_deco_time = internal_deco_time [0]; |
718 | |
719 { | |
720 overlay unsigned char x, y; | |
721 | |
722 for(x=0; x<NUM_STOPS; x++) | |
723 { | |
724 char_O_deco_depth[x] = internal_deco_depth[x]; | |
725 char_O_deco_time [x] = internal_deco_time [x]; | |
111 | 726 char_O_deco_gas [x] = internal_deco_gas [x]; |
0 | 727 } |
728 | |
729 //Now fill the char_O_deco_time_for_log array | |
730 //---- First: search the first non-null depth | |
731 for(x=(NUM_STOPS-1); x != 0; --x) | |
732 if( internal_deco_depth[x] != 0 ) break; | |
733 | |
734 //---- Second: copy to output table (in reverse order) | |
735 for(y=0; y<NUM_STOPS; y++, --x) | |
736 { | |
737 char_O_deco_time_for_log[y] = internal_deco_time [x]; | |
738 | |
739 // Stop only once the last transfer is done. | |
740 if( x == 0 ) break; | |
741 } | |
742 | |
743 //---- Third: fill table end with null | |
744 for(y++; y<NUM_STOPS; y++) | |
745 { | |
746 char_O_deco_time_for_log [y] = 0; | |
747 } | |
748 } | |
749 } | |
750 | |
751 ////////////////////////////////////////////////////////////////////////////// | |
752 // temp_tissue_safety // | |
753 // | |
754 // outsourced in v.102 | |
755 // | |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
756 // Apply safety factors for both ZH-L16 models. |
0 | 757 // |
758 static void temp_tissue_safety(void) | |
759 { | |
760 assert( 0.0 < float_desaturation_multiplier && float_desaturation_multiplier <= 1.0 ); | |
761 assert( 1.0 <= float_saturation_multiplier && float_saturation_multiplier <= 2.0 ); | |
762 | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
763 if( temp_tissue < 0.0 ) |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
764 temp_tissue *= float_desaturation_multiplier; |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
765 else |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
766 temp_tissue *= float_saturation_multiplier; |
0 | 767 } |
768 | |
769 ////////////////////////////////////////////////////////////////////////////// | |
770 ////////////////////////////////////////////////////////////////////////////// | |
771 // ** THE JUMP-IN CODE ** | |
772 // ** for the asm code ** | |
773 ////////////////////////////////////////////////////////////////////////////// | |
774 ////////////////////////////////////////////////////////////////////////////// | |
775 | |
776 ////////////////////////////////////////////////////////////////////////////// | |
777 // Called every 2 seconds during diving. | |
778 // update tissues every time. | |
779 // | |
780 // Every 6 seconds (or slower when TTS > 16): | |
781 // - update deco table (char_O_deco_time/depth) with new values. | |
782 // - update ascent time, | |
783 // - set status to zero (so we can check there is new results). | |
784 // | |
785 void deco_calc_hauptroutine(void) | |
786 { | |
787 RESET_C_STACK | |
788 calc_hauptroutine(); | |
789 int_O_desaturation_time = 65535; | |
790 } | |
791 | |
792 ////////////////////////////////////////////////////////////////////////////// | |
793 // Reset decompression model: | |
794 // + Set all tissues to equilibrium with Air at ambient pressure. | |
795 // + Reset last stop to 0m | |
796 // + Reset all model output. | |
797 void deco_clear_tissue(void) | |
798 { | |
799 RESET_C_STACK | |
800 clear_tissue(); | |
801 } | |
802 | |
803 ////////////////////////////////////////////////////////////////////////////// | |
804 // Called every 1 min during decoplanning. | |
805 // Update tissues for 1 min. | |
806 // | |
807 void deco_calc_tissue(void) | |
808 { | |
809 RESET_C_STACK | |
810 calc_hauptroutine_update_tissues(); | |
811 } | |
812 | |
813 ////////////////////////////////////////////////////////////////////////////// | |
814 | |
815 void deco_calc_wo_deco_step_1_min(void) | |
816 { | |
817 RESET_C_STACK | |
818 calc_wo_deco_step_1_min(); | |
819 deco_calc_desaturation_time(); | |
820 } | |
821 | |
822 ////////////////////////////////////////////////////////////////////////////// | |
823 | |
824 void deco_calc_dive_interval(void) | |
825 { | |
826 RESET_C_STACK | |
827 calc_dive_interval(); | |
828 } | |
829 | |
830 ////////////////////////////////////////////////////////////////////////////// | |
831 // Find current gas in the list (if any). | |
832 // | |
833 // Input: char_I_current_gas = 1..6 | |
834 // | |
835 // Output: sim_gas_last_depth = 0..5, temp_depth_limit. | |
836 // | |
837 static void gas_switch_find_current(void) | |
838 { | |
323
19f7dc2b12d6
FIX minor assert: separate diluant list means current gas can go up to 10.
jDG
parents:
322
diff
changeset
|
839 assert( 0 < char_I_current_gas && char_I_current_gas <= (2*NUM_GAS) ); |
0 | 840 |
841 if( char_I_current_gas <= NUM_GAS ) // Gas1..Gas5 | |
842 { | |
843 sim_gas_last_used = char_I_current_gas; | |
844 | |
845 // Note: if current is first gas, we must find it, but not set | |
846 // last depth change to surface. | |
847 if( char_I_deco_gas_change[sim_gas_last_used-1] ) | |
848 sim_gas_last_depth = char_I_deco_gas_change[sim_gas_last_used-1]; | |
849 } | |
850 else | |
851 sim_gas_last_used = 0; // Gas 6 = manual set | |
852 } | |
853 | |
854 ////////////////////////////////////////////////////////////////////////////// | |
855 // Find deepest available gas. | |
856 // | |
857 // Input: temp_depth_limit, | |
858 // deco_gas_change[] | |
859 // sim_gas_depth_used, sim_dive_mins. | |
860 // | |
861 // RETURNS TRUE if a stop is needed for gas switch. | |
862 // | |
863 // Output: temp_depth_limit, sim_gas_depth_used IFF the is a switch. | |
864 // | |
865 // NOTE: might be called from bottom (when sim_gas_delay and sim_gas_depth_used | |
866 // are null), or during the ascent to make sure we are not passing a | |
867 // stop (in which case both can be already set). | |
868 // | |
869 static unsigned char gas_switch_deepest(void) | |
870 { | |
871 overlay unsigned char switch_deco = 0, switch_last = 0; | |
872 | |
873 if (char_I_const_ppO2 == 0) | |
874 { | |
875 overlay unsigned char j; | |
876 | |
877 // Loop over all enabled gas, to find the deepest one, | |
878 // above last used gas, but below temp_depth_limit. | |
879 for(j=0; j<NUM_GAS; ++j) | |
880 { | |
881 // Gas not (yet) allowed ? Skip ! | |
882 if( temp_depth_limit > deco_gas_change[j] ) | |
883 continue; | |
884 | |
885 // Gas deeper (or equal) than the current one ? Skip ! | |
886 if( sim_gas_last_depth && deco_gas_change[j] >= sim_gas_last_depth ) | |
887 continue; | |
888 | |
889 // First, or deeper ? | |
890 if( switch_deco < deco_gas_change[j] ) | |
891 { | |
892 switch_deco = deco_gas_change[j]; | |
893 switch_last = j+1; // 1..5 | |
894 } | |
895 } | |
896 } | |
897 | |
898 // If there is a better gas available | |
899 if( switch_deco ) | |
900 { | |
901 assert( !sim_gas_last_depth || sim_gas_last_depth > switch_deco ); | |
902 | |
903 sim_gas_last_depth = switch_deco; | |
904 sim_gas_last_used = switch_last; | |
905 } | |
906 return 0; | |
907 } | |
908 | |
909 ////////////////////////////////////////////////////////////////////////////// | |
910 // Calculate gas switches | |
911 // | |
912 // | |
913 // Input: N2_ratio, He_ratio. | |
914 // sim_gas_last_used | |
915 // | |
916 // Output: calc_N2_ratio, calc_He_ratio | |
917 // | |
918 static void gas_switch_set(void) | |
919 { | |
920 assert( sim_gas_last_used <= NUM_GAS ); | |
921 | |
922 if( sim_gas_last_used == 0 ) // Gas6 = manualy set gas. | |
923 { | |
924 calc_N2_ratio = N2_ratio; | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
925 calc_He_ratio = He_ratio; |
0 | 926 } |
927 else | |
928 { | |
929 calc_N2_ratio = char_I_deco_N2_ratio[sim_gas_last_used-1] * 0.01; | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
930 calc_He_ratio = char_I_deco_He_ratio[sim_gas_last_used-1] * 0.01; |
0 | 931 } |
932 | |
933 assert( 0.0 <= calc_N2_ratio && calc_N2_ratio <= 0.95 ); | |
114
109df032cc54
minor fix: assert should pass for pure helium diluant
heinrichsweikamp
parents:
111
diff
changeset
|
934 assert( 0.0 <= calc_He_ratio && calc_He_ratio <= 1.00 ); |
0 | 935 assert( (calc_N2_ratio + calc_He_ratio) <= 1.00 ); |
936 } | |
937 | |
938 ////////////////////////////////////////////////////////////////////////////// | |
939 // | |
940 // Input: calc_N2_ratio, calc_He_ratio : simulated gas mix. | |
941 // temp_deco : simulated respiration pressure | |
942 // float_deco_distance : security factor. | |
943 // Water-vapor pressure inside limbs (ppWater). | |
944 // | |
945 // Output: ppN2, ppHe. | |
946 // | |
947 static void sim_alveolar_presures(void) | |
948 { | |
949 overlay float deco_diluent = temp_deco; // new in v.101 | |
950 | |
951 // Take deco offset into account, but not at surface. | |
952 // Note: this should be done on ambiant pressure, hence before | |
953 // computing the diluant partial pressure... | |
954 if( deco_diluent > pres_surface ) | |
955 deco_diluent += float_deco_distance; | |
956 | |
957 //---- CCR mode : deco gas switch ? -------------------------------------- | |
958 if( char_I_const_ppO2 != 0 ) | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
959 { |
0 | 960 // In CCR mode, use calc_XX_ratio instead of XX_ratio. |
961 // Note: PPO2 and ratios are known outside the lumbs, so there is no | |
962 // ppWater in the equations below: | |
963 deco_diluent -= const_ppO2; | |
964 deco_diluent /= calc_N2_ratio + calc_He_ratio; | |
965 | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
966 } |
0 | 967 |
968 if( deco_diluent > ppWater ) | |
969 { | |
970 ppN2 = calc_N2_ratio * (deco_diluent - ppWater); | |
971 ppHe = calc_He_ratio * (deco_diluent - ppWater); | |
972 } | |
973 else | |
974 { | |
975 ppN2 = 0.0; | |
976 ppHe = 0.0; | |
977 } | |
978 assert( 0.0 <= ppN2 && ppN2 < 14.0 ); | |
979 assert( 0.0 <= ppHe && ppHe < 14.0 ); | |
980 } | |
981 | |
982 ////////////////////////////////////////////////////////////////////////////// | |
983 // clear_tissue | |
984 // | |
985 // optimized in v.101 (var_N2_a) | |
986 // | |
987 // preload tissues with standard pressure for the given ambient pressure. | |
988 // Note: fixed N2_ratio for standard air. | |
989 // | |
990 static void clear_tissue(void) | |
991 { | |
992 overlay float p; | |
993 | |
994 // Kludge: the 0.0002 of 0.7902 are missing with standard air. | |
995 N2_ratio = 0.7902; | |
996 pres_respiration = int_I_pres_respiration * 0.001; | |
997 | |
998 p = N2_ratio * (pres_respiration - ppWater); | |
999 for(ci=0; ci<NUM_COMP; ci++) | |
1000 { | |
313 | 1001 // cycle through the 16 Buhlmann N2 tissues |
0 | 1002 pres_tissue_N2[ci] = p; |
1003 | |
313 | 1004 // cycle through the 16 Buhlmann tissues for Helium |
0 | 1005 pres_tissue_He[ci] = 0.0; |
1006 } | |
1007 | |
1008 clear_deco_table(); | |
1009 char_O_deco_status = 0; | |
1010 char_O_nullzeit = 0; | |
1011 int_O_ascenttime = 0; | |
1012 char_O_gradient_factor = 0; | |
1013 | |
1014 calc_lead_tissue_limit = 0.0; | |
1015 char_O_gtissue_no = 0; | |
1016 } | |
1017 | |
1018 ////////////////////////////////////////////////////////////////////////////// | |
1019 // calc_hauptroutine | |
1020 // | |
1021 // this is the major code in dive mode calculates: | |
1022 // the tissues, | |
1023 // the bottom time, | |
1024 // and simulates the ascend with all deco stops. | |
1025 // | |
1026 // The deco_state sequence is : | |
1027 // 3 (at surface) | |
1028 // +---> 0 : calc nullzeit | |
1029 // | 2 : simulate ascent to first stop (at 10m/min, less that 16x 1min simu) | |
1030 // | +-> 1 : simulate up to 16min of stops. | |
1031 // | +------< not finished | |
1032 // +--------< finish | |
1033 // | |
1034 // Added steps 6,5 for @+5 calculation: | |
1035 // 6 = ascent to first stop (same as 2), except continue to 7 | |
1036 // 7 = same as 1, except loop to 7. | |
1037 // | |
1038 static void calc_hauptroutine(void) | |
1039 { | |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
1040 static unsigned char backup_gas_used = 0; |
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
1041 static unsigned char backup_gas_depth = 0; |
0 | 1042 |
1043 calc_hauptroutine_data_input(); | |
1044 | |
1045 calc_hauptroutine_update_tissues(); | |
1046 calc_gradient_factor(); | |
1047 | |
1048 // toggle between calculation for nullzeit (bottom time), | |
1049 // deco stops | |
1050 // and more deco stops (continue) | |
1051 switch( char_O_deco_status ) | |
1052 { | |
1053 case 3: //---- At surface: start a new dive ------------------------------ | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1054 clear_deco_table(); |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1055 copy_deco_table(); |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1056 int_O_ascenttime = 0; // Reset DTR. |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1057 int_O_extra_ascenttime = 0; |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1058 char_O_nullzeit = 0; // Reset bottom time. |
0 | 1059 char_O_deco_status = 0; // Calc bottom-time/nullzeit next iteration. |
1060 | |
1061 // Values that should be reset just once for the full real dive. | |
1062 // This is used to record the lowest stop for the whole dive, | |
1063 // Including ACCROSS all simulated ascent. | |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
1064 low_depth = 0.0; |
0 | 1065 locked_GF_step = 0.0; |
1066 | |
1067 // Reset gas switch history. | |
1068 backup_gas_used = sim_gas_last_used = 0; | |
1069 backup_gas_depth = sim_gas_last_depth = 0; | |
1070 sim_dive_mins = 0; | |
1071 break; | |
1072 | |
1073 case 0: //---- bottom time ----------------------------------------------- | |
1074 default: | |
1075 gas_switch_find_current(); // Lookup for current gas & time. | |
1076 gas_switch_set(); // setup calc_ratio's | |
1077 | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1078 calc_nullzeit(); |
0 | 1079 if( char_O_nullzeit > 0 ) // Some NDL time left ? |
1080 { | |
1081 char_O_deco_status = 0; // YES: recalc ndl next time. | |
1082 clear_deco_table(); // Also clear stops ! | |
1083 copy_deco_table(); | |
1084 char_O_deco_last_stop = 0; // And last stop (OSTC menu anim) | |
1085 } | |
1086 else | |
1087 char_O_deco_status = 2; // NO: calc ascent next time. | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1088 break; |
0 | 1089 |
1090 case 2: //---- Simulate ascent to first stop ----------------------------- | |
1091 case 6: // @+5min variation | |
1092 // Check proposed gas at begin of ascent simulation | |
1093 sim_dive_mins = int_I_divemins; // Init current time. | |
1094 | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1095 gas_switch_find_current(); // Lookup for current gas & time. |
0 | 1096 gas_switch_set(); // setup calc_ratio's |
1097 | |
1098 backup_gas_used = sim_gas_last_used; // And save for later simu steps. | |
1099 backup_gas_depth = sim_gas_last_depth; // And save for later simu steps. | |
1100 | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1101 sim_ascent_to_first_stop(); |
0 | 1102 |
1103 // Calc stops next time (deco or gas switch). | |
1104 char_O_deco_status = 1 | ( char_O_deco_status & 4 ); | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1105 break; |
0 | 1106 |
1107 case 1: //---- Simulate stops -------------------------------------------- | |
1108 case 5: // @+5 variation. | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1109 calc_hauptroutine_calc_deco(); |
0 | 1110 |
1111 // If simulation is finished, restore the GF low reference, so that | |
1112 // next ascent simulation is done from the current depth: | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1113 if( (char_O_deco_status & 3) == 0 ) |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1114 { |
0 | 1115 sim_gas_last_used = backup_gas_used; |
1116 sim_gas_last_depth = backup_gas_depth; | |
1117 } | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1118 break; |
0 | 1119 } |
1120 } | |
1121 | |
1122 ////////////////////////////////////////////////////////////////////////////// | |
1123 // calc_hauptroutine_data_input | |
1124 // | |
1125 // Reset all C-code dive parameters from their ASM-code values. | |
1126 // Detect gas change condition. | |
1127 // | |
1128 void calc_hauptroutine_data_input(void) | |
1129 { | |
1130 overlay short int_temp; | |
1131 overlay unsigned char g; | |
1132 | |
1133 pres_respiration = int_I_pres_respiration * 0.001; | |
1134 pres_surface = int_I_pres_surface * 0.001; | |
1135 N2_ratio = char_I_N2_ratio * 0.01; | |
1136 He_ratio = char_I_He_ratio * 0.01; | |
379 | 1137 float_deco_distance = char_I_deco_distance * 0.01; // Get offset in mbar |
0 | 1138 |
1139 // ____________________________________________________ | |
1140 // | |
1141 // _____________ G A S _ C H A N G E S ________________ | |
1142 // ____________________________________________________ | |
1143 | |
1144 // Keep a margin of 150mbar = 1.50m | |
1145 int_temp = (int_I_pres_respiration - int_I_pres_surface) | |
1146 + MBAR_REACH_GASCHANGE_AUTO_CHANGE_OFF; | |
1147 | |
1148 // Gas are selectable if we did not pass the change depth by more than 1.50m: | |
1149 for(g=0; g < NUM_GAS; ++g) | |
1150 { | |
1151 deco_gas_change[g] = 0; | |
1152 if(char_I_deco_gas_change[g]) | |
1153 if( int_temp > 100 *(short)char_I_deco_gas_change[g] ) | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1154 deco_gas_change[g] = char_I_deco_gas_change[g]; |
0 | 1155 } |
1156 | |
1157 const_ppO2 = char_I_const_ppO2 * 0.01; | |
1158 float_desaturation_multiplier = char_I_desaturation_multiplier * 0.01; | |
1159 float_saturation_multiplier = char_I_saturation_multiplier * 0.01; | |
1160 GF_low = char_I_GF_Low_percentage * 0.01; | |
1161 GF_high = char_I_GF_High_percentage * 0.01; | |
1162 GF_delta = GF_high - GF_low; | |
1163 } | |
1164 | |
1165 ////////////////////////////////////////////////////////////////////////////// | |
1166 // | |
1167 // | |
1168 void calc_hauptroutine_update_tissues(void) | |
1169 { | |
1170 assert( 0.00 <= N2_ratio && N2_ratio <= 1.00 ); | |
1171 assert( 0.00 <= He_ratio && He_ratio <= 1.00 ); | |
114
109df032cc54
minor fix: assert should pass for pure helium diluant
heinrichsweikamp
parents:
111
diff
changeset
|
1172 assert( (N2_ratio + He_ratio) <= 1.00 ); |
0 | 1173 assert( 0.800 < pres_respiration && pres_respiration < 14.0 ); |
1174 | |
1175 pres_diluent = pres_respiration; | |
1176 if( char_I_const_ppO2 != 0 ) | |
1177 { | |
1178 overlay float flush_ppO2 = pres_respiration * (1.0 - N2_ratio - He_ratio); | |
1179 | |
1180 pres_diluent -= const_ppO2; | |
1181 pres_diluent /= N2_ratio + He_ratio; | |
1182 if( pres_diluent < 0.0 ) | |
1183 pres_diluent = 0.0; | |
1184 | |
1185 char_O_diluent = (unsigned char)(pres_diluent/pres_respiration*100.0 + 0.5); | |
1186 | |
1187 if( flush_ppO2 > 2.545) flush_ppO2 = 2.55; | |
1188 if( flush_ppO2 < 0.0 ) flush_ppO2 = 0.0; | |
1189 char_O_flush_ppO2 = (unsigned char)(flush_ppO2*100.0 + 0.5); | |
1190 } | |
1191 | |
1192 if( pres_diluent > ppWater ) | |
1193 { | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1194 overlay float EAD, END; |
0 | 1195 |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1196 ppN2 = N2_ratio * (pres_diluent - ppWater); |
0 | 1197 ppHe = He_ratio * (pres_diluent - ppWater); |
1198 | |
1199 // EAD : Equivalent Air Dive. Equivalent depth for the same N2 level | |
1200 // with plain air. | |
1201 // ppN2 = 79% * (P_EAD - ppWater) | |
1202 // EAD = (P_EAD - Psurface) * 10 | |
1203 // ie: EAD = (ppN2 / 0.7902 + ppWater -Psurface) * 10 | |
1204 EAD = (ppN2 / 0.7902 + ppWater - pres_surface) * BAR_TO_METER; | |
1205 if( EAD < 0.0 || EAD > 245.5 ) EAD = 0.0; | |
1206 char_O_EAD = (unsigned char)(EAD + 0.5); | |
1207 | |
1208 // END : Equivalent Narcotic Dive. | |
1209 // Here we count O2 as narcotic too. Hence everything but helium (has a narcosis factor of | |
1210 // 0.23 btw). Hence the formula becomes: | |
1211 // END * BarPerMeter * (1.0 - 0.0) - ppWater + Psurface == Pambient - ppHe - ppWater | |
1212 // ie: END = (Pambient - ppHe - Psurface) * BAR_TO_METER | |
1213 // | |
1214 // Source cited: | |
1215 // The Physiology and Medicine of Diving by Peter Bennett and David Elliott, | |
1216 // 4th edition, 1993, W.B.Saunders Company Ltd, London. | |
1217 END = (pres_respiration - ppHe - pres_surface) * BAR_TO_METER; | |
1218 if( END < 0.0 || END > 245.5 ) END = 0.0; | |
1219 char_O_END = (unsigned char)(END + 0.5); | |
1220 } | |
1221 else // new in v.101 | |
1222 { | |
1223 ppN2 = 0.0; | |
1224 ppHe = 0.0; | |
1225 char_O_EAD = char_O_END = 0; | |
1226 } | |
1227 | |
1228 if(!char_I_step_is_1min) | |
1229 calc_tissue(0); | |
1230 else | |
1231 calc_tissue(1); | |
1232 | |
1233 // Calc limit for surface, ie. GF_high. | |
1234 calc_limit(); | |
1235 | |
126 | 1236 // Fill int_O_ceiling if ceiling is below the surface |
1237 if ((calc_lead_tissue_limit-pres_surface)>0) | |
1238 int_O_ceiling = (short)((calc_lead_tissue_limit-pres_surface)*1000); | |
1239 else | |
1240 int_O_ceiling = 0; | |
1241 | |
0 | 1242 int_O_gtissue_press = (short)((pres_tissue_N2[char_O_gtissue_no] + pres_tissue_He[char_O_gtissue_no]) * 1000); |
1243 } | |
1244 | |
1245 | |
1246 ////////////////////////////////////////////////////////////////////////////// | |
1247 // Compute stops. | |
1248 // | |
1249 // Note: because this can be very long, break on 16 iterations, and set state | |
1250 // to 0 when finished, or to 1 when needing to continue. | |
1251 // Note: because each iteration might be very long too (~ 66 ms in 1.84beta), | |
1252 // break the loop when total time > 512msec. | |
1253 // | |
1254 void calc_hauptroutine_calc_deco(void) | |
1255 { | |
1256 overlay unsigned char loop; | |
1257 | |
34 | 1258 for(loop = 0; loop < 16; ++loop) |
0 | 1259 { |
1260 // Limit loops to 512ms, using timer 5: | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1261 if( tmr5() & (512*32) ) |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1262 break; |
34 | 1263 |
0 | 1264 if( calc_nextdecodepth() ) |
1265 { | |
1266 if( temp_depth_limit == 0 ) | |
1267 goto Surface; | |
1268 | |
1269 //---- We hit a stop at temp_depth_limit --------------------- | |
1270 temp_deco = temp_depth_limit * METER_TO_BAR // Convert to relative bar, | |
342
7812ec7ef694
Fix low_depth shall be stored as a float (no rounding).
jDG
parents:
324
diff
changeset
|
1271 + pres_surface; // To absolute. |
0 | 1272 if( !update_deco_table() ) // Adds a one minute stops. |
1273 goto Surface; // Deco table full: abort... | |
1274 } | |
1275 else | |
1276 { | |
1277 //---- No stop ----------------------------------------------- | |
1278 temp_deco -= (10*METER_TO_BAR); // Ascend 10m, no wait. | |
1279 | |
1280 //---- Finish computations once surface is reached ----------- | |
1281 if( temp_deco <= pres_surface ) | |
1282 { | |
1283 Surface: | |
1284 if( char_O_deco_status == 1 ) // Don't in @+5min variant. | |
1285 copy_deco_table(); | |
1286 | |
1287 calc_ascenttime(); | |
1288 char_O_deco_status = 0; // calc nullzeit next time. | |
1289 char_O_deco_last_stop = 0; // Surface reached (to animate menu) | |
351
de8c45fb2ab9
BUGFIX gas volume: do not forget deco gas with switch >= dive's bottom depth.
jdg@air
parents:
348
diff
changeset
|
1290 return; |
0 | 1291 } |
1292 } | |
1293 //---- Then update tissue -------------------------------------------- | |
1294 sim_dive_mins++; // Advance simulated time by 1 minute. | |
1295 gas_switch_set(); // Apply any simulated gas change, once validated. | |
1296 sim_alveolar_presures(); // Updates ppN2 and ppHe. | |
1297 sim_tissue(1); // Simulate compartiments for 1 minute. | |
1298 } | |
1299 | |
1300 // Surface not reached, need more stops... for menu animation. | |
1301 char_O_deco_last_stop = temp_depth_limit; // Reached depth. | |
1302 } | |
1303 | |
1304 | |
1305 ////////////////////////////////////////////////////////////////////////////// | |
1306 // Simulation ascention to first deco stop. | |
1307 // | |
1308 // Note: because we ascent with a constant speed (10m/mn, ie. 1bar/mn), | |
1309 // there is no need to break on more that 16 iterations | |
1310 // (or we are already in deep shit). | |
1311 // | |
1312 // Input: pres_respiration | |
1313 // Output: temp_deco | |
1314 // | |
1315 // if char_O_deco_status indicate @+5 variant, add extra time at current depth, | |
1316 // before ascent. | |
1317 void sim_ascent_to_first_stop(void) | |
1318 { | |
1319 overlay unsigned char fast = 1; // 1min or 2sec steps. | |
1320 | |
1321 update_startvalues(); | |
1322 clear_deco_table(); | |
1323 | |
1324 temp_deco = pres_respiration; // Starts from current real depth. | |
1325 | |
1326 // Are we doing the special @+5min variation ? | |
1327 if(char_O_deco_status & 4) | |
1328 sim_extra_time(); | |
1329 | |
1330 //---- Loop until first stop, gas switch, or surface is reached ---------- | |
1331 for(;;) | |
1332 { | |
1333 overlay float old_deco = temp_deco; // Pamb backup (bars) | |
1334 | |
1335 // Try ascending 1 full minute (fast) or 2sec (!fast): | |
1336 if( fast ) | |
1337 temp_deco -= 10*METER_TO_BAR; // 1 min, at 10m/min. ~ 1bar. | |
1338 else | |
1339 temp_deco -= (10.0/30.0)*METER_TO_BAR; // 2sec at 10m/min. | |
1340 | |
1341 if( temp_deco < pres_surface ) // But don't go over surface. | |
1342 temp_deco = pres_surface; | |
1343 | |
1344 // Recompute sim_lead_tissue_limit at GF_low (deepest stop), because | |
1345 // one minute passed. | |
1346 sim_limit(GF_low); | |
1347 | |
1348 // Did we reach deepest remaining stop ? | |
1349 if( temp_deco < sim_lead_tissue_limit ) | |
1350 { | |
1351 temp_deco = old_deco; // Restore last correct depth, | |
1352 | |
1353 if( fast ) | |
1354 { | |
1355 fast = 0; // Retry with 2sec steps. | |
1356 continue; | |
1357 } | |
1358 else | |
1359 break; // Done... | |
1360 } | |
1361 | |
1362 // Did we reach surface ? | |
1363 // NOTE: we should round BEFORE checking surface is reached. | |
1364 temp_depth_limit = (unsigned char)(0.5 + (temp_deco - pres_surface) * BAR_TO_METER); | |
1365 if( temp_depth_limit == 0 ) | |
1366 { | |
1367 temp_deco = pres_surface; // Yes: finished ! | |
1368 break; | |
1369 } | |
1370 | |
1371 // Check for gas change below new depth ? | |
1372 if( gas_switch_deepest() ) | |
1373 { | |
1374 assert( temp_depth_limit > 0); | |
1375 | |
1376 temp_deco = temp_depth_limit * METER_TO_BAR + pres_surface; | |
1377 break; | |
1378 } | |
1379 | |
1380 if( fast ) | |
1381 sim_dive_mins++; // Advance simulated time by 1 minute. | |
1382 sim_alveolar_presures(); // temp_deco --> ppN2/ppHe | |
1383 sim_tissue(fast); // and update tissues for 1 min. | |
1384 } | |
1385 } | |
1386 | |
1387 ////////////////////////////////////////////////////////////////////////////// | |
1388 // Simulation extra time at the current depth. | |
1389 // | |
1390 // This routine is used for @+5min feature. | |
1391 void sim_extra_time(void) | |
1392 { | |
1393 overlay unsigned char extra = char_I_extra_time; | |
1394 do { | |
1395 sim_dive_mins++; // Advance simulated time by 1 minute. | |
1396 sim_tissue(1); // and update tissues for 1 min. | |
1397 } while( --extra != 0 ); | |
1398 } | |
1399 | |
1400 ////////////////////////////////////////////////////////////////////////////// | |
1401 // calc_tissue | |
1402 // | |
1403 // optimized in v.101 | |
1404 // | |
1405 static void calc_tissue(PARAMETER unsigned char period) | |
1406 { | |
1407 assert( 0.00 <= ppN2 && ppN2 < 11.2 ); // 80% N2 at 130m | |
1408 assert( 0.00 <= ppHe && ppHe < 12.6 ); // 90% He at 130m | |
1409 | |
1410 for (ci=0;ci<NUM_COMP;ci++) | |
1411 { | |
1412 read_buhlmann_times(period); // 2 sec or 1 min period. | |
1413 | |
1414 // N2 | |
1415 temp_tissue = (ppN2 - pres_tissue_N2[ci]) * var_N2_e; | |
1416 temp_tissue_safety(); | |
1417 pres_tissue_N2[ci] += temp_tissue; | |
1418 | |
1419 // He | |
1420 temp_tissue = (ppHe - pres_tissue_He[ci]) * var_He_e; | |
1421 temp_tissue_safety(); | |
1422 pres_tissue_He[ci] += temp_tissue; | |
1423 } | |
1424 } | |
1425 | |
1426 ////////////////////////////////////////////////////////////////////////////// | |
1427 // calc_limit | |
1428 // | |
1429 // New in v.111 : separated from calc_tissue(), and depends on GF value. | |
1430 // | |
1431 static void calc_limit(void) | |
1432 { | |
1433 char_O_gtissue_no = 255; | |
1434 calc_lead_tissue_limit = 0.0; | |
1435 | |
1436 for(ci=0; ci<NUM_COMP;ci++) | |
1437 { | |
1438 overlay float N2 = pres_tissue_N2[ci]; | |
1439 overlay float He = pres_tissue_He[ci]; | |
1440 overlay float p = N2 + He; | |
1441 | |
1442 read_buhlmann_coefficients(); | |
1443 var_N2_a = (var_N2_a * N2 + var_He_a * He) / p; | |
1444 var_N2_b = (var_N2_b * N2 + var_He_b * He) / p; | |
1445 | |
1446 // Apply the Eric Baker's varying gradient factor correction. | |
1447 // Note: the correction factor depends both on GF and b, | |
1448 // Actual values are in the 1.5 .. 1.0 range (for a GF=30%), | |
1449 // so that can change who is the leading gas... | |
1450 // Note: Also depends of the GF. So the calcul is different for | |
1451 // GF_low, current GF, or GF_high... | |
1452 // *BUT* calc_tissue() is used to compute bottom time, | |
1453 // hence what would happend at surface, | |
1454 // hence at GF_high. | |
1455 if( char_I_deco_model != 0 ) | |
1456 p = ( p - var_N2_a * GF_high) * var_N2_b | |
1457 / (GF_high + var_N2_b * (1.0 - GF_high)); | |
1458 else | |
1459 p = (p - var_N2_a) * var_N2_b; | |
1460 if( p < 0.0 ) p = 0.0; | |
1461 | |
1462 if( p > calc_lead_tissue_limit ) | |
1463 { | |
1464 char_O_gtissue_no = ci; | |
1465 calc_lead_tissue_limit = p; | |
1466 } | |
1467 } | |
1468 | |
1469 assert( char_O_gtissue_no < NUM_COMP ); | |
1470 assert( 0.0 <= calc_lead_tissue_limit && calc_lead_tissue_limit <= 14.0); | |
1471 } | |
1472 | |
1473 ////////////////////////////////////////////////////////////////////////////// | |
1474 // calc_nullzeit | |
1475 // | |
1476 // calculates the remaining bottom time | |
1477 // | |
1478 // NOTE: Erik Baker's closed formula works for Nitroxes. Trimix adds a second | |
1479 // exponential term to the M-value equation, making it impossible to | |
1480 // invert... So we have to make a fast-simu until we find a better way. | |
1481 // | |
1482 // Input: pres_respiration | |
1483 // Output: char_O_nullzeit | |
1484 // | |
1485 static void calc_nullzeit(void) | |
1486 { | |
1487 //---- Compute ppN2 and ppHe --------------------------------------------- | |
1488 temp_deco = pres_respiration; | |
1489 sim_alveolar_presures(); | |
1490 | |
1491 char_O_nullzeit = 240; | |
1492 for(ci=0; ci<NUM_COMP; ci++) | |
1493 { | |
1494 //---- Read A/B values and loading factor for N2 and He -------------- | |
1495 overlay float tN2 = pres_tissue_N2[ci]; | |
1496 overlay float tHe = pres_tissue_He[ci]; | |
1497 overlay float t = tN2 + tHe; | |
1498 overlay unsigned char ndl; | |
1499 overlay unsigned char period = 10; | |
1500 | |
1501 read_buhlmann_coefficients(); | |
1502 read_buhlmann_times(2); // Starts with a 10min period. | |
1503 | |
1504 //---- Simulate for that tissue -------------------------------------- | |
1505 // NOTE: No need to simulate for longuer than the already found NDL. | |
1506 for(ndl=0; ndl<char_O_nullzeit;) | |
1507 { | |
1508 //---- Compute updated mix M-value at surface | |
1509 overlay float a = (var_N2_a * tN2 + var_He_a * tHe) / t; | |
1510 overlay float b = (var_N2_b * tN2 + var_He_b * tHe) / t; | |
1511 overlay float M0 = (a + pres_surface/b); | |
1512 | |
1513 //---- Add 10min/1min to N2/He tissues | |
1514 overlay float dTN2 = (ppN2 - tN2) * var_N2_e; | |
1515 overlay float dTHe = (ppHe - tHe) * var_He_e; | |
1516 | |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
1517 //---- Apply security margin for both models |
324
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
1518 // NDL can be computed while ascending... SO we have |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
1519 // to check wether we are saturating or desaturating. |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
1520 if( dTN2 > 0.0 ) dTN2 *= float_saturation_multiplier; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
1521 else dTN2 *= float_desaturation_multiplier; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
1522 |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
1523 if( dTHe > 0.0 ) dTHe *= float_saturation_multiplier; |
0e9dcdcf03c1
FIX NDL prediction error with Buhlmann model: might be desaturating too fast.
jdg@air
parents:
323
diff
changeset
|
1524 else dTHe *= float_saturation_multiplier; |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
1525 |
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
1526 if (char_I_deco_model != 0 ) |
0 | 1527 M0 = GF_high * (M0 - pres_surface) + pres_surface; |
1528 | |
1529 //---- Simulate off-gasing while going to surface | |
1530 // TODO ! | |
1531 // dTN2 -= exp( ... ascent time ... ppN2...) | |
1532 // dTHe -= exp( ... ascent time ... ppHe...) | |
1533 | |
1534 //---- Ok now, and still ok to surface after 1 or 10 minutes ? | |
1535 if( (t <= M0) && (t + dTN2 + dTHe <= M0) ) | |
1536 { | |
1537 tN2 += dTN2; // YES: apply gas loadings, | |
1538 tHe += dTHe; | |
1539 t = tN2 + tHe; | |
1540 ndl += period; // increment NDL, | |
1541 continue; // and loop. | |
1542 } | |
1543 | |
1544 //---- Should we retry with smaller steps ? | |
1545 if( period == 10 ) | |
1546 { | |
1547 read_buhlmann_times(1); // 1min coefs. | |
1548 period = 1; | |
1549 continue; | |
1550 } | |
1551 | |
1552 //---- ELSE make a linear approx for the last minute | |
1553 // Usefull to have a meaningfull rounding of NDL. | |
1554 // But ONLY it positive (negativ casted to unsigned is bad). | |
1555 if( M0 > t ) | |
1556 ndl += (unsigned char)(0.5f + (M0-t)/(dTN2+dTHe)); | |
1557 break; | |
1558 } | |
1559 | |
1560 // Keep the shortest NDL found | |
1561 if( ndl < char_O_nullzeit ) | |
1562 char_O_nullzeit = ndl; | |
1563 } | |
1564 } | |
1565 | |
1566 ////////////////////////////////////////////////////////////////////////////// | |
1567 // calc_ascenttime | |
1568 // | |
1569 // Summup ascent from bottom to surface, at 1 bar/min, 1min for last 3 meters, | |
1570 // and all stops. | |
1571 // | |
1572 // Result in int_O_ascenttime, or int_O_extra_ascenttime if in @+5min variant. | |
1573 static void calc_ascenttime(void) | |
1574 { | |
1575 overlay unsigned char x; | |
1576 overlay unsigned short sum; | |
1577 | |
1578 // + 0.7 to count 1 minute ascent time from 3 metre to surface | |
1579 overlay float ascent = pres_respiration - pres_surface + 0.7; | |
1580 if (ascent < 0.0) | |
1581 ascent = 0.0; | |
1582 sum = (unsigned short)(ascent + 0.99); | |
1583 | |
1584 for(x=0; x<NUM_STOPS && internal_deco_depth[x]; x++) | |
1585 sum += (unsigned short)internal_deco_time[x]; | |
1586 | |
1587 if( char_O_deco_status == 1 ) | |
1588 int_O_ascenttime = sum; | |
1589 else | |
1590 int_O_extra_ascenttime = sum; | |
1591 | |
1592 } | |
1593 | |
1594 ////////////////////////////////////////////////////////////////////////////// | |
1595 // update_startvalues | |
1596 // | |
1597 // updated in v.102 | |
1598 // | |
1599 void update_startvalues(void) | |
1600 { | |
1601 overlay unsigned char x; | |
1602 | |
1603 // Start ascent simulation with current tissue partial pressures. | |
1604 for(x=0; x<NUM_COMP; x++) | |
1605 { | |
1606 sim_pres_tissue_N2[x] = pres_tissue_N2[x]; | |
1607 sim_pres_tissue_He[x] = pres_tissue_He[x]; | |
1608 } | |
1609 | |
1610 // No leading tissue (yet) for this ascent simulation. | |
1611 sim_lead_tissue_limit = 0.0; | |
1612 sim_lead_tissue_no = 255; | |
1613 } | |
1614 | |
1615 ////////////////////////////////////////////////////////////////////////////// | |
1616 // sim_tissue | |
1617 // | |
1618 // optimized in v.101 | |
1619 // | |
1620 // Function very simular to calc_tissue, but: | |
1621 // + Use a 1min or 10min period. | |
1622 // + Do it on sim_pres_tissue, instead of pres_tissue. | |
1623 static void sim_tissue(PARAMETER unsigned char period) | |
1624 { | |
1625 assert( 0.00 <= ppN2 && ppN2 < 11.2 ); // 80% N2 at 130m | |
1626 assert( 0.00 <= ppHe && ppHe < 12.6 ); // 90% He at 130m | |
1627 | |
1628 for(ci=0; ci<NUM_COMP; ci++) | |
1629 { | |
1630 read_buhlmann_times(period); // 1 or 10 minute(s) interval | |
1631 | |
1632 // N2 | |
1633 temp_tissue = (ppN2 - sim_pres_tissue_N2[ci]) * var_N2_e; | |
1634 temp_tissue_safety(); | |
1635 sim_pres_tissue_N2[ci] += temp_tissue; | |
1636 | |
1637 // He | |
1638 temp_tissue = (ppHe - sim_pres_tissue_He[ci]) * var_He_e; | |
1639 temp_tissue_safety(); | |
1640 sim_pres_tissue_He[ci] += temp_tissue; | |
1641 } | |
1642 } | |
1643 | |
1644 ////////////////////////////////////////////////////////////////////////////// | |
1645 // sim_limit() | |
1646 // | |
1647 // New in v.111 | |
1648 // | |
1649 // Function separated from sim_tissue() to allow recomputing limit on | |
1650 // different depth, because it depends on current gradient factor. | |
1651 // | |
1652 static void sim_limit(PARAMETER float GF_current) | |
1653 { | |
1654 assert( 0.0 < GF_current && GF_current <= 1.0f); | |
1655 | |
1656 sim_lead_tissue_limit = 0.0; | |
1657 sim_lead_tissue_no = 0; // If no one is critic, keep first tissue. | |
1658 | |
1659 for(ci=0; ci<NUM_COMP; ci++) | |
1660 { | |
1661 overlay float N2 = sim_pres_tissue_N2[ci]; | |
1662 overlay float He = sim_pres_tissue_He[ci]; | |
1663 overlay float p = N2 + He; | |
1664 | |
1665 read_buhlmann_coefficients(); | |
1666 var_N2_a = (var_N2_a * N2 + var_He_a * He) / p; | |
1667 var_N2_b = (var_N2_b * N2 + var_He_b * He) / p; | |
1668 | |
1669 // Apply the Eric Baker's varying gradient factor correction. | |
1670 // Note: the correction factor depends both on GF and b, | |
1671 // Actual values are in the 1.5 .. 1.0 range (for a GF=30%), | |
1672 // so that can change who is the leading gas... | |
1673 // Note: Also depends of the GF_current... | |
1674 if( char_I_deco_model != 0 ) | |
1675 p = ( p - var_N2_a * GF_current) | |
1676 / (GF_current / var_N2_b + 1.0 - GF_current); | |
1677 else | |
1678 p = (p - var_N2_a) * var_N2_b; | |
1679 | |
1680 if( p > sim_lead_tissue_limit ) | |
1681 { | |
1682 sim_lead_tissue_no = ci; | |
1683 sim_lead_tissue_limit = p; | |
1684 } | |
1685 } // for ci | |
1686 | |
1687 assert( sim_lead_tissue_no < NUM_COMP ); | |
1688 assert( 0.0 <= sim_lead_tissue_limit && sim_lead_tissue_limit <= 14.0 ); | |
1689 } | |
1690 | |
1691 ////////////////////////////////////////////////////////////////////////////// | |
1692 // clear_deco_table | |
1693 // | |
1694 // unchanged in v.101 | |
1695 // | |
1696 static void clear_deco_table(void) | |
1697 { | |
1698 overlay unsigned char x; | |
1699 | |
1700 for(x=0; x<NUM_STOPS; ++x) | |
1701 { | |
1702 internal_deco_time [x] = 0; | |
1703 internal_deco_depth[x] = 0; | |
1704 } | |
1705 } | |
1706 | |
1707 ////////////////////////////////////////////////////////////////////////////// | |
1708 // update_deco_table | |
1709 // | |
1710 // Add 1 min to current stop. | |
1711 // | |
1712 // Inputs: | |
1713 // temp_depth_limit = stop's depth, in meters. | |
1714 // In/Out: | |
1715 // internal_deco_depth[] : depth (in metres) of each stops. | |
1716 // internal_deco_time [] : time (in minutes) of each stops. | |
1717 // | |
1718 static unsigned char update_deco_table() | |
1719 { | |
1720 overlay unsigned char x; | |
1721 assert( temp_depth_limit < 128 ); // Can't be negativ (overflown). | |
1722 assert( temp_depth_limit > 0 ); // No stop at surface... | |
1723 | |
1724 for(x=0; x<NUM_STOPS; ++x) | |
1725 { | |
1726 // Make sure deco-stops are recorded in order: | |
111 | 1727 assert( !internal_deco_depth[x] || temp_depth_limit <= internal_deco_depth[x] ); |
0 | 1728 |
111 | 1729 if( internal_deco_depth[x]== temp_depth_limit ) |
0 | 1730 { |
1731 // Do not overflow (max 255') | |
1732 if( internal_deco_time[x] < 255 ) | |
1733 { | |
1734 internal_deco_time[x]++; | |
1735 return 1; | |
1736 } | |
1737 // But store extra in the next stop... | |
1738 } | |
1739 | |
1740 if( internal_deco_depth[x] == 0 ) | |
1741 { | |
1742 internal_deco_depth[x] = temp_depth_limit; | |
1743 | |
1744 internal_deco_time[x] = 1; | |
111 | 1745 internal_deco_gas[x] = sim_gas_last_used; |
0 | 1746 return 1; |
1747 } | |
1748 } | |
1749 | |
1750 // Can't store stops at more than 96m. | |
1751 // Or stops at less that 3m too. | |
1752 // Just do nothing with that... | |
1753 return 0; | |
1754 } | |
1755 | |
1756 ////////////////////////////////////////////////////////////////////////////// | |
1757 // calc_gradient_factor | |
1758 // | |
1759 // optimized in v.101 (var_N2_a) | |
1760 // new code in v.102 | |
1761 // | |
1762 static void calc_gradient_factor(void) | |
1763 { | |
1764 overlay float gf; | |
1765 overlay float N2 = pres_tissue_N2[char_O_gtissue_no]; | |
1766 overlay float He = pres_tissue_He[char_O_gtissue_no]; | |
1767 | |
1768 assert( char_O_gtissue_no < NUM_COMP ); | |
1769 assert( 0.800 <= pres_respiration && pres_respiration < 14.0 ); | |
1770 | |
1771 // tissue > respiration (currently off-gasing) | |
1772 // GF = 0% when respiration == tissue, ie. bubbles are at equilibrium. | |
1773 // GF = 100% when respiration == limit. | |
1774 temp_tissue = N2 + He; | |
1775 if( temp_tissue <= pres_respiration ) | |
1776 gf = 0.0; | |
1777 else | |
1778 { | |
1779 overlay float limit = calc_lead_tissue_limit; | |
1780 // NOTE: in GF model, calc_lead_tissue_limit include already the | |
1781 // correction due to gradient factor. To compute the actual | |
1782 // current GF, we need to (re-)compute the raw ambiant-pressure | |
313 | 1783 // limit from the Buhlmann model. |
0 | 1784 if( char_I_deco_model != 0 ) |
1785 { | |
1786 ci = char_O_gtissue_no; | |
1787 read_buhlmann_coefficients(); | |
1788 var_N2_a = (var_N2_a * N2 + var_He_a * He) / temp_tissue; | |
1789 var_N2_b = (var_N2_b * N2 + var_He_b * He) / temp_tissue; | |
1790 limit = (temp_tissue - var_N2_a) * var_N2_b; | |
1791 } | |
1792 | |
1793 gf = (temp_tissue - pres_respiration) | |
1794 / (temp_tissue - limit) | |
1795 * 100.0; | |
1796 if( gf > 254.5 ) gf = 255.0; | |
1797 if( gf < 0.0 ) gf = 0.0; | |
1798 } | |
1799 char_O_gradient_factor = (unsigned char)(gf+0.5f); | |
1800 | |
1801 } | |
1802 | |
1803 ////////////////////////////////////////////////////////////////////////////// | |
1804 // deco_calc_desaturation_time | |
1805 // | |
1806 // FIXED N2_ratio | |
1807 // unchanged in v.101 | |
1808 // Inputs: int_I_pres_surface, ppWater, char_I_desaturation_multiplier | |
1809 // Outputs: int_O_desaturation_time, char_O_tissue_saturation[0..31] | |
1810 // | |
1811 void deco_calc_desaturation_time(void) | |
1812 { | |
1813 RESET_C_STACK | |
1814 | |
1815 assert( 800 < int_I_pres_surface && int_I_pres_surface < 1100 ); | |
1816 assert( 0 < char_I_desaturation_multiplier && char_I_desaturation_multiplier <= 100 ); | |
1817 | |
313 | 1818 N2_ratio = 0.7902; // FIXED sum as stated in buhlmann |
0 | 1819 pres_surface = int_I_pres_surface * 0.001; |
1820 ppN2 = N2_ratio * (pres_surface - ppWater); | |
1821 int_O_desaturation_time = 0; | |
1822 float_desaturation_multiplier = char_I_desaturation_multiplier * (0.01 * SURFACE_DESAT_FACTOR); | |
1823 | |
1824 for(ci=0; ci<NUM_COMP; ci++) | |
1825 { | |
1826 overlay unsigned short desat_time; // For a particular compartiment, in min. | |
1827 overlay float temp1; | |
1828 overlay float temp2; | |
1829 overlay float temp3; | |
1830 overlay float temp4; | |
1831 | |
1832 read_buhlmann_ht(); | |
1833 | |
1834 // saturation_time (for flight) and N2_saturation in multiples of halftime | |
1835 // version v.100: 1.1 = 10 percent distance to totally clean (totally clean is not possible, would take infinite time ) | |
1836 // new in version v.101: 1.07 = 7 percent distance to totally clean (totally clean is not possible, would take infinite time ) | |
1837 // changes in v.101: 1.05 = 5 percent dist to totally clean is new desaturation point for display and NoFly calculations | |
1838 // N2 | |
1839 temp1 = 1.05 * ppN2 - pres_tissue_N2[ci]; | |
1840 temp2 = ppN2 - pres_tissue_N2[ci]; | |
1841 if (temp2 >= 0.0) | |
1842 temp1 = 0.0; | |
1843 else | |
1844 temp1 = temp1 / temp2; | |
1845 | |
1846 if( 0.0 < temp1 && temp1 < 1.0 ) | |
1847 { | |
1848 // 0.6931 is ln(2), because the math function log() calculates with a base of e not 2 as requested. | |
1849 // minus because log is negative. | |
1850 temp1 = log(1.0 - temp1) / -0.6931; // temp1 is the multiples of half times necessary. | |
1851 temp2 = var_N2_ht * temp1 / float_desaturation_multiplier; // time necessary (in minutes ) for complete desaturation (see comment about 5 percent) | |
1852 } | |
1853 else | |
1854 { | |
1855 temp1 = 0.0; | |
1856 temp2 = 0.0; | |
1857 } | |
1858 | |
1859 // He | |
1860 temp3 = 0.1 - pres_tissue_He[ci]; | |
1861 if (temp3 >= 0.0) | |
1862 temp3 = 0.0; | |
1863 else | |
1864 temp3 = - temp3 / pres_tissue_He[ci]; | |
1865 | |
1866 if( 0.0 < temp3 && temp3 < 1.0 ) | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1867 { |
0 | 1868 temp3 = log(1.0 - temp3) / -0.6931; // temp1 is the multiples of half times necessary. |
1869 // 0.6931 is ln(2), because the math function log() calculates with a base of e not 2 as requested. | |
1870 // minus because log is negative | |
1871 temp4 = var_He_ht * temp3 / float_desaturation_multiplier; // time necessary (in minutes ) for "complete" desaturation, new in v.101 float_desaturation_multiplier | |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1872 } |
0 | 1873 else |
287
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1874 { |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1875 temp3 = 0.0; |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1876 temp4 = 0.0; |
f342853afcd9
FIX gas_volumes: shall take stops in forward order.
jdg@air
parents:
284
diff
changeset
|
1877 } |
0 | 1878 |
1879 // saturation_time (for flight) | |
1880 if (temp4 > temp2) | |
1881 desat_time = (unsigned short)temp4; | |
1882 else | |
1883 desat_time = (unsigned short)temp2; | |
1884 | |
1885 if(desat_time > int_O_desaturation_time) | |
1886 int_O_desaturation_time = desat_time; | |
1887 | |
1888 // N2 saturation in multiples of halftime for display purposes | |
1889 temp2 = temp1 * 20.0; // 0 = 1/8, 120 = 0, 249 = 8 | |
1890 temp2 = temp2 + 80.0; // set center | |
1891 if (temp2 < 0.0) | |
1892 temp2 = 0.0; | |
1893 if (temp2 > 255.0) | |
1894 temp2 = 255.0; | |
1895 char_O_tissue_N2_saturation[ci] = (char)temp2; | |
1896 | |
1897 // He saturation in multiples of halftime for display purposes | |
1898 temp4 = temp3 * 20.0; // 0 = 1/8, 120 = 0, 249 = 8 | |
1899 temp4 = temp4 + 80.0; // set center | |
1900 if (temp4 < 0.0) | |
1901 temp4 = 0.0; | |
1902 if (temp4 > 255.0) | |
1903 temp4 = 255.0; | |
1904 char_O_tissue_He_saturation[ci] = (char)temp4; | |
1905 } // for | |
1906 } | |
1907 | |
1908 ////////////////////////////////////////////////////////////////////////////// | |
1909 // calc_wo_deco_step_1_min | |
1910 // | |
1911 // FIXED N2 Ratio | |
1912 // optimized in v.101 (...saturation_multiplier) | |
1913 // desaturation slowed down to 70,42% | |
1914 // | |
1915 static void calc_wo_deco_step_1_min(void) | |
1916 { | |
1917 assert( 800 < int_I_pres_surface && int_I_pres_surface < 1100 ); | |
1918 assert( 800 < int_I_pres_respiration && int_I_pres_respiration < 1100 ); | |
1919 assert( 100 <= char_I_saturation_multiplier && char_I_saturation_multiplier < 200 ); | |
1920 assert( 0 < char_I_desaturation_multiplier && char_I_desaturation_multiplier <= 100 ); | |
1921 | |
1922 N2_ratio = 0.7902; // FIXED, sum lt. buehlmann | |
1923 pres_respiration = pres_surface = int_I_pres_surface * 0.001; | |
1924 ppN2 = N2_ratio * (pres_respiration - ppWater); | |
1925 ppHe = 0.0; | |
1926 float_desaturation_multiplier = char_I_desaturation_multiplier * (0.01 * SURFACE_DESAT_FACTOR); | |
1927 float_saturation_multiplier = char_I_saturation_multiplier * 0.01; | |
1928 | |
1929 calc_tissue(1); // update the pressure in the tissues N2/He in accordance with the new ambient pressure | |
1930 | |
1931 clear_deco_table(); | |
1932 char_O_deco_status = 3; // surface new in v.102 : stays in surface state. | |
1933 char_O_nullzeit = 0; | |
1934 int_O_ascenttime = 0; | |
1935 int_O_extra_ascenttime = 0; | |
1936 calc_gradient_factor(); | |
1937 } | |
1938 | |
1939 ////////////////////////////////////////////////////////////////////////////// | |
1940 // calc_dive_interval | |
1941 // | |
1942 // Prepare tissue for delay before the next dive simulation. | |
1943 // | |
1944 // Inputs: char_I_dive_interval == delay before dive (in 10' steps). | |
1945 // Outputs: pres_tissue_N2/He[], CNS_fraction | |
1946 // | |
1947 // Should be protected by deco_push_tissues_to_vault(), | |
1948 // deco_pull_tissues_from_vault() | |
1949 // | |
1950 // desaturation slowed down to 70,42%. | |
1951 // | |
1952 static void calc_dive_interval(void) | |
1953 { | |
1954 overlay unsigned char t; | |
521
06e9370c6d75
CHANGE: Apply safety margin parameters to both models (GF and non-GF)
heinrichsweikamp
parents:
519
diff
changeset
|
1955 |
0 | 1956 //---- Initialize simulation parameters ---------------------------------- |
1957 N2_ratio = 0.7902; // FIXED, sum lt. buehlmann | |
1958 pres_respiration = pres_surface = int_I_pres_surface * 0.001; | |
1959 ppN2 = N2_ratio * (pres_respiration - ppWater); | |
1960 ppHe = 0.0; | |
1961 float_desaturation_multiplier = char_I_desaturation_multiplier * (0.01 * SURFACE_DESAT_FACTOR); | |
1962 float_saturation_multiplier = char_I_saturation_multiplier * 0.01; | |
1963 | |
1964 //---- Perform simulation ------------------------------------------------ | |
1965 for(t=0; t<char_I_dive_interval; ++t) | |
1966 { | |
1967 calc_tissue(2); // period = 10min. | |
1968 CNS_fraction = 0.92587471 * CNS_fraction; // Half-time = 90min: (1/2)^(1/9) | |
1969 } | |
322 | 1970 assert( 0.0 <= CNS_fraction && CNS_fraction <= 9.99 ); // 999 % |
1971 int_O_CNS_fraction = (unsigned short)(CNS_fraction * 100.0 + 0.5); | |
0 | 1972 |
1973 } | |
1974 | |
1975 ////////////////////////////////////////////////////////////////////////////// | |
1976 // deco_clear_CNS_fraction | |
1977 // | |
1978 // new in v.101 | |
1979 // | |
1980 void deco_clear_CNS_fraction(void) | |
1981 { | |
1982 RESET_C_STACK | |
1983 | |
1984 CNS_fraction = 0.0; | |
1985 int_O_CNS_fraction = 0; | |
1986 } | |
1987 | |
1988 ////////////////////////////////////////////////////////////////////////////// | |
1989 // deco_calc_CNS_fraction | |
1990 // | |
1991 // Input: char_I_actual_ppO2 : Current condition (in decibars). | |
1992 // char_I_step_is_1min : use 1min or 10min steps instead of 2sec. | |
1993 // CNS_fraction : velue before period. | |
1994 // Output: CNS_fraction, int_O_CNS_fraction | |
1995 // | |
1996 void deco_calc_CNS_fraction(void) | |
1997 { | |
1998 overlay float time_factor = 1.0f; | |
1999 RESET_C_STACK | |
2000 | |
322 | 2001 assert( 0.0 <= CNS_fraction && CNS_fraction <= 9.99 ); |
0 | 2002 assert( char_I_actual_ppO2 > 15 ); |
2003 | |
2004 if( char_I_step_is_1min == 1 ) | |
2005 time_factor = 30.0f; | |
2006 else if( char_I_step_is_1min == 2 ) | |
2007 time_factor = 300.0f; | |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2008 //------------------------------------------------------------------------ |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2009 // Don't increase CNS below 0.5 bar, but keep it steady. |
0 | 2010 if (char_I_actual_ppO2 < 50) |
2011 ; // no changes | |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2012 //------------------------------------------------------------------------ |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2013 // Below (and including) 1.60 bar |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2014 else if (char_I_actual_ppO2 < 61) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2015 CNS_fraction += time_factor/(-533.07 * char_I_actual_ppO2 + 54000.0); |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2016 else if (char_I_actual_ppO2 < 71) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2017 CNS_fraction += time_factor/(-444.22 * char_I_actual_ppO2 + 48600.0); |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2018 else if (char_I_actual_ppO2 < 81) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2019 CNS_fraction += time_factor/(-355.38 * char_I_actual_ppO2 + 42300.0); |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2020 else if (char_I_actual_ppO2 < 91) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2021 CNS_fraction += time_factor/(-266.53 * char_I_actual_ppO2 + 35100.0); |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2022 else if (char_I_actual_ppO2 < 111) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2023 CNS_fraction += time_factor/(-177.69 * char_I_actual_ppO2 + 27000.0); |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2024 else if (char_I_actual_ppO2 < 152) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2025 CNS_fraction += time_factor/( -88.84 * char_I_actual_ppO2 + 17100.0); |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2026 else if (char_I_actual_ppO2 < 167) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2027 CNS_fraction += time_factor/(-222.11 * char_I_actual_ppO2 + 37350.0); |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2028 //------------------------------------------------------------------------ |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2029 // Arieli et all.(2002): Modeling pulmonary and CNS O2 toxicity: |
322 | 2030 // J Appl Physiol 92: 248--256, 2002, doi:10.1152/japplphysiol.00434.2001 |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2031 // Formula (A1) based on value for 1.55 and c=20 |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2032 // example calculation: Sqrt((1.7/1.55)^20)*0.000404 |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2033 else if (char_I_actual_ppO2 < 172) |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2034 CNS_fraction += time_factor*0.00102; |
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2035 else if (char_I_actual_ppO2 < 177) |
0 | 2036 CNS_fraction += time_factor*0.00136; |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2037 else if (char_I_actual_ppO2 < 182) |
0 | 2038 CNS_fraction += time_factor*0.00180; |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2039 else if (char_I_actual_ppO2 < 187) |
0 | 2040 CNS_fraction += time_factor*0.00237; |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2041 else if (char_I_actual_ppO2 < 192) |
0 | 2042 CNS_fraction += time_factor*0.00310; |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2043 else if (char_I_actual_ppO2 < 198) |
0 | 2044 CNS_fraction += time_factor*0.00401; |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2045 else if (char_I_actual_ppO2 < 203) |
0 | 2046 CNS_fraction += time_factor*0.00517; |
33
5d4a4fb0e8a6
Bugfix: NOAA tables for CNS are in ATA, not bar
heinrichsweikamp
parents:
0
diff
changeset
|
2047 else if (char_I_actual_ppO2 < 233) |
0 | 2048 CNS_fraction += time_factor*0.0209; |
2049 else | |
2050 CNS_fraction += time_factor*0.0482; // value for 2.5 | |
2051 | |
322 | 2052 if( CNS_fraction > 9.99) // Limit display to 999% |
0 | 2053 CNS_fraction = 9.99; |
2054 if( CNS_fraction < 0.0 ) | |
2055 CNS_fraction = 0.0; | |
2056 | |
322 | 2057 int_O_CNS_fraction = (unsigned short)(100.0 * CNS_fraction + 0.5); |
0 | 2058 } |
2059 | |
2060 ////////////////////////////////////////////////////////////////////////////// | |
2061 // deco_calc_CNS_planning | |
2062 // | |
2063 // Compute CNS during predicted ascent. | |
2064 // | |
2065 // Note: Needs a call to deco_push_tissues_to_vault(), | |
2066 // deco_pull_tissues_from_vault() to avoid trashing everything... | |
2067 // | |
2068 // Input: CNS_fraction, char_O_deco_time[], char_O_deco_depth[] | |
2069 // Output: CNS_fraction, int_O_CNS_fraction | |
2070 // | |
2071 void deco_calc_CNS_planning(void) | |
2072 { | |
2073 overlay unsigned char backup_gas_last_depth; | |
2074 overlay unsigned char backup_gas_last_used; | |
2075 overlay unsigned short backup_dive_mins; | |
2076 overlay unsigned char backup_actual_ppO2; | |
2077 | |
2078 RESET_C_STACK | |
2079 | |
2080 // Backup state machine | |
2081 backup_gas_last_depth = sim_gas_last_depth; | |
2082 backup_gas_last_used = sim_gas_last_used; | |
2083 backup_dive_mins = sim_dive_mins; | |
2084 backup_actual_ppO2 = char_I_actual_ppO2; | |
2085 | |
2086 // Uses 1min CNS period: | |
2087 char_I_step_is_1min = 1; | |
2088 | |
2089 //---- Retrieve bottom Gas used, and set variables. | |
2090 sim_gas_last_used = char_I_first_gas; | |
2091 sim_gas_last_depth = 0; // Surface gas marker. | |
2092 gas_switch_set(); // Sets initial calc_N2/He_ratio | |
2093 | |
2094 //---- CCR mode : do the full TTS at once -------------------------------- | |
2095 if( char_I_const_ppO2 != 0 ) | |
2096 { | |
2097 overlay unsigned short t; // Needs 16bits here ! | |
2098 char_I_actual_ppO2 = char_I_const_ppO2; | |
2099 for(t=0; t<int_O_ascenttime; ++t) | |
2100 deco_calc_CNS_fraction(); | |
2101 } | |
2102 else //---- OC mode : have to follow all gas switches... ----------------- | |
2103 { | |
2104 overlay unsigned char i = 0; // Decostop loop counter | |
2105 overlay float actual_ppO2; | |
2106 overlay unsigned char time, t; | |
2107 | |
2108 //---- Ascent to surface delay | |
2109 // NOTE: count as if time is spent with bottom pressure, | |
2110 // AND the bottom gas | |
2111 actual_ppO2 = (pres_surface + char_I_bottom_depth * METER_TO_BAR) | |
2112 * (1.0 - calc_N2_ratio - calc_He_ratio); | |
2113 if( actual_ppO2 < 0.0 ) actual_ppO2 = 0.0; | |
2114 if( actual_ppO2 > 2.50 ) actual_ppO2 = 2.55; | |
2115 char_I_actual_ppO2 = (unsigned char)(100.0 * actual_ppO2 + 0.5); | |
2116 | |
2117 // Ascent time (rounded up): | |
2118 time = (unsigned char)(0.1 * char_I_bottom_depth + 0.5); | |
2119 | |
2120 for(t=0; t<time; ++t) | |
2121 { | |
2122 deco_calc_CNS_fraction(); | |
2123 sim_dive_mins++; | |
2124 } | |
2125 | |
2126 //---- Do all further stops ------------------------------------------ | |
2127 for(i=0; i<NUM_STOPS; ++i) | |
2128 { | |
111 | 2129 overlay unsigned char stop_gas; |
0 | 2130 |
210
7dbc1d780ca5
do not clear char_O_deco_gas in copy_deco_table
heinrichsweikamp
parents:
135
diff
changeset
|
2131 //---- Get next stop --------------------------------------------- |
0 | 2132 { |
2133 time = char_O_deco_time[(NUM_STOPS-1)-i]; | |
2134 temp_depth_limit = char_O_deco_depth[(NUM_STOPS-1)-i]; | |
111 | 2135 stop_gas = char_O_deco_gas[(NUM_STOPS-1)-i]; |
0 | 2136 } |
2137 if( time == 0 ) continue; | |
2138 | |
2139 //---- Gas Switch ? ---------------------------------------------- | |
111 | 2140 if( stop_gas != sim_gas_last_used ) |
0 | 2141 { |
111 | 2142 sim_gas_last_depth = deco_gas_change[stop_gas-1]; |
2143 sim_gas_last_used = stop_gas; | |
0 | 2144 gas_switch_set(); |
2145 } | |
2146 | |
2147 //---- Convert Depth and N2_ratio to ppO2 ------------------------ | |
2148 actual_ppO2 = (pres_surface + temp_depth_limit * METER_TO_BAR) | |
2149 * (1.0 - calc_N2_ratio - calc_He_ratio); | |
2150 if( actual_ppO2 < 0.0 ) actual_ppO2 = 0.0; | |
2151 if( actual_ppO2 > 2.50 ) actual_ppO2 = 2.55; | |
2152 char_I_actual_ppO2 = (unsigned char)(100.0 * actual_ppO2 + 0.5); | |
2153 | |
2154 //---- Apply the stop | |
2155 for(t=0; t<time; ++t) | |
2156 { | |
2157 deco_calc_CNS_fraction(); | |
2158 sim_dive_mins++; | |
2159 } | |
2160 } | |
2161 } | |
2162 | |
2163 //---- Back to normal mode... -------------------------------------------- | |
2164 char_I_step_is_1min = 0; | |
2165 sim_gas_last_depth = backup_gas_last_depth; | |
2166 sim_gas_last_used = backup_gas_last_used; | |
2167 sim_dive_mins = backup_dive_mins; | |
2168 char_I_actual_ppO2 = backup_actual_ppO2; | |
2169 } | |
2170 | |
2171 ////////////////////////////////////////////////////////////////////////////// | |
2172 // deco_calc_CNS_decrease_15min | |
2173 // | |
2174 // new in v.101 | |
2175 // | |
2176 // calculates the half time of 90 minutes in 6 steps of 15 min | |
2177 // (Used in sleepmode, for low battery mode). | |
2178 // | |
2179 // Output: int_O_CNS_fraction | |
2180 // Uses and Updates: CNS_fraction | |
2181 // | |
2182 void deco_calc_CNS_decrease_15min(void) | |
2183 { | |
2184 RESET_C_STACK | |
322 | 2185 assert( 0.0 <= CNS_fraction && CNS_fraction <= 9.99 ); |
0 | 2186 |
2187 CNS_fraction = 0.890899 * CNS_fraction; | |
322 | 2188 int_O_CNS_fraction = (unsigned short)(CNS_fraction * 100.0 + 0.5); |
0 | 2189 } |
2190 | |
2191 ////////////////////////////////////////////////////////////////////////////// | |
2192 // deco_calc_percentage | |
2193 // | |
2194 // new in v.101 | |
2195 // | |
2196 // calculates int_I_temp * char_I_temp / 100 | |
2197 // output is int_I_temp | |
2198 // | |
2199 // Used to compute NoFly remaining time. | |
2200 // | |
2201 void deco_calc_percentage(void) | |
2202 { | |
2203 RESET_C_STACK | |
2204 | |
2205 assert( 60 <= char_I_temp && char_I_temp <= 100 ); | |
2206 assert( int_I_temp < 5760 ); // Less than 4 days = 96h... | |
2207 | |
2208 int_I_temp = (unsigned short)(((float)int_I_temp * (float)char_I_temp) * 0.01 ); | |
2209 | |
2210 assert( int_I_temp < 5760 ); // Less than 96h too... | |
2211 } | |
2212 | |
2213 ////////////////////////////////////////////////////////////////////////////// | |
2214 // deco_gas_volumes | |
2215 // | |
2216 // new in v.111 | |
2217 // | |
2218 // calculates volumes for each gas. | |
2219 // | |
2220 // Input: char_I_bottom_depth, char_I_bottom_time for planned dive. | |
2221 // Gas list. | |
2222 // char_I_first_gas is the bottom gas. | |
2223 // decoplan (char_O_deco_depth, char_O_deco_time). | |
298
2fe34fc0e2ae
new submenu for gas consumption, show actual mix instead of GAS1-GAS5 in deco planner
heinrichsweikamp
parents:
289
diff
changeset
|
2224 // char_I_bottom_usage is bottom liters/minutes (5 .. 50) or bar/min. |
2fe34fc0e2ae
new submenu for gas consumption, show actual mix instead of GAS1-GAS5 in deco planner
heinrichsweikamp
parents:
289
diff
changeset
|
2225 // char_I_deco_usage is deco liters/minutes (5 .. 50) or bar/min. |
0 | 2226 // Output: int_O_gas_volumes[0..4] in litters * 0.1 |
2227 // | |
2228 void deco_gas_volumes(void) | |
2229 { | |
2230 overlay float volumes[NUM_GAS]; | |
2231 overlay float bottom_usage, deco_usage; | |
135 | 2232 overlay unsigned char i; |
0 | 2233 overlay unsigned char gas, depth; |
348 | 2234 overlay unsigned char lastGasStop; |
0 | 2235 RESET_C_STACK |
2236 | |
289 | 2237 //---- initialize -------------------------------------------------------- |
0 | 2238 for(i=0; i<NUM_GAS; ++i) // Nothing yet... |
2239 volumes[i] = 0.0; | |
2240 | |
298
2fe34fc0e2ae
new submenu for gas consumption, show actual mix instead of GAS1-GAS5 in deco planner
heinrichsweikamp
parents:
289
diff
changeset
|
2241 bottom_usage = char_I_bottom_usage; // In liter/minutes. |
2fe34fc0e2ae
new submenu for gas consumption, show actual mix instead of GAS1-GAS5 in deco planner
heinrichsweikamp
parents:
289
diff
changeset
|
2242 deco_usage = char_I_deco_usage; // In liter/minutes. |
289 | 2243 |
2244 // Early return if not defined: | |
2245 if( deco_usage <= 0.0 || bottom_usage <= 0.0 ) | |
2246 goto done; | |
2247 | |
2248 //---- Bottom usage ----------------------------------------------------- | |
0 | 2249 assert(1 <= char_I_first_gas && char_I_first_gas <= NUM_GAS); |
2250 gas = char_I_first_gas - 1; | |
2251 | |
289 | 2252 if( char_I_const_ppO2 == 0 ) |
0 | 2253 volumes[gas] |
2254 = (char_I_bottom_depth*0.1 + 1.0) // Use Psurface = 1.0 bar. | |
2255 * char_I_bottom_time // in minutes. | |
2256 * bottom_usage; // In liter/minutes. | |
2257 | |
2258 //---- Ascent usage ------------------------------------------------------ | |
351
de8c45fb2ab9
BUGFIX gas volume: do not forget deco gas with switch >= dive's bottom depth.
jdg@air
parents:
348
diff
changeset
|
2259 depth = char_I_bottom_depth; |
de8c45fb2ab9
BUGFIX gas volume: do not forget deco gas with switch >= dive's bottom depth.
jdg@air
parents:
348
diff
changeset
|
2260 lastGasStop = 255; // Allow deco gas at or below bottom depth |
0 | 2261 |
2262 for(i=0; i<NUM_STOPS; ++i) | |
2263 { | |
348 | 2264 overlay unsigned char newDepth, time; |
0 | 2265 |
348 | 2266 time = char_O_deco_time [i]; |
2267 if( time == 0 ) break; // End of table: done. | |
0 | 2268 |
288
08986d479b94
FIX gas_volume shall read gas switches from char_O_deco_gas
jdg@air
parents:
287
diff
changeset
|
2269 newDepth = char_O_deco_depth[i]; |
348 | 2270 assert(0 < newDepth && newDepth <= depth); |
2271 | |
2272 //---- Any gas switch before this stop ------------------------------- | |
2273 for(;;) | |
2274 { | |
2275 overlay unsigned char newGas = 0; | |
351
de8c45fb2ab9
BUGFIX gas volume: do not forget deco gas with switch >= dive's bottom depth.
jdg@air
parents:
348
diff
changeset
|
2276 overlay unsigned char newStop = 0; // Mark as NO CHANGE yet |
348 | 2277 overlay unsigned char j; |
288
08986d479b94
FIX gas_volume shall read gas switches from char_O_deco_gas
jdg@air
parents:
287
diff
changeset
|
2278 |
348 | 2279 for(j=0; j<NUM_GAS; ++j) |
2280 { | |
2281 // Skip gas without changing depth: | |
2282 if( ! char_I_deco_gas_change[j] ) | |
2283 continue; | |
2284 // Select gas changed between [newDepth .. lastGasStop[ | |
2285 // Note that <= means changing gas at BEGINNING of this stop. | |
2286 // Note that < means we cant use the same gas twice | |
2287 if( newDepth <= char_I_deco_gas_change[j] | |
2288 && char_I_deco_gas_change[j] < lastGasStop ) | |
2289 { | |
2290 // Keep the DEEPEST gas in that range: | |
2291 if( char_I_deco_gas_change[j] >= newStop ) | |
2292 { | |
2293 newGas = j; | |
2294 newStop = char_I_deco_gas_change[j]; | |
2295 } | |
2296 } | |
2297 } | |
0 | 2298 |
348 | 2299 // Did we find something ? |
2300 if( !newStop ) | |
2301 break; | |
2302 | |
2303 //---- usage BEFORE gas switch (if any), at 10m/min : | |
2304 if( depth > newStop ) | |
2305 // Plus usage during ascent to the next stop, at 10m/min. | |
2306 volumes[gas] += ((depth+newStop)*0.05 + 1.0) // average depth --> bar. | |
2307 * (depth-newStop)*0.1 // metre --> min | |
2308 * deco_usage; | |
2309 | |
2310 //---- Do gas switch: | |
2311 gas = newGas; | |
0 | 2312 |
348 | 2313 lastGasStop = newStop; // Mark last used gas |
2314 if( newStop < depth ) // ascent to gas switch, | |
2315 depth = newStop; | |
2316 } | |
2317 | |
2318 // Are we back to gas from the deco list (just in case): | |
2319 assert(gas == char_O_deco_gas[i]-1); | |
2320 | |
2321 //---- usage AFTER gas switch (if any), at 10m/min : | |
2322 if( depth > newDepth ) | |
2323 volumes[gas] += ((depth+newDepth)*0.05 + 1.0) // average depth --> bar. | |
2324 * (depth-newDepth)*0.1 // metre --> min | |
2325 * deco_usage; | |
2326 | |
2327 //---- Do stop: | |
0 | 2328 depth = newDepth; |
2329 | |
289 | 2330 //---- Usage at stop: |
2331 volumes[gas] += (depth*0.1 + 1.0) // depth --> bar. | |
2332 * time // in minutes. | |
2333 * deco_usage; // in xxx / min @ 1bar. | |
0 | 2334 } |
2335 | |
2336 // From last stop to surface | |
289 | 2337 volumes[gas] += (depth*0.05 + 1.0) // avg depth --> bar. |
2338 * depth * 0.1 // time to surface, in minutes. | |
2339 * deco_usage; // in xxx / min @ 1bar. | |
0 | 2340 |
2341 //---- convert results for the ASM interface ----------------------------- | |
289 | 2342 done: |
0 | 2343 for(i=0; i<NUM_GAS; ++i) |
2344 if( volumes[i] > 65534.0 ) | |
2345 int_O_gas_volumes[i] = 65535; | |
2346 else | |
2347 int_O_gas_volumes[i] = (unsigned short)(volumes[i] + 0.5); | |
2348 } | |
2349 | |
2350 ////////////////////////////////////////////////////////////////////////////// | |
2351 | |
2352 void deco_push_tissues_to_vault(void) | |
2353 { | |
2354 overlay unsigned char x; | |
2355 RESET_C_STACK | |
2356 | |
2357 cns_vault = CNS_fraction; | |
2358 low_depth_vault = low_depth; | |
2359 | |
2360 for (x=0;x<NUM_COMP;x++) | |
2361 { | |
2362 pres_tissue_N2_vault[x] = pres_tissue_N2[x]; | |
2363 pres_tissue_He_vault[x] = pres_tissue_He[x]; | |
2364 } | |
2365 } | |
2366 | |
2367 void deco_pull_tissues_from_vault(void) | |
2368 { | |
2369 overlay unsigned char x; | |
2370 RESET_C_STACK | |
2371 | |
2372 for (x=0; x<NUM_COMP; x++) | |
2373 { | |
2374 pres_tissue_N2[x] = pres_tissue_N2_vault[x]; | |
2375 pres_tissue_He[x] = pres_tissue_He_vault[x]; | |
2376 } | |
2377 | |
2378 // Restore both CNS variable, too. | |
2379 CNS_fraction = cns_vault; | |
322 | 2380 int_O_CNS_fraction = (unsigned short)(CNS_fraction * 100.0 + 0.5); |
0 | 2381 |
2382 // GF history too: | |
2383 low_depth = low_depth_vault; | |
2384 locked_GF_step = GF_delta / low_depth; | |
2385 } | |
2386 | |
2387 ////////////////////////////////////////////////////////////////////////////// | |
2388 // | |
2389 #ifndef CROSS_COMPILE | |
2390 void main() {} | |
2391 #endif |