From a66d6a2fc780bceb463c1b50d46ecd85a8163963 Mon Sep 17 00:00:00 2001 From: Bruce Luckcuck Date: Mon, 22 Oct 2018 19:46:51 -0400 Subject: [PATCH] Improve OSD g-force display and avoid math when not needed Previously the g-force element was being display using integer math and truncating the float value. This lead to the g-force displaying 0.9 much of the time. Changed to use rounding to one decimal place. Also avoid the g-force calculation unless the element or post-flight statistic are enabled. This saves unnecessary math including a sqrt(). --- src/main/io/osd.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/io/osd.c b/src/main/io/osd.c index dfc41763df..20f6b4e3df 100644 --- a/src/main/io/osd.c +++ b/src/main/io/osd.c @@ -742,8 +742,11 @@ static bool osdDrawSingleElement(uint8_t item) } case OSD_G_FORCE: - tfp_sprintf(buff, "%01d.%01dG", (int)osdGForce, (int)(osdGForce * 10) % 10); - break; + { + const int gForce = lrintf(osdGForce * 10); + tfp_sprintf(buff, "%01d.%01dG", gForce / 10, gForce % 10); + break; + } case OSD_ROLL_PIDS: osdFormatPID(buff, "ROL", ¤tPidProfile->pid[PID_ROLL]); @@ -1077,13 +1080,16 @@ static void osdDrawElements(void) return; } + osdGForce = 0.0f; if (sensors(SENSOR_ACC)) { - osdGForce = 0.0f; - for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) { - const float a = accAverage[axis]; - osdGForce += a * a; + // only calculate the G force if the element is visible or the stat is enabled + if (VISIBLE(osdConfig()->item_pos[OSD_G_FORCE]) || osdStatGetState(OSD_STAT_MAX_G_FORCE)) { + for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) { + const float a = accAverage[axis]; + osdGForce += a * a; + } + osdGForce = sqrtf(osdGForce) * acc.dev.acc_1G_rec; } - osdGForce = sqrtf(osdGForce) * acc.dev.acc_1G_rec; osdDrawSingleElement(OSD_ARTIFICIAL_HORIZON); osdDrawSingleElement(OSD_G_FORCE); } @@ -1551,8 +1557,9 @@ static void osdShowStats(uint16_t endBatteryVoltage) } #endif - if (osdStatGetState(OSD_STAT_MAX_G_FORCE)) { - tfp_sprintf(buff, "%01d.%01dG", (int)stats.max_g_force, (int)(stats.max_g_force * 10) % 10); + if (osdStatGetState(OSD_STAT_MAX_G_FORCE) && sensors(SENSOR_ACC)) { + const int gForce = lrintf(stats.max_g_force * 10); + tfp_sprintf(buff, "%01d.%01dG", gForce / 10, gForce % 10); osdDisplayStatisticLabel(top++, "MAX G-FORCE", buff); }