changeset 867:3311b720a072 Evo_2_23

Decrease calculation interval for ascend speed: In the previous version the ascend speed was calculated every 2 seconds. To improve the visualization the interval has been reduced to 400ms. The average function of the depth calculation ensures a smooth dynamic transition of the values. For testing a simulation profile, which changes between several speeds, may be acivated using the compile switch.
author Ideenmodellierer
date Mon, 12 Aug 2024 14:30:22 +0200
parents 0c89c6fa949c
children db92692c014f
files Small_CPU/Src/pressure.c Small_CPU/Src/scheduler.c
diffstat 2 files changed, 44 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/Small_CPU/Src/pressure.c	Tue Jul 02 20:05:08 2024 +0200
+++ b/Small_CPU/Src/pressure.c	Mon Aug 12 14:30:22 2024 +0200
@@ -531,12 +531,17 @@
 
 
 #ifdef SIMULATE_PRESSURE
+
+#define SECDIV		10		/* update every 100ms */
+
 void pressure_simulation()
 {
 	static uint32_t tickstart = 0;
 	static float pressure_sim_mbar = 0;
 	static uint32_t passedSecond = 0;
 	static uint32_t secondtick = 0;
+	static uint32_t lastsecondtick = 0;
+	static float delta_mbar = 0.0;
 
 	uint32_t lasttick = 0;
 
@@ -550,11 +555,16 @@
 	}
 
 	lasttick = HAL_GetTick();
-	if(time_elapsed_ms(secondtick,lasttick) > 1000) /* one second passed since last tick */
+	if(time_elapsed_ms(secondtick,lasttick) >= (1000 / SECDIV)) /* one second passed since last tick */
 	{
+		if(time_elapsed_ms(lastsecondtick,lasttick) > 1000)
+		{
+			passedSecond++;
+			lastsecondtick = lasttick;
+		}
 		secondtick = lasttick;
-		passedSecond++;
 
+#define DIVE_AT_SPEED 1
 #ifdef DIVE_AFTER_LANDING
 		if(passedSecond < 10) pressure_sim_mbar = 1000.0;	 /* stay stable for 10 seconds */
 		else if(passedSecond < 300) pressure_sim_mbar -= 1.0; /* decrease pressure in 5 minutes target 770mbar => delta 330 */
@@ -565,6 +575,26 @@
 		else if(passedSecond < 2300) pressure_sim_mbar += 0.0;  /* stay on depth */
 		else if(passedSecond < 2500) pressure_sim_mbar -= 10.0; /* return to surface */
 		else pressure_sim_mbar = 1000.0;					/* final state */
+#endif
+#ifdef DIVE_AT_SPEED
+		if(passedSecond < 10) pressure_sim_mbar = 1000.0;	   /* stay stable for 10 seconds */
+		else if(passedSecond < 20) delta_mbar = 200.0 / SECDIV; /* Start dive */
+		else if(passedSecond < 30) delta_mbar = 0.0;	/*stay on depth*/
+		else if(passedSecond < 45) delta_mbar -= 0.2 / SECDIV;	/* return to surface */
+		else if(passedSecond < 40) delta_mbar -= 0.4 / SECDIV;   /* stay */
+		else if(passedSecond < 50) delta_mbar += 0.3 / SECDIV; /* get ready for second dive */
+		else if(passedSecond < 60) delta_mbar -= 0.4;	/*stay on depth*/
+		else if(passedSecond < 70) delta_mbar = 0.2;
+		else if(passedSecond < 1060) pressure_sim_mbar -= 10.0/ SECDIV;	/* return to surface */
+		else if(passedSecond < 1200) pressure_sim_mbar += 0.0;   /* stay */
+		else { pressure_sim_mbar = 1000.0;	delta_mbar = 0.0;}				/* final state */
+
+		pressure_sim_mbar += delta_mbar;
+		if(pressure_sim_mbar < surface_pressure_mbar)
+		{
+			pressure_sim_mbar = surface_pressure_mbar;
+		}
+
 #else	/* short dive */
 		if(passedSecond < 10) pressure_sim_mbar = 1000.0;	   /* stay stable for 10 seconds */
 		else if(passedSecond < 180) pressure_sim_mbar += 10.0; /* Start dive */
--- a/Small_CPU/Src/scheduler.c	Tue Jul 02 20:05:08 2024 +0200
+++ b/Small_CPU/Src/scheduler.c	Mon Aug 12 14:30:22 2024 +0200
@@ -480,6 +480,8 @@
 {
 	uint32_t ticksdiff = 0; 
 	uint32_t lasttick = 0;
+	uint32_t lastPressureTick = 0;
+	uint32_t tickPressureDiff = 0;
 	uint8_t extAdcChannel = 0;
 	uint8_t counterAscentRate = 0;
 	float lastPressure_bar = 0.0f;
@@ -552,18 +554,21 @@
 						global.lifeData.counterSecondsShallowDepth = (global.settings.timeoutDiveReachedZeroDepth - 10);
 				}
 #endif
-				
-				//Calc ascentrate every two second (20 * 100 ms)
+
 				counterAscentRate++;
-				if(counterAscentRate == 20)
+				if(counterAscentRate == 4)
 				{
-					global.lifeData.pressure_ambient_bar = get_pressure_mbar() / 1000.0f;
-					if(lastPressure_bar >= 0)
+					tickPressureDiff = time_elapsed_ms(lastPressureTick,lasttick); /* Calculate ascent rate every 400ms use timer to take care for small time shifts */
+					if(tickPressureDiff != 0)
 					{
-							//2 seconds * 30 == 1 minute, bar * 10 = meter
-							global.lifeData.ascent_rate_meter_per_min = (lastPressure_bar - global.lifeData.pressure_ambient_bar)  * 30 * 10;
+						global.lifeData.pressure_ambient_bar = get_pressure_mbar() / 1000.0f;
+						if(lastPressure_bar >= 0)
+						{
+							global.lifeData.ascent_rate_meter_per_min = (lastPressure_bar - global.lifeData.pressure_ambient_bar)  * (60000.0 / tickPressureDiff) * 10; /* bar * 10 = meter */
+						}
 					}
 					lastPressure_bar = global.lifeData.pressure_ambient_bar;
+					lastPressureTick = lasttick;
 					counterAscentRate = 0;
 				}
 				copyPressureData();