mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-18 22:05:17 +03:00
Replace old lowpass with BiQuad for Vbat and Servo
This commit is contained in:
parent
eebacc2542
commit
27772ba56a
7 changed files with 23 additions and 170 deletions
1
Makefile
1
Makefile
|
@ -291,7 +291,6 @@ COMMON_SRC = build_config.c \
|
||||||
flight/pid.c \
|
flight/pid.c \
|
||||||
flight/imu.c \
|
flight/imu.c \
|
||||||
flight/mixer.c \
|
flight/mixer.c \
|
||||||
flight/lowpass.c \
|
|
||||||
drivers/bus_i2c_soft.c \
|
drivers/bus_i2c_soft.c \
|
||||||
drivers/serial.c \
|
drivers/serial.c \
|
||||||
drivers/sound_beeper.c \
|
drivers/sound_beeper.c \
|
||||||
|
|
|
@ -334,7 +334,7 @@ void resetMixerConfig(mixerConfig_t *mixerConfig) {
|
||||||
mixerConfig->yaw_jump_prevention_limit = 200;
|
mixerConfig->yaw_jump_prevention_limit = 200;
|
||||||
#ifdef USE_SERVOS
|
#ifdef USE_SERVOS
|
||||||
mixerConfig->tri_unarmed_servo = 1;
|
mixerConfig->tri_unarmed_servo = 1;
|
||||||
mixerConfig->servo_lowpass_freq = 400;
|
mixerConfig->servo_lowpass_freq = 400.0f;
|
||||||
mixerConfig->servo_lowpass_enable = 0;
|
mixerConfig->servo_lowpass_enable = 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,118 +0,0 @@
|
||||||
/*
|
|
||||||
* This file is part of Cleanflight.
|
|
||||||
*
|
|
||||||
* Cleanflight is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* Cleanflight is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
#include "common/maths.h"
|
|
||||||
#include "flight/lowpass.h"
|
|
||||||
|
|
||||||
//#define DEBUG_LOWPASS
|
|
||||||
|
|
||||||
void generateLowpassCoeffs2(int16_t freq, lowpass_t *filter)
|
|
||||||
{
|
|
||||||
float fixedScaler;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
// generates coefficients for a 2nd-order butterworth low-pass filter
|
|
||||||
float freqf = (float)freq*0.001f;
|
|
||||||
float omega = tan_approx((float)M_PI*freqf/2.0f);
|
|
||||||
float scaling = 1.0f / (omega*omega + 1.4142136f*omega + 1.0f);
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(UNIT_TEST) && defined(DEBUG_LOWPASS)
|
|
||||||
printf("lowpass cutoff: %f, omega: %f\n", freqf, omega);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
filter->bf[0] = scaling * omega*omega;
|
|
||||||
filter->bf[1] = 2.0f * filter->bf[0];
|
|
||||||
filter->bf[2] = filter->bf[0];
|
|
||||||
|
|
||||||
filter->af[0] = 1.0f;
|
|
||||||
filter->af[1] = scaling * (2.0f * omega*omega - 2.0f);
|
|
||||||
filter->af[2] = scaling * (omega*omega - 1.4142136f * omega + 1.0f);
|
|
||||||
|
|
||||||
// Scale for fixed-point
|
|
||||||
filter->input_bias = 1500; // Typical servo range is 1500 +/- 500
|
|
||||||
filter->input_shift = 16;
|
|
||||||
filter->coeff_shift = 24;
|
|
||||||
fixedScaler = (float)(1ULL << filter->coeff_shift);
|
|
||||||
for (i = 0; i < LOWPASS_NUM_COEF; i++) {
|
|
||||||
filter->a[i] = LPF_ROUND(filter->af[i] * fixedScaler);
|
|
||||||
filter->b[i] = LPF_ROUND(filter->bf[i] * fixedScaler);
|
|
||||||
#if defined(UNIT_TEST) && defined(DEBUG_LOWPASS)
|
|
||||||
printf("(%d) bf: %f af: %f b: %ld a: %ld\n", i,
|
|
||||||
filter->bf[i], filter->af[i], filter->b[i], filter->a[i]);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
filter->freq = freq;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t lowpassFixed(lowpass_t *filter, int32_t in, int16_t freq)
|
|
||||||
{
|
|
||||||
int16_t coefIdx;
|
|
||||||
int64_t out;
|
|
||||||
int32_t in_s;
|
|
||||||
|
|
||||||
// Check to see if cutoff frequency changed
|
|
||||||
if (freq != filter->freq) {
|
|
||||||
filter->init = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize if needed
|
|
||||||
if (!filter->init) {
|
|
||||||
generateLowpassCoeffs2(freq, filter);
|
|
||||||
for (coefIdx = 0; coefIdx < LOWPASS_NUM_COEF; coefIdx++) {
|
|
||||||
filter->x[coefIdx] = (in - filter->input_bias) << filter->input_shift;
|
|
||||||
filter->y[coefIdx] = (in - filter->input_bias) << filter->input_shift;
|
|
||||||
}
|
|
||||||
filter->init = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unbias input and scale
|
|
||||||
in_s = (in - filter->input_bias) << filter->input_shift;
|
|
||||||
|
|
||||||
// Delays
|
|
||||||
for (coefIdx = LOWPASS_NUM_COEF-1; coefIdx > 0; coefIdx--) {
|
|
||||||
filter->x[coefIdx] = filter->x[coefIdx-1];
|
|
||||||
filter->y[coefIdx] = filter->y[coefIdx-1];
|
|
||||||
}
|
|
||||||
filter->x[0] = in_s;
|
|
||||||
|
|
||||||
// Accumulate result
|
|
||||||
out = filter->x[0] * filter->b[0];
|
|
||||||
for (coefIdx = 1; coefIdx < LOWPASS_NUM_COEF; coefIdx++) {
|
|
||||||
out -= filter->y[coefIdx] * filter->a[coefIdx];
|
|
||||||
out += filter->x[coefIdx] * filter->b[coefIdx];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Scale output by coefficient shift
|
|
||||||
out >>= filter->coeff_shift;
|
|
||||||
filter->y[0] = (int32_t)out;
|
|
||||||
|
|
||||||
// Scale output by input shift and round
|
|
||||||
out = (out + (1 << (filter->input_shift-1))) >> filter->input_shift;
|
|
||||||
|
|
||||||
// Reapply bias
|
|
||||||
out += filter->input_bias;
|
|
||||||
|
|
||||||
return (int32_t)out;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,41 +0,0 @@
|
||||||
/*
|
|
||||||
* This file is part of Cleanflight.
|
|
||||||
*
|
|
||||||
* Cleanflight is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* Cleanflight is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
|
|
||||||
#define LOWPASS_NUM_COEF 3
|
|
||||||
#define LPF_ROUND(x) (x < 0 ? (x - 0.5f) : (x + 0.5f))
|
|
||||||
|
|
||||||
typedef struct lowpass_s {
|
|
||||||
bool init;
|
|
||||||
int16_t freq; // Normalized freq in 1/1000ths
|
|
||||||
float bf[LOWPASS_NUM_COEF];
|
|
||||||
float af[LOWPASS_NUM_COEF];
|
|
||||||
int64_t b[LOWPASS_NUM_COEF];
|
|
||||||
int64_t a[LOWPASS_NUM_COEF];
|
|
||||||
int16_t coeff_shift;
|
|
||||||
int16_t input_shift;
|
|
||||||
int32_t input_bias;
|
|
||||||
float xf[LOWPASS_NUM_COEF];
|
|
||||||
float yf[LOWPASS_NUM_COEF];
|
|
||||||
int32_t x[LOWPASS_NUM_COEF];
|
|
||||||
int32_t y[LOWPASS_NUM_COEF];
|
|
||||||
} lowpass_t;
|
|
||||||
|
|
||||||
void generateLowpassCoeffs2(int16_t freq, lowpass_t *filter);
|
|
||||||
int32_t lowpassFixed(lowpass_t *filter, int32_t in, int16_t freq);
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
@ -27,6 +28,7 @@
|
||||||
|
|
||||||
#include "common/axis.h"
|
#include "common/axis.h"
|
||||||
#include "common/maths.h"
|
#include "common/maths.h"
|
||||||
|
#include "common/filter.h"
|
||||||
|
|
||||||
#include "drivers/system.h"
|
#include "drivers/system.h"
|
||||||
#include "drivers/pwm_output.h"
|
#include "drivers/pwm_output.h"
|
||||||
|
@ -34,6 +36,7 @@
|
||||||
#include "drivers/sensor.h"
|
#include "drivers/sensor.h"
|
||||||
#include "drivers/accgyro.h"
|
#include "drivers/accgyro.h"
|
||||||
#include "drivers/system.h"
|
#include "drivers/system.h"
|
||||||
|
#include "drivers/gyro_sync.h"
|
||||||
|
|
||||||
#include "rx/rx.h"
|
#include "rx/rx.h"
|
||||||
|
|
||||||
|
@ -48,7 +51,6 @@
|
||||||
#include "flight/failsafe.h"
|
#include "flight/failsafe.h"
|
||||||
#include "flight/pid.h"
|
#include "flight/pid.h"
|
||||||
#include "flight/imu.h"
|
#include "flight/imu.h"
|
||||||
#include "flight/lowpass.h"
|
|
||||||
|
|
||||||
#include "config/runtime_config.h"
|
#include "config/runtime_config.h"
|
||||||
#include "config/config.h"
|
#include "config/config.h"
|
||||||
|
@ -77,7 +79,6 @@ int16_t servo[MAX_SUPPORTED_SERVOS];
|
||||||
static int useServo;
|
static int useServo;
|
||||||
STATIC_UNIT_TESTED uint8_t servoCount;
|
STATIC_UNIT_TESTED uint8_t servoCount;
|
||||||
static servoParam_t *servoConf;
|
static servoParam_t *servoConf;
|
||||||
static lowpass_t lowpassFilters[MAX_SUPPORTED_SERVOS];
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static const motorMixer_t mixerQuadX[] = {
|
static const motorMixer_t mixerQuadX[] = {
|
||||||
|
@ -924,6 +925,8 @@ void filterServos(void)
|
||||||
{
|
{
|
||||||
#ifdef USE_SERVOS
|
#ifdef USE_SERVOS
|
||||||
int16_t servoIdx;
|
int16_t servoIdx;
|
||||||
|
static bool servoFilterIsSet;
|
||||||
|
biquad_t servoFitlerState[MAX_SUPPORTED_SERVOS];
|
||||||
|
|
||||||
#if defined(MIXER_DEBUG)
|
#if defined(MIXER_DEBUG)
|
||||||
uint32_t startTime = micros();
|
uint32_t startTime = micros();
|
||||||
|
@ -931,7 +934,11 @@ void filterServos(void)
|
||||||
|
|
||||||
if (mixerConfig->servo_lowpass_enable) {
|
if (mixerConfig->servo_lowpass_enable) {
|
||||||
for (servoIdx = 0; servoIdx < MAX_SUPPORTED_SERVOS; servoIdx++) {
|
for (servoIdx = 0; servoIdx < MAX_SUPPORTED_SERVOS; servoIdx++) {
|
||||||
servo[servoIdx] = (int16_t)lowpassFixed(&lowpassFilters[servoIdx], servo[servoIdx], mixerConfig->servo_lowpass_freq);
|
if (!servoFilterIsSet) {
|
||||||
|
BiQuadNewLpf(mixerConfig->servo_lowpass_freq, &servoFitlerState[servoIdx], targetLooptime);
|
||||||
|
servoFilterIsSet = true;
|
||||||
|
}
|
||||||
|
servo[servoIdx] = lrintf(applyBiQuadFilter((float) servo[servoIdx], &servoFitlerState[servoIdx]));
|
||||||
|
|
||||||
// Sanity check
|
// Sanity check
|
||||||
servo[servoIdx] = constrain(servo[servoIdx], servoConf[servoIdx].min, servoConf[servoIdx].max);
|
servo[servoIdx] = constrain(servo[servoIdx], servoConf[servoIdx].min, servoConf[servoIdx].max);
|
||||||
|
|
|
@ -620,7 +620,7 @@ const clivalue_t valueTable[] = {
|
||||||
{ "yaw_jump_prevention_limit", VAR_UINT16 | MASTER_VALUE, &masterConfig.mixerConfig.yaw_jump_prevention_limit, .config.minmax = { YAW_JUMP_PREVENTION_LIMIT_LOW, YAW_JUMP_PREVENTION_LIMIT_HIGH } },
|
{ "yaw_jump_prevention_limit", VAR_UINT16 | MASTER_VALUE, &masterConfig.mixerConfig.yaw_jump_prevention_limit, .config.minmax = { YAW_JUMP_PREVENTION_LIMIT_LOW, YAW_JUMP_PREVENTION_LIMIT_HIGH } },
|
||||||
#ifdef USE_SERVOS
|
#ifdef USE_SERVOS
|
||||||
{ "tri_unarmed_servo", VAR_INT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.mixerConfig.tri_unarmed_servo, .config.lookup = { TABLE_OFF_ON } },
|
{ "tri_unarmed_servo", VAR_INT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.mixerConfig.tri_unarmed_servo, .config.lookup = { TABLE_OFF_ON } },
|
||||||
{ "servo_lowpass_freq", VAR_INT16 | MASTER_VALUE, &masterConfig.mixerConfig.servo_lowpass_freq, .config.minmax = { 10, 400} },
|
{ "servo_lowpass_freq", VAR_FLOAT | MASTER_VALUE, &masterConfig.mixerConfig.servo_lowpass_freq, .config.minmax = { 10, 400} },
|
||||||
{ "servo_lowpass_enable", VAR_INT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.mixerConfig.servo_lowpass_enable, .config.lookup = { TABLE_OFF_ON } },
|
{ "servo_lowpass_enable", VAR_INT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.mixerConfig.servo_lowpass_enable, .config.lookup = { TABLE_OFF_ON } },
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -17,10 +17,12 @@
|
||||||
|
|
||||||
#include "stdbool.h"
|
#include "stdbool.h"
|
||||||
#include "stdint.h"
|
#include "stdint.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
|
|
||||||
#include "common/maths.h"
|
#include "common/maths.h"
|
||||||
|
#include "common/filter.h"
|
||||||
|
|
||||||
#include "drivers/adc.h"
|
#include "drivers/adc.h"
|
||||||
#include "drivers/system.h"
|
#include "drivers/system.h"
|
||||||
|
@ -33,7 +35,6 @@
|
||||||
#include "rx/rx.h"
|
#include "rx/rx.h"
|
||||||
|
|
||||||
#include "io/rc_controls.h"
|
#include "io/rc_controls.h"
|
||||||
#include "flight/lowpass.h"
|
|
||||||
#include "io/beeper.h"
|
#include "io/beeper.h"
|
||||||
|
|
||||||
#define VBATT_PRESENT_THRESHOLD_MV 10
|
#define VBATT_PRESENT_THRESHOLD_MV 10
|
||||||
|
@ -54,7 +55,6 @@ int32_t mAhDrawn = 0; // milliampere hours drawn from the battery
|
||||||
batteryConfig_t *batteryConfig;
|
batteryConfig_t *batteryConfig;
|
||||||
|
|
||||||
static batteryState_e batteryState;
|
static batteryState_e batteryState;
|
||||||
static lowpass_t lowpassFilter;
|
|
||||||
|
|
||||||
uint16_t batteryAdcToVoltage(uint16_t src)
|
uint16_t batteryAdcToVoltage(uint16_t src)
|
||||||
{
|
{
|
||||||
|
@ -66,12 +66,18 @@ uint16_t batteryAdcToVoltage(uint16_t src)
|
||||||
static void updateBatteryVoltage(void)
|
static void updateBatteryVoltage(void)
|
||||||
{
|
{
|
||||||
uint16_t vbatSample;
|
uint16_t vbatSample;
|
||||||
uint16_t vbatFiltered;
|
biquad_t vbatFilterState;
|
||||||
|
static bool vbatFilterIsSet;
|
||||||
|
|
||||||
|
if (!vbatFilterIsSet) {
|
||||||
|
BiQuadNewLpf(0.5f, &vbatFilterState, 20000);
|
||||||
|
vbatFilterIsSet = true;
|
||||||
|
}
|
||||||
|
|
||||||
// store the battery voltage with some other recent battery voltage readings
|
// store the battery voltage with some other recent battery voltage readings
|
||||||
vbatSample = vbatLatestADC = adcGetChannel(ADC_BATTERY);
|
vbatSample = vbatLatestADC = adcGetChannel(ADC_BATTERY);
|
||||||
vbatFiltered = (uint16_t)lowpassFixed(&lowpassFilter, vbatSample, VBATT_LPF_FREQ);
|
vbatSample = lrintf(applyBiQuadFilter((float) vbatSample, &vbatFilterState));
|
||||||
vbat = batteryAdcToVoltage(vbatFiltered);
|
vbat = batteryAdcToVoltage(vbatSample);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define VBATTERY_STABLE_DELAY 40
|
#define VBATTERY_STABLE_DELAY 40
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue