diff Small_CPU/Src/pressure.c @ 482:230aed360da0

Merged in Ideenmodellierer/ostc4/Improve_Button_Sleep (pull request #45) Improve Button Sleep
author heinrichsweikamp <bitbucket@heinrichsweikamp.com>
date Tue, 19 May 2020 07:27:18 +0000
parents 3fe9cc747c5c
children 9eeab3fead8f
line wrap: on
line diff
--- a/Small_CPU/Src/pressure.c	Tue Apr 28 15:37:47 2020 +0000
+++ b/Small_CPU/Src/pressure.c	Tue May 19 07:27:18 2020 +0000
@@ -50,7 +50,6 @@
 /* #define SIMULATE_PRESSURE */
 
 #define PRESSURE_SURFACE_MAX_MBAR			(1030.0f)		/* It is unlikely that pressure at surface is greater than this value => clip to it */
-#define PRESSURE_HISTORY_SIZE				(8u)
 
 #define PRESSURE_SURFACE_QUE					(30u)			/* history buffer [minutes] for past pressure measurements */
 #define PRESSURE_SURFACE_EVA_WINDOW				(15u)			/* Number of entries evaluated during instability test. Used to avoid detection while dive enters water */
@@ -100,8 +99,6 @@
 static float surface_pressure_stable_value = 0;
 static uint8_t surface_pressure_stable = 0;
 
-static float pressure_history_mbar[PRESSURE_HISTORY_SIZE];
-
 static uint8_t secondCounterSurfaceRing = 0;
 static uint8_t avgCount = 0;
 static float runningAvg = 0;
@@ -137,14 +134,6 @@
 	}
 }
 
-void init_pressure_history(void)
-{
-	for(int i=0; i<PRESSURE_HISTORY_SIZE; i++)
-	{
-		pressure_history_mbar[i] = 1000.0;
-	}
-}
-
 uint8_t is_surface_pressure_stable(void)
 {
 	return surface_pressure_stable;
@@ -220,8 +209,6 @@
 }
 void update_surface_pressure(uint8_t call_rhythm_seconds)
 {
-
-
 	if(is_init_pressure_done())
 	{
 		runningAvg = (runningAvg * avgCount + ambient_pressure_mbar) / (avgCount +1);
@@ -343,7 +330,6 @@
 	uint8_t retValue = 0xFF;
 	
 	pressureSensorInitSuccess = false;
-	init_pressure_history();
 
 /* Probe new sensor first */
 	retValue = I2C_Master_Transmit(  DEVICE_PRESSURE_MS5837, buffer, 1);
@@ -391,7 +377,6 @@
 	{
 		pressureSensorInitSuccess = 1;
 		retValue = pressure_update();
-
 	}
 	return retValue;
 }
@@ -401,14 +386,17 @@
 {
 	uint8_t buffer[1];
 	uint8_t resivebuf[4];
-	uint32_t answer = 0;
+	uint32_t answer = 0xFFFFFFFF;
 
 	buffer[0] = 0x00; // Get ADC
-	I2C_Master_Transmit( PRESSURE_ADDRESS, buffer, 1);
-	I2C_Master_Receive(  PRESSURE_ADDRESS, resivebuf, 4);
-	resivebuf[3] = 0;
-	answer = 256*256 *(uint32_t)resivebuf[0]  + 256 * (uint32_t)resivebuf[1] + (uint32_t)resivebuf[2];
-
+	if(I2C_Master_Transmit( PRESSURE_ADDRESS, buffer, 1) == HAL_OK)
+	{
+		if(I2C_Master_Receive(  PRESSURE_ADDRESS, resivebuf, 4) == HAL_OK)
+		{
+			resivebuf[3] = 0;
+			answer = 256*256 *(uint32_t)resivebuf[0]  + 256 * (uint32_t)resivebuf[1] + (uint32_t)resivebuf[2];
+		}
+	}
 	return answer;
 }
 
@@ -460,6 +448,7 @@
 static uint32_t pressure_sensor_get_one_value(uint8_t cmd, HAL_StatusTypeDef *statusReturn)
 {
 	uint8_t command = CMD_ADC_CONV + cmd;
+	uint32_t adcValue = 0;
 	HAL_StatusTypeDef statusReturnTemp = HAL_TIMEOUT;
 	
 	statusReturnTemp = I2C_Master_Transmit( PRESSURE_ADDRESS, &command, 1);
@@ -468,16 +457,26 @@
 	{
 		*statusReturn = statusReturnTemp;
 	}
-	
+
 	switch (cmd & 0x0f) // wait necessary conversion time
 	{
-	case CMD_ADC_256 : HAL_Delay(1); break;
-	case CMD_ADC_512 : HAL_Delay(3); break;
-	case CMD_ADC_1024: HAL_Delay(4); break;
-	case CMD_ADC_2048: HAL_Delay(6); break;
-	case CMD_ADC_4096: HAL_Delay(10); break;
-	}	
-	return get_adc();
+		case CMD_ADC_256 : HAL_Delay(1); break;
+		case CMD_ADC_512 : HAL_Delay(3); break;
+		case CMD_ADC_1024: HAL_Delay(4); break;
+		case CMD_ADC_2048: HAL_Delay(6); break;
+		case CMD_ADC_4096: HAL_Delay(10); break;
+		default:
+			break;
+	}
+	adcValue = get_adc();
+	if(adcValue == 0xFFFFFFFF)
+	{
+		if(statusReturn)
+		{
+			*statusReturn = HAL_ERROR;
+		}
+	}
+	return adcValue;
 }
 
 
