test_obstacle_points.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, Ubiquity Robotics
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  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are
27  * those of the authors and should not be interpreted as representing official
28  * policies, either expressed or implied, of the FreeBSD Project.
29  *
30  */
31 
32 #include <gtest/gtest.h>
33 #include <gmock/gmock.h>
34 
35 #include <ros/ros.h>
36 #include <tf2_ros/buffer.h>
39 #include <sensor_msgs/Range.h>
40 #include <string>
41 
43 
44 class ObstaclePointsTests : public ::testing::Test {
45 protected:
46  virtual void SetUp() {
49 
50  sonar_pub = nh.advertise<sensor_msgs::Range>("/sonars", 1);
51  }
52 
53  virtual void TearDown() {
54  delete listener;
55  delete obstacle_points;
56  }
57 
59 
64 
66 };
67 
68 using ::testing::ElementsAre;
69 using ::testing::UnorderedElementsAre;
70 using ::testing::Pair;
71 
73  auto points = obstacle_points->get_points(ros::Duration(10));
74  auto lines = obstacle_points->get_lines(ros::Duration(10));
75  ASSERT_EQ(points.size(), 0u);
76  ASSERT_EQ(lines.size(), 0u);
77 }
78 
80  obstacle_points->add_test_point(tf2::Vector3(1.0,0.0,0.0));
81  auto points = obstacle_points->get_points(ros::Duration(10));
82  auto lines = obstacle_points->get_lines(ros::Duration(10));
83  ASSERT_EQ(points.size(), 1u);
84  ASSERT_EQ(points[0], tf2::Vector3(1.0,0.0,0.0));
85  ASSERT_EQ(lines.size(), 0u);
86 
87 
88  obstacle_points->add_test_point(tf2::Vector3(2.0,0.0,0.0));
91  ASSERT_EQ(points.size(), 2u);
92  ASSERT_THAT(points, ElementsAre(tf2::Vector3(1,0,0), tf2::Vector3(2,0,0)));
93  ASSERT_EQ(lines.size(), 0u);
94 
98  ASSERT_EQ(points.size(), 0u);
99  ASSERT_EQ(lines.size(), 0u);
100 }
101 
103  ros::Duration(0.1).sleep(); // If we don't do this the publish never happens for some reason
104  sensor_msgs::Range msg;
105  msg.field_of_view = 1.5708;
106  msg.min_range = 0.05;
107  msg.max_range = 10;
108  msg.radiation_type = sensor_msgs::Range::ULTRASOUND;
109  msg.header.stamp = ros::Time::now();
110  msg.header.frame_id = "base_link";
111  msg.range = 1.0;
112  sonar_pub.publish(msg);
113 
114  // TODO fix this to be less ugly
115  ros::spinOnce();
116  ros::Duration(0.1).sleep();
117  ros::spinOnce();
118 
119  auto points = obstacle_points->get_points(ros::Duration(10));
120  auto lines = obstacle_points->get_lines(ros::Duration(10));
121  ASSERT_EQ(points.size(), 2u);
122  ASSERT_NEAR(points[0].x(), sqrt(2)/2.0, 0.01);
123  ASSERT_NEAR(points[0].y(), -sqrt(2)/2.0, 0.01);
124  ASSERT_NEAR(points[1].x(), sqrt(2)/2.0, 0.01);
125  ASSERT_NEAR(points[1].y(), sqrt(2)/2.0, 0.01);
126 
127  // We expect one line that consists of those 2 points
128  ASSERT_EQ(lines.size(), 1u);
129  ASSERT_THAT(lines, ElementsAre(Pair(points[0], points[1])));
130 
131  // Do the same tests with a different range
132  msg.header.stamp = ros::Time::now();
133  msg.range = 2.0;
134  sonar_pub.publish(msg);
135 
136  // TODO fix this to be less ugly
137  ros::spinOnce();
138  ros::Duration(0.1).sleep();
139  ros::spinOnce();
140 
141  // We also want to make sure that the same sonar just updates the same points,
142  // instead of adding new ones
145  ASSERT_EQ(points.size(), 2u);
146  ASSERT_NEAR(points[0].x(), sqrt(2), 0.01);
147  ASSERT_NEAR(points[0].y(), -sqrt(2), 0.01);
148  ASSERT_NEAR(points[1].x(), sqrt(2), 0.01);
149  ASSERT_NEAR(points[1].y(), sqrt(2), 0.01);
150  ASSERT_EQ(lines.size(), 1u);
151  ASSERT_THAT(lines, ElementsAre(Pair(points[0], points[1])));
152 }
153 
154 int main(int argc, char **argv) {
155  ros::init(argc, argv, "obstacle_points_test");
156  ros::start();
157  testing::InitGoogleTest(&argc, argv);
158  return RUN_ALL_TESTS();
159 }
ROSCPP_DECL void start()
void publish(const boost::shared_ptr< M > &message) const
TEST_F(ObstaclePointsTests, noSensors)
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
tf2_ros::TransformBroadcaster tf_broadcaster
TFSIMD_FORCE_INLINE const tfScalar & y() const
tf2_ros::TransformListener * listener
TFSIMD_FORCE_INLINE const tfScalar & x() const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
void add_test_point(tf2::Vector3 p)
ObstaclePoints * obstacle_points
std::vector< Line > get_lines(ros::Duration max_age)
int main(int argc, char **argv)
std::vector< tf2::Vector3 > get_points(ros::Duration max_age)
static Time now()
ROSCPP_DECL void spinOnce()


move_basic
Author(s): Jim Vaughan
autogenerated on Fri Mar 26 2021 02:46:58