test_filter_bayes_update.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 #include "filters.hpp"
3 #include <filters/filter_chain.h>
4 #include <math.h>
5 #include <Eigen/Dense>
6 
7 #define GTEST_COUT std::cerr << "[ INFO ] "
8 
9 TEST(BayesUpdate, active)
10 {
11  EXPECT_EQ(0, 0);
12 }
13 
18 TEST(BayesUpdate, layers_dont_exist)
19 {
20  ros::NodeHandle nh("~");
21 
22  grid_map::GridMap gridMap({"random_layer"});
23  gridMap.setFrameId("random");
24  gridMap.setGeometry(grid_map::Length(3, 3), 1);
25 
26  filters::FilterChain<grid_map::GridMap> filterChain("grid_map::GridMap");
27 
28  if (!filterChain.configure("mitre_fast_layered_map_filters_bayes_update", nh))
29  {
30  GTEST_FATAL_FAILURE_("Unable to configure filter chain."); // We should never get here
31  }
32 
33  EXPECT_FALSE(filterChain.update(gridMap, gridMap));
34 
35  // Bayes filter needs current_probability and history layers
36  gridMap.add("current_probability", 0);
37  EXPECT_FALSE(filterChain.update(gridMap, gridMap));
38 
39  // Expects 4 history layers, only add 2 and ensure it throws error
40  gridMap.add("history_0", 0);
41  gridMap.add("history_1", 0);
42  EXPECT_FALSE(filterChain.update(gridMap, gridMap));
43 
44  // Add other 2 history layers
45  gridMap.add("history_2", 0);
46  gridMap.add("history_3", 0);
47  EXPECT_TRUE(filterChain.update(gridMap, gridMap));
48 
49 }
50 
51 // TO ADD: TEST RUNNING FILTER WHEN FRAMES_IN_SCOPE IS JUST ALL 0's
52 
53 /***
54  * All of the tests below this point are just checking that the math
55  * comes out as expected. You can use this to refer to the expected performance
56  * given the settings.
57  */
58 
62 TEST(BayesUpdate, binary)
63 {
64  ros::NodeHandle nh("~");
65 
66  // NOTE: Current probability will be filled with nans, but the filter should take care of it
67  grid_map::GridMap gridMap({"current_probability"});
68  gridMap.setFrameId("random");
69  gridMap.setGeometry(grid_map::Length(3, 3), 1);
70 
71  filters::FilterChain<grid_map::GridMap> filterChain("grid_map::GridMap");
72 
73  if (!filterChain.configure("mitre_fast_layered_map_filters_bayes_update", nh))
74  {
75  GTEST_FATAL_FAILURE_("Unable to configure filter chain."); // We should never get here
76  }
77 
78  // Add all of our history layers, -1 for no data
79  gridMap.add("history_0", -1);
80  gridMap.add("history_1", -1);
81  gridMap.add("history_2", -1);
82  gridMap.add("history_3", -1);
83 
84  //
85  // FRAME ONE
86  //
87 
88  // Initialize obstacle detected, only two cells have detection in first frame
89  gridMap["history_0"] <<
90  1, 1, 0,
91  0, 0, 0,
92  0, 0, 0;
93 
94  // Create matrix of expected output to compare to
95  Eigen::MatrixXf answerMat(3, 3);
96  answerMat << (2.0 / 3), (2.0 / 3), (1.0 / 3),
97  (1.0 / 3), (1.0 / 3), (1.0 / 3),
98  (1.0 / 3), (1.0 / 3), (1.0 / 3);
99 
100  if (!filterChain.update(gridMap, gridMap))
101  {
102  GTEST_FATAL_FAILURE_("Unable to update grid map.");
103  }
104 
105  for (grid_map::GridMapIterator it(gridMap); !it.isPastEnd(); ++it)
106  {
107  const grid_map::Index index(*it);
108 
109  EXPECT_FLOAT_EQ(answerMat(index(0), index(1)), gridMap.at("current_probability", index)) << "Index: " << index(0) << " " << index(1);
110  }
111 
112  //
113  // FRAME TWO
114  //
115 
116  // Set points for second history frame
117  gridMap["history_1"] <<
118  1, 0, 0,
119  1, 0, 0,
120  0, 0, 0;
121 
122  // Create matrix of expected output to compare to
123  answerMat << 0.8, 0.5, 0.2,
124  0.5, 0.2, 0.2,
125  0.2, 0.2, 0.2;
126 
127  if (!filterChain.update(gridMap, gridMap))
128  {
129  GTEST_FATAL_FAILURE_("Unable to update grid map.");
130  }
131 
132  for (grid_map::GridMapIterator it(gridMap); !it.isPastEnd(); ++it)
133  {
134  const grid_map::Index index(*it);
135 
136  EXPECT_FLOAT_EQ(answerMat(index(0), index(1)), gridMap.at("current_probability", index)) << "Index: " << index(0) << " " << index(1);
137  }
138 }
139 
140 // /**
141 // * Ensure its outputting expected values for non-binary readings
142 // */
143 TEST(BayesUpdate, readings)
144 {
145  ros::NodeHandle nh("~");
146 
147  // NOTE: Current probability will be filled with nans, but the filter should take care of it
148  grid_map::GridMap gridMap({"current_probability"});
149  gridMap.setFrameId("random");
150  gridMap.setGeometry(grid_map::Length(3, 3), 1);
151 
152  filters::FilterChain<grid_map::GridMap> filterChain("grid_map::GridMap");
153 
154  if (!filterChain.configure("mitre_fast_layered_map_filters_bayes_update", nh))
155  {
156  GTEST_FATAL_FAILURE_("Unable to configure filter chain."); // We should never get here
157  }
158 
159  //
160  // FRAME ONE
161  //
162 
163  // Add all of our history layers, -1 for no data
164  gridMap.add("history_0", -1);
165  gridMap.add("history_1", -1);
166  gridMap.add("history_2", -1);
167  gridMap.add("history_3", -1);
168 
169  gridMap["history_0"] <<
170  0, 1, 2,
171  3, 4, 5,
172  6, 7, 8;
173 
174  // Create matrix of expected output to compare to
175  // ALL PROBABILITY CALCULATED USING src/utils/bayes_filter_profiler.py
176  Eigen::MatrixXf answerMat(3, 3);
177  answerMat << (1.0 / 3), (2.0 / 3), 0.8333333,
178  0.9836, 0.9859, 0.9876,
179  0.9890, 0.99, 0.99;
180 
181  if (!filterChain.update(gridMap, gridMap))
182  {
183  GTEST_FATAL_FAILURE_("Unable to update grid map.");
184  }
185 
186  for (grid_map::GridMapIterator it(gridMap); !it.isPastEnd(); ++it)
187  {
188  const grid_map::Index index(*it);
189 
190  EXPECT_NEAR(answerMat(index(0), index(1)), gridMap.at("current_probability", index), 0.001) << "Index: " << index(0) << " " << index(1);
191  }
192 
193  //
194  // FRAME TWO
195  //
196 
197  gridMap["history_1"] <<
198  4, 3, 0,
199  5, 2, 1,
200  8, 9, 3;
201 
202  // Create matrix of expected output to compare to
203  answerMat << 0.9722, 0.9917, 0.714285,
204  0.9997, 0.9971, 0.9937,
205  0.9998, 0.9998, 0.9998;
206 
207  if (!filterChain.update(gridMap, gridMap))
208  {
209  GTEST_FATAL_FAILURE_("Unable to update grid map.");
210  }
211 
212  for (grid_map::GridMapIterator it(gridMap); !it.isPastEnd(); ++it)
213  {
214  const grid_map::Index index(*it);
215 
216  EXPECT_NEAR(answerMat(index(0), index(1)), gridMap.at("current_probability", index), 0.001) << "Index: " << index(0) << " " << index(1);
217  }
218 }
219 
220 // TEST(BayesUpdate, maxReadings)
221 // {
222 // // CREATE TEST THAT USES MAX NUMBER OF HISTORY READINGS
223 // }
Eigen::Array2i Index
TEST(BayesUpdate, active)
bool update(const T &data_in, T &data_out)
bool configure(std::string param_name, ros::NodeHandle node=ros::NodeHandle())
Filters that operate on a grid map instance.
void setFrameId(const std::string &frameId)
Eigen::Array2d Length


mitre_fast_layered_map
Author(s):
autogenerated on Thu Mar 11 2021 03:06:49