global_oscillation_test.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2017, Locus Robotics
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the copyright holder nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 #include <ros/ros.h>
00036 #include <nav_core2/exceptions.h>
00037 #include <global_planner_tests/easy_costmap.h>
00038 #include <global_planner_tests/global_planner_tests.h>
00039 #include <dlux_global_planner/dlux_global_planner.h>
00040 #include <nav_grid/coordinate_conversion.h>
00041 #include <gtest/gtest.h>
00042 #include <string>
00043 #include <vector>
00044 
00045 const std::string map_path = "package://dlux_plugins/test/robert_frost.png";
00046 
00047 int barrier_x = 5;
00048 int barrier_y0 = 1;
00049 int barrier_y1 = 4;
00050 
00051 void setBarrier(nav_core2::Costmap& costmap, const unsigned char value)
00052 {
00053   for (int y=barrier_y0; y <= barrier_y1; y++)
00054   {
00055     costmap.setCost(barrier_x, y, value);
00056   }
00057 }
00058 
00059 bool pathsEqual(const nav_2d_msgs::Path2D& path_a, const nav_2d_msgs::Path2D& path_b)
00060 {
00061   if (path_a.header.frame_id != path_b.header.frame_id || path_a.poses.size() != path_b.poses.size())
00062   {
00063     return false;
00064   }
00065   for (unsigned int i=0; i < path_a.poses.size(); i++)
00066   {
00067     if (path_a.poses[i].x != path_b.poses[i].x || path_a.poses[i].y != path_b.poses[i].y
00068        || path_a.poses[i].theta != path_b.poses[i].theta)
00069     {
00070       return false;
00071     }
00072   }
00073   return true;
00074 }
00075 
00084 void replanning_test(const std::string& ns, bool expect_different,
00085                      bool path_caching = false, double improvement_threshold = -1.0)
00086 {
00087   TFListenerPtr tf = std::make_shared<tf::TransformListener>(ros::Duration(10));
00088   std::shared_ptr<global_planner_tests::EasyCostmap> easy_costmap =
00089         std::make_shared<global_planner_tests::EasyCostmap>(map_path, 1.0);
00090 
00091   dlux_global_planner::DluxGlobalPlanner global_planner;
00092   ros::NodeHandle nh("~"), private_nh("~/" + ns);
00093   private_nh.setParam("path_caching", path_caching);
00094   private_nh.setParam("improvement_threshold", improvement_threshold);
00095   private_nh.setParam("potential_calculator", "dlux_plugins::Dijkstra");
00096   private_nh.setParam("traceback", "dlux_plugins::GridPath");
00097   private_nh.setParam("print_statistics", true);
00098   global_planner.initialize(nh, ns, tf, easy_costmap);
00099 
00100   nav_core2::Costmap& costmap = *easy_costmap;
00101   const nav_grid::NavGridInfo& info = costmap.getInfo();
00102 
00103   nav_2d_msgs::Pose2DStamped start;
00104   nav_2d_msgs::Pose2DStamped goal;
00105 
00106   start.header.frame_id = info.frame_id;
00107   gridToWorld(info, 2, 5, start.pose.x, start.pose.y);
00108   goal.header.frame_id = info.frame_id;
00109   gridToWorld(info, 17, 5, goal.pose.x, goal.pose.y);
00110 
00111   // Block the lower path
00112   setBarrier(costmap, 254);
00113 
00114   // Get a path (takes upper route)
00115   nav_2d_msgs::Path2D first_path = global_planner.makePlan(start, goal);
00116 
00117   // Clear the barrier and open up shorter route
00118   setBarrier(costmap, 0);
00119 
00120   // Plan again
00121   nav_2d_msgs::Path2D second_path = global_planner.makePlan(start, goal);
00122 
00123   if (expect_different)
00124   {
00125     EXPECT_FALSE(pathsEqual(first_path, second_path));
00126   }
00127   else
00128   {
00129     EXPECT_TRUE(pathsEqual(first_path, second_path));
00130   }
00131 }
00132 
00133 TEST(GlobalPlannerReplanning, no_cache)
00134 {
00135   // With no path caching, we expect two different paths
00136   replanning_test("no_cache", true);
00137 }
00138 
00139 TEST(GlobalPlannerReplanning, any_cache)
00140 {
00141   // Once we turn on path caching, we expect they will be the same
00142   replanning_test("any_cache", false, true);
00143 }
00144 
00145 TEST(GlobalPlannerReplanning, improve_cache)
00146 {
00147   // If the new path is better, we expect they will be different
00148   replanning_test("improve_cache", true, true, 0.0);
00149 }
00150 
00151 TEST(GlobalPlannerReplanning, big_improve)
00152 {
00153   // The new path needs to be 1000pts better, so we expect they will be the same
00154   replanning_test("big_improve", false, true, 1000.0);
00155 }
00156 
00157 int main(int argc, char **argv)
00158 {
00159   ros::init(argc, argv, "global_oscillation_test");
00160   testing::InitGoogleTest(&argc, argv);
00161   return RUN_ALL_TESTS();
00162 }


dlux_plugins
Author(s):
autogenerated on Wed Jun 26 2019 20:09:55