Skip to content

Commit 5bfc556

Browse files
authored
Merge pull request #1548 from bitcraze/rik/kalman_defs_overwrite_param
Kalman: Fix persistent parameters being overwritten at init
2 parents 6fd19e9 + c2b0655 commit 5bfc556

File tree

4 files changed

+104
-37
lines changed

4 files changed

+104
-37
lines changed

src/modules/interface/kalman_core/kalman_core.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,19 @@ typedef struct {
135135
float attitudeReversion;
136136
} kalmanCoreParams_t;
137137

138-
/* - Load default parameters */
138+
/**
139+
* @brief Initialize Kalman core parameters with default values
140+
*
141+
* This function exists primarily for Python bindings to initialize their own
142+
* kalmanCoreParams_t structs. In the firmware, default parameters are initialized
143+
* via a static initializer in estimator_kalman.c to avoid overwriting persistent
144+
* parameters loaded from storage.
145+
*
146+
* Default values are defined in kalman_core_params_defaults.h to maintain a
147+
* single source of truth.
148+
*
149+
* @param params Pointer to the parameter struct to initialize
150+
*/
139151
void kalmanCoreDefaultParams(kalmanCoreParams_t *params);
140152

141153
/* - Initialize Kalman State */
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* ,---------, ____ _ __
3+
* | ,-^-, | / __ )(_) /_______________ _____ ___
4+
* | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \
5+
* | / ,--' | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
6+
* +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/
7+
*
8+
* Crazyflie control firmware
9+
*
10+
* Copyright (C) 2025 Bitcraze AB
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU General Public License as published by
14+
* the Free Software Foundation, in version 3.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
#pragma once
27+
28+
/**
29+
* @file kalman_core_params_defaults.h
30+
* @brief Single source of truth for Kalman core parameter default values
31+
*
32+
* This header defines default parameter values as a macro to avoid duplication.
33+
* The macro is used in two places:
34+
* 1. estimator_kalman.c - Static initializer for firmware (avoids overwriting persistent params)
35+
* 2. kalman_core.c - kalmanCoreDefaultParams() function for Python bindings
36+
*/
37+
38+
// Process noise defaults depend on configuration
39+
#ifdef CONFIG_ESTIMATOR_KALMAN_GENERAL_PURPOSE
40+
#define KALMAN_CORE_PROC_NOISE_DEFAULTS \
41+
.procNoiseAcc_xy = 0.5f, \
42+
.procNoiseAcc_z = 0.5f
43+
#else
44+
#define KALMAN_CORE_PROC_NOISE_DEFAULTS \
45+
.procNoiseAcc_xy = 0.5f, \
46+
.procNoiseAcc_z = 1.0f
47+
#endif
48+
49+
/**
50+
* @brief Macro containing default values for kalmanCoreParams_t
51+
*
52+
* Use as a designated initializer, e.g.:
53+
* kalmanCoreParams_t params = { KALMAN_CORE_DEFAULT_PARAMS_INIT };
54+
*/
55+
#define KALMAN_CORE_DEFAULT_PARAMS_INIT \
56+
/* Initial variances, uncertain of position, but know we're stationary and roughly flat */ \
57+
.stdDevInitialPosition_xy = 100, \
58+
.stdDevInitialPosition_z = 1, \
59+
.stdDevInitialVelocity = 0.01, \
60+
.stdDevInitialAttitude_rollpitch = 0.01, \
61+
.stdDevInitialAttitude_yaw = 0.01, \
62+
\
63+
KALMAN_CORE_PROC_NOISE_DEFAULTS, \
64+
.procNoiseVel = 0, \
65+
.procNoisePos = 0, \
66+
.procNoiseAtt = 0, \
67+
.measNoiseBaro = 2.0f, /* meters */ \
68+
.measNoiseGyro_rollpitch = 0.1f, /* radians per second */ \
69+
.measNoiseGyro_yaw = 0.1f, /* radians per second */ \
70+
\
71+
.initialX = 0.0, \
72+
.initialY = 0.0, \
73+
.initialZ = 0.0, \
74+
/* Initial yaw of the Crazyflie in radians. */ \
75+
/* 0 --- facing positive X */ \
76+
/* PI / 2 --- facing positive Y */ \
77+
/* PI --- facing negative X */ \
78+
/* 3 * PI / 2 --- facing negative Y */ \
79+
.initialYaw = 0.0, \
80+
\
81+
/* Roll/pitch/yaw zero reversion is on by default. Will be overridden by estimatorKalmanInit() if requested by the deck. */ \
82+
.attitudeReversion = 0.001f

