global_oscillation_test.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <ros/ros.h>
36 #include <nav_core2/exceptions.h>
37 #include <global_planner_tests/easy_costmap.h>
38 #include <global_planner_tests/global_planner_tests.h>
41 #include <gtest/gtest.h>
42 #include <string>
43 #include <vector>
44 
45 const std::string map_path = "package://dlux_plugins/test/robert_frost.png";
46 
47 int barrier_x = 5;
48 int barrier_y0 = 1;
49 int barrier_y1 = 4;
50 
51 void setBarrier(nav_core2::Costmap& costmap, const unsigned char value)
52 {
53  for (int y=barrier_y0; y <= barrier_y1; y++)
54  {
55  costmap.setCost(barrier_x, y, value);
56  }
57 }
58 
59 bool pathsEqual(const nav_2d_msgs::Path2D& path_a, const nav_2d_msgs::Path2D& path_b)
60 {
61  if (path_a.header.frame_id != path_b.header.frame_id || path_a.poses.size() != path_b.poses.size())
62  {
63  return false;
64  }
65  for (unsigned int i=0; i < path_a.poses.size(); i++)
66  {
67  if (path_a.poses[i].x != path_b.poses[i].x || path_a.poses[i].y != path_b.poses[i].y
68  || path_a.poses[i].theta != path_b.poses[i].theta)
69  {
70  return false;
71  }
72  }
73  return true;
74 }
75 
84 void replanning_test(const std::string& ns, bool expect_different,
85  bool path_caching = false, double improvement_threshold = -1.0)
86 {
87  TFListenerPtr tf = std::make_shared<tf::TransformListener>(ros::Duration(10));
88  std::shared_ptr<global_planner_tests::EasyCostmap> easy_costmap =
89  std::make_shared<global_planner_tests::EasyCostmap>(map_path, 1.0);
90 
92  ros::NodeHandle nh("~"), private_nh("~/" + ns);
93  private_nh.setParam("path_caching", path_caching);
94  private_nh.setParam("improvement_threshold", improvement_threshold);
95  private_nh.setParam("potential_calculator", "dlux_plugins::Dijkstra");
96  private_nh.setParam("traceback", "dlux_plugins::GridPath");
97  private_nh.setParam("print_statistics", true);
98  global_planner.initialize(nh, ns, tf, easy_costmap);
99 
100  nav_core2::Costmap& costmap = *easy_costmap;
101  const nav_grid::NavGridInfo& info = costmap.getInfo();
102 
103  nav_2d_msgs::Pose2DStamped start;
104  nav_2d_msgs::Pose2DStamped goal;
105 
106  start.header.frame_id = info.frame_id;
107  gridToWorld(info, 2, 5, start.pose.x, start.pose.y);
108  goal.header.frame_id = info.frame_id;
109  gridToWorld(info, 17, 5, goal.pose.x, goal.pose.y);
110 
111  // Block the lower path
112  setBarrier(costmap, 254);
113 
114  // Get a path (takes upper route)
115  nav_2d_msgs::Path2D first_path = global_planner.makePlan(start, goal);
116 
117  // Clear the barrier and open up shorter route
118  setBarrier(costmap, 0);
119 
120  // Plan again
121  nav_2d_msgs::Path2D second_path = global_planner.makePlan(start, goal);
122 
123  if (expect_different)
124  {
125  EXPECT_FALSE(pathsEqual(first_path, second_path));
126  }
127  else
128  {
129  EXPECT_TRUE(pathsEqual(first_path, second_path));
130  }
131 }
132 
133 TEST(GlobalPlannerReplanning, no_cache)
134 {
135  // With no path caching, we expect two different paths
136  replanning_test("no_cache", true);
137 }
138 
139 TEST(GlobalPlannerReplanning, any_cache)
140 {
141  // Once we turn on path caching, we expect they will be the same
142  replanning_test("any_cache", false, true);
143 }
144 
145 TEST(GlobalPlannerReplanning, improve_cache)
146 {
147  // If the new path is better, we expect they will be different
148  replanning_test("improve_cache", true, true, 0.0);
149 }
150 
151 TEST(GlobalPlannerReplanning, big_improve)
152 {
153  // The new path needs to be 1000pts better, so we expect they will be the same
154  replanning_test("big_improve", false, true, 1000.0);
155 }
156 
157 int main(int argc, char **argv)
158 {
159  ros::init(argc, argv, "global_oscillation_test");
160  testing::InitGoogleTest(&argc, argv);
161  return RUN_ALL_TESTS();
162 }
void setBarrier(nav_core2::Costmap &costmap, const unsigned char value)
void replanning_test(const std::string &ns, bool expect_different, bool path_caching=false, double improvement_threshold=-1.0)
Check to see whether path caching is working as expected.
void setCost(const unsigned int x, const unsigned int y, const unsigned char cost)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void gridToWorld(const NavGridInfo &info, int mx, int my, double &wx, double &wy)
TFSIMD_FORCE_INLINE const tfScalar & y() const
const std::string map_path
bool pathsEqual(const nav_2d_msgs::Path2D &path_a, const nav_2d_msgs::Path2D &path_b)
TEST(GlobalPlannerReplanning, no_cache)
nav_2d_msgs::Path2D makePlan(const nav_2d_msgs::Pose2DStamped &start, const nav_2d_msgs::Pose2DStamped &goal) override
NavGridInfo getInfo() const
void initialize(const ros::NodeHandle &parent, const std::string &name, TFListenerPtr tf, nav_core2::Costmap::Ptr costmap) override
int main(int argc, char **argv)
std::shared_ptr< tf::TransformListener > TFListenerPtr
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const


dlux_plugins
Author(s):
autogenerated on Wed Jun 26 2019 20:06:32