Nav2 Navigation Stack - lyrical  lyrical
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_ros_common/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_ros_common/simple_action_server.hpp"
34 #include "nav2_ros_common/service_server.hpp"
35 #include "nav2_ros_common/tf2_factories.hpp"
36 #include "nav2_costmap_2d/costmap_2d_ros.hpp"
37 #include "pluginlib/class_loader.hpp"
38 #include "pluginlib/class_list_macros.hpp"
39 #include "nav2_core/global_planner.hpp"
40 #include "nav2_msgs/srv/is_path_valid.hpp"
41 #include "nav2_core/planner_exceptions.hpp"
42 #include "nav2_planner/is_path_valid_service.hpp"
43 #include "nav2_planner/parameter_handler.hpp"
44 
45 namespace nav2_planner
46 {
53 {
54 public:
59  explicit PlannerServer(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
64 
65  using PlannerMap = std::unordered_map<std::string, nav2_core::GlobalPlanner::Ptr>;
66 
75  nav_msgs::msg::Path getPlan(
76  const geometry_msgs::msg::PoseStamped & start,
77  const geometry_msgs::msg::PoseStamped & goal,
78  const std::vector<geometry_msgs::msg::PoseStamped> & viapoints,
79  const std::string & planner_id,
80  std::function<bool()> cancel_checker);
81 
82 protected:
88  nav2::CallbackReturn on_configure(const rclcpp_lifecycle::State & state) override;
94  nav2::CallbackReturn on_activate(const rclcpp_lifecycle::State & state) override;
100  nav2::CallbackReturn on_deactivate(const rclcpp_lifecycle::State & state) override;
106  nav2::CallbackReturn on_cleanup(const rclcpp_lifecycle::State & state) override;
112  nav2::CallbackReturn on_shutdown(const rclcpp_lifecycle::State & state) override;
113 
114  using ActionToPose = nav2_msgs::action::ComputePathToPose;
115  using ActionToPoseResult = ActionToPose::Result;
116  using ActionThroughPoses = nav2_msgs::action::ComputePathThroughPoses;
117  using ActionThroughPosesResult = ActionThroughPoses::Result;
120 
126  template<typename T>
127  bool goalReceived(std::shared_ptr<const typename T::Goal> goal);
128 
134  template<typename T>
135  bool isServerInactive(typename nav2::SimpleActionServer<T>::SharedPtr & action_server);
136 
142  template<typename T>
143  bool isCancelRequested(typename nav2::SimpleActionServer<T>::SharedPtr & action_server);
144 
150  double waitForCostmap();
151 
158  template<typename T>
160  typename nav2::SimpleActionServer<T>::SharedPtr & action_server,
161  typename std::shared_ptr<const typename T::Goal> goal);
162 
170  template<typename T>
171  bool getStartPose(
172  typename std::shared_ptr<const typename T::Goal> goal,
173  geometry_msgs::msg::PoseStamped & start);
174 
183  geometry_msgs::msg::PoseStamped & curr_start,
184  geometry_msgs::msg::PoseStamped & curr_goal);
185 
194  template<typename T>
195  bool validatePath(
196  const geometry_msgs::msg::PoseStamped & curr_goal,
197  const nav_msgs::msg::Path & path,
198  const std::string & planner_id);
199 
204  void computePlan();
205 
211 
216  void publishPlan(const nav_msgs::msg::Path & path);
217 
218  void exceptionWarning(
219  const geometry_msgs::msg::PoseStamped & start,
220  const geometry_msgs::msg::PoseStamped & goal,
221  const std::string & planner_id,
222  const std::exception & ex,
223  std::string & msg);
224 
225  // Our action server implements the ComputePathToPose action
226  typename ActionServerToPose::SharedPtr action_server_pose_;
227  typename ActionServerThroughPoses::SharedPtr action_server_poses_;
228 
229  // Parameter handler
230  std::unique_ptr<nav2_planner::ParameterHandler> param_handler_;
231  Parameters * params_;
232 
233  // Planner
234  PlannerMap planners_;
235  pluginlib::ClassLoader<nav2_core::GlobalPlanner> gp_loader_;
236  std::string planner_ids_concat_;
237 
238  // TF buffer
239  nav2::TransformBuffer::SharedPtr tf_;
240 
241  // Global Costmap
242  std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros_;
243  std::unique_ptr<nav2::NodeThread> costmap_thread_;
244  nav2_costmap_2d::Costmap2D * costmap_;
245 
246  // Publishers for the path
247  nav2::Publisher<nav_msgs::msg::Path>::SharedPtr plan_publisher_;
248 
249  // Service to determine if the path is valid
250  std::unique_ptr<IsPathValidService> is_path_valid_service_;
251 };
252 
253 } // namespace nav2_planner
254 
255 #endif // NAV2_PLANNER__PLANNER_SERVER_HPP_
A lifecycle node wrapper to enable common Nav2 needs such as manipulating parameters.
An action server wrapper to make applications simpler using Actions.
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:69
An action server implements the behavior tree's ComputePathToPose interface and hosts various plugins...
nav2::CallbackReturn on_configure(const rclcpp_lifecycle::State &state) override
Configure member variables and initializes planner.
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.
nav2::CallbackReturn on_deactivate(const rclcpp_lifecycle::State &state) override
Deactivate member variables.
bool isServerInactive(typename nav2::SimpleActionServer< T >::SharedPtr &action_server)
Check if an action server is valid / active.
bool getStartPose(typename std::shared_ptr< const typename T::Goal > goal, geometry_msgs::msg::PoseStamped &start)
Get the starting pose from costmap or message, if valid.
PlannerServer(const rclcpp::NodeOptions &options=rclcpp::NodeOptions())
A constructor for nav2_planner::PlannerServer.
~PlannerServer()
A destructor for nav2_planner::PlannerServer.
bool goalReceived(std::shared_ptr< const typename T::Goal > goal)
Goal received callback to validate a new goal before acceptance.
nav2::CallbackReturn on_shutdown(const rclcpp_lifecycle::State &state) override
Called when in shutdown state.
void computePlanThroughPoses()
The action server callback which calls planner to get the path ComputePathThroughPoses.
bool isCancelRequested(typename nav2::SimpleActionServer< T >::SharedPtr &action_server)
Check if an action server has a cancellation request pending.
nav2::CallbackReturn on_cleanup(const rclcpp_lifecycle::State &state) override
Reset member variables.
double waitForCostmap()
Wait for costmap to be valid with updated sensor data or repopulate after a clearing recovery....
void getPreemptedGoalIfRequested(typename nav2::SimpleActionServer< T >::SharedPtr &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...
bool validatePath(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.
nav_msgs::msg::Path getPlan(const geometry_msgs::msg::PoseStamped &start, const geometry_msgs::msg::PoseStamped &goal, const std::vector< geometry_msgs::msg::PoseStamped > &viapoints, const std::string &planner_id, std::function< bool()> cancel_checker)
Method to get plan from the desired plugin.
nav2::CallbackReturn on_activate(const rclcpp_lifecycle::State &state) override
Activate member variables.
bool transformPosesToGlobalFrame(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.