1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-11-30 08:24:11 +01:00

Optimize FlightMode switch position calculation

This commit is contained in:
Oleg Semyonov 2012-06-25 00:30:42 +03:00
parent cd6387d0a6
commit 02fa6fde09
2 changed files with 13 additions and 20 deletions

View File

@ -862,17 +862,12 @@ static void processFlightMode(ManualControlSettingsData *settings, float flightM
return;
}
// Scale flightMode from [-1..+1] to [0..1] range and calculate a delta
float scaledFlightMode = (flightMode + 1.0) / 2.0;
float delta = 1.0 / (float)(settings->FlightModeNumber);
float bound = delta;
// Convert flightMode value into the switch position in the range [0..N-1]
uint8_t pos = (uint8_t)((flightMode + 1.0f) * settings->FlightModeNumber) >> 1;
if (pos >= settings->FlightModeNumber)
pos = settings->FlightModeNumber - 1;
uint8_t i;
for (i = 1; i < settings->FlightModeNumber; i++, bound += delta) {
if (scaledFlightMode < bound)
break;
}
uint8_t newMode = settings->FlightModePosition[i - 1];
uint8_t newMode = settings->FlightModePosition[pos];
if (flightStatus.FlightMode != newMode) {
flightStatus.FlightMode = newMode;

View File

@ -1148,22 +1148,20 @@ void ConfigInputWidget::moveFMSlider()
// Bound and scale FlightMode from [-1..+1] to [0..1] range
if (valueScaled < -1.0)
valueScaled = -1.0;
else
if (valueScaled > 1.0)
valueScaled = 1.0;
float scaledFlightMode = (valueScaled + 1.0) / 2.0;
// Display current channel value for tuning (in percents)
float scaledFlightMode = (valueScaled + 1.0) / 2.0;
m_config->fmsValue->setText(QString::number(scaledFlightMode * 100.0, 'f', 0));
// Find and display the current FlightMode using the same logic as in a flight code
float delta = 1.0 / (float)(manualSettingsDataPriv.FlightModeNumber);
float bound = delta;
int i;
for (i = 1; i < manualSettingsDataPriv.FlightModeNumber; i++, bound += delta) {
if (scaledFlightMode < bound)
break;
}
m_config->fmsSlider->setValue(i - 1);
// 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;
if (pos >= manualSettingsDataPriv.FlightModeNumber)
pos = manualSettingsDataPriv.FlightModeNumber - 1;
m_config->fmsSlider->setValue(pos);
}
void ConfigInputWidget::updatePositionSlider()