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 <memory>
43 #include <string>
44 #include <vector>
45 
46 const char map_path[] = "package://dlux_plugins/test/robert_frost.png";
47 
48 int barrier_x = 5;
49 int barrier_y0 = 1;
50 int barrier_y1 = 4;
51 
52 void setBarrier(nav_core2::Costmap& costmap, const unsigned char value)
53 {
54  for (int y=barrier_y0; y <= barrier_y1; y++)
55  {
56  costmap.setCost(barrier_x, y, value);
57  }
58 }
59 
60 bool pathsEqual(const nav_2d_msgs::Path2D& path_a, const nav_2d_msgs::Path2D& path_b)
61 {
62  if (path_a.header.frame_id != path_b.header.frame_id || path_a.poses.size() != path_b.poses.size())
63  {
64  return false;
65  }
66  for (unsigned int i=0; i < path_a.poses.size(); i++)
67  {
68  if (path_a.poses[i].x != path_b.poses[i].x || path_a.poses[i].y != path_b.poses[i].y
69  || path_a.poses[i].theta != path_b.poses[i].theta)
70  {
71  return false;
72  }
73  }
74  return true;
75 }
76 
85 void replanning_test(const std::string& ns, bool expect_different,
86  bool path_caching = false, double improvement_threshold = -1.0)
87 {
88  TFListenerPtr tf = std::make_shared<tf::TransformListener>(ros::Duration(10));
89  std::shared_ptr<global_planner_tests::EasyCostmap> easy_costmap =
90  std::make_shared<global_planner_tests::EasyCostmap>(std::string(map_path), 1.0);
91 
93  ros::NodeHandle nh("~"), private_nh("~/" + ns);
94  private_nh.setParam("path_caching", path_caching);
95  private_nh.setParam("improvement_threshold", improvement_threshold);
96  private_nh.setParam("potential_calculator", "dlux_plugins::Dijkstra");
97  private_nh.setParam("traceback", "dlux_plugins::GridPath");
98  private_nh.setParam("print_statistics", true);
99  global_planner.initialize(nh, ns, tf, easy_costmap);
100 
101  nav_core2::Costmap& costmap = *easy_costmap;
102  const nav_grid::NavGridInfo& info = costmap.getInfo();
103 
104  nav_2d_msgs::Pose2DStamped start;
105  nav_2d_msgs::Pose2DStamped goal;
106 
107  start.header.frame_id = info.frame_id;
108  gridToWorld(info, 2, 5, start.pose.x, start.pose.y);
109  goal.header.frame_id = info.frame_id;
110  gridToWorld(info, 17, 5, goal.pose.x, goal.pose.y);
111 
112  // Block the lower path
113  setBarrier(costmap, 254);
114 
115  // Get a path (takes upper route)
116  nav_2d_msgs::Path2D first_path = global_planner.makePlan(start, goal);
117 
118  // Clear the barrier and open up shorter route
119  setBarrier(costmap, 0);
120 
121  // Plan again
122  nav_2d_msgs::Path2D second_path = global_planner.makePlan(start, goal);
123 
124  if (expect_different)
125  {
126  EXPECT_FALSE(pathsEqual(first_path, second_path));
127  }
128  else
129  {
130  EXPECT_TRUE(pathsEqual(first_path, second_path));
131  }
132 }
133 
134 TEST(GlobalPlannerReplanning, no_cache)
135 {
136  // With no path caching, we expect two different paths
137  replanning_test("no_cache", true);
138 }
139 
140 TEST(GlobalPlannerReplanning, any_cache)
141 {
142  // Once we turn on path caching, we expect they will be the same
143  replanning_test("any_cache", false, true);
144 }
145 
146 TEST(GlobalPlannerReplanning, improve_cache)
147 {
148  // If the new path is better, we expect they will be different
149  replanning_test("improve_cache", true, true, 0.0);
150 }
151 
152 TEST(GlobalPlannerReplanning, big_improve)
153 {
154  // The new path needs to be 1000pts better, so we expect they will be the same
155  replanning_test("big_improve", false, true, 1000.0);
156 }
157 
158 int main(int argc, char **argv)
159 {
160  ros::init(argc, argv, "global_oscillation_test");
161  testing::InitGoogleTest(&argc, argv);
162  return RUN_ALL_TESTS();
163 }
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)
const char map_path[]
void gridToWorld(const NavGridInfo &info, int mx, int my, double &wx, double &wy)
TFSIMD_FORCE_INLINE const tfScalar & y() const
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 Sun Jan 10 2021 04:08:54