Skip to content

Commit afea17f

Browse files
committed
feat(filter): Add PT1 smoothing to Kalman gain
Add temporal smoothing to Kalman filter gain using PT1 low-pass filter. This aligns IMU-F implementation with EmuFlight's proven Kalman design. Changes: - Export pt1Filter_t type and functions from filter.h - Add pt1Filter_t kFilter to kalman_t structure - Initialize PT1 filter with 50 Hz cutoff in init_kalman() - Apply PT1 filter to calculated gain in kalman_process() Benefits: - Smoother filter transitions - Reduced oscillation risk - More predictable filter behavior - Complete alignment with EmuFlight implementation This enhancement builds upon the covariance signal fix (v257) and provides additional stability through gain smoothing. Bump version to 258
1 parent e2eeeff commit afea17f

5 files changed

Lines changed: 19 additions & 6 deletions

File tree

src/filter/filter.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,8 @@ volatile filter_config_t filterConfig =
2323
3, //init defaults for: uint16_t ptnFilterType;
2424
};
2525

26-
// PT1 Low Pass filter
26+
// PT1 Low Pass filter (type now in filter.h)
2727
bool acc_filter_initialized = false;
28-
typedef struct pt1Filter_s {
29-
float state;
30-
float k;
31-
} pt1Filter_t;
3228

3329
pt1Filter_t ax_filter;
3430
pt1Filter_t ay_filter;

src/filter/filter.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
#define DEFAULT_YAW_Q 3000
99
#define BASE_LPF_HZ 70.0f
1010

11+
// PT1 Low Pass filter
12+
typedef struct pt1Filter_s {
13+
float state;
14+
float k;
15+
} pt1Filter_t;
16+
1117
typedef enum filterAxisTypedef {
1218
ROLL = 0,
1319
PITCH = 1,
@@ -41,4 +47,9 @@ extern void allow_filter_init(void);
4147
extern void filter_init(void);
4248
extern void filter_data(volatile axisData_t* gyroRateData, volatile axisData_t* gyroAccData, float gyroTempData, filteredData_t* filteredData);
4349

50+
// PT1 filter functions
51+
extern float pt1FilterGain(uint16_t f_cut, float dT);
52+
extern void pt1FilterInit(pt1Filter_t *filter, float k, float val);
53+
extern float pt1FilterApply(pt1Filter_t *filter, float input);
54+
4455
void filter_acc(volatile axisData_t *gyroAccData);

src/filter/kalman.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ void init_kalman(kalman_t *filter, float q)
1414
filter->r = 88.0f; //seeding R at 88.0f
1515
filter->p = 30.0f; //seeding P at 30.0f
1616
filter->e = 1.0f;
17+
18+
// Initialize PT1 filter for Kalman gain smoothing
19+
// 50 Hz cutoff with 32kHz sample rate (REFRESH_RATE = 1/32000 = 0.00003125s)
20+
pt1FilterInit(&filter->kFilter, pt1FilterGain(50, REFRESH_RATE), 0.0f);
1721
}
1822

1923
void kalman_init(void)
@@ -104,6 +108,7 @@ inline float kalman_process(kalman_t* kalmanState, volatile float input) {
104108

105109
//measurement update
106110
kalmanState->k = kalmanState->p / (kalmanState->p + 10.0f);
111+
kalmanState->k = pt1FilterApply(&kalmanState->kFilter, kalmanState->k);
107112
kalmanState->x += kalmanState->k * (input - kalmanState->x);
108113
kalmanState->p = (1.0f - kalmanState->k) * kalmanState->p;
109114
return kalmanState->x;

src/filter/kalman.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef struct kalman
2020
float lastX; //previous state
2121
float e; //multiplier or adder to q
2222
float s; //changes how dynamic e is
23+
pt1Filter_t kFilter; //PT1 filter for gain smoothing
2324
} kalman_t;
2425

2526
typedef struct variance

src/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
#define HARDWARE_VERSION 101
44
#define BOOTLOADER_VERSION 101
55

6-
#define FIRMWARE_VERSION 257
6+
#define FIRMWARE_VERSION 258

0 commit comments

Comments
 (0)