Mercurial > public > ostc4
annotate Discovery/Src/simulation.c @ 898:fac13aa6ba93 Evo_2_23
Disabled debug features
| author | ideenmodellierer |
|---|---|
| date | Thu, 26 Sep 2024 18:40:41 +0200 |
| parents | 65772ddee88c |
| children | e4e9acfde839 |
| rev | line source |
|---|---|
| 38 | 1 /////////////////////////////////////////////////////////////////////////////// |
| 2 /// -*- coding: UTF-8 -*- | |
| 3 /// | |
| 4 /// \file Discovery/Src/simulation.c | |
| 5 /// \brief Contains dive simulation functionality | |
| 6 /// \author Heinrichs Weikamp gmbh | |
| 7 /// \date 13-Oct-2014 | |
| 8 /// | |
| 9 /// \details | |
| 10 /// The simulation uses "extern SDiveState stateSim" defined in dataCentral.h" | |
| 11 /// | |
| 12 /// simulation_start(void) sets stateUsed to stateSim and initializes simulation | |
| 13 /// simulation_UpdateLifeData should be called at least once per second | |
| 14 /// simulation_end() sets stateUsed back to stateReal | |
| 15 /// | |
| 16 /// $Id$ | |
| 17 /////////////////////////////////////////////////////////////////////////////// | |
| 18 /// \par Copyright (c) 2014-2018 Heinrichs Weikamp gmbh | |
| 19 /// | |
| 20 /// This program is free software: you can redistribute it and/or modify | |
| 21 /// it under the terms of the GNU General Public License as published by | |
| 22 /// the Free Software Foundation, either version 3 of the License, or | |
| 23 /// (at your option) any later version. | |
| 24 /// | |
| 25 /// This program is distributed in the hope that it will be useful, | |
| 26 /// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 27 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 28 /// GNU General Public License for more details. | |
| 29 /// | |
| 30 /// You should have received a copy of the GNU General Public License | |
| 31 /// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 32 ////////////////////////////////////////////////////////////////////////////// | |
| 33 | |
| 34 #include <string.h> | |
| 35 #include "simulation.h" | |
| 36 | |
| 37 #include "decom.h" | |
| 38 #include "calc_crush.h" | |
| 39 #include "data_exchange.h" | |
|
308
1203255481e4
cleanup: introduce function setAvgDepth
Jan Mulder <jlmulder@xs4all.nl>
parents:
307
diff
changeset
|
40 #include "data_exchange_main.h" |
| 38 | 41 #include "timer.h" |
| 42 #include "check_warning.h" | |
| 43 #include "vpm.h" | |
| 44 #include "buehlmann.h" | |
| 45 #include "logbook_miniLive.h" | |
| 46 | |
| 450 | 47 #include "configuration.h" |
| 48 | |
| 38 | 49 //Private state variables |
|
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
50 static float sim_aim_depth_meter; |
|
760
21949c88da90
Quit simualted dives after the dive time set in the SIM tab.
heinrichsweikamp
parents:
748
diff
changeset
|
51 static float sim_aim_time_minutes; |
|
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
52 static _Bool sim_heed_decostops = 1; |
| 38 | 53 |
| 897 | 54 static float sim_descent_rate_meter_per_min = 20; |
| 55 | |
| 56 static uint16_t* pReplayData; /* pointer to source dive data */ | |
| 57 static uint8_t simReplayActive = 0; | |
| 38 | 58 |
| 59 | |
| 60 //Private functions | |
|
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
233
diff
changeset
|
61 static float sim_get_ambient_pressure(SDiveState * pDiveState); |
|
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
62 static void sim_reduce_deco_time_one_second(SDiveState* pDiveState); |
|
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
63 static void simulation_set_aim_depth(int depth_meter); |
| 38 | 64 |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
65 #define NUM_OF_SENSORS (3u) |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
66 #define SIM_PPO2_STEP (1.1f) |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
67 static float simSensmVOffset[NUM_OF_SENSORS]; |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
68 |
| 38 | 69 /** |
| 70 ****************************************************************************** | |
| 71 * @brief sets heed_decostops_while_ascending | |
| 72 ****************************************************************************** | |
| 73 * @param heed_decostops_while_ascending : true -> deco_stops are considered while ascending | |
| 74 * @return void | |
| 75 */ | |
| 76 void simulation_set_heed_decostops(_Bool heed_decostops_while_ascending) | |
| 77 { | |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
78 sim_heed_decostops = heed_decostops_while_ascending; |
| 38 | 79 } |
| 80 | |
| 81 /** | |
| 82 ****************************************************************************** | |
| 83 * @brief start of simulation | |
| 84 ****************************************************************************** | |
| 85 * @return void | |
| 86 */ | |
|
760
21949c88da90
Quit simualted dives after the dive time set in the SIM tab.
heinrichsweikamp
parents:
748
diff
changeset
|
87 void simulation_start(int aim_depth, uint16_t aim_time_minutes) |
| 38 | 88 { |
| 897 | 89 uint16_t replayDataLength = 0; |
| 90 uint8_t* pReplayMarker; | |
| 91 uint16_t max_depth = 10; | |
| 92 uint16_t diveMinutes = 0; | |
| 93 | |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
94 copyDiveSettingsToSim(); |
| 38 | 95 copyVpmRepetetiveDataToSim(); |
| 96 //vpm_init(&stateSimGetPointerWrite()->vpm, stateSimGetPointerWrite()->diveSettings.vpm_conservatism, 0, 0); | |
| 97 stateSimGetPointerWrite()->lifeData.counterSecondsShallowDepth = 0; | |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
98 stateSimGetPointerWrite()->mode = MODE_DIVE; |
| 38 | 99 if(aim_depth <= 0) |
| 100 aim_depth = 20; | |
| 897 | 101 sim_descent_rate_meter_per_min = 20; |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
102 simulation_set_aim_depth(aim_depth); |
|
760
21949c88da90
Quit simualted dives after the dive time set in the SIM tab.
heinrichsweikamp
parents:
748
diff
changeset
|
103 sim_aim_time_minutes = aim_time_minutes; |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
104 timer_init(); |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
105 set_stateUsedToSim(); |
| 38 | 106 stateSim.lifeData.boolResetAverageDepth = 1; |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
107 decoLock = DECO_CALC_init_as_is_start_of_dive; |
| 38 | 108 |
| 109 stateSim.lifeData.apnea_total_max_depth_meter = 0; | |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
110 memset(simSensmVOffset,0,sizeof(simSensmVOffset)); |
| 897 | 111 if(getReplayOffset() != 0xFFFF) |
| 112 { | |
| 113 simReplayActive = 1; | |
| 114 getReplayInfo(&pReplayData, &pReplayMarker, &replayDataLength, &max_depth, &diveMinutes); | |
| 115 } | |
| 38 | 116 } |
| 117 | |
| 118 /** | |
| 119 ****************************************************************************** | |
| 120 * @brief end of simulation | |
| 121 ****************************************************************************** | |
| 122 * | |
| 123 * @return void | |
| 124 */ | |
| 125 void simulation_exit(void) | |
| 126 { | |
| 127 timer_Stopwatch_Stop(); | |
|
805
dd7ce655db26
Adds a simple countdown timer, available as a custom view in surface and dive mode.
heinrichsweikamp
parents:
790
diff
changeset
|
128 |
|
dd7ce655db26
Adds a simple countdown timer, available as a custom view in surface and dive mode.
heinrichsweikamp
parents:
790
diff
changeset
|
129 disableTimer(); |
|
dd7ce655db26
Adds a simple countdown timer, available as a custom view in surface and dive mode.
heinrichsweikamp
parents:
790
diff
changeset
|
130 |
| 38 | 131 set_stateUsedToReal(); |
| 132 } | |
| 133 | |
| 134 /** | |
| 135 ****************************************************************************** | |
| 136 * @brief simulates change of Lifedata (saturation, depth change, etc.) within one second | |
| 137 ****************************************************************************** | |
| 138 * | |
| 139 * @param checkOncePerSecond : true -> simulation in real time (function is evaluated only once per second) | |
| 140 * and copy of parts of LifeData from SmallCPU with each call from HAL_TIM_PeriodElapsedCallback() | |
| 141 * : false -> fast simulation (many simulation cycles per second are possible) | |
| 142 * @return void | |
| 143 */ | |
| 144 void simulation_UpdateLifeData( _Bool checkOncePerSecond) | |
| 145 { | |
| 146 SDiveState * pDiveState = &stateSim; | |
| 813 | 147 const SDiveState * pRealState = stateRealGetPointer(); |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
148 SSettings *pSettings; |
| 38 | 149 |
| 150 static int last_second = -1; | |
| 151 static _Bool two_second = 0; | |
| 152 static float lastPressure_bar = 0; | |
| 153 | |
| 897 | 154 if ((sim_aim_time_minutes && sim_aim_time_minutes * 60 <= pDiveState->lifeData.dive_time_seconds) |
| 155 && (!simReplayActive)) | |
| 156 { | |
|
760
21949c88da90
Quit simualted dives after the dive time set in the SIM tab.
heinrichsweikamp
parents:
748
diff
changeset
|
157 simulation_set_aim_depth(0); |
|
21949c88da90
Quit simualted dives after the dive time set in the SIM tab.
heinrichsweikamp
parents:
748
diff
changeset
|
158 } |
|
21949c88da90
Quit simualted dives after the dive time set in the SIM tab.
heinrichsweikamp
parents:
748
diff
changeset
|
159 |
| 699 | 160 float localCalibCoeff[3] = { 0.0, 0.0, 0.0 }; |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
161 uint8_t index, index2; |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
162 |
| 38 | 163 if(checkOncePerSecond) |
| 164 { | |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
165 |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
166 pSettings = settingsGetPointer(); |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
167 for(index = 0; index < 3; index++) |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
168 { |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
169 localCalibCoeff[index] = pSettings->ppo2sensors_calibCoeff[index]; |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
170 if(localCalibCoeff[index] < 0.01) |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
171 { |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
172 for(index2 = 0; index2 < 3; index2++) /* no valid coeff => check other entries */ |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
173 { |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
174 if(pSettings->ppo2sensors_calibCoeff[index2] > 0.01) |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
175 { |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
176 localCalibCoeff[index] = pSettings->ppo2sensors_calibCoeff[index2]; |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
177 break; |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
178 } |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
179 if(index2 == 3) /* no coeff at all => use default */ |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
180 { |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
181 localCalibCoeff[index] = 0.02; |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
182 } |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
183 } |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
184 } |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
185 } |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
186 |
| 813 | 187 pDiveState->lifeData.temperature_celsius = pRealState->lifeData.temperature_celsius; |
| 188 pDiveState->lifeData.battery_charge = pRealState->lifeData.battery_charge; | |
| 189 pDiveState->lifeData.compass_heading = pRealState->lifeData.compass_heading; | |
| 190 pDiveState->lifeData.compass_roll = pRealState->lifeData.compass_roll; | |
| 191 pDiveState->lifeData.compass_pitch = pRealState->lifeData.compass_pitch; | |
| 192 | |
| 193 for(index = 0; index < 3; index++) | |
| 194 { | |
| 195 memcpy(&pDiveState->lifeData.extIf_sensor_data[index], &pRealState->lifeData.extIf_sensor_data[index], 32); | |
| 196 } | |
|
548
e7e44986684a
Update roll and pitch value in simulation mode:
Ideenmodellierer
parents:
450
diff
changeset
|
197 |
|
446
f1257a32f2d4
Introduced configuration header for variant managment:
ideenmodellierer
parents:
308
diff
changeset
|
198 #ifdef ENABLE_BOTTLE_SENSOR |
| 813 | 199 pDiveState->lifeData.bottle_bar[pDiveState->lifeData.actualGas.GasIdInSettings] = pRealState->lifeData.bottle_bar[pRealState->lifeData.actualGas.GasIdInSettings]; |
| 200 pDiveState->lifeData.bottle_bar_age_MilliSeconds[pDiveState->lifeData.actualGas.GasIdInSettings] = pRealState->lifeData.bottle_bar_age_MilliSeconds[pRealState->lifeData.actualGas.GasIdInSettings]; | |
|
446
f1257a32f2d4
Introduced configuration header for variant managment:
ideenmodellierer
parents:
308
diff
changeset
|
201 #endif |
| 38 | 202 int now = current_second(); |
| 203 if( last_second == now) | |
| 204 return; | |
| 205 last_second = now; | |
| 206 | |
| 207 if(!two_second) | |
| 208 two_second = 1; | |
| 209 else | |
| 210 { | |
| 211 two_second = 0; | |
| 212 } | |
| 213 } | |
| 214 else if(pDiveState->lifeData.depth_meter <= (float)(decom_get_actual_deco_stop(pDiveState) + 0.001)) | |
| 215 sim_reduce_deco_time_one_second(pDiveState); | |
| 216 | |
| 897 | 217 |
| 38 | 218 pDiveState->lifeData.dive_time_seconds += 1; |
|
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
233
diff
changeset
|
219 pDiveState->lifeData.pressure_ambient_bar = sim_get_ambient_pressure(pDiveState); |
| 897 | 220 if(pDiveState->lifeData.pressure_ambient_bar < 1.5) |
| 221 { | |
| 222 lastPressure_bar = 0; | |
| 223 pDiveState->lifeData.ascent_rate_meter_per_min = 0; | |
| 224 } | |
| 225 if(lastPressure_bar > 0) | |
| 226 { | |
| 227 //1 second * 60 == 1 minute, bar * 10 = meter | |
| 228 pDiveState->lifeData.ascent_rate_meter_per_min = (lastPressure_bar - pDiveState->lifeData.pressure_ambient_bar) * 600.0; | |
| 229 } | |
| 230 lastPressure_bar = pDiveState->lifeData.pressure_ambient_bar; | |
| 38 | 231 |
| 813 | 232 pDiveState->lifeData.sensorVoltage_mV[0] = pRealState->lifeData.sensorVoltage_mV[0] + simSensmVOffset[0]; |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
233 if(pDiveState->lifeData.sensorVoltage_mV[0] < 0.0) { pDiveState->lifeData.sensorVoltage_mV[0] = 0.0; } |
| 813 | 234 pDiveState->lifeData.sensorVoltage_mV[1] = pRealState->lifeData.sensorVoltage_mV[1] + simSensmVOffset[1]; |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
235 if(pDiveState->lifeData.sensorVoltage_mV[1] < 0.0) { pDiveState->lifeData.sensorVoltage_mV[1] = 0.0; } |
| 813 | 236 pDiveState->lifeData.sensorVoltage_mV[2] = pRealState->lifeData.sensorVoltage_mV[2] + simSensmVOffset[2]; |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
237 if(pDiveState->lifeData.sensorVoltage_mV[2] < 0.0) { pDiveState->lifeData.sensorVoltage_mV[2] = 0.0; } |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
238 |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
239 pDiveState->lifeData.ppO2Sensor_bar[0] = pDiveState->lifeData.sensorVoltage_mV[0] * localCalibCoeff[0] * pDiveState->lifeData.pressure_ambient_bar; |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
240 pDiveState->lifeData.ppO2Sensor_bar[1] = pDiveState->lifeData.sensorVoltage_mV[1] * localCalibCoeff[1] * pDiveState->lifeData.pressure_ambient_bar; |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
241 pDiveState->lifeData.ppO2Sensor_bar[2] = pDiveState->lifeData.sensorVoltage_mV[2] * localCalibCoeff[2] * pDiveState->lifeData.pressure_ambient_bar; |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
242 |
| 813 | 243 pDiveState->lifeData.CO2_data.CO2_ppm = pRealState->lifeData.CO2_data.CO2_ppm; |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
244 |
| 38 | 245 if(is_ambient_pressure_close_to_surface(&pDiveState->lifeData)) // new hw 170214 |
| 246 { | |
| 247 if(!(stateSimGetPointer()->lifeData.counterSecondsShallowDepth)) | |
| 248 { | |
| 249 if(pDiveState->diveSettings.diveMode != DIVEMODE_Apnea) | |
| 250 pDiveState->lifeData.counterSecondsShallowDepth = settingsGetPointer()->timeoutDiveReachedZeroDepth - 15; | |
| 251 else | |
| 252 { | |
| 253 pDiveState->lifeData.apnea_last_dive_time_seconds = pDiveState->lifeData.dive_time_seconds; | |
| 254 if(pDiveState->lifeData.apnea_last_dive_time_seconds > pDiveState->lifeData.dive_time_seconds_without_surface_time) | |
| 255 pDiveState->lifeData.apnea_last_dive_time_seconds = pDiveState->lifeData.dive_time_seconds_without_surface_time; | |
| 256 pDiveState->lifeData.apnea_last_max_depth_meter = pDiveState->lifeData.max_depth_meter; | |
| 257 pDiveState->lifeData.counterSecondsShallowDepth = 1; | |
| 258 } | |
| 259 } | |
| 260 } | |
| 261 else | |
| 262 { | |
| 263 pDiveState->lifeData.counterSecondsShallowDepth = 0; | |
| 264 } | |
| 265 | |
|
304
43b44f8d4fb0
bugfix, simulator: fix the 1 sec difference between stopwatch and divetime
Jan Mulder <jlmulder@xs4all.nl>
parents:
300
diff
changeset
|
266 if(!is_ambient_pressure_close_to_surface(&pDiveState->lifeData) && !(stateSimGetPointer()->lifeData.counterSecondsShallowDepth) ) |
|
43b44f8d4fb0
bugfix, simulator: fix the 1 sec difference between stopwatch and divetime
Jan Mulder <jlmulder@xs4all.nl>
parents:
300
diff
changeset
|
267 { |
|
43b44f8d4fb0
bugfix, simulator: fix the 1 sec difference between stopwatch and divetime
Jan Mulder <jlmulder@xs4all.nl>
parents:
300
diff
changeset
|
268 pDiveState->lifeData.dive_time_seconds_without_surface_time += 1; |
|
43b44f8d4fb0
bugfix, simulator: fix the 1 sec difference between stopwatch and divetime
Jan Mulder <jlmulder@xs4all.nl>
parents:
300
diff
changeset
|
269 } |
|
43b44f8d4fb0
bugfix, simulator: fix the 1 sec difference between stopwatch and divetime
Jan Mulder <jlmulder@xs4all.nl>
parents:
300
diff
changeset
|
270 |
| 38 | 271 pDiveState->lifeData.depth_meter = (pDiveState->lifeData.pressure_ambient_bar - pDiveState->lifeData.pressure_surface_bar) * 10.0f; |
| 272 if(pDiveState->lifeData.max_depth_meter < pDiveState->lifeData.depth_meter) | |
| 273 pDiveState->lifeData.max_depth_meter = pDiveState->lifeData.depth_meter; | |
| 274 | |
| 275 /* apnoe specials | |
| 276 */ | |
| 277 if(pDiveState->diveSettings.diveMode == DIVEMODE_Apnea) | |
| 278 { | |
| 279 if(pDiveState->lifeData.max_depth_meter > pDiveState->lifeData.apnea_total_max_depth_meter) | |
| 280 pDiveState->lifeData.apnea_total_max_depth_meter = pDiveState->lifeData.max_depth_meter; | |
| 281 | |
| 282 if(pDiveState->lifeData.counterSecondsShallowDepth) | |
| 283 { | |
| 284 pDiveState->lifeData.dive_time_seconds = 0; | |
| 285 pDiveState->lifeData.max_depth_meter = 0; | |
| 286 pDiveState->lifeData.boolResetAverageDepth = 1; | |
| 287 } | |
| 288 } | |
| 289 | |
|
308
1203255481e4
cleanup: introduce function setAvgDepth
Jan Mulder <jlmulder@xs4all.nl>
parents:
307
diff
changeset
|
290 setAvgDepth(pDiveState); |
| 38 | 291 |
| 292 /* Exposure Tissues | |
| 293 */ | |
| 294 decom_tissues_exposure(1, &pDiveState->lifeData); | |
| 295 decom_oxygen_calculate_cns_exposure(1, &pDiveState->lifeData.actualGas, pDiveState->lifeData.pressure_ambient_bar, &pDiveState->lifeData.cns); | |
|
233
9f0efc4df01e
cleanup, bugfix: do not exit simulator on 5h dive time
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
296 |
| 38 | 297 if(stateSimGetPointer()->lifeData.counterSecondsShallowDepth) |
| 298 { | |
| 299 stateSimGetPointerWrite()->lifeData.counterSecondsShallowDepth += 1; | |
| 300 if(stateSimGetPointer()->lifeData.counterSecondsShallowDepth >= settingsGetPointer()->timeoutDiveReachedZeroDepth) | |
| 301 simulation_exit(); | |
| 302 } | |
| 303 vpm_crush(pDiveState); | |
| 304 } | |
| 305 | |
| 306 /** | |
| 307 ****************************************************************************** | |
| 308 * @brief adds extra time for fast simulation | |
| 309 ****************************************************************************** | |
| 310 *@param minutes | |
| 311 * @return float : new pressure | |
| 312 */ | |
|
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
313 static void simulation_add_time(int minutes) |
| 38 | 314 { |
| 315 for(int i = 0; i < 60 * minutes; i++) | |
| 316 { | |
| 317 simulation_UpdateLifeData(0); | |
| 318 updateMiniLiveLogbook(0); | |
| 319 timer_UpdateSecond(0); | |
| 320 } | |
| 321 } | |
| 322 | |
| 323 /** | |
| 324 ****************************************************************************** | |
| 325 * @brief get aim_depth | |
| 326 ****************************************************************************** | |
| 327 * @return sim_aim_depth_meter; | |
| 328 */ | |
| 329 | |
| 330 uint16_t simulation_get_aim_depth(void) | |
| 331 { | |
| 332 return (uint16_t)sim_aim_depth_meter; | |
| 333 } | |
| 334 | |
| 335 /** | |
| 336 ****************************************************************************** | |
| 337 * @brief get heed decostops | |
| 338 ****************************************************************************** | |
| 339 * @return true if ascend follows decostops; | |
| 340 */ | |
| 341 | |
| 342 _Bool simulation_get_heed_decostops(void) | |
| 343 { | |
|
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
344 return sim_heed_decostops; |
| 38 | 345 } |
| 346 | |
| 347 /** | |
| 348 ****************************************************************************** | |
| 349 * @brief sets aim_depth | |
| 350 ****************************************************************************** | |
| 351 *@param depth_meter | |
| 352 * @return float : new pressure | |
| 353 */ | |
|
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
354 static void simulation_set_aim_depth(int depth_meter) |
| 38 | 355 { |
| 356 sim_aim_depth_meter = depth_meter; | |
| 357 } | |
| 358 | |
| 359 /** | |
| 360 ****************************************************************************** | |
|
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
233
diff
changeset
|
361 * @brief simulates ambient pressure depending on aim depth |
| 38 | 362 ****************************************************************************** |
| 363 * @note if aim_depth != actual depth, the depth change within one second | |
| 364 * (depending on descent or ascent) rate is calculated | |
| 365 * @param SDiveState* pDiveState: | |
|
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
233
diff
changeset
|
366 * @return float : new ambient pressure |
| 38 | 367 */ |
|
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
233
diff
changeset
|
368 static float sim_get_ambient_pressure(SDiveState * pDiveState) |
| 38 | 369 { |
| 370 //Calc next depth | |
| 371 uint8_t actual_deco_stop = decom_get_actual_deco_stop(pDiveState); | |
| 372 float depth_meter = pDiveState->lifeData.depth_meter; | |
| 373 float surface_pressure_bar = pDiveState->lifeData.pressure_surface_bar; | |
| 897 | 374 static uint8_t sampleToggle = 0; |
| 375 | |
| 376 if(simReplayActive) /* precondition: function is called once per second, sample rate is 2 seconds */ | |
| 377 { | |
| 378 if(sampleToggle == 0) | |
| 379 { | |
| 380 sampleToggle = 1; | |
| 381 sim_aim_depth_meter = (float)(*pReplayData++/100.0); | |
| 382 sim_descent_rate_meter_per_min = (sim_aim_depth_meter - depth_meter) * 30; | |
| 383 } | |
| 384 else | |
| 385 { | |
| 386 sampleToggle = 0; | |
| 387 } | |
| 388 } | |
| 389 | |
| 38 | 390 if(depth_meter < sim_aim_depth_meter) |
| 391 { | |
| 392 depth_meter = depth_meter + sim_descent_rate_meter_per_min / 60; | |
| 393 if(depth_meter > sim_aim_depth_meter) | |
| 394 depth_meter = sim_aim_depth_meter; | |
| 395 } | |
| 396 else if(depth_meter > sim_aim_depth_meter) | |
| 397 { | |
| 398 | |
| 399 depth_meter -= pDiveState->diveSettings.ascentRate_meterperminute / 60; | |
| 400 if(depth_meter < sim_aim_depth_meter) | |
| 401 depth_meter = sim_aim_depth_meter; | |
| 402 | |
|
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
403 if(sim_heed_decostops && depth_meter < actual_deco_stop) |
| 38 | 404 { |
| 405 if(actual_deco_stop < (depth_meter + pDiveState->diveSettings.ascentRate_meterperminute / 60)) | |
| 406 depth_meter = actual_deco_stop; | |
| 407 else | |
| 408 depth_meter += pDiveState->diveSettings.ascentRate_meterperminute / 60; | |
| 409 } | |
| 410 | |
| 411 } | |
| 412 | |
| 413 return surface_pressure_bar + depth_meter / 10; | |
| 414 } | |
| 415 | |
| 416 | |
| 417 /** | |
| 418 ****************************************************************************** | |
| 419 * @brief Reduces deco time of deepest stop by one second | |
| 420 ****************************************************************************** | |
| 421 * @note called during fast simulation | |
| 422 * @param SDiveState* pDiveState: | |
| 423 * @return void | |
| 424 */ | |
|
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
233
diff
changeset
|
425 static void sim_reduce_deco_time_one_second(SDiveState* pDiveState) |
| 38 | 426 { |
| 427 SDecoinfo* pDecoinfo; | |
| 428 if(pDiveState->diveSettings.deco_type.ub.standard == GF_MODE) | |
| 429 pDecoinfo = &pDiveState->decolistBuehlmann; | |
| 430 else | |
| 431 pDecoinfo = &pDiveState->decolistVPM; | |
| 432 | |
| 433 //Reduce deco time of deepest stop by one second | |
| 434 for(int i = DECOINFO_STRUCT_MAX_STOPS -1 ;i >= 0; i--) | |
| 435 { | |
| 436 if(pDecoinfo->output_stop_length_seconds[i] > 0) | |
| 437 { | |
| 438 pDecoinfo->output_stop_length_seconds[i]--; | |
| 439 break; | |
| 440 } | |
| 441 } | |
| 442 } | |
| 443 | |
| 444 SDecoinfo* simulation_decoplaner(uint16_t depth_meter, uint16_t intervall_time_minutes, uint16_t dive_time_minutes, uint8_t *gasChangeListDepthGas20x2) | |
| 445 { | |
| 446 uint8_t ptrGasChangeList = 0; // new hw 160704 | |
|
888
07af9efd7c13
Dev bugfix: Consider decogas in planner independen from calculation setting:
Ideenmodellierer
parents:
830
diff
changeset
|
447 uint8_t index = 0; |
|
678
05cdd367dbd0
Bugfix: deco planner did not initialize properly
Jan Mulder <jan@jlmulder.nl>
parents:
629
diff
changeset
|
448 for (int i = 0; i < 40; i++) |
|
05cdd367dbd0
Bugfix: deco planner did not initialize properly
Jan Mulder <jan@jlmulder.nl>
parents:
629
diff
changeset
|
449 gasChangeListDepthGas20x2[i] = 0; |
|
05cdd367dbd0
Bugfix: deco planner did not initialize properly
Jan Mulder <jan@jlmulder.nl>
parents:
629
diff
changeset
|
450 |
| 38 | 451 SDiveState * pDiveState = &stateSim; |
| 452 copyDiveSettingsToSim(); | |
|
888
07af9efd7c13
Dev bugfix: Consider decogas in planner independen from calculation setting:
Ideenmodellierer
parents:
830
diff
changeset
|
453 |
|
07af9efd7c13
Dev bugfix: Consider decogas in planner independen from calculation setting:
Ideenmodellierer
parents:
830
diff
changeset
|
454 /* activate deco calculation for all deco gases */ |
|
07af9efd7c13
Dev bugfix: Consider decogas in planner independen from calculation setting:
Ideenmodellierer
parents:
830
diff
changeset
|
455 for(index = 0; index < 1 + (2*NUM_GASES); index++) |
|
07af9efd7c13
Dev bugfix: Consider decogas in planner independen from calculation setting:
Ideenmodellierer
parents:
830
diff
changeset
|
456 { |
|
07af9efd7c13
Dev bugfix: Consider decogas in planner independen from calculation setting:
Ideenmodellierer
parents:
830
diff
changeset
|
457 if(pDiveState->diveSettings.gas[index].note.ub.deco) |
|
07af9efd7c13
Dev bugfix: Consider decogas in planner independen from calculation setting:
Ideenmodellierer
parents:
830
diff
changeset
|
458 { |
|
07af9efd7c13
Dev bugfix: Consider decogas in planner independen from calculation setting:
Ideenmodellierer
parents:
830
diff
changeset
|
459 pDiveState->diveSettings.gas[index].note.ub.decocalc = 1; |
|
07af9efd7c13
Dev bugfix: Consider decogas in planner independen from calculation setting:
Ideenmodellierer
parents:
830
diff
changeset
|
460 } |
|
07af9efd7c13
Dev bugfix: Consider decogas in planner independen from calculation setting:
Ideenmodellierer
parents:
830
diff
changeset
|
461 } |
|
07af9efd7c13
Dev bugfix: Consider decogas in planner independen from calculation setting:
Ideenmodellierer
parents:
830
diff
changeset
|
462 |
| 38 | 463 vpm_init(&pDiveState->vpm, pDiveState->diveSettings.vpm_conservatism, 0, 0); |
| 464 //buehlmann_init(); | |
| 465 //timer_init(); | |
| 466 memset(&pDiveState->events,0, sizeof(SEvents)); | |
| 467 pDiveState->diveSettings.internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero = 0; | |
| 468 //Calc desaturation during intervall (with Air) | |
| 469 setActualGasAir(&pDiveState->lifeData); | |
| 470 if(intervall_time_minutes > 0) | |
| 471 { | |
| 472 decom_tissues_exposure(intervall_time_minutes * 60, &pDiveState->lifeData); | |
| 473 decom_oxygen_calculate_cns_degrade(&pDiveState->lifeData.cns, intervall_time_minutes * 60); | |
| 474 } | |
| 475 | |
| 476 //Switch to first Gas | |
| 477 setActualGasFirst(&pDiveState->lifeData); | |
| 478 | |
| 479 // new hw 160704 | |
| 480 if(gasChangeListDepthGas20x2) | |
| 481 { | |
| 482 gasChangeListDepthGas20x2[ptrGasChangeList++] = 0; | |
| 483 gasChangeListDepthGas20x2[ptrGasChangeList++] = pDiveState->lifeData.actualGas.GasIdInSettings; | |
| 484 gasChangeListDepthGas20x2[0] =0; // depth zero | |
| 485 } | |
| 486 | |
| 487 //Going down / descent | |
| 488 simulation_set_aim_depth(depth_meter); | |
|
790
3b5f9557c053
Fix bug introduced in 04b98a2, causing the deco planner to not show deco stops.
heinrichsweikamp
parents:
760
diff
changeset
|
489 sim_aim_time_minutes = 0; |
| 38 | 490 for(int i = 0; i < 60 * dive_time_minutes; i++) |
| 491 { | |
| 492 simulation_UpdateLifeData(0); | |
| 493 check_warning2(pDiveState); | |
| 494 if(pDiveState->warnings.betterGas) | |
| 495 { | |
| 496 setActualGas(&pDiveState->lifeData,actualBetterGasId(),pDiveState->lifeData.actualGas.setPoint_cbar); | |
| 497 if(gasChangeListDepthGas20x2 && (pDiveState->diveSettings.diveMode == DIVEMODE_OC)) | |
| 498 { | |
| 499 gasChangeListDepthGas20x2[ptrGasChangeList++] = pDiveState->lifeData.depth_meter; | |
| 500 gasChangeListDepthGas20x2[ptrGasChangeList++] = actualBetterGasId(); | |
| 501 } | |
| 502 } | |
| 503 } | |
| 504 | |
| 505 decom_CreateGasChangeList(&pDiveState->diveSettings, &pDiveState->lifeData); // was there before and needed for buehlmann_calc_deco and vpm_calc | |
| 506 | |
| 507 // new hw 160704 | |
| 508 if(gasChangeListDepthGas20x2 && (pDiveState->diveSettings.diveMode == DIVEMODE_OC)) | |
| 509 { | |
| 510 // change direction from better gas to deco gas | |
| 511 gasChangeListDepthGas20x2[ptrGasChangeList++] = 255; | |
| 512 gasChangeListDepthGas20x2[ptrGasChangeList++] = 255; | |
| 513 | |
| 514 // ascend (deco) gases | |
| 515 for(int i=1; i<=5;i++) | |
| 516 { | |
|
830
b7d93ff6b3b2
Added selection if an active gas shall be used for deco calculation or not:
Ideenmodellierer
parents:
813
diff
changeset
|
517 if((pDiveState->diveSettings.decogaslist[i].change_during_ascent_depth_meter_otherwise_zero == 0) |
|
b7d93ff6b3b2
Added selection if an active gas shall be used for deco calculation or not:
Ideenmodellierer
parents:
813
diff
changeset
|
518 || (pDiveState->diveSettings.gas[pDiveState->diveSettings.decogaslist[i].GasIdInSettings].note.ub.decocalc == 0)) |
| 38 | 519 break; |
| 520 gasChangeListDepthGas20x2[ptrGasChangeList++] = pDiveState->diveSettings.decogaslist[i].change_during_ascent_depth_meter_otherwise_zero; | |
| 521 gasChangeListDepthGas20x2[ptrGasChangeList++] = pDiveState->diveSettings.decogaslist[i].GasIdInSettings; | |
| 522 } | |
| 523 gasChangeListDepthGas20x2[0] = 0; | |
| 524 } | |
| 525 | |
| 526 // deco and ascend calc | |
| 527 if(pDiveState->diveSettings.deco_type.ub.standard == GF_MODE) | |
| 528 { | |
| 529 /* this does modify the cns now 11.06.2015 */ | |
| 530 buehlmann_calc_deco(&pDiveState->lifeData,&pDiveState->diveSettings,&pDiveState->decolistBuehlmann); | |
| 531 pDiveState->lifeData.cns += buehlmann_get_gCNS(); | |
| 532 return &pDiveState->decolistBuehlmann; | |
| 533 } | |
| 534 else | |
| 535 { | |
| 536 /* this does modify the cns now 11.06.2015 */ | |
| 537 vpm_calc(&pDiveState->lifeData,&pDiveState->diveSettings,&pDiveState->vpm,&pDiveState->decolistVPM, DECOSTOPS); | |
| 538 pDiveState->lifeData.cns += vpm_get_CNS(); | |
| 539 return &pDiveState->decolistVPM; | |
| 540 } | |
| 541 } | |
| 542 | |
|
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
176
diff
changeset
|
543 static float sGChelper_bar(uint16_t depth_meter) |
| 38 | 544 { |
| 545 SDiveState * pDiveState = &stateSim; | |
| 546 float ambient, surface, density, meter; | |
| 547 | |
| 548 surface = pDiveState->lifeData.pressure_surface_bar; | |
| 549 | |
| 550 if(!depth_meter) | |
| 551 return surface; | |
| 552 | |
| 553 density = ((float)( 100 + settingsGetPointer()->salinity)) / 100.0f; | |
| 554 meter = depth_meter * (0.09807f * density); | |
| 555 ambient = (meter + surface); | |
| 556 | |
| 557 return ambient; | |
| 558 } | |
| 559 | |
| 560 | |
| 561 /** | |
| 562 ****************************************************************************** | |
| 563 * @brief simulation_helper_change_points | |
| 564 ****************************************************************************** | |
| 565 * @param | |
| 566 * @return void | |
| 567 */ | |
| 568 void simulation_helper_change_points(SSimDataSummary *outputSummary, uint16_t depth_meter, uint16_t dive_time_minutes, SDecoinfo *decoInfoInput, const uint8_t *gasChangeListDepthGas20x2) | |
| 569 { | |
| 570 uint8_t ptrDecoInfo = 0; | |
| 571 uint16_t actualDepthPoint = 0; | |
| 572 uint16_t nextDepthPoint = 0; | |
| 573 uint8_t actualConsumGasId = 0; | |
| 574 uint8_t nextGasChangeMeter = 0; | |
| 575 uint8_t ptrChangeList = 0; | |
| 576 | |
| 577 float timeThis = 0; | |
| 578 float timeSummary = 0; | |
| 579 float sim_descent_rate_meter_per_min_local = 10; | |
| 580 float sim_ascent_rate_meter_per_min_local = 10; | |
| 581 | |
| 582 SDiveState * pDiveState = &stateSim; | |
| 583 | |
| 584 uint8_t depthDecoNext, depthLast, depthSecond, depthInc; | |
| 585 | |
| 586 if(pDiveState->diveSettings.deco_type.ub.standard == GF_MODE) | |
| 587 { | |
| 588 sim_descent_rate_meter_per_min_local = sim_descent_rate_meter_per_min; // const float | |
| 589 sim_ascent_rate_meter_per_min_local = pDiveState->diveSettings.ascentRate_meterperminute; | |
| 590 } | |
| 591 else | |
| 592 { | |
| 593 sim_descent_rate_meter_per_min_local = sim_descent_rate_meter_per_min; // const float | |
| 594 sim_ascent_rate_meter_per_min_local = 10;// fix in vpm_calc_deco(); | |
| 595 } | |
| 596 | |
| 597 outputSummary->descentRateMeterPerMinute = sim_descent_rate_meter_per_min_local; | |
| 598 outputSummary->ascentRateMeterPerMinute = sim_ascent_rate_meter_per_min_local; | |
| 599 | |
| 600 // bottom gas ppO2 | |
| 601 if(gasChangeListDepthGas20x2) | |
| 602 { | |
| 603 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
| 604 actualConsumGasId = gasChangeListDepthGas20x2[ptrChangeList++]; | |
| 605 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
| 606 | |
| 607 while(actualDepthPoint < depth_meter) | |
| 608 { | |
| 609 if(nextGasChangeMeter && (nextGasChangeMeter < depth_meter) && (gasChangeListDepthGas20x2[ptrChangeList] != 255)) // list has 255,255 for turn from travel to deco | |
| 610 { | |
| 611 nextDepthPoint = nextGasChangeMeter; | |
| 612 } | |
| 613 else | |
| 614 { | |
| 615 nextDepthPoint = depth_meter; | |
| 616 } | |
| 617 | |
| 618 if(actualConsumGasId > 5) // safety first | |
| 619 actualConsumGasId = 0; | |
| 620 | |
| 621 actualDepthPoint = nextDepthPoint; | |
| 622 | |
| 623 if(actualDepthPoint != depth_meter) | |
| 624 { | |
| 625 actualConsumGasId = gasChangeListDepthGas20x2[ptrChangeList++]; | |
| 626 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
| 627 } | |
| 628 } | |
| 629 } | |
| 630 else | |
| 631 { | |
| 632 actualConsumGasId = pDiveState->lifeData.actualGas.GasIdInSettings; | |
| 633 nextGasChangeMeter = 0; | |
| 634 } | |
| 635 outputSummary->ppO2AtBottom = (sGChelper_bar(depth_meter) - WATER_VAPOUR_PRESSURE) * pDiveState->diveSettings.gas[actualConsumGasId].oxygen_percentage / 100.0f; | |
| 636 | |
| 637 | |
| 638 // going down | |
| 639 actualDepthPoint = 0; | |
| 640 nextDepthPoint = depth_meter; | |
| 641 | |
| 642 timeThis = ((float)(nextDepthPoint - actualDepthPoint)) / sim_descent_rate_meter_per_min_local; | |
| 643 timeSummary += timeThis; | |
| 644 outputSummary->timeToBottom = (uint16_t)timeThis; | |
| 645 | |
| 646 // bottom time | |
| 647 timeThis = ((float)dive_time_minutes) - timeSummary; | |
| 648 timeSummary += timeThis; | |
| 649 outputSummary->timeAtBottom = (uint16_t)timeSummary; | |
| 650 | |
| 651 | |
| 652 // ascend to first deco stop | |
| 653 actualDepthPoint = depth_meter; // that is where we are | |
| 654 timeThis = 0; | |
| 655 | |
| 656 if(!decoInfoInput->output_stop_length_seconds[0]) // NDL dive | |
| 657 { | |
| 658 depthLast = 0; | |
| 659 ptrDecoInfo = 0; | |
| 660 depthDecoNext = 0; | |
| 661 } | |
| 662 else | |
| 663 { | |
| 664 // prepare deco stop list | |
| 665 depthLast = (uint8_t)(stateUsed->diveSettings.last_stop_depth_bar * 10); | |
| 666 depthSecond = (uint8_t)(stateUsed->diveSettings.input_second_to_last_stop_depth_bar * 10); | |
| 667 depthInc = (uint8_t)(stateUsed->diveSettings.input_next_stop_increment_depth_bar * 10); | |
| 668 | |
| 669 for(ptrDecoInfo=DECOINFO_STRUCT_MAX_STOPS-1; ptrDecoInfo>0; ptrDecoInfo--) | |
| 670 if(decoInfoInput->output_stop_length_seconds[ptrDecoInfo]) break; | |
| 671 | |
| 672 if(ptrDecoInfo == 0) | |
| 673 { | |
| 674 depthDecoNext = depthLast; | |
| 675 } | |
| 676 else | |
| 677 depthDecoNext = depthSecond + (( ptrDecoInfo - 1 )* depthInc); | |
| 678 } | |
| 679 | |
| 680 nextDepthPoint = depthDecoNext; | |
| 681 if(actualDepthPoint > nextDepthPoint) | |
| 682 { | |
| 683 // flip signs! It's going up | |
| 684 timeThis = ((float)(actualDepthPoint - nextDepthPoint)) / sim_ascent_rate_meter_per_min_local; | |
| 685 actualDepthPoint = nextDepthPoint; // that is where we are | |
| 686 } | |
| 687 timeSummary += timeThis; | |
| 688 outputSummary->timeToFirstStop = (uint16_t)timeSummary; | |
| 689 outputSummary->depthMeterFirstStop = actualDepthPoint; | |
| 690 | |
| 691 //ascent | |
| 692 nextDepthPoint = 0; | |
| 693 timeThis = 0; | |
| 694 if(actualDepthPoint > nextDepthPoint) // only if deco | |
| 695 { | |
| 696 // ascent time | |
| 697 timeThis = ((float)(actualDepthPoint - nextDepthPoint)) / sim_ascent_rate_meter_per_min_local; | |
| 698 | |
| 699 // deco stop time | |
| 700 for(ptrDecoInfo=0;ptrDecoInfo < DECOINFO_STRUCT_MAX_STOPS; ptrDecoInfo++) | |
| 701 { | |
| 702 timeThis += decoInfoInput->output_stop_length_seconds[ptrDecoInfo] / 60; | |
| 703 if(!decoInfoInput->output_stop_length_seconds[ptrDecoInfo]) break; | |
| 704 } | |
| 705 } | |
| 706 timeSummary += timeThis; | |
| 707 outputSummary->timeToSurface = (uint16_t)timeSummary; | |
| 708 | |
| 709 } | |
| 710 | |
| 711 | |
| 712 /** | |
| 713 ****************************************************************************** | |
| 714 * @brief simulation_gas_consumption | |
| 715 ****************************************************************************** | |
| 716 * @note called by openEdit_PlanResult() in tMenuEditPlanner.c | |
| 717 * @note the ascend and descend time is taken from pDiveState->lifeData.ascent_rate_meter_per_min and const float sim_descent_rate_meter_per_min | |
| 718 * @param outputConsumptionList list from 1 to 5 for gas 1 to 5 | |
| 719 * @param depth_meter for descend | |
| 720 * @param dive_time_minutes for descend and bottom time | |
| 721 * @param the calculated deco list | |
| 722 * @param gasConsumTravelInput: how many l/min for all but deco stops | |
| 723 * @param gasConsumDecoInput: how many l/min for deco stops only | |
| 724 * @return void | |
| 725 */ | |
| 726 | |
| 727 void simulation_gas_consumption(uint16_t *outputConsumptionList, uint16_t depth_meter, uint16_t dive_time_minutes, SDecoinfo *decoInfoInput, uint8_t gasConsumTravelInput, uint8_t gasConsumDecoInput, const uint8_t *gasChangeListDepthGas20x2) | |
| 728 { | |
| 729 uint8_t ptrDecoInfo = 0; | |
| 730 uint8_t ptrChangeList = 0; | |
| 731 uint8_t actualConsumGasId = 0; | |
| 732 uint8_t nextGasChangeMeter = 0; | |
| 733 uint16_t actualDepthPoint = 0; | |
| 734 uint16_t nextDepthPoint = 0; | |
| 735 uint16_t inBetweenDepthPoint = 0; | |
| 736 float timeThis = 0; | |
| 737 float consumThis = 0; | |
| 738 float timeSummary = 0; | |
| 739 float outputConsumptionTempFloat[6]; | |
| 740 float sim_descent_rate_meter_per_min_local = 10; | |
| 741 float sim_ascent_rate_meter_per_min_local = 10; | |
| 742 | |
| 743 SDiveState * pDiveState = &stateSim; | |
| 744 | |
|
51
8f8ea3a32e82
Resolved warnings pointing to possible invalid memory access
Ideenmodellierer
parents:
38
diff
changeset
|
745 uint8_t depthDecoNext = 0; |
|
8f8ea3a32e82
Resolved warnings pointing to possible invalid memory access
Ideenmodellierer
parents:
38
diff
changeset
|
746 uint8_t depthLast = 0; |
|
8f8ea3a32e82
Resolved warnings pointing to possible invalid memory access
Ideenmodellierer
parents:
38
diff
changeset
|
747 uint8_t depthSecond = 0; |
|
8f8ea3a32e82
Resolved warnings pointing to possible invalid memory access
Ideenmodellierer
parents:
38
diff
changeset
|
748 uint8_t depthInc = 0; |
| 38 | 749 |
| 750 for(int i = 1; i < 6; i++) | |
| 751 outputConsumptionTempFloat[i] = 0; | |
| 752 | |
| 753 if(gasChangeListDepthGas20x2) | |
| 754 { | |
| 755 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
| 756 actualConsumGasId = gasChangeListDepthGas20x2[ptrChangeList++]; | |
| 757 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
| 758 } | |
| 759 else | |
| 760 { | |
| 761 actualConsumGasId = pDiveState->lifeData.actualGas.GasIdInSettings; | |
| 762 nextGasChangeMeter = 0; | |
| 763 } | |
| 764 | |
| 765 if(pDiveState->diveSettings.deco_type.ub.standard == GF_MODE) | |
| 766 { | |
| 767 sim_descent_rate_meter_per_min_local = sim_descent_rate_meter_per_min; // const float | |
| 768 sim_ascent_rate_meter_per_min_local = pDiveState->diveSettings.ascentRate_meterperminute; | |
| 769 } | |
| 770 else | |
| 771 { | |
| 772 sim_descent_rate_meter_per_min_local = sim_descent_rate_meter_per_min; // const float | |
| 773 sim_ascent_rate_meter_per_min_local = 10;// fix in vpm_calc_deco(); | |
| 774 } | |
| 775 | |
| 776 // while((nextGasChangeMeter < depth_meter) && (actualDepthPoint < depth_meter)) | |
| 777 while(actualDepthPoint < depth_meter) | |
| 778 { | |
| 779 if(nextGasChangeMeter && (nextGasChangeMeter < depth_meter) && (gasChangeListDepthGas20x2[ptrChangeList] != 255)) // list has 255,255 for turn from travel to deco | |
| 780 { | |
| 781 nextDepthPoint = nextGasChangeMeter; | |
| 782 } | |
| 783 else | |
| 784 { | |
| 785 nextDepthPoint = depth_meter; | |
| 786 } | |
| 787 | |
| 788 if(actualConsumGasId > 5) // safety first | |
| 789 actualConsumGasId = 0; | |
| 790 | |
| 791 timeThis = ((float)(nextDepthPoint - actualDepthPoint)) / sim_descent_rate_meter_per_min_local; | |
| 792 if(actualDepthPoint) // not if on surface | |
| 793 { | |
| 794 consumThis = ((float)gasConsumTravelInput) * sGChelper_bar(actualDepthPoint) * timeThis; | |
| 795 } | |
| 796 consumThis += ((float)gasConsumTravelInput) * sGChelper_bar(nextDepthPoint -actualDepthPoint) * timeThis / 2; | |
| 797 outputConsumptionTempFloat[actualConsumGasId] += consumThis; | |
| 798 timeSummary += timeThis; | |
| 799 | |
| 800 actualDepthPoint = nextDepthPoint; | |
| 801 | |
| 802 if(actualDepthPoint != depth_meter) | |
| 803 { | |
| 804 actualConsumGasId = gasChangeListDepthGas20x2[ptrChangeList++]; | |
| 805 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
| 806 } | |
| 807 } | |
| 808 | |
| 809 // bottom Time | |
| 810 timeThis = ((float)dive_time_minutes) - timeSummary; | |
| 811 | |
| 812 if(timeThis > 0) | |
| 813 { | |
| 814 consumThis = ((float)gasConsumTravelInput) * sGChelper_bar(depth_meter) * timeThis; | |
| 815 outputConsumptionTempFloat[actualConsumGasId] += consumThis; | |
| 816 } | |
| 817 | |
| 818 // ascend with deco stops prepare | |
| 819 if(gasChangeListDepthGas20x2) | |
| 820 { | |
| 821 ptrChangeList++;// gasChangeListDepthGas20x2[ptrChangeList++]; // should be the 255 | |
| 822 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
| 823 } | |
| 824 else | |
| 825 { | |
| 826 nextGasChangeMeter = 0; | |
| 827 } | |
| 828 | |
| 829 | |
| 830 if(!decoInfoInput->output_stop_length_seconds[0]) // NDL dive | |
| 831 { | |
| 832 depthLast = 0; | |
| 833 ptrDecoInfo = 0; | |
| 834 } | |
| 835 else | |
| 836 { | |
| 837 // prepare deco stop list | |
| 838 depthLast = (uint8_t)(stateUsed->diveSettings.last_stop_depth_bar * 10); | |
| 839 depthSecond = (uint8_t)(stateUsed->diveSettings.input_second_to_last_stop_depth_bar * 10); | |
| 840 depthInc = (uint8_t)(stateUsed->diveSettings.input_next_stop_increment_depth_bar * 10); | |
| 841 | |
| 842 for(ptrDecoInfo=DECOINFO_STRUCT_MAX_STOPS-1; ptrDecoInfo>0; ptrDecoInfo--) | |
| 843 if(decoInfoInput->output_stop_length_seconds[ptrDecoInfo]) break; | |
| 844 } | |
| 845 | |
| 846 actualDepthPoint = depth_meter; // that is where we are | |
| 847 | |
| 848 // ascend with deco stops | |
| 849 while(actualDepthPoint) | |
| 850 { | |
| 851 if(ptrDecoInfo == 0) | |
| 852 { | |
| 853 depthDecoNext = depthLast; | |
| 854 } | |
| 855 else | |
| 856 depthDecoNext = depthSecond + (( ptrDecoInfo - 1 )* depthInc); | |
| 857 | |
| 858 if(nextGasChangeMeter && (nextGasChangeMeter > depthDecoNext)) | |
| 859 { | |
| 860 nextDepthPoint = nextGasChangeMeter; | |
| 861 } | |
| 862 else | |
| 863 { | |
| 864 nextDepthPoint = depthDecoNext; | |
| 865 } | |
| 866 | |
| 867 if(actualConsumGasId > 5) // safety first | |
| 868 actualConsumGasId = 0; | |
| 869 | |
| 870 if(actualDepthPoint > nextDepthPoint) | |
| 871 { | |
| 872 // flip signs! It's going up | |
| 873 timeThis = ((float)(actualDepthPoint - nextDepthPoint)) / sim_ascent_rate_meter_per_min_local; | |
| 874 inBetweenDepthPoint = nextDepthPoint + ((actualDepthPoint - nextDepthPoint)/2); | |
| 875 consumThis = ((float)gasConsumDecoInput) * sGChelper_bar(inBetweenDepthPoint) * timeThis; | |
| 876 /* | |
| 877 if(nextDepthPoint) | |
| 878 { | |
| 879 consumThis = ((float)gasConsumDecoInput) * sGChelper_bar(nextDepthPoint) * timeThis; | |
| 880 } | |
| 881 else | |
| 882 { | |
| 883 consumThis = 0; | |
| 884 } | |
| 885 consumThis += ((float)gasConsumDecoInput) * sGChelper_bar(actualDepthPoint - nextDepthPoint) * timeThis / 2; | |
| 886 */ | |
| 887 outputConsumptionTempFloat[actualConsumGasId] += consumThis; | |
| 888 } | |
| 889 | |
| 890 if(nextGasChangeMeter && (nextDepthPoint == nextGasChangeMeter)) | |
| 891 { | |
| 892 actualConsumGasId = gasChangeListDepthGas20x2[ptrChangeList++]; | |
| 893 nextGasChangeMeter = gasChangeListDepthGas20x2[ptrChangeList++]; | |
| 894 } | |
| 895 | |
| 896 if(actualConsumGasId > 5) // safety first | |
| 897 actualConsumGasId = 0; | |
| 898 | |
| 899 if(nextDepthPoint && (nextDepthPoint == depthDecoNext)) | |
| 900 { | |
| 901 if(decoInfoInput->output_stop_length_seconds[ptrDecoInfo]) | |
| 902 { | |
| 903 timeThis = ((float)(decoInfoInput->output_stop_length_seconds[ptrDecoInfo])) / 60.0f; | |
| 904 consumThis = ((float)gasConsumDecoInput) * sGChelper_bar(nextDepthPoint) * timeThis; | |
| 905 outputConsumptionTempFloat[actualConsumGasId] += consumThis; | |
| 906 } | |
| 907 if(ptrDecoInfo != 0) | |
| 908 { | |
| 909 ptrDecoInfo--; | |
| 910 } | |
| 911 else | |
| 912 { | |
| 913 depthLast = 0; | |
| 914 } | |
| 915 } | |
| 916 actualDepthPoint = nextDepthPoint; | |
| 917 } | |
| 918 | |
| 919 // copy and return | |
| 920 for(int i = 1; i < 6; i++) | |
| 921 outputConsumptionList[i] = (uint16_t)(outputConsumptionTempFloat[i]); | |
| 922 } | |
| 923 | |
| 924 /** | |
| 925 ****************************************************************************** | |
| 926 * @brief Simulator control during simulated dive | |
| 927 ****************************************************************************** | |
| 928 * @note called by user via tHomeDiveMenuControl() | |
| 929 * @param void | |
| 930 * @return void | |
| 931 */ | |
| 932 | |
| 933 | |
| 934 void Sim_Descend (void) | |
| 935 { | |
| 936 stateSimGetPointerWrite()->lifeData.counterSecondsShallowDepth = 0; | |
| 937 if(simulation_get_aim_depth() < 200) | |
| 938 simulation_set_aim_depth(simulation_get_aim_depth() + 1); | |
| 939 } | |
| 940 | |
| 941 | |
| 942 void Sim_Ascend (void) | |
| 943 { | |
| 944 if(simulation_get_aim_depth() > 0) | |
| 945 simulation_set_aim_depth(simulation_get_aim_depth() - 1); | |
| 946 } | |
| 947 | |
| 948 | |
| 949 void Sim_Divetime (void) | |
| 950 { | |
| 951 simulation_add_time(5); | |
| 952 } | |
| 953 | |
| 954 | |
| 955 void Sim_Quit (void) | |
| 956 { | |
| 957 if(stateSimGetPointer()->lifeData.counterSecondsShallowDepth) | |
| 958 { | |
| 959 simulation_exit(); | |
| 960 return; | |
| 961 } | |
| 962 | |
| 963 if(simulation_get_aim_depth() > 0) | |
| 964 { | |
| 965 simulation_set_aim_depth(0); | |
| 966 } | |
| 967 else | |
| 968 { | |
| 969 stateSimGetPointerWrite()->lifeData.depth_meter = 0; | |
| 970 if(stateSimGetPointer()->diveSettings.diveMode == DIVEMODE_Apnea) | |
| 971 { | |
| 972 stateSimGetPointerWrite()->lifeData.counterSecondsShallowDepth = 1; | |
| 973 } | |
| 974 else | |
| 975 { | |
| 976 stateSimGetPointerWrite()->lifeData.counterSecondsShallowDepth = settingsGetPointer()->timeoutDiveReachedZeroDepth - 15; | |
| 977 } | |
| 978 } | |
| 979 } | |
|
760
21949c88da90
Quit simualted dives after the dive time set in the SIM tab.
heinrichsweikamp
parents:
748
diff
changeset
|
980 |
|
629
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
981 void Sim_IncreasePPO(uint8_t sensorIdx) |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
982 { |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
983 if((sensorIdx < NUM_OF_SENSORS) && (simSensmVOffset[sensorIdx] + SIM_PPO2_STEP < 100.0) && ((stateUsed->diveSettings.ppo2sensors_deactivated & (1 << sensorIdx)) == 0)) |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
984 { |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
985 simSensmVOffset[sensorIdx] += SIM_PPO2_STEP; |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
986 } |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
987 } |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
988 void Sim_DecreasePPO(uint8_t sensorIdx) |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
989 { |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
990 if((sensorIdx < NUM_OF_SENSORS) && (simSensmVOffset[sensorIdx] - SIM_PPO2_STEP >= -100.0)) |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
991 { |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
992 simSensmVOffset[sensorIdx] -= SIM_PPO2_STEP; |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
993 } |
|
55a9aa740f13
Added functionality to add ppo2 mV offset:
Ideenmodellierer
parents:
548
diff
changeset
|
994 } |
