Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
path_ops.cpp
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 <cmath>
36 
37 #include "nav_2d_utils/path_ops.hpp"
38 #include "tf2/convert.hpp"
39 #include "tf2/utils.hpp"
40 #include "nav2_util/geometry_utils.hpp"
41 
42 using std::sqrt;
43 
44 namespace nav_2d_utils
45 {
46 nav_msgs::msg::Path adjustPlanResolution(
47  const nav_msgs::msg::Path & global_plan_in,
48  double resolution)
49 {
50  nav_msgs::msg::Path global_plan_out;
51  if (global_plan_in.poses.size() == 0) {
52  return global_plan_out;
53  }
54 
55  geometry_msgs::msg::PoseStamped last = global_plan_in.poses[0];
56  global_plan_out.poses.push_back(last);
57 
58  // we can take "holes" in the plan smaller than 2 grid cells (squared = 4)
59  double min_sq_resolution = resolution * resolution * 4.0;
60 
61  for (unsigned int i = 1; i < global_plan_in.poses.size(); ++i) {
62  geometry_msgs::msg::PoseStamped loop = global_plan_in.poses[i];
63  double sq_dist = (loop.pose.position.x - last.pose.position.x) *
64  (loop.pose.position.x - last.pose.position.x) +
65  (loop.pose.position.y - last.pose.position.y) *
66  (loop.pose.position.y - last.pose.position.y);
67  if (sq_dist > min_sq_resolution) {
68  // add points in-between
69  double diff = sqrt(sq_dist) - sqrt(min_sq_resolution);
70  int steps = static_cast<int>(diff / resolution) - 1;
71  double steps_double = static_cast<double>(steps);
72 
73  double theta_last = tf2::getYaw(last.pose.orientation);
74  double theta_loop = tf2::getYaw(loop.pose.orientation);
75  double delta_x = (loop.pose.position.x - last.pose.position.x) / steps_double;
76  double delta_y = (loop.pose.position.y - last.pose.position.y) / steps_double;
77  double delta_t = (theta_loop - theta_last) / steps_double;
78 
79  for (int j = 1; j < steps; ++j) {
80  geometry_msgs::msg::PoseStamped pose;
81  pose.pose.position.x = last.pose.position.x + j * delta_x;
82  pose.pose.position.y = last.pose.position.y + j * delta_y;
83  pose.pose.orientation = nav2_util::geometry_utils::orientationAroundZAxis(
84  theta_last + j * delta_t);
85  global_plan_out.poses.push_back(pose);
86  }
87  }
88  global_plan_out.poses.push_back(global_plan_in.poses[i]);
89  last.pose.position.x = loop.pose.position.x;
90  last.pose.position.y = loop.pose.position.y;
91  }
92  return global_plan_out;
93 }
94 } // namespace nav_2d_utils