comparison Discovery/Src/check_warning.c @ 771:29d9b5bc7946

Revised automatic setpoint change. The proposed approach is essentially the approach used by most controllers of eCCR ('upshift' on descent, 'downshift' on ascent), so that the OSTC4 when used as a backup computer for eCCR will make the changes at the same time as the eCCR itself.
author heinrichsweikamp
date Fri, 21 Apr 2023 09:48:23 +0200
parents 6de83d8205a0
children 2c243233c999
comparison
equal deleted inserted replaced
770:8deb28b2d4da 771:29d9b5bc7946
30 ****************************************************************************** 30 ******************************************************************************
31 */ 31 */
32 32
33 /* Includes ------------------------------------------------------------------*/ 33 /* Includes ------------------------------------------------------------------*/
34 34
35 #include <stdbool.h>
36
35 #include "data_exchange.h" 37 #include "data_exchange.h"
36 #include "check_warning.h" 38 #include "check_warning.h"
37 #include "settings.h" 39 #include "settings.h"
38 #include "decom.h" 40 #include "decom.h"
39 #include "tCCR.h" 41 #include "tCCR.h"
42 #define DEBOUNCE_FALLBACK_TIME_MS (5000u) /* set warning after 5 seconds of pending error condition */ 44 #define DEBOUNCE_FALLBACK_TIME_MS (5000u) /* set warning after 5 seconds of pending error condition */
43 45
44 46
45 /* Private variables with access ----------------------------------------------*/ 47 /* Private variables with access ----------------------------------------------*/
46 static uint8_t betterGasId = 0; 48 static uint8_t betterGasId = 0;
47 static uint8_t betterSetpointId = 0; 49 static uint8_t betterSetpointId = 1;
48 static int8_t fallback = 0; 50 static int8_t fallback = 0;
49 static uint16_t debounceFallbackTimeMS = 0; 51 static uint16_t debounceFallbackTimeMS = 0;
50 52
51 /* Private function prototypes -----------------------------------------------*/ 53 /* Private function prototypes -----------------------------------------------*/
52 static int8_t check_fallback(SDiveState * pDiveState); 54 static int8_t check_fallback(SDiveState * pDiveState);
284 } 286 }
285 } 287 }
286 return pDiveState->warnings.betterGas; 288 return pDiveState->warnings.betterGas;
287 } 289 }
288 290
291
292 uint8_t getSetpointLowId(void)
293 {
294 uint8_t setpointLowId = 0;
295 uint8_t setpointLowDepthM = UINT8_MAX;
296 for (unsigned i = 1; i <= NUM_GASES; i++) {
297 if (stateUsed->diveSettings.setpoint[i].depth_meter && stateUsed->diveSettings.setpoint[i].depth_meter < setpointLowDepthM) {
298 setpointLowId = i;
299 setpointLowDepthM = stateUsed->diveSettings.setpoint[i].depth_meter;
300 }
301 }
302
303 return setpointLowId;
304 }
305
306
307 uint8_t getSetpointHighId(void)
308 {
309 uint8_t setpointHighId = 0;
310 uint8_t setpointHighDepthM = 0;
311 for (unsigned i = 1; i <= NUM_GASES; i++) {
312 if (stateUsed->diveSettings.setpoint[i].depth_meter && stateUsed->diveSettings.setpoint[i].depth_meter >= setpointHighDepthM) {
313 setpointHighId = i;
314 setpointHighDepthM = stateUsed->diveSettings.setpoint[i].depth_meter;
315 }
316 }
317
318 return setpointHighId;
319 }
320
321
289 /* check for better travel!!! setpoint hw 151210 322 /* check for better travel!!! setpoint hw 151210
290 */ 323 */
291 static int8_t check_BetterSetpoint(SDiveState * pDiveState) 324 static int8_t check_BetterSetpoint(SDiveState *diveState)
292 { 325 {
293 pDiveState->warnings.betterSetpoint = 0; 326 diveState->warnings.betterSetpoint = 0;
294 betterSetpointId = 0; 327
295 uint8_t bestSetpointDepth = 0; // travel the deeper, the better 328 if (stateUsed->mode != MODE_DIVE) {
296 uint8_t betterSetpointIdLocal = 0; // nothing better 329 betterSetpointId = 1;
297 330
298 331 return 0;
299 if((stateUsed->mode == MODE_DIVE) && (pDiveState->diveSettings.diveMode == DIVEMODE_CCR)) 332 }
300 { 333
301 if(!actualLeftMaxDepth(pDiveState)) /* travel gases */ 334 SSettings *settings = settingsGetPointer();
302 { 335
303 for(int i=1; i<=NUM_GASES; i++) 336 float currentDepthM = diveState->lifeData.depth_meter;
304 { 337 if (settings->dive_mode == DIVEMODE_CCR && diveState->lifeData.lastSetpointChangeDepthM != currentDepthM) {
305 if( (pDiveState->diveSettings.setpoint[i].note.ub.active) 338 float lastChangeDepthM = diveState->lifeData.lastSetpointChangeDepthM;
306 && (pDiveState->diveSettings.setpoint[i].depth_meter) 339 bool descending = currentDepthM > lastChangeDepthM;
307 && (pDiveState->diveSettings.setpoint[i].depth_meter <= ( pDiveState->lifeData.depth_meter + 0.01f )) 340 uint8_t setpointLowId = getSetpointLowId();
308 && (pDiveState->diveSettings.setpoint[i].depth_meter >= bestSetpointDepth) 341 uint8_t setpointHighId = getSetpointHighId();
309 ) 342 uint8_t betterSetpointIdLocal = 0;
310 { 343 uint8_t betterSetpointSwitchDepthM = descending ? 0 : UINT8_MAX;
311 betterSetpointIdLocal = i; 344
312 bestSetpointDepth = pDiveState->diveSettings.setpoint[i].depth_meter; 345 for (unsigned i = 1; i <= NUM_GASES; i++) {
313 } 346 uint8_t switchDepthM = diveState->diveSettings.setpoint[i].depth_meter;
314 } 347 if (!switchDepthM || (descending && i == setpointLowId) || (!descending && i == setpointHighId)) {
315 if((betterSetpointIdLocal) && (pDiveState->diveSettings.setpoint[betterSetpointIdLocal].setpoint_cbar != pDiveState->lifeData.actualGas.setPoint_cbar)) 348 continue;
316 { 349 }
317 betterSetpointId = betterSetpointIdLocal; 350
318 pDiveState->warnings.betterSetpoint = 1; 351 if ((descending && lastChangeDepthM < switchDepthM && switchDepthM < currentDepthM && switchDepthM > betterSetpointSwitchDepthM)
319 } 352 || (!descending && lastChangeDepthM > switchDepthM && switchDepthM > currentDepthM && switchDepthM <= betterSetpointSwitchDepthM)) {
320 } 353 betterSetpointIdLocal = i;
321 } 354 betterSetpointSwitchDepthM = switchDepthM;
322 return pDiveState->warnings.betterSetpoint; 355 }
356 }
357
358 if (betterSetpointIdLocal) {
359 betterSetpointId = betterSetpointIdLocal;
360 if (diveState->diveSettings.diveMode == DIVEMODE_CCR && diveState->diveSettings.setpoint[betterSetpointIdLocal].setpoint_cbar != diveState->lifeData.actualGas.setPoint_cbar) {
361 diveState->warnings.betterSetpoint = 1;
362 }
363 }
364 }
365
366 return diveState->warnings.betterSetpoint;
323 } 367 }
324 368
325 369
326 /* hw 151030 370 /* hw 151030
327 */ 371 */