Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
navfn_planner.hpp
1 // Copyright (c) 2018 Intel Corporation
2 // Copyright (c) 2018 Simbe Robotics
3 // Copyright (c) 2019 Samsung Research America
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef NAV2_NAVFN_PLANNER__NAVFN_PLANNER_HPP_
18 #define NAV2_NAVFN_PLANNER__NAVFN_PLANNER_HPP_
19 
20 #include <chrono>
21 #include <string>
22 #include <memory>
23 #include <vector>
24 
25 #include "geometry_msgs/msg/point.hpp"
26 #include "geometry_msgs/msg/pose_stamped.hpp"
27 #include "nav2_core/global_planner.hpp"
28 #include "nav2_core/planner_exceptions.hpp"
29 #include "nav_msgs/msg/path.hpp"
30 #include "nav2_navfn_planner/navfn.hpp"
31 #include "nav2_navfn_planner/parameter_handler.hpp"
32 #include "nav2_util/robot_utils.hpp"
33 #include "nav2_ros_common/lifecycle_node.hpp"
34 #include "nav2_costmap_2d/costmap_2d_ros.hpp"
35 #include "nav2_util/geometry_utils.hpp"
36 #include "nav2_ros_common/tf2_factories.hpp"
37 
38 namespace nav2_navfn_planner
39 {
40 
42 {
43 public:
47  NavfnPlanner();
48 
52  ~NavfnPlanner();
53 
61  void configure(
62  const nav2::LifecycleNode::WeakPtr & parent,
63  std::string name, nav2::TransformBuffer::SharedPtr tf,
64  std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros) override;
65 
69  void cleanup() override;
70 
74  void activate() override;
75 
79  void deactivate() override;
80 
81 
89  nav_msgs::msg::Path createPlan(
90  const geometry_msgs::msg::PoseStamped & start,
91  const geometry_msgs::msg::PoseStamped & goal,
92  const std::vector<geometry_msgs::msg::PoseStamped> & viapoints,
93  std::function<bool()> cancel_checker) override;
94 
95 protected:
105  bool makePlan(
106  const geometry_msgs::msg::Pose & start,
107  const geometry_msgs::msg::Pose & goal, double tolerance,
108  std::function<bool()> cancel_checker,
109  nav_msgs::msg::Path & plan);
110 
116  bool computePotential(const geometry_msgs::msg::Point & world_point);
117 
125  const geometry_msgs::msg::Pose & goal,
126  nav_msgs::msg::Path & plan);
127 
134  const geometry_msgs::msg::Pose & goal,
135  nav_msgs::msg::Path & plan);
136 
143  double getPointPotential(const geometry_msgs::msg::Point & world_point);
144 
145  // Check for a valid potential value at a given point in the world
146  // - must call computePotential first
147  // - currently unused
148  // bool validPointPotential(const geometry_msgs::msg::Point & world_point);
149  // bool validPointPotential(const geometry_msgs::msg::Point & world_point, double tolerance);
150 
157  inline double squared_distance(
158  const geometry_msgs::msg::Pose & p1,
159  const geometry_msgs::msg::Pose & p2)
160  {
161  double dx = p1.position.x - p2.position.x;
162  double dy = p1.position.y - p2.position.y;
163  return dx * dx + dy * dy;
164  }
165 
174  bool worldToMap(double wx, double wy, unsigned int & mx, unsigned int & my);
175 
183  void mapToWorld(double mx, double my, double & wx, double & wy);
184 
190  void clearRobotCell(unsigned int mx, unsigned int my);
191 
196  bool isPlannerOutOfDate();
197 
198  // Planner based on ROS1 NavFn algorithm
199  std::unique_ptr<NavFn> planner_;
200 
201  // TF buffer
202  nav2::TransformBuffer::SharedPtr tf_;
203 
204  // Clock
205  rclcpp::Clock::SharedPtr clock_;
206 
207  // Logger
208  rclcpp::Logger logger_{rclcpp::get_logger("NavfnPlanner")};
209 
210  // Global Costmap
211  nav2_costmap_2d::Costmap2D * costmap_;
212 
213  // The global frame of the costmap
214  std::string global_frame_, name_;
215 
216  // parent node weak ptr
217  nav2::LifecycleNode::WeakPtr node_;
218 
219  Parameters * params_;
220  std::unique_ptr<nav2_navfn_planner::ParameterHandler> param_handler_;
221 };
222 
223 } // namespace nav2_navfn_planner
224 
225 #endif // NAV2_NAVFN_PLANNER__NAVFN_PLANNER_HPP_
Abstract interface for global planners to adhere to with pluginlib.
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:69
double squared_distance(const geometry_msgs::msg::Pose &p1, const geometry_msgs::msg::Pose &p2)
Compute the squared distance between two points.
double getPointPotential(const geometry_msgs::msg::Point &world_point)
Compute the potential, or navigation cost, at a given point in the world must call computePotential f...
bool computePotential(const geometry_msgs::msg::Point &world_point)
Compute the navigation function given a seed point in the world to start from.
nav_msgs::msg::Path createPlan(const geometry_msgs::msg::PoseStamped &start, const geometry_msgs::msg::PoseStamped &goal, const std::vector< geometry_msgs::msg::PoseStamped > &viapoints, std::function< bool()> cancel_checker) override
Creating a plan from start and goal poses.
void activate() override
Activate lifecycle node.
void mapToWorld(double mx, double my, double &wx, double &wy)
Transform a point from map to world frame.
bool makePlan(const geometry_msgs::msg::Pose &start, const geometry_msgs::msg::Pose &goal, double tolerance, std::function< bool()> cancel_checker, nav_msgs::msg::Path &plan)
Compute a plan given start and goal poses, provided in global world frame.
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my)
Transform a point from world to map frame.
bool isPlannerOutOfDate()
Determine if a new planner object should be made.
void configure(const nav2::LifecycleNode::WeakPtr &parent, std::string name, nav2::TransformBuffer::SharedPtr tf, std::shared_ptr< nav2_costmap_2d::Costmap2DROS > costmap_ros) override
Configuring plugin.
void clearRobotCell(unsigned int mx, unsigned int my)
Set the corresponding cell cost to be free space.
void smoothApproachToGoal(const geometry_msgs::msg::Pose &goal, nav_msgs::msg::Path &plan)
Remove artifacts at the end of the path - originated from planning on a discretized world.
void cleanup() override
Cleanup lifecycle node.
void deactivate() override
Deactivate lifecycle node.
bool getPlanFromPotential(const geometry_msgs::msg::Pose &goal, nav_msgs::msg::Path &plan)
Compute a plan to a goal from a potential - must call computePotential first.