Skip to content

Commit e15e94b

Browse files
committed
HoverThrustEstimator: Add convergence tests
With noisy accel and thrust in hover, climb and descent conditions.
1 parent 0f1c759 commit e15e94b

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

src/lib/hover_thrust_estimator/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ px4_add_library(HoverThrustEstimator
3535
hover_thrust_estimator.cpp
3636
zero_order_hover_thrust_ekf.cpp
3737
)
38+
39+
px4_add_unit_gtest(SRC zero_order_hover_thrust_ekf_test.cpp LINKLIBS HoverThrustEstimator)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (C) 2020 PX4 Development Team. All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in
13+
* the documentation and/or other materials provided with the
14+
* distribution.
15+
* 3. Neither the name PX4 nor the names of its contributors may be
16+
* used to endorse or promote products derived from this software
17+
* without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
* POSSIBILITY OF SUCH DAMAGE.
31+
*
32+
****************************************************************************/
33+
34+
/**
35+
* Test code for the Zero Order Hover Thrust Estimator
36+
* Run this test only using make tests TESTFILTER=zero_order_hover_thrust_ekf
37+
*/
38+
39+
#include <gtest/gtest.h>
40+
#include <matrix/matrix/math.hpp>
41+
#include <random>
42+
43+
#include "zero_order_hover_thrust_ekf.hpp"
44+
45+
using namespace matrix;
46+
47+
class ZeroOrderHoverThrustEkfTest : public ::testing::Test
48+
{
49+
public:
50+
float computeAccelFromThrustAndHoverThrust(float thrust, float hover_thrust);
51+
ZeroOrderHoverThrustEkf::status runEkf(float accel, float thrust, float time, float accel_noise = 0.f,
52+
float thr_noise = 0.f);
53+
54+
std::normal_distribution<float> standard_normal_distribution_;
55+
std::default_random_engine random_generator_; // Pseudo-random generator with constant seed
56+
57+
private:
58+
ZeroOrderHoverThrustEkf _ekf{};
59+
static constexpr float _dt = 0.02f;
60+
};
61+
62+
float ZeroOrderHoverThrustEkfTest::computeAccelFromThrustAndHoverThrust(float thrust, float hover_thrust)
63+
{
64+
return CONSTANTS_ONE_G * thrust / hover_thrust - CONSTANTS_ONE_G;
65+
}
66+
67+
ZeroOrderHoverThrustEkf::status ZeroOrderHoverThrustEkfTest::runEkf(float accel, float thrust, float time,
68+
float accel_noise, float thr_noise)
69+
{
70+
ZeroOrderHoverThrustEkf::status status{};
71+
72+
for (float t = 0.f; t <= time; t += _dt) {
73+
_ekf.predict(_dt);
74+
float noisy_accel = accel + accel_noise * standard_normal_distribution_(random_generator_);
75+
float noisy_thrust = thrust + thr_noise * standard_normal_distribution_(random_generator_);
76+
_ekf.fuseAccZ(noisy_accel, noisy_thrust, status);
77+
}
78+
79+
return status;
80+
}
81+
82+
TEST_F(ZeroOrderHoverThrustEkfTest, testStaticCase)
83+
{
84+
// GIVEN: a vehicle at hover, (the estimator starting at the true value)
85+
const float thrust = 0.5f;
86+
const float hover_thrust_true = 0.5f;
87+
const float accel_meas = 0.f;
88+
89+
// WHEN: we input noiseless data and run the filter
90+
ZeroOrderHoverThrustEkf::status status = runEkf(accel_meas, thrust, 1.f);
91+
92+
// THEN: The estimate should not move and its variance decrease quickly
93+
EXPECT_NEAR(status.hover_thrust, hover_thrust_true, 1e-4f);
94+
EXPECT_NEAR(status.hover_thrust_var, 0.f, 1e-3f);
95+
EXPECT_NEAR(status.accel_noise_var, 0.f, 1.f); // The noise learning is slow and takes more than 1s to go to zero
96+
}
97+
98+
TEST_F(ZeroOrderHoverThrustEkfTest, testStaticConvergence)
99+
{
100+
// GIVEN: a vehicle at hover, but the estimator is starting at hover_thrust = 0.5
101+
const float thrust = 0.72f;
102+
const float hover_thrust_true = 0.72f;
103+
const float accel_meas = computeAccelFromThrustAndHoverThrust(thrust, hover_thrust_true);
104+
105+
// WHEN: we input noiseless data and run the filter
106+
ZeroOrderHoverThrustEkf::status status = runEkf(accel_meas, thrust, 2.f);
107+
108+
// THEN: the state should converge to the true value and its variance decrease
109+
EXPECT_NEAR(status.hover_thrust, hover_thrust_true, 1e-2f);
110+
EXPECT_NEAR(status.hover_thrust_var, 0.f, 1e-3f);
111+
EXPECT_NEAR(status.accel_noise_var, 0.f, 1.f); // The noise learning is slow and takes more than 1s to go to zero
112+
}
113+
114+
TEST_F(ZeroOrderHoverThrustEkfTest, testStaticConvergenceWithNoise)
115+
{
116+
// GIVEN: a vehicle at hover, the estimator starts with the wrong estimate and the measurements are noisy
117+
const float sigma_noise = 3.f;
118+
const float noise_var = sigma_noise * sigma_noise;
119+
const float thrust = 0.72f;
120+
const float hover_thrust_true = 0.72f;
121+
const float accel_meas = computeAccelFromThrustAndHoverThrust(thrust, hover_thrust_true);
122+
const float t_sim = 10.f;
123+
124+
// WHEN: we input noisy accel data and run the filter
125+
ZeroOrderHoverThrustEkf::status status = runEkf(accel_meas, thrust, t_sim, sigma_noise);
126+
127+
// THEN: the estimate should converge and the accel noise variance should be close to the true noise value
128+
EXPECT_NEAR(status.hover_thrust, hover_thrust_true, 5e-2f);
129+
EXPECT_NEAR(status.hover_thrust_var, 0.f, 1e-3f);
130+
EXPECT_NEAR(status.accel_noise_var, noise_var, 0.3f * noise_var);
131+
}
132+
133+
TEST_F(ZeroOrderHoverThrustEkfTest, testLargeAccelNoiseAndBias)
134+
{
135+
// GIVEN: a vehicle descending, the estimator starts with the wrong estimate, the measurements are really noisy
136+
const float sigma_noise = 7.f;
137+
const float noise_var = sigma_noise * sigma_noise;
138+
const float thrust = 0.4f; // Below hover thrust
139+
const float hover_thrust_true = 0.72f;
140+
const float accel_meas = computeAccelFromThrustAndHoverThrust(thrust, hover_thrust_true);
141+
const float t_sim = 15.f;
142+
143+
// WHEN: we input noisy accel data and run the filter
144+
ZeroOrderHoverThrustEkf::status status = runEkf(accel_meas, thrust, t_sim, sigma_noise);
145+
146+
// THEN: the estimate should converge and the accel noise variance should be close to the true noise value
147+
EXPECT_NEAR(status.hover_thrust, hover_thrust_true, 5e-2);
148+
EXPECT_NEAR(status.hover_thrust_var, 0.f, 1e-3f);
149+
EXPECT_NEAR(status.accel_noise_var, noise_var, 0.2f * noise_var);
150+
}
151+
152+
TEST_F(ZeroOrderHoverThrustEkfTest, testThrustAndAccelNoise)
153+
{
154+
// GIVEN: a vehicle climbing, the estimator starts with the wrong estimate, the measurements
155+
// and the input thrust are noisy
156+
const float accel_noise = 2.f;
157+
const float accel_var = accel_noise * accel_noise;
158+
const float thr_noise = 0.1f;
159+
const float thrust = 0.72f; // Above hover thrust
160+
const float hover_thrust_true = 0.6f;
161+
const float accel_meas = computeAccelFromThrustAndHoverThrust(thrust, hover_thrust_true);
162+
const float t_sim = 15.f;
163+
164+
// WHEN: we input noisy accel and thrust data, and run the filter
165+
ZeroOrderHoverThrustEkf::status status = runEkf(accel_meas, thrust, t_sim, accel_noise, thr_noise);
166+
167+
// THEN: the estimate should converge and the accel noise variance should be close to the true noise value
168+
EXPECT_NEAR(status.hover_thrust, hover_thrust_true, 5e-2f);
169+
EXPECT_NEAR(status.hover_thrust_var, 0.f, 1e-3f);
170+
// Because of the nonlinear measurment model and the thust noise, the accel noise estimation is a bit worse
171+
EXPECT_NEAR(status.accel_noise_var, accel_var, 0.5f * accel_var);
172+
}

0 commit comments

Comments
 (0)