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