diff Discovery/Src/data_central.c @ 696:cc542448fb28

Merge
author heinrichsweikamp
date Fri, 19 Aug 2022 11:30:24 +0200
parents 6f5a18bb25be
children 8a2337c7af52
line wrap: on
line diff
--- a/Discovery/Src/data_central.c	Mon Nov 01 12:39:34 2021 +0100
+++ b/Discovery/Src/data_central.c	Fri Aug 19 11:30:24 2022 +0200
@@ -71,6 +71,7 @@
 #include "ostc.h" // for button adjust on hw testboard 1
 #include "tCCR.h"
 #include "crcmodel.h"
+#include "configuration.h"
 
 static SDiveState stateReal = { 0 };
 SDiveState stateSim = { 0 };
@@ -316,21 +317,29 @@
 
 void createDiveSettings(void)
 {
+	int i;
 	SSettings* pSettings = settingsGetPointer();
 
-	setActualGasFirst(&stateReal.lifeData);
-
 	stateReal.diveSettings.compassHeading = pSettings->compassBearing;
 	stateReal.diveSettings.ascentRate_meterperminute = 10;
 
 	stateReal.diveSettings.diveMode = pSettings->dive_mode;
 	stateReal.diveSettings.CCR_Mode = pSettings->CCR_Mode;
-	if(stateReal.diveSettings.diveMode == DIVEMODE_CCR)
+	if((stateReal.diveSettings.diveMode == DIVEMODE_PSCR) && (stateReal.diveSettings.CCR_Mode == CCRMODE_FixedSetpoint))
+	{
+		/* TODO: update selection of sensor used on/off (currently sensor/fixpoint). As PSCR has no fixed setpoint change to simulated ppo2 if sensors are not active */
+		stateReal.diveSettings.CCR_Mode = CCRMODE_Simulation;
+	}
+
+	if(isLoopMode(stateReal.diveSettings.diveMode))
 		stateReal.diveSettings.ccrOption = 1;
 	else
 		stateReal.diveSettings.ccrOption = 0;
 	memcpy(stateReal.diveSettings.gas, pSettings->gas,sizeof(pSettings->gas));
 	memcpy(stateReal.diveSettings.setpoint, pSettings->setpoint,sizeof(pSettings->setpoint));
+
+	setActualGasFirst(&stateReal.lifeData);
+
 	stateReal.diveSettings.gf_high = pSettings->GF_high;
 	stateReal.diveSettings.gf_low = pSettings->GF_low;
 	stateReal.diveSettings.input_next_stop_increment_depth_bar = ((float)pSettings->stop_increment_depth_meter) / 10.0f;
@@ -341,13 +350,24 @@
 	stateReal.diveSettings.ppo2sensors_deactivated = pSettings->ppo2sensors_deactivated;
 	stateReal.diveSettings.future_TTS_minutes = pSettings->future_TTS;
 	
+	stateReal.diveSettings.pscr_lung_ratio = pSettings->pscr_lung_ratio;
+	stateReal.diveSettings.pscr_o2_drop = pSettings->pscr_o2_drop;
+
+	if(stateReal.diveSettings.diveMode == DIVEMODE_PSCR)
+	{
+		for(i=0; i<5; i++)
+		{
+			stateReal.diveSettings.decogaslist[i].pscr_factor = 1.0 / stateReal.diveSettings.pscr_lung_ratio * stateReal.diveSettings.pscr_o2_drop;
+		}
+	}
+
 	decom_CreateGasChangeList(&stateReal.diveSettings, &stateReal.lifeData); // decogaslist
 	stateReal.diveSettings.internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero = 0;
 
 	/* for safety */
 	stateReal.diveSettings.input_second_to_last_stop_depth_bar = stateReal.diveSettings.last_stop_depth_bar + stateReal.diveSettings.input_next_stop_increment_depth_bar;
 	/* and the proper calc */
-	for(int i = 1; i <10; i++)
+	for(i = 1; i <10; i++)
 	{
 		if(stateReal.diveSettings.input_next_stop_increment_depth_bar * i > stateReal.diveSettings.last_stop_depth_bar)
 		{
@@ -389,9 +409,11 @@
 }
 
 
+
+
 void updateSetpointStateUsed(void)
 {
-	if(stateUsed->diveSettings.diveMode != DIVEMODE_CCR)
+	if(!isLoopMode(stateUsed->diveSettings.diveMode))
 	{
 		stateUsedWrite->lifeData.actualGas.setPoint_cbar = 0;
 		stateUsedWrite->lifeData.ppO2 = decom_calc_ppO2(stateUsed->lifeData.pressure_ambient_bar, &stateUsed->lifeData.actualGas);
@@ -402,7 +424,17 @@
 		{
 			stateUsedWrite->lifeData.actualGas.setPoint_cbar = get_ppO2SensorWeightedResult_cbar();
 		}
-
+#ifdef ENABLE_PSCR_MODE
+		if(stateUsed->diveSettings.diveMode == DIVEMODE_PSCR)	/* calculate a ppO2 value based on assumptions ( transfered approach from hwos code) */
+		{
+			stateUsedWrite->lifeData.ppo2Simulated_bar = decom_calc_SimppO2_O2based(stateUsed->lifeData.pressure_ambient_bar, stateReal.diveSettings.gas[stateUsed->lifeData.actualGas.GasIdInSettings].oxygen_percentage, stateUsed->lifeData.actualGas.pscr_factor);
+			if(stateUsed->diveSettings.CCR_Mode == CCRMODE_Simulation)
+			{
+				stateUsedWrite->lifeData.actualGas.setPoint_cbar = stateUsedWrite->lifeData.ppo2Simulated_bar * 100;
+			}
+		}
+#endif
+	/* limit calculated value to the physically possible if needed */
 		if((stateUsed->lifeData.pressure_ambient_bar * 100) < stateUsed->lifeData.actualGas.setPoint_cbar)
 			stateUsedWrite->lifeData.ppO2 = stateUsed->lifeData.pressure_ambient_bar;
 		else
@@ -417,7 +449,7 @@
 	uint8_t gasId = 0;
 	uint8_t setpoint_cbar = 0;
 
-	if(pSettings->dive_mode == DIVEMODE_CCR)
+	if(isLoopMode(pSettings->dive_mode))
 	{
 		setpoint_cbar = pSettings->setpoint[1].setpoint_cbar;
 		start = NUM_OFFSET_DILUENT+1;
@@ -449,6 +481,7 @@
 	lifeData->actualGas.helium_percentage =0;
 	lifeData->actualGas.setPoint_cbar = 0;
 	lifeData->actualGas.change_during_ascent_depth_meter_otherwise_zero = 0;
+	lifeData->actualGas.AppliedDiveMode = stateUsed->diveSettings.diveMode;
 }
 
 
@@ -466,8 +499,9 @@
 	lifeData->actualGas.helium_percentage = pSettings->gas[gasId].helium_percentage;
 	lifeData->actualGas.setPoint_cbar = setpoint_cbar;
 	lifeData->actualGas.change_during_ascent_depth_meter_otherwise_zero = 0;
-	
-	if((pSettings->dive_mode == DIVEMODE_CCR) && (gasId > NUM_OFFSET_DILUENT))
+	lifeData->actualGas.AppliedDiveMode = stateUsed->diveSettings.diveMode;
+	lifeData->actualGas.pscr_factor = 1.0 / pSettings->pscr_lung_ratio * pSettings->pscr_o2_drop;
+	if(isLoopMode(pSettings->dive_mode) && (gasId > NUM_OFFSET_DILUENT))
 		lifeData->lastDiluent_GasIdInSettings = gasId;
 }
 
@@ -525,7 +559,7 @@
   lifeData->actualGas.helium_percentage = helium;
   lifeData->actualGas.setPoint_cbar = setpoint_cbar;
   lifeData->actualGas.change_during_ascent_depth_meter_otherwise_zero = 0;
-
+  lifeData->actualGas.AppliedDiveMode = stateUsed->diveSettings.diveMode;
 }
 
 void setButtonResponsiveness(uint8_t *ButtonSensitivyList)
@@ -814,3 +848,12 @@
 	return compass_compensated;
 }
 
+uint8_t isLoopMode(uint8_t Mode)
+{
+	uint8_t retVal = 0;
+	if((Mode == DIVEMODE_CCR) || (Mode == DIVEMODE_PSCR))
+	{
+		retVal = 1;
+	}
+	return retVal;
+}