PX4 Firmware
PX4 Autopilot Software http://px4.io
test_EKF_imuSampling.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2019 ECL 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 #include <gtest/gtest.h>
35 #include <math.h>
36 #include "EKF/ekf.h"
37 
38 class EkfSamplingTestParametrized : public ::testing::TestWithParam<std::tuple<float,float>>
39 {
40  public:
41 
42  Ekf _ekf{};
43 
44  uint32_t _t_us{0};
45 
46  // Setup the Ekf with synthetic measurements
47  void SetUp() override
48  {
49  _ekf.init(0);
50 
51  }
52 
53  void TearDown() override
54  {
55 
56  }
57 };
58 
59 TEST_P(EkfSamplingTestParametrized, imuSamplingAtMultipleRates)
60 {
61  // WHEN: adding imu samples at a higher rate than the update loop
62  // THEN: imu sample should be down sampled
63  // WHEN: adding imu samples at a same or lower rate than the update loop
64  // THEN: imu sample should reach buffer unchanged
65 
66  uint32_t dt_us = std::get<0>(GetParam()) * (_ekf.FILTER_UPDATE_PERIOD_MS * 1000);
67  uint32_t expected_dt_us = std::get<1>(GetParam()) * (_ekf.FILTER_UPDATE_PERIOD_MS * 1000);
68 
69  Vector3f ang_vel{0.0f,0.0f,0.0f};
70  Vector3f accel{-0.46f,0.87f,0.0f};
71  imuSample imu_sample;
72  imu_sample.delta_ang_dt = dt_us * 1.e-6f;
73  imu_sample.delta_ang = ang_vel * imu_sample.delta_ang_dt;
74  imu_sample.delta_vel_dt = dt_us * 1.e-6f;
75  imu_sample.delta_vel = accel * imu_sample.delta_vel_dt;
76 
77  // The higher the imu rate is the more measurements we have to set before reaching the FILTER_UPDATE_PERIOD
78  for(int i = 0; i<(int)20/std::get<0>(GetParam()); ++i)
79  {
80  imu_sample.time_us = _t_us;
81  _ekf.setIMUData(imu_sample);
82  _t_us += dt_us;
83  }
84 
85  // Get the imu sample that was put into the buffer
86  imuSample imu_sample_buffered = _ekf.get_imu_sample_delayed();
87  EXPECT_NEAR(expected_dt_us / 1e6f, imu_sample_buffered.delta_ang_dt, 1e-5f);
88  EXPECT_NEAR(expected_dt_us / 1e6f, imu_sample_buffered.delta_vel_dt, 1e-5f);
89 
90  // WHEN: downsampling the imu measurement
91  // THEN: the delta vel should be accumulated correctly
92  EXPECT_NEAR(ang_vel(0), imu_sample_buffered.delta_ang(0)/imu_sample_buffered.delta_ang_dt, 1e-3f);
93  EXPECT_NEAR(ang_vel(1), imu_sample_buffered.delta_ang(1)/imu_sample_buffered.delta_ang_dt, 1e-3f);
94  EXPECT_NEAR(ang_vel(2), imu_sample_buffered.delta_ang(2)/imu_sample_buffered.delta_ang_dt, 1e-3f);
95  EXPECT_NEAR(accel(0), imu_sample_buffered.delta_vel(0)/imu_sample_buffered.delta_vel_dt, 1e-3f);
96  EXPECT_NEAR(accel(1), imu_sample_buffered.delta_vel(1)/imu_sample_buffered.delta_vel_dt, 1e-3f);
97  EXPECT_NEAR(accel(2), imu_sample_buffered.delta_vel(2)/imu_sample_buffered.delta_vel_dt, 1e-3f);
98 
99 }
100 
101 INSTANTIATE_TEST_CASE_P(imuSamplingAtMultipleRates,
103  ::testing::Values(
104  std::make_tuple<float,float>(1.0f,1.0f),
105  std::make_tuple<float,float>(1.6f,1.6f),
106  std::make_tuple<float,float>(0.333f,1.0f)
107  ));
void setIMUData(const imuSample &imu_sample)
Vector3f delta_ang
delta angle in body frame (integrated gyro measurements) (rad)
Definition: common.h:107
INSTANTIATE_TEST_CASE_P(imuSamplingAtMultipleRates, EkfSamplingTestParametrized, ::testing::Values(std::make_tuple< float, float >(1.0f, 1.0f), std::make_tuple< float, float >(1.6f, 1.6f), std::make_tuple< float, float >(0.333f, 1.0f)))
TEST_P(EkfSamplingTestParametrized, imuSamplingAtMultipleRates)
uint64_t time_us
timestamp of the measurement (uSec)
Definition: common.h:111
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
imuSample get_imu_sample_delayed()
Vector3< float > Vector3f
Definition: Vector3.hpp:136
Definition: ekf.h:47
static constexpr unsigned FILTER_UPDATE_PERIOD_MS
bool init(uint64_t timestamp) override
Definition: ekf.cpp:47
Vector3f delta_vel
delta velocity in body frame (integrated accelerometer measurements) (m/sec)
Definition: common.h:108
float delta_ang_dt
delta angle integration period (sec)
Definition: common.h:109
Class for core functions for ekf attitude and position estimator.
float delta_vel_dt
delta velocity integration period (sec)
Definition: common.h:110