test_EKF_imuSampling.cpp
9.43 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
/****************************************************************************
*
* Copyright (c) 2019 ECL 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.
*
****************************************************************************/
#include <gtest/gtest.h>
#include <math.h>
#include "EKF/ekf.h"
#include "EKF/imu_down_sampler.hpp"
class EkfImuSamplingTest : public ::testing::TestWithParam<std::tuple<float,float,Vector3f, Vector3f>>
{
public:
Ekf _ekf{};
uint32_t _t_us{0};
// Setup the Ekf with synthetic measurements
void SetUp() override
{
_ekf.init(0);
}
void TearDown() override
{
}
};
TEST_P(EkfImuSamplingTest, imuSamplingAtMultipleRates)
{
// WHEN: adding imu samples at a higher rate than the update loop
// THEN: imu sample should be down sampled
// WHEN: adding imu samples at a same or lower rate than the update loop
// THEN: imu sample should reach buffer unchanged
uint32_t dt_us = std::get<0>(GetParam()) * (_ekf.FILTER_UPDATE_PERIOD_MS * 1000);
uint32_t expected_dt_us = std::get<1>(GetParam()) * (_ekf.FILTER_UPDATE_PERIOD_MS * 1000);
Vector3f ang_vel= std::get<2>(GetParam());
Vector3f accel = std::get<3>(GetParam());
imuSample imu_sample;
imu_sample.delta_ang_dt = dt_us * 1.0e-6f;
imu_sample.delta_ang = ang_vel * imu_sample.delta_ang_dt;
imu_sample.delta_vel_dt = dt_us * 1.0e-6f;
imu_sample.delta_vel = accel * imu_sample.delta_vel_dt;
// The higher the imu rate is the more measurements we have to set before reaching the FILTER_UPDATE_PERIOD
int n_samples = 0;
for(int i = 0; i<(int)20/std::get<0>(GetParam()); ++i)
{
n_samples++;
imu_sample.time_us = _t_us;
_ekf.setIMUData(imu_sample);
_t_us += dt_us;
}
// Get the imu sample that was put into the buffer
imuSample imu_sample_buffered = _ekf.get_imu_sample_delayed();
EXPECT_NEAR(expected_dt_us / 1e6f, imu_sample_buffered.delta_ang_dt, 1e-5f);
EXPECT_NEAR(expected_dt_us / 1e6f, imu_sample_buffered.delta_vel_dt, 1e-5f);
// WHEN: downsampling the imu measurement
// THEN: the delta vel should be accumulated correctly
// Allow for accumulation of rounding error with each sample
EXPECT_TRUE(matrix::isEqual(ang_vel, imu_sample_buffered.delta_ang/imu_sample_buffered.delta_ang_dt, float(n_samples) * 1e-7f));
EXPECT_TRUE(matrix::isEqual(accel, imu_sample_buffered.delta_vel/imu_sample_buffered.delta_vel_dt, float(n_samples) * 1e-7f));
}
INSTANTIATE_TEST_SUITE_P(imuSamplingAtMultipleRates,
EkfImuSamplingTest,
::testing::Values(
std::make_tuple<float,float,Vector3f,Vector3f>(1.0f, 1.0f,Vector3f{0.0f,0.0f,0.0f},Vector3f{-0.46f,0.87f,0.20f}),
std::make_tuple<float,float,Vector3f,Vector3f>(0.5f, 1.0f,Vector3f{0.0f,0.0f,0.0f},Vector3f{-0.46f,0.87f,0.20f}),
std::make_tuple<float,float,Vector3f,Vector3f>(1.6f, 1.6f,Vector3f{0.0f,0.0f,0.0f},Vector3f{-0.46f,0.87f,0.20f}),
std::make_tuple<float,float,Vector3f,Vector3f>(0.333f,1.0f,Vector3f{0.0f,0.0f,0.0f},Vector3f{-0.46f,0.87f,0.20f}),
std::make_tuple<float,float,Vector3f,Vector3f>(1.0f, 1.0f,Vector3f{1.0f,0.0f,0.0f},Vector3f{0.0f,0.0f,0.0f}),
std::make_tuple<float,float,Vector3f,Vector3f>(0.5f, 1.0f,Vector3f{1.0f,0.0f,0.0f},Vector3f{0.0f,0.0f,0.0f}),
std::make_tuple<float,float,Vector3f,Vector3f>(1.6f, 1.6f,Vector3f{1.0f,0.0f,0.0f},Vector3f{0.0f,0.0f,0.0f}),
std::make_tuple<float,float,Vector3f,Vector3f>(0.333f,1.0f,Vector3f{1.0f,0.0f,0.0f},Vector3f{0.0f,0.0f,0.0f}),
std::make_tuple<float,float,Vector3f,Vector3f>(1.0f, 1.0f,Vector3f{0.0f,1.0f,0.0f},Vector3f{0.0f,0.0f,0.0f}),
std::make_tuple<float,float,Vector3f,Vector3f>(0.5f, 1.0f,Vector3f{0.0f,1.0f,0.0f},Vector3f{0.0f,0.0f,0.0f}),
std::make_tuple<float,float,Vector3f,Vector3f>(1.6f, 1.6f,Vector3f{0.0f,1.0f,0.0f},Vector3f{0.0f,0.0f,0.0f}),
std::make_tuple<float,float,Vector3f,Vector3f>(0.333f,1.0f,Vector3f{0.0f,1.0f,0.0f},Vector3f{0.0f,0.0f,0.0f}),
std::make_tuple<float,float,Vector3f,Vector3f>(1.0f, 1.0f,Vector3f{0.0f,0.0f,1.0f},Vector3f{0.0f,0.0f,0.0f}),
std::make_tuple<float,float,Vector3f,Vector3f>(0.5f, 1.0f,Vector3f{0.0f,0.0f,1.0f},Vector3f{0.0f,0.0f,0.0f}),
std::make_tuple<float,float,Vector3f,Vector3f>(1.6f, 1.6f,Vector3f{0.0f,0.0f,1.0f},Vector3f{0.0f,0.0f,0.0f}),
std::make_tuple<float,float,Vector3f,Vector3f>(0.333f,1.0f,Vector3f{0.0f,0.0f,1.0f},Vector3f{0.0f,0.0f,0.0f})
));
TEST_F(EkfImuSamplingTest, accelDownSampling)
{
ImuDownSampler sampler(0.008f);
Vector3f ang_vel{0.0f,0.0f,0.0f};
Vector3f accel{-0.46f,0.87f,0.0f};
imuSample input_sample;
input_sample.delta_ang_dt = 0.004f;
input_sample.delta_ang = ang_vel * input_sample.delta_ang_dt;
input_sample.delta_vel_dt = 0.004f;
input_sample.delta_vel = accel * input_sample.delta_vel_dt;
input_sample.time_us = 0;
// WHEN: adding samples at the double rate as the target rate
EXPECT_FALSE(sampler.update(input_sample));
input_sample.time_us = 4000;
// THEN: after two samples a first downsampled sample is ready
EXPECT_TRUE(sampler.update(input_sample));
// THEN: downsampled sample should fit to input data
imuSample output_sample = sampler.getDownSampledImuAndTriggerReset();
EXPECT_FLOAT_EQ(output_sample.delta_ang_dt, 0.008f);
EXPECT_FLOAT_EQ(output_sample.delta_vel_dt, 0.008f);
EXPECT_TRUE(matrix::isEqual(ang_vel * 0.008f, output_sample.delta_ang, 1e-10f));
EXPECT_TRUE(matrix::isEqual(accel * 0.008f, output_sample.delta_vel, 1e-10f));
}
TEST_F(EkfImuSamplingTest, gyroDownSampling)
{
ImuDownSampler sampler(0.008f);
Vector3f ang_vel{0.0f,0.0f,1.0f};
Vector3f accel{0.0f,0.0f,0.0f};
imuSample input_sample;
input_sample.delta_ang_dt = 0.004f;
input_sample.delta_ang = ang_vel * input_sample.delta_ang_dt;
input_sample.delta_vel_dt = 0.004f;
input_sample.delta_vel = accel * input_sample.delta_vel_dt;
input_sample.time_us = 0;
// WHEN: adding samples at the double rate as the target rate
EXPECT_FALSE(sampler.update(input_sample));
input_sample.time_us += 4000;
// THEN: after two samples a first downsampled sample is ready
EXPECT_TRUE(sampler.update(input_sample));
input_sample.time_us += 4000;
// THEN: downsampled sample should fit to input data
imuSample output_sample = sampler.getDownSampledImuAndTriggerReset();
EXPECT_FLOAT_EQ(output_sample.delta_ang_dt, 0.008f);
EXPECT_FLOAT_EQ(output_sample.delta_vel_dt, 0.008f);
EXPECT_TRUE(matrix::isEqual(ang_vel * 0.008f, output_sample.delta_ang, 1e-10f));
EXPECT_TRUE(matrix::isEqual(accel * 0.008f, output_sample.delta_vel, 1e-10f));
ang_vel = Vector3f{0.0f,1.0f,0.0f};
input_sample.delta_ang = ang_vel * input_sample.delta_ang_dt;
input_sample.delta_vel = accel * input_sample.delta_vel_dt;
// WHEN: adding samples at the double rate as the target rate
EXPECT_FALSE(sampler.update(input_sample));
input_sample.time_us += 4000;
// THEN: after two more samples a second downsampled sample is ready
EXPECT_TRUE(sampler.update(input_sample));
input_sample.time_us += 4000;
// THEN: downsampled sample should fit the adapted input data
output_sample = sampler.getDownSampledImuAndTriggerReset();
EXPECT_FLOAT_EQ(output_sample.delta_ang_dt, 0.008f);
EXPECT_FLOAT_EQ(output_sample.delta_vel_dt, 0.008f);
EXPECT_TRUE(matrix::isEqual(ang_vel * 0.008f, output_sample.delta_ang, 1e-10f));
EXPECT_TRUE(matrix::isEqual(accel * 0.008f, output_sample.delta_vel, 1e-10f));
ang_vel = Vector3f{1.0f,0.0f,0.0f};
input_sample.delta_ang = ang_vel * input_sample.delta_ang_dt;
input_sample.delta_vel = accel * input_sample.delta_vel_dt;
// WHEN: adding samples at the double rate as the target rate
EXPECT_FALSE(sampler.update(input_sample));
input_sample.time_us += 4000;
// THEN: after two more samples a second downsampled sample is ready
EXPECT_TRUE(sampler.update(input_sample));
input_sample.time_us += 4000;
// THEN: downsampled sample should fit the adapted input data
output_sample = sampler.getDownSampledImuAndTriggerReset();
EXPECT_FLOAT_EQ(output_sample.delta_ang_dt, 0.008f);
EXPECT_FLOAT_EQ(output_sample.delta_vel_dt, 0.008f);
EXPECT_TRUE(matrix::isEqual(ang_vel * 0.008f, output_sample.delta_ang, 1e-10f));
EXPECT_TRUE(matrix::isEqual(accel * 0.008f, output_sample.delta_vel, 1e-10f));
}