-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Expand file tree
/
Copy pathtest_EKF_gps.cpp
More file actions
230 lines (190 loc) · 7.81 KB
/
test_EKF_gps.cpp
File metadata and controls
230 lines (190 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/****************************************************************************
*
* Copyright (c) 2019-2023 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* Test the gps fusion
* @author Kamil Ritz <ka.ritz@hotmail.com>
*/
#include <gtest/gtest.h>
#include "EKF/ekf.h"
#include "sensor_simulator/sensor_simulator.h"
#include "sensor_simulator/ekf_wrapper.h"
#include "test_helper/reset_logging_checker.h"
class EkfGpsTest : public ::testing::Test
{
public:
EkfGpsTest(): ::testing::Test(),
_ekf{std::make_shared<Ekf>()},
_sensor_simulator(_ekf),
_ekf_wrapper(_ekf) {};
std::shared_ptr<Ekf> _ekf;
SensorSimulator _sensor_simulator;
EkfWrapper _ekf_wrapper;
// Setup the Ekf with synthetic measurements
void SetUp() override
{
// run briefly to init, then manually set in air and at rest (default for a real vehicle)
_ekf->init(0);
_sensor_simulator.runSeconds(0.1);
_ekf->set_in_air_status(false);
_ekf->set_vehicle_at_rest(true);
_sensor_simulator.runSeconds(2);
_ekf_wrapper.enableGpsFusion();
_sensor_simulator.startGps();
_sensor_simulator.runSeconds(11);
}
// Use this method to clean up any memory, network etc. after each test
void TearDown() override
{
}
};
TEST_F(EkfGpsTest, gpsTimeout)
{
// GIVEN:EKF that fuses GPS
EXPECT_TRUE(_ekf_wrapper.isIntendingGpsFusion());
// WHEN: the number of satellites drops below the minimum
_sensor_simulator._gps.setNumberOfSatellites(3);
// THEN: the GNSS fusion does not stop because other metrics are good enough
_sensor_simulator.runSeconds(8);
EXPECT_TRUE(_ekf_wrapper.isIntendingGpsFusion());
// WHEN: the fix type drops
_sensor_simulator._gps.setFixType(0);
// THEN: the GNSS fusion stops after some time
_sensor_simulator.runSeconds(8);
EXPECT_FALSE(_ekf_wrapper.isIntendingGpsFusion());
// BUT WHEN: the fix type is good again
_sensor_simulator._gps.setFixType(3);
// THEN: the GNSS fusion restarts
_sensor_simulator.runSeconds(6);
EXPECT_TRUE(_ekf_wrapper.isIntendingGpsFusion());
}
TEST_F(EkfGpsTest, gpsFixLoss)
{
// GIVEN:EKF that fuses GPS
EXPECT_TRUE(_ekf_wrapper.isIntendingGpsFusion());
// WHEN: the fix is loss
_sensor_simulator._gps.setFixType(0);
// THEN: after dead-reconing for a couple of seconds, the local position gets invalidated
_sensor_simulator.runSeconds(6);
EXPECT_TRUE(_ekf->control_status_flags().inertial_dead_reckoning);
EXPECT_FALSE(_ekf->isLocalHorizontalPositionValid());
// The control logic takes a bit more time to deactivate the GNSS fusion completely
_sensor_simulator.runSeconds(5);
EXPECT_FALSE(_ekf_wrapper.isIntendingGpsFusion());
}
TEST_F(EkfGpsTest, resetToGpsVelocity)
{
ResetLoggingChecker reset_logging_checker(_ekf);
// GIVEN:EKF that fuses GPS
// and has gps checks already passed
// WHEN: stopping GPS fusion
_sensor_simulator.stopGps();
_sensor_simulator.runSeconds(11);
reset_logging_checker.capturePreResetState();
// AND: simulate constant velocity gps samples for short time
_sensor_simulator.startGps();
const Vector3f simulated_velocity(0.5f, 1.0f, -0.3f);
_sensor_simulator._gps.setVelocity(simulated_velocity);
const uint64_t dt_us = 1e5;
_sensor_simulator._gps.stepHorizontalPositionByMeters(Vector2f(simulated_velocity) * dt_us * 1e-6);
_sensor_simulator._gps.stepHeightByMeters(simulated_velocity(2) * dt_us * 1e-6f);
_ekf->set_in_air_status(true);
_ekf->set_vehicle_at_rest(false);
_sensor_simulator.runSeconds(1.2); // required to pass the checks
_sensor_simulator.runMicroseconds(dt_us);
// THEN: a reset to GPS velocity should be done
const Vector3f estimated_velocity = _ekf->getVelocity();
EXPECT_NEAR(estimated_velocity(0), simulated_velocity(0), 1e-3f);
EXPECT_NEAR(estimated_velocity(1), simulated_velocity(1), 1e-3f);
EXPECT_NEAR(estimated_velocity(2), simulated_velocity(2), 1e-3f);
// AND: the reset in velocity should be saved correctly
reset_logging_checker.capturePostResetState();
EXPECT_TRUE(reset_logging_checker.isHorizontalVelocityResetCounterIncreasedBy(1));
EXPECT_TRUE(reset_logging_checker.isVerticalVelocityResetCounterIncreasedBy(1));
EXPECT_TRUE(reset_logging_checker.isVelocityDeltaLoggedCorrectly(1e-2f));
}
TEST_F(EkfGpsTest, resetToGpsPosition)
{
// GIVEN:EKF that fuses GPS
// and has gps checks already passed
const Vector3f previous_position = _ekf->getPosition();
// WHEN: stopping GPS fusion
_sensor_simulator.stopGps();
_sensor_simulator.runSeconds(11);
// AND: simulate jump in position
_sensor_simulator.startGps();
const Vector3f simulated_position_change(20.0f, -1.0f, 0.f);
_sensor_simulator._gps.stepHorizontalPositionByMeters(
Vector2f(simulated_position_change));
_sensor_simulator.runSeconds(6);
// THEN: a reset to the new GPS position should be done
const Vector3f estimated_position = _ekf->getPosition();
EXPECT_TRUE(isEqual(estimated_position,
previous_position + simulated_position_change, 1e-2f));
}
TEST_F(EkfGpsTest, gpsHgtToBaroFallback)
{
// GIVEN: EKF that fuses GPS and flow, and in GPS height mode
_sensor_simulator._flow.setData(_sensor_simulator._flow.dataAtRest());
_ekf_wrapper.enableFlowFusion();
_sensor_simulator.startFlow();
_sensor_simulator.startRangeFinder();
_ekf_wrapper.enableGpsHeightFusion();
_sensor_simulator.runSeconds(1);
EXPECT_TRUE(_ekf_wrapper.isIntendingGpsHeightFusion());
EXPECT_TRUE(_ekf_wrapper.isIntendingFlowFusion());
EXPECT_TRUE(_ekf_wrapper.isIntendingBaroHeightFusion());
// WHEN: stopping GPS fusion
_sensor_simulator.stopGps();
_sensor_simulator.runSeconds(11);
// THEN: the height source should automatically change to baro
EXPECT_FALSE(_ekf_wrapper.isIntendingGpsHeightFusion());
EXPECT_TRUE(_ekf_wrapper.isIntendingBaroHeightFusion());
}
TEST_F(EkfGpsTest, altitudeDrift)
{
// GIVEN: a drifting GNSS altitude
const float dt = 0.2f;
const float height_rate = 0.15f;
const float duration = 80.f;
// WHEN: running on ground
for (int i = 0; i < (duration / dt); i++) {
_sensor_simulator._gps.stepHeightByMeters(height_rate * dt);
_sensor_simulator.runSeconds(dt);
}
float baro_innov = _ekf->aid_src_baro_hgt().innovation;
BiasEstimator::status status = _ekf->getBaroBiasEstimatorStatus();
printf("baro innov = %f\n", (double)baro_innov);
printf("bias: %f, innov bias = %f\n", (double)status.bias, (double)status.innov);
// THEN: the baro and local position should follow it
EXPECT_LT(fabsf(baro_innov), 0.1f);
}