@@ -606,39 +605,6 @@
 #endif
 }
 
-static uint8_t pressure_plausible(float pressurevalue)
-{
-	static uint8_t pressurewriteindex = 0;
-	uint8_t retval = 0;
-	uint8_t index;
-	float pressure_average = 0;
-
-	for(index = 0; index < PRESSURE_HISTORY_SIZE; index++)
-	{
-		pressure_average += pressure_history_mbar[index];
-	}
-	pressure_average /= PRESSURE_HISTORY_SIZE;
-	if(pressure_average == 1000.0) /* first pressure calculation */
-	{
-		if(fabs(pressurevalue - pressure_average) < 11000.0)  /* just in case a reset occur during dive assume value equal < 100m as valid */
-		{
-			for(index = 0; index < PRESSURE_HISTORY_SIZE; index++)
-			{
-				pressure_history_mbar[index] = pressurevalue;	/* set history to current value */
-				retval = 1;
-			}
-		}
-	}
-	else
-	{
-			pressure_history_mbar[pressurewriteindex++] = pressurevalue;
-			pressurewriteindex &= 0x7;	/* wrap around if necessary */
-			retval = 1;
-	}
-
-	return retval;
-}
-
 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void)
 {
 	static float runningAvg = 0;
@@ -712,15 +678,12 @@
 	calc_pressure = ((float)local_Px10) / 10;
 	calc_pressure += pressure_offset;
 
-	if(pressure_plausible(calc_pressure))
-	{
-		runningAvg = (avgCnt * runningAvg + calc_pressure) / (avgCnt + 1);
-		if (avgCnt < 10)	/* build an average considering the last measurements to have a weight "1 of 10" */
-		{					/* Main reason for this is the jitter of up to +-10 HPa in surface mode which is caused */
-			avgCnt++;		/* by the measurement range of the sensor which is focused on under water pressure measurement */
-		}
-		ambient_pressure_mbar = runningAvg;
+	runningAvg = (avgCnt * runningAvg + calc_pressure) / (avgCnt + 1);
+	if (avgCnt < 10)	/* build an average considering the last measurements to have a weight "1 of 10" */
+	{					/* Main reason for this is the jitter of up to +-10 HPa in surface mode which is caused */
+		avgCnt++;		/* by the measurement range of the sensor which is focused on under water pressure measurement */
 	}
+	ambient_pressure_mbar = runningAvg;
 }