src/modules/src/estimator/estimator_kalman.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
*/
6060

6161
#include "kalman_core.h"
62+
#include "kalman_core_params_defaults.h"
6263
#include "kalman_supervisor.h"
6364

6465
#include "FreeRTOS.h"
@@ -158,7 +159,9 @@ static OutlierFilterLhState_t sweepOutlierFilterState;
158159
// Indicates that the internal state is corrupt and should be reset
159160
bool resetEstimation = false;
160161

161-
static kalmanCoreParams_t coreParams;
162+
static kalmanCoreParams_t coreParams = {
163+
KALMAN_CORE_DEFAULT_PARAMS_INIT
164+
};
162165

163166
// Data used to enable the task and stabilizer loop to run with minimal locking
164167
static state_t taskEstimatorState; // The estimator state produced by the task, copied to the stabilizer when needed.
@@ -191,7 +194,6 @@ STATIC_MEM_TASK_ALLOC_STACK_NO_DMA_CCM_SAFE(kalmanTask, KALMAN_TASK_STACKSIZE);
191194

192195
// Called one time during system startup
193196
void estimatorKalmanTaskInit() {
194-
kalmanCoreDefaultParams(&coreParams);
195197
// It would be logical to set the params->attitudeReversion here, based on deck requirements, but the decks are
196198
// not initialized yet at this point so it is done in estimatorKalmanInit().
197199

src/modules/src/kalman_core/kalman_core.c

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
*/
5959

6060
#include "kalman_core.h"
61+
#include "kalman_core_params_defaults.h"
6162
#include "cfassert.h"
6263
#include "autoconf.h"
6364

@@ -116,42 +117,12 @@ static void assertStateNotNaN(const kalmanCoreData_t* this)
116117
// Small number epsilon, to prevent dividing by zero
117118
#define EPS (1e-6f)
118119

120+
__attribute__((used))
119121
void kalmanCoreDefaultParams(kalmanCoreParams_t* params)
120122
{
121-
// Initial variances, uncertain of position, but know we're stationary and roughly flat
122-
params->stdDevInitialPosition_xy = 100;
123-
params->stdDevInitialPosition_z = 1;
124-
params->stdDevInitialVelocity = 0.01;
125-
params->stdDevInitialAttitude_rollpitch = 0.01;
126-
params->stdDevInitialAttitude_yaw = 0.01;
127-
128-
#ifdef CONFIG_ESTIMATOR_KALMAN_GENERAL_PURPOSE
129-
params->procNoiseAcc_xy = 0.5f;
130-
params->procNoiseAcc_z = 0.5f;
131-
#else
132-
params->procNoiseAcc_xy = 0.5f;
133-
params->procNoiseAcc_z = 1.0f;
134-
#endif
135-
params->procNoiseVel = 0;
136-
params->procNoisePos = 0;
137-
params->procNoiseAtt = 0;
138-
params->measNoiseBaro = 2.0f; // meters
139-
params->measNoiseGyro_rollpitch = 0.1f; // radians per second
140-
params->measNoiseGyro_yaw = 0.1f; // radians per second
141-
142-
params->initialX = 0.0;
143-
params->initialY = 0.0;
144-
params->initialZ = 0.0;
145-
146-
// Initial yaw of the Crazyflie in radians.
147-
// 0 --- facing positive X
148-
// PI / 2 --- facing positive Y
149-
// PI --- facing negative X
150-
// 3 * PI / 2 --- facing negative Y
151-
params->initialYaw = 0.0;
152-
153-
// Roll/pitch/yaw zero reversion is on by default. Will be overridden by estimator_kalman.c if requested by the deck.
154-
params->attitudeReversion = 0.001f;
123+
*params = (kalmanCoreParams_t){
124+
KALMAN_CORE_DEFAULT_PARAMS_INIT
125+
};
155126
}
156127

157128
void kalmanCoreInit(kalmanCoreData_t *this, const kalmanCoreParams_t *params, const uint32_t nowMs)

0 commit comments

Comments
 (0)