diff Discovery/Src/data_central.c @ 539:d784f281833a

Added inertia simulation for compass heading: In previous version calculated compass values were directly used for visualization of the compass. This causes a fast changing (jumping) of values. With the inertia introduction the compass behalfs more like an analog compass. The final value is reached slowly and the displayed values are more stable. For configuration a new menu item has been added to the compass menu allowing to switch inertia off (default), small and large inertia simulation
author Ideenmodellierer
date Sat, 10 Oct 2020 16:59:18 +0200
parents 95928ef3986f
children 1b995079c045
line wrap: on
line diff
--- a/Discovery/Src/data_central.c	Sat Oct 10 13:51:44 2020 +0200
+++ b/Discovery/Src/data_central.c	Sat Oct 10 16:59:18 2020 +0200
@@ -61,6 +61,7 @@
 
 /* Includes ------------------------------------------------------------------*/
 #include <string.h>
+#include <math.h>
 #include "data_central.h"
 #include "calc_crush.h"
 #include "decom.h"
@@ -103,6 +104,11 @@
 const SDiveState *stateUsed = &stateReal;
 SDiveState *stateUsedWrite = &stateReal;
 
+
+#define COMPASS_FRACTION		(4.0f)		/* delay till value changes to new actual */
+
+static float compass_compensated = 0;
+
 void set_stateUsedToReal(void)
 {
 	stateUsed = stateUsedWrite = &stateReal;
@@ -770,3 +776,41 @@
 	else
 		return false;
 }
+
+void compass_Inertia(float newHeading)
+{
+	float newTarget = newHeading;
+
+	if(settingsGetPointer()->compassInertia == 0)
+	{
+		compass_compensated = newHeading;
+	}
+	else
+	{
+		if((compass_compensated > 270.0) && (newHeading < 90.0))		/* transition passing 0 clockwise */
+		{
+			newTarget = newHeading + 360.0;
+		}
+
+		if((compass_compensated < 90.0) && (newHeading > 270.0))		/* transition passing 0 counter clockwise */
+		{
+			newTarget = newHeading - 360.0;
+		}
+
+		compass_compensated = compass_compensated + ((newTarget - compass_compensated) / (COMPASS_FRACTION * (settingsGetPointer()->compassInertia)));
+		if(compass_compensated < 0.0)
+		{
+			compass_compensated += 360.0;
+		}
+		if(compass_compensated >= 360.0)
+		{
+			compass_compensated -= 360.0;
+		}
+	}
+}
+
+float compass_getCompensated()
+{
+	return compass_compensated;
+}
+