test_landmark.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, the mcl_3dl authors
3  * 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 are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <limits>
31 #include <utility>
32 #include <vector>
33 
34 #include <ros/ros.h>
35 
36 #include <geometry_msgs/PoseArray.h>
37 #include <geometry_msgs/PoseWithCovarianceStamped.h>
38 
39 #include <gtest/gtest.h>
40 
41 namespace
42 {
43 inline geometry_msgs::PoseWithCovarianceStamped generatePoseWithCov(
44  const float y, const float y_var, const float var_measure = 0.0)
45 {
46  geometry_msgs::PoseWithCovarianceStamped pose;
47  pose.header.frame_id = "map";
48  pose.header.stamp = ros::Time::now();
49  pose.pose.pose.position.y = y;
50  pose.pose.pose.orientation.w = 1.0;
51  pose.pose.covariance[6 * 0 + 0] = var_measure;
52  pose.pose.covariance[6 * 1 + 1] = y_var;
53  pose.pose.covariance[6 * 2 + 2] = var_measure;
54  pose.pose.covariance[6 * 3 + 3] = var_measure;
55  pose.pose.covariance[6 * 4 + 4] = var_measure;
56  pose.pose.covariance[6 * 5 + 5] = var_measure;
57  return pose;
58 }
59 std::pair<float, float> getMean(const std::vector<geometry_msgs::Pose>& poses)
60 {
61  float mean = 0;
62  for (const geometry_msgs::Pose p : poses)
63  {
64  mean += p.position.y;
65  }
66  mean /= poses.size();
67 
68  float root_mean = 0;
69  for (const geometry_msgs::Pose p : poses)
70  {
71  root_mean += std::pow(p.position.y - mean, 2);
72  }
73  root_mean /= poses.size();
74 
75  return std::pair<float, float>(mean, root_mean);
76 }
77 } // namespace
78 
79 TEST(Landmark, Measurement)
80 {
81  geometry_msgs::PoseArray::ConstPtr poses;
82 
83  const boost::function<void(const geometry_msgs::PoseArray::ConstPtr&)> cb_pose =
84  [&poses](const geometry_msgs::PoseArray::ConstPtr& msg) -> void
85  {
86  poses = msg;
87  };
88 
89  ros::NodeHandle nh("");
90  ros::Subscriber sub_pose = nh.subscribe("mcl_3dl/particles", 1, cb_pose);
91  ros::Publisher pub_init =
92  nh.advertise<geometry_msgs::PoseWithCovarianceStamped>("initialpose", 1, true);
93  ros::Publisher pub_landmark =
94  nh.advertise<geometry_msgs::PoseWithCovarianceStamped>("mcl_measurement", 1, true);
95 
96  ros::Duration(1.0).sleep();
97  pub_init.publish(generatePoseWithCov(2.0, 1.0));
98 
99  ros::Rate wait(10);
100  for (int i = 0; i < 100; i++)
101  {
102  wait.sleep();
103  ros::spinOnce();
104  if (poses)
105  break;
106  ASSERT_TRUE(ros::ok());
107  }
108  ros::Duration(0.1).sleep();
109  ros::spinOnce();
110 
111  ASSERT_TRUE(static_cast<bool>(poses));
112  for (const geometry_msgs::Pose p : poses->poses)
113  {
114  ASSERT_FLOAT_EQ(p.position.x, 0.0f);
115  ASSERT_FLOAT_EQ(p.position.z, 0.0f);
116  ASSERT_FLOAT_EQ(p.orientation.x, 0.0f);
117  ASSERT_FLOAT_EQ(p.orientation.y, 0.0f);
118  ASSERT_FLOAT_EQ(p.orientation.z, 0.0f);
119  ASSERT_FLOAT_EQ(p.orientation.w, 1.0f);
120  }
121  const std::pair<float, float> mean_init = getMean(poses->poses);
122  ASSERT_NEAR(mean_init.first, 2.0f, 0.1f);
123  ASSERT_NEAR(mean_init.second, 1.0f, 0.1f);
124 
125  poses = nullptr;
126  pub_landmark.publish(generatePoseWithCov(2.6, 1.0, 1000.0 * 1000.0));
127  ros::Duration(0.1).sleep();
128  ros::spinOnce();
129 
130  ASSERT_TRUE(static_cast<bool>(poses));
131  for (const geometry_msgs::Pose p : poses->poses)
132  {
133  ASSERT_FLOAT_EQ(p.position.x, 0.0f);
134  ASSERT_FLOAT_EQ(p.position.z, 0.0f);
135  ASSERT_FLOAT_EQ(p.orientation.x, 0.0f);
136  ASSERT_FLOAT_EQ(p.orientation.y, 0.0f);
137  ASSERT_FLOAT_EQ(p.orientation.z, 0.0f);
138  ASSERT_FLOAT_EQ(p.orientation.w, 1.0f);
139  }
140  const std::pair<float, float> mean_measured = getMean(poses->poses);
141  ASSERT_NEAR(mean_measured.first, 2.3f, 0.1f);
142  ASSERT_NEAR(mean_measured.second, 0.5f, 0.1f);
143 }
144 
145 int main(int argc, char** argv)
146 {
147  testing::InitGoogleTest(&argc, argv);
148  ros::init(argc, argv, "test_landmark");
149 
150  return RUN_ALL_TESTS();
151 }
msg
void publish(const boost::shared_ptr< M > &message) const
TEST(Landmark, Measurement)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
void wait(int seconds)
static Time now()
ROSCPP_DECL void spinOnce()
int main(int argc, char **argv)


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Wed May 12 2021 02:16:29