diff --git a/Makefile b/Makefile
index 6501d287b2..0e3feab7bf 100644
--- a/Makefile
+++ b/Makefile
@@ -291,6 +291,7 @@ COMMON_SRC = build_config.c \
flight/pid.c \
flight/imu.c \
flight/mixer.c \
+ flight/lowpass.c \
drivers/bus_i2c_soft.c \
drivers/serial.c \
drivers/sound_beeper.c \
diff --git a/src/main/config/config.c b/src/main/config/config.c
index e2339cfed5..58f626410f 100755
--- a/src/main/config/config.c
+++ b/src/main/config/config.c
@@ -334,7 +334,7 @@ void resetMixerConfig(mixerConfig_t *mixerConfig) {
mixerConfig->yaw_jump_prevention_limit = 200;
#ifdef USE_SERVOS
mixerConfig->tri_unarmed_servo = 1;
- mixerConfig->servo_lowpass_freq = 400.0f;
+ mixerConfig->servo_lowpass_freq = 400;
mixerConfig->servo_lowpass_enable = 0;
#endif
}
diff --git a/src/main/flight/lowpass.c b/src/main/flight/lowpass.c
new file mode 100755
index 0000000000..bb1b71c9d9
--- /dev/null
+++ b/src/main/flight/lowpass.c
@@ -0,0 +1,118 @@
+/*
+ * 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 .
+ */
+
+#include
+#include
+#include
+#include
+
+#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;
+}
+
diff --git a/src/main/flight/lowpass.h b/src/main/flight/lowpass.h
new file mode 100644
index 0000000000..f1b57a315e
--- /dev/null
+++ b/src/main/flight/lowpass.h
@@ -0,0 +1,41 @@
+/*
+ * 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 .
+ */
+
+#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);
diff --git a/src/main/flight/mixer.c b/src/main/flight/mixer.c
index 68c3115e76..ff01a08439 100755
--- a/src/main/flight/mixer.c
+++ b/src/main/flight/mixer.c
@@ -19,7 +19,6 @@
#include
#include
#include
-#include
#include "platform.h"
#include "debug.h"
@@ -28,7 +27,6 @@
#include "common/axis.h"
#include "common/maths.h"
-#include "common/filter.h"
#include "drivers/system.h"
#include "drivers/pwm_output.h"
@@ -36,7 +34,6 @@
#include "drivers/sensor.h"
#include "drivers/accgyro.h"
#include "drivers/system.h"
-#include "drivers/gyro_sync.h"
#include "rx/rx.h"
@@ -51,6 +48,7 @@
#include "flight/failsafe.h"
#include "flight/pid.h"
#include "flight/imu.h"
+#include "flight/lowpass.h"
#include "config/runtime_config.h"
#include "config/config.h"
@@ -79,6 +77,7 @@ int16_t servo[MAX_SUPPORTED_SERVOS];
static int useServo;
STATIC_UNIT_TESTED uint8_t servoCount;
static servoParam_t *servoConf;
+static lowpass_t lowpassFilters[MAX_SUPPORTED_SERVOS];
#endif
static const motorMixer_t mixerQuadX[] = {
@@ -925,8 +924,6 @@ void filterServos(void)
{
#ifdef USE_SERVOS
int16_t servoIdx;
- static bool servoFilterIsSet;
- biquad_t servoFitlerState[MAX_SUPPORTED_SERVOS];
#if defined(MIXER_DEBUG)
uint32_t startTime = micros();
@@ -934,11 +931,7 @@ void filterServos(void)
if (mixerConfig->servo_lowpass_enable) {
for (servoIdx = 0; servoIdx < MAX_SUPPORTED_SERVOS; servoIdx++) {
- if (!servoFilterIsSet) {
- BiQuadNewLpf(mixerConfig->servo_lowpass_freq, &servoFitlerState[servoIdx], targetLooptime);
- servoFilterIsSet = true;
- }
- servo[servoIdx] = lrintf(applyBiQuadFilter((float) servo[servoIdx], &servoFitlerState[servoIdx]));
+ servo[servoIdx] = (int16_t)lowpassFixed(&lowpassFilters[servoIdx], servo[servoIdx], mixerConfig->servo_lowpass_freq);
// Sanity check
servo[servoIdx] = constrain(servo[servoIdx], servoConf[servoIdx].min, servoConf[servoIdx].max);
diff --git a/src/main/io/serial_cli.c b/src/main/io/serial_cli.c
index aae7a22524..db53f67724 100644
--- a/src/main/io/serial_cli.c
+++ b/src/main/io/serial_cli.c
@@ -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 } },
#ifdef USE_SERVOS
{ "tri_unarmed_servo", VAR_INT8 | MASTER_VALUE | MODE_LOOKUP, &masterConfig.mixerConfig.tri_unarmed_servo, .config.lookup = { TABLE_OFF_ON } },
- { "servo_lowpass_freq", VAR_FLOAT | MASTER_VALUE, &masterConfig.mixerConfig.servo_lowpass_freq, .config.minmax = { 10, 400} },
+ { "servo_lowpass_freq", VAR_INT16 | 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 } },
#endif
diff --git a/src/main/sensors/battery.c b/src/main/sensors/battery.c
index e8b585f44c..7419eded90 100644
--- a/src/main/sensors/battery.c
+++ b/src/main/sensors/battery.c
@@ -17,12 +17,10 @@
#include "stdbool.h"
#include "stdint.h"
-#include
#include "platform.h"
#include "common/maths.h"
-#include "common/filter.h"
#include "drivers/adc.h"
#include "drivers/system.h"
@@ -35,6 +33,7 @@
#include "rx/rx.h"
#include "io/rc_controls.h"
+#include "flight/lowpass.h"
#include "io/beeper.h"
#define VBATT_PRESENT_THRESHOLD_MV 10
@@ -55,6 +54,7 @@ int32_t mAhDrawn = 0; // milliampere hours drawn from the battery
batteryConfig_t *batteryConfig;
static batteryState_e batteryState;
+static lowpass_t lowpassFilter;
uint16_t batteryAdcToVoltage(uint16_t src)
{
@@ -66,18 +66,12 @@ uint16_t batteryAdcToVoltage(uint16_t src)
static void updateBatteryVoltage(void)
{
uint16_t vbatSample;
- biquad_t vbatFilterState;
- static bool vbatFilterIsSet;
-
- if (!vbatFilterIsSet) {
- BiQuadNewLpf(0.5f, &vbatFilterState, 20000);
- vbatFilterIsSet = true;
- }
+ uint16_t vbatFiltered;
// store the battery voltage with some other recent battery voltage readings
vbatSample = vbatLatestADC = adcGetChannel(ADC_BATTERY);
- vbatSample = lrintf(applyBiQuadFilter((float) vbatSample, &vbatFilterState));
- vbat = batteryAdcToVoltage(vbatSample);
+ vbatFiltered = (uint16_t)lowpassFixed(&lowpassFilter, vbatSample, VBATT_LPF_FREQ);
+ vbat = batteryAdcToVoltage(vbatFiltered);
}
#define VBATTERY_STABLE_DELAY 40