From 727e67d7fde695c8c4befd2a14d3ac9d64362de9 Mon Sep 17 00:00:00 2001 From: Oleg Semyonov Date: Mon, 25 Jun 2012 11:41:42 +0300 Subject: [PATCH] Even more optimize FlightMode switch position calculation This runs in a high frequency loop and should use as little of floating point as possible. Thanks to Kenn for the idea. --- flight/Modules/ManualControl/manualcontrol.c | 2 +- ground/openpilotgcs/src/plugins/config/configinputwidget.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/flight/Modules/ManualControl/manualcontrol.c b/flight/Modules/ManualControl/manualcontrol.c index 911d96e7d..cad5d065d 100644 --- a/flight/Modules/ManualControl/manualcontrol.c +++ b/flight/Modules/ManualControl/manualcontrol.c @@ -860,7 +860,7 @@ static void processFlightMode(ManualControlSettingsData *settings, float flightM FlightStatusGet(&flightStatus); // Convert flightMode value into the switch position in the range [0..N-1] - uint8_t pos = (uint8_t)((flightMode + 1.0f) * settings->FlightModeNumber) >> 1; + uint8_t pos = ((int16_t)(flightMode * 256.0f) + 256) * settings->FlightModeNumber >> 9; if (pos >= settings->FlightModeNumber) pos = settings->FlightModeNumber - 1; diff --git a/ground/openpilotgcs/src/plugins/config/configinputwidget.cpp b/ground/openpilotgcs/src/plugins/config/configinputwidget.cpp index 550e55129..75c390643 100644 --- a/ground/openpilotgcs/src/plugins/config/configinputwidget.cpp +++ b/ground/openpilotgcs/src/plugins/config/configinputwidget.cpp @@ -1158,8 +1158,8 @@ void ConfigInputWidget::moveFMSlider() m_config->fmsValue->setText(QString::number(scaledFlightMode * 100.0, 'f', 0)); // Convert flightMode value into the switch position in the range [0..N-1] - // This should use the same logic as flight code to be consistent - uint8_t pos = (uint8_t)((valueScaled + 1.0) * manualSettingsDataPriv.FlightModeNumber) >> 1; + // This uses the same optimized computation as flight code to be consistent + uint8_t pos = ((int16_t)(valueScaled * 256) + 256) * manualSettingsDataPriv.FlightModeNumber >> 9; if (pos >= manualSettingsDataPriv.FlightModeNumber) pos = manualSettingsDataPriv.FlightModeNumber - 1; m_config->fmsSlider->setValue(pos);