changeset 339:37f45300bc2e PressureMeasure_Improvment

Apply averaging to pressure measurement: In pre versions calculated pressure value jittered +/-10hPa. Since we measure the pressure several time a second but only use one value a second, calc average including not used values Activated pressure / temperature offsets: The functionality to store offsets was already present in the firmware but values have not been apllied in the RTE => added functionality to include offsets in calculation Set Max possible surface pressure to 1070hPa (Black sea level)
author ideenmodellierer
date Sat, 17 Aug 2019 19:03:47 +0200
parents b6a59e93cc91
children 0756013e43b3
files Small_CPU/Inc/pressure.h Small_CPU/Src/baseCPU2.c Small_CPU/Src/pressure.c
diffstat 3 files changed, 50 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/Small_CPU/Inc/pressure.h	Sat Aug 17 18:57:15 2019 +0200
+++ b/Small_CPU/Inc/pressure.h	Sat Aug 17 19:03:47 2019 +0200
@@ -5,6 +5,8 @@
 #include <stdint.h>
 
 uint8_t init_pressure(void);
+void pressure_set_offset (float pressureOffset, float temperatureOffset);
+
 uint8_t pressure_update(void);
 void pressure_update_alternating(void);
 
@@ -18,7 +20,7 @@
 float get_pressure_mbar(void);
 float get_surface_mbar(void);
 
-void init_surface_ring(void);
+void init_surface_ring(uint8_t force);
 void update_surface_pressure(uint8_t call_rhythm_seconds);
 
 uint32_t demo_modify_temperature_and_pressure(int32_t divetime_in_seconds, uint8_t subseconds, float ceiling_mbar);
--- a/Small_CPU/Src/baseCPU2.c	Sat Aug 17 18:57:15 2019 +0200
+++ b/Small_CPU/Src/baseCPU2.c	Sat Aug 17 19:03:47 2019 +0200
@@ -334,7 +334,7 @@
 
 	if(is_init_pressure_done())
 	{
-		init_surface_ring();
+		init_surface_ring(0);
 	}
 	init_battery_gas_gauge();
 	HAL_Delay(10);
@@ -363,7 +363,7 @@
 	global.mode = MODE_POWERUP;
 #else
 	init_pressure();
-	init_surface_ring();
+	init_surface_ring(0);
 
 	ADCx_Init();
 	GPIO_Power_MainCPU_Init();
--- a/Small_CPU/Src/pressure.c	Sat Aug 17 18:57:15 2019 +0200
+++ b/Small_CPU/Src/pressure.c	Sat Aug 17 19:03:47 2019 +0200
@@ -46,6 +46,7 @@
 #define CMD_ADC_4096 0x08 // ADC OSR=4096
 #define CMD_PROM_RD 0xA0 // Prom read command
 
+#define PRESSURE_SURFACE_MAX_MBAR		(1070.0f)		/* It is very unlikely that pressure at surface is greater than this value => clip to it */
 #define PRESSURE_HISTORY_SIZE			(8u)
 #define PRESSURE_JUMP_VALID_MBAR	    (500.0f)		/* values are measure several times a second => jumps > 5m very unlikely */
 
@@ -77,6 +78,8 @@
 short UT1 = -1;
 short C6plus100 = -1;
 */
+static float pressure_offset = 0.0;		/* Offset value which may be specified by the user via PC Software */
+static float temperature_offset = 0.0;	/* Offset value which may be specified by the user via PC Software */
 
 static float ambient_temperature = 0;
 static float ambient_pressure_mbar = 1000.0;
@@ -85,7 +88,9 @@
 
 static float pressure_history_mbar[PRESSURE_HISTORY_SIZE];
 
-uint8_t secondCounterSurfaceRing = 0;
+static uint8_t secondCounterSurfaceRing = 0;
+static uint8_t avgCount = 0;
+static float runningAvg = 0;
 
 float get_temperature(void)
 {
@@ -103,10 +108,14 @@
 }
 
 
-void init_surface_ring(void)
+void init_surface_ring(uint8_t force)
 {
-	if(surface_ring_mbar[0] == 0)					/* only initialize once. Keep value in place in case of an i2c recovery */
+	if((surface_ring_mbar[0] == 0) || (force))		/* only initialize once. Keep value in place in case of an i2c recovery */
 	{
+		secondCounterSurfaceRing = 0;				/* restart calculation */
+		avgCount = 0;
+		runningAvg = 0;
+
 		for(int i=0; i<PRESSURE_SURFACE_QUE; i++)
 			surface_ring_mbar[i] = ambient_pressure_mbar;
 		surface_pressure_mbar = ambient_pressure_mbar;
@@ -125,8 +134,7 @@
 void update_surface_pressure(uint8_t call_rhythm_seconds)
 {
 	static uint8_t writeIndex = 0;		/* Reinitialization will reset all entries to the same value => no need to reinit write index */
-	static uint8_t avgCount = 0;
-	static float runningAvg = 0;
+
 
 	if(is_init_pressure_done())
 	{
@@ -136,7 +144,14 @@
 
 		if(secondCounterSurfaceRing >= 60)
 		{
-			surface_ring_mbar[writeIndex] = runningAvg;
+			if(runningAvg < PRESSURE_SURFACE_MAX_MBAR)
+			{
+				surface_ring_mbar[writeIndex] = runningAvg;
+			}
+			else
+			{
+				surface_ring_mbar[writeIndex] =	PRESSURE_SURFACE_MAX_MBAR;
+			}
 			writeIndex++; /* the write index is now pointing to the oldest value in the buffer which will be overwritten next time */
 
 			if(writeIndex == PRESSURE_SURFACE_QUE)
@@ -432,7 +447,7 @@
 	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 occure during dive assume value equal < 100m as valid */
+		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++)
 			{
@@ -444,9 +459,11 @@
 	else
 	{
 		if(fabs(pressurevalue - pressure_average) < PRESSURE_JUMP_VALID_MBAR)
-		pressure_history_mbar[pressurewriteindex++] = pressurevalue;
-		pressurewriteindex &= 0x7;	/* wrap around if necessary */
-		retval = 1;
+		{
+			pressure_history_mbar[pressurewriteindex++] = pressurevalue;
+			pressurewriteindex &= 0x7;	/* wrap around if necessary */
+			retval = 1;
+		}
 	}
 
 	return retval;
@@ -519,13 +536,16 @@
 							(((int64_t)((local_D1 * local_SENS) / 2097152)) - local_OFF)
 								/  8192 );//     )) / 10; // pow(2,21), pow(2,13)
 
-	ambient_temperature 	= ((float)local_Tx100) / 100;
+	ambient_temperature = ((float)local_Tx100) / 100;
+	ambient_temperature	+= temperature_offset;
 
 	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 a a weight "1 of 10" */
+		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 */
 		}
@@ -734,4 +754,17 @@
 		pressure_calculation() ;
 };
 */
+void pressure_set_offset (float pressureOffset, float temperatureOffset)
+{
+	if(pressure_offset != pressureOffset)				/* we received a new value => reinit surface que */
+	{
+		ambient_pressure_mbar -= pressure_offset;		/* revert old value */
+		ambient_pressure_mbar += pressureOffset;		/* apply new offset */
+		init_surface_ring(1);
+	}
 
+	pressure_offset = pressureOffset;
+	temperature_offset = temperatureOffset;
+}
+
+