-
Notifications
You must be signed in to change notification settings - Fork 913
/
Copy pathtest_ekf.cpp
124 lines (103 loc) · 4.33 KB
/
test_ekf.cpp
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
/*
* Copyright (c) 2014, 2015, 2016 Charles River Analytics, Inc.
* 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 of the copyright holder 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 HOLDER 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 <limits>
#include <memory>
#include <vector>
#include "gtest/gtest.h"
#include "robot_localization/ekf.hpp"
#include "robot_localization/filter_base.hpp"
#include "robot_localization/filter_common.hpp"
#include "robot_localization/ros_filter.hpp"
#include "robot_localization/ros_filter_types.hpp"
using robot_localization::Ekf;
using robot_localization::RosEkf;
using robot_localization::STATE_SIZE;
TEST(EkfTest, Measurements) {
rclcpp::NodeOptions options;
options.arguments({"ekf_filter_node"});
std::shared_ptr<robot_localization::RosEkf> filter =
std::make_shared<robot_localization::RosEkf>(options);
filter->initialize();
// create the instance of the class and pass parameters
Eigen::MatrixXd initialCovar(15, 15);
initialCovar.setIdentity();
initialCovar *= 0.5;
filter->getFilter().setEstimateErrorCovariance(initialCovar);
Eigen::VectorXd measurement(STATE_SIZE);
measurement.setIdentity();
for (size_t i = 0; i < STATE_SIZE; ++i) {
measurement[i] = i * 0.01 * STATE_SIZE;
}
Eigen::MatrixXd measurementCovariance(STATE_SIZE, STATE_SIZE);
measurementCovariance.setIdentity();
for (size_t i = 0; i < STATE_SIZE; ++i) {
measurementCovariance(i, i) = 1e-9;
}
std::vector<bool> updateVector(STATE_SIZE, true);
// Ensure that measurements are being placed in the queue correctly
rclcpp::Time time1(1000);
filter->robot_localization::RosEkf::enqueueMeasurement(
"odom0", measurement, measurementCovariance, updateVector,
std::numeric_limits<double>::max(), time1);
filter->robot_localization::RosEkf::integrateMeasurements(rclcpp::Time(1001));
EXPECT_EQ(filter->getFilter().getState(), measurement);
EXPECT_EQ(
filter->getFilter().getEstimateErrorCovariance(),
measurementCovariance);
filter->getFilter().setEstimateErrorCovariance(initialCovar);
// Now fuse another measurement and check the output.
// We know what the filter's state should be when
// this is complete, so we'll check the difference and
// make sure it's suitably small.
Eigen::VectorXd measurement2 = measurement;
measurement2 *= 2.0;
for (size_t i = 0; i < STATE_SIZE; ++i) {
measurementCovariance(i, i) = 1e-9;
}
rclcpp::Time time2(1002);
filter->robot_localization::RosEkf::enqueueMeasurement(
"odom0", measurement2, measurementCovariance, updateVector,
std::numeric_limits<double>::max(), time2);
filter->robot_localization::RosEkf::integrateMeasurements(rclcpp::Time(1003));
measurement = measurement2.eval() - filter->getFilter().getState();
for (size_t i = 0; i < STATE_SIZE; ++i) {
EXPECT_LT(::fabs(measurement[i]), 0.001);
}
}
int main(int argc, char ** argv)
{
rclcpp::init(argc, argv);
::testing::InitGoogleTest(&argc, argv);
int ret = RUN_ALL_TESTS();
rclcpp::shutdown();
return ret;
}