Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
planner_server.hpp
1 // Copyright (c) 2019 Samsung Research America
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef NAV2_PLANNER__PLANNER_SERVER_HPP_
16 #define NAV2_PLANNER__PLANNER_SERVER_HPP_
17 
18 #include <chrono>
19 #include <string>
20 #include <memory>
21 #include <vector>
22 #include <unordered_map>
23 #include <mutex>
24 
25 #include "geometry_msgs/msg/point.hpp"
26 #include "geometry_msgs/msg/pose_stamped.hpp"
27 #include "nav_msgs/msg/path.hpp"
28 #include "nav2_util/lifecycle_node.hpp"
29 #include "nav2_msgs/action/compute_path_to_pose.hpp"
30 #include "nav2_msgs/action/compute_path_through_poses.hpp"
31 #include "nav2_msgs/msg/costmap.hpp"
32 #include "nav2_util/robot_utils.hpp"
33 #include "nav2_util/simple_action_server.hpp"
34 #include "visualization_msgs/msg/marker.hpp"
35 #include "tf2_ros/transform_listener.h"
36 #include "tf2_ros/create_timer_ros.h"
37 #include "nav2_costmap_2d/costmap_2d_ros.hpp"
38 #include "nav2_costmap_2d/footprint_collision_checker.hpp"
39 #include "pluginlib/class_loader.hpp"
40 #include "pluginlib/class_list_macros.hpp"
41 #include "nav2_core/global_planner.hpp"
42 #include "nav2_msgs/srv/is_path_valid.hpp"
43 
44 namespace nav2_planner
45 {
52 {
53 public:
58  explicit PlannerServer(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
63 
64  using PlannerMap = std::unordered_map<std::string, nav2_core::GlobalPlanner::Ptr>;
65 
72  nav_msgs::msg::Path getPlan(
73  const geometry_msgs::msg::PoseStamped & start,
74  const geometry_msgs::msg::PoseStamped & goal,
75  const std::string & planner_id);
76 
77 protected:
83  nav2_util::CallbackReturn on_configure(const rclcpp_lifecycle::State & state) override;
89  nav2_util::CallbackReturn on_activate(const rclcpp_lifecycle::State & state) override;
95  nav2_util::CallbackReturn on_deactivate(const rclcpp_lifecycle::State & state) override;
101  nav2_util::CallbackReturn on_cleanup(const rclcpp_lifecycle::State & state) override;
107  nav2_util::CallbackReturn on_shutdown(const rclcpp_lifecycle::State & state) override;
108 
109  using ActionToPose = nav2_msgs::action::ComputePathToPose;
110  using ActionThroughPoses = nav2_msgs::action::ComputePathThroughPoses;
113 
119  template<typename T>
120  bool isServerInactive(std::unique_ptr<nav2_util::SimpleActionServer<T>> & action_server);
121 
127  template<typename T>
128  bool isCancelRequested(std::unique_ptr<nav2_util::SimpleActionServer<T>> & action_server);
129 
134  void waitForCostmap();
135 
142  template<typename T>
144  std::unique_ptr<nav2_util::SimpleActionServer<T>> & action_server,
145  typename std::shared_ptr<const typename T::Goal> goal);
146 
154  template<typename T>
155  bool getStartPose(
156  std::unique_ptr<nav2_util::SimpleActionServer<T>> & action_server,
157  typename std::shared_ptr<const typename T::Goal> goal,
158  geometry_msgs::msg::PoseStamped & start);
159 
168  template<typename T>
170  std::unique_ptr<nav2_util::SimpleActionServer<T>> & action_server,
171  geometry_msgs::msg::PoseStamped & curr_start,
172  geometry_msgs::msg::PoseStamped & curr_goal);
173 
182  template<typename T>
183  bool validatePath(
184  std::unique_ptr<nav2_util::SimpleActionServer<T>> & action_server,
185  const geometry_msgs::msg::PoseStamped & curr_goal,
186  const nav_msgs::msg::Path & path,
187  const std::string & planner_id);
188 
189  // Our action server implements the ComputePathToPose action
190  std::unique_ptr<ActionServerToPose> action_server_pose_;
191  std::unique_ptr<ActionServerThroughPoses> action_server_poses_;
192 
197  void computePlan();
198 
204 
210  void isPathValid(
211  const std::shared_ptr<nav2_msgs::srv::IsPathValid::Request> request,
212  std::shared_ptr<nav2_msgs::srv::IsPathValid::Response> response);
213 
218  void publishPlan(const nav_msgs::msg::Path & path);
219 
224  rcl_interfaces::msg::SetParametersResult
225  dynamicParametersCallback(std::vector<rclcpp::Parameter> parameters);
226 
227  // Dynamic parameters handler
228  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr dyn_params_handler_;
229  std::mutex dynamic_params_lock_;
230 
231  // Planner
232  PlannerMap planners_;
233  pluginlib::ClassLoader<nav2_core::GlobalPlanner> gp_loader_;
234  std::vector<std::string> default_ids_;
235  std::vector<std::string> default_types_;
236  std::vector<std::string> planner_ids_;
237  std::vector<std::string> planner_types_;
238  double max_planner_duration_;
239  std::string planner_ids_concat_;
240 
241  // TF buffer
242  std::shared_ptr<tf2_ros::Buffer> tf_;
243 
244  // Global Costmap
245  std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros_;
246  std::unique_ptr<nav2_util::NodeThread> costmap_thread_;
247  nav2_costmap_2d::Costmap2D * costmap_;
248  std::unique_ptr<nav2_costmap_2d::FootprintCollisionChecker<nav2_costmap_2d::Costmap2D *>>
249  collision_checker_;
250 
251  // Publishers for the path
252  rclcpp_lifecycle::LifecyclePublisher<nav_msgs::msg::Path>::SharedPtr plan_publisher_;
253 
254  // Service to deterime if the path is valid
255  rclcpp::Service<nav2_msgs::srv::IsPathValid>::SharedPtr is_path_valid_service_;
256 };
257 
258 } // namespace nav2_planner
259 
260 #endif // NAV2_PLANNER__PLANNER_SERVER_HPP_
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:68
An action server implements the behavior tree's ComputePathToPose interface and hosts various plugins...
void publishPlan(const nav_msgs::msg::Path &path)
Publish a path for visualization purposes.
void computePlan()
The action server callback which calls planner to get the path ComputePathToPose.
void getPreemptedGoalIfRequested(std::unique_ptr< nav2_util::SimpleActionServer< T >> &action_server, typename std::shared_ptr< const typename T::Goal > goal)
Check if an action server has a preemption request and replaces the goal with the new preemption goal...
nav2_util::CallbackReturn on_cleanup(const rclcpp_lifecycle::State &state) override
Reset member variables.
void waitForCostmap()
Wait for costmap to be valid with updated sensor data or repopulate after a clearing recovery....
PlannerServer(const rclcpp::NodeOptions &options=rclcpp::NodeOptions())
A constructor for nav2_planner::PlannerServer.
~PlannerServer()
A destructor for nav2_planner::PlannerServer.
bool isServerInactive(std::unique_ptr< nav2_util::SimpleActionServer< T >> &action_server)
Check if an action server is valid / active.
void isPathValid(const std::shared_ptr< nav2_msgs::srv::IsPathValid::Request > request, std::shared_ptr< nav2_msgs::srv::IsPathValid::Response > response)
The service callback to determine if the path is still valid.
void computePlanThroughPoses()
The action server callback which calls planner to get the path ComputePathThroughPoses.
bool transformPosesToGlobalFrame(std::unique_ptr< nav2_util::SimpleActionServer< T >> &action_server, geometry_msgs::msg::PoseStamped &curr_start, geometry_msgs::msg::PoseStamped &curr_goal)
Transform start and goal poses into the costmap global frame for path planning plugins to utilize.
bool getStartPose(std::unique_ptr< nav2_util::SimpleActionServer< T >> &action_server, typename std::shared_ptr< const typename T::Goal > goal, geometry_msgs::msg::PoseStamped &start)
Get the starting pose from costmap or message, if valid.
rcl_interfaces::msg::SetParametersResult dynamicParametersCallback(std::vector< rclcpp::Parameter > parameters)
Callback executed when a parameter change is detected.
bool isCancelRequested(std::unique_ptr< nav2_util::SimpleActionServer< T >> &action_server)
Check if an action server has a cancellation request pending.
nav_msgs::msg::Path getPlan(const geometry_msgs::msg::PoseStamped &start, const geometry_msgs::msg::PoseStamped &goal, const std::string &planner_id)
Method to get plan from the desired plugin.
nav2_util::CallbackReturn on_configure(const rclcpp_lifecycle::State &state) override
Configure member variables and initializes planner.
bool validatePath(std::unique_ptr< nav2_util::SimpleActionServer< T >> &action_server, const geometry_msgs::msg::PoseStamped &curr_goal, const nav_msgs::msg::Path &path, const std::string &planner_id)
Validate that the path contains a meaningful path.
nav2_util::CallbackReturn on_shutdown(const rclcpp_lifecycle::State &state) override
Called when in shutdown state.
nav2_util::CallbackReturn on_activate(const rclcpp_lifecycle::State &state) override
Activate member variables.
nav2_util::CallbackReturn on_deactivate(const rclcpp_lifecycle::State &state) override
Deactivate member variables.
A lifecycle node wrapper to enable common Nav2 needs such as manipulating parameters.
An action server wrapper to make applications simpler using Actions.