Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
route_server.hpp
1 // Copyright (c) 2025 Open Navigation LLC
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_ROUTE__ROUTE_SERVER_HPP_
16 #define NAV2_ROUTE__ROUTE_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 "nav2_util/lifecycle_node.hpp"
26 #include "tf2_ros/transform_listener.h"
27 #include "tf2_ros/create_timer_ros.h"
28 #include "nav2_util/simple_action_server.hpp"
29 #include "nav2_util/node_utils.hpp"
30 #include "nav2_util/robot_utils.hpp"
31 #include "nav2_msgs/action/compute_route.hpp"
32 #include "nav2_msgs/action/compute_and_track_route.hpp"
33 #include "nav2_msgs/msg/route.hpp"
34 #include "nav2_msgs/msg/route_node.hpp"
35 #include "nav2_msgs/srv/set_route_graph.hpp"
36 #include "nav2_core/route_exceptions.hpp"
37 #include "visualization_msgs/msg/marker_array.hpp"
38 
39 #include "nav2_route/types.hpp"
40 #include "nav2_route/utils.hpp"
41 #include "nav2_route/graph_loader.hpp"
42 #include "nav2_route/route_planner.hpp"
43 #include "nav2_route/path_converter.hpp"
44 #include "nav2_route/route_tracker.hpp"
45 #include "nav2_route/goal_intent_extractor.hpp"
46 
47 namespace nav2_route
48 {
55 {
56 public:
57  using ComputeRoute = nav2_msgs::action::ComputeRoute;
58  using ComputeRouteGoal = ComputeRoute::Goal;
59  using ComputeRouteResult = ComputeRoute::Result;
61 
62  using ComputeAndTrackRoute = nav2_msgs::action::ComputeAndTrackRoute;
63  using ComputeAndTrackRouteGoal = ComputeAndTrackRoute::Goal;
64  using ComputeAndTrackRouteFeedback = ComputeAndTrackRoute::Feedback;
65  using ComputeAndTrackRouteResult = ComputeAndTrackRoute::Result;
67 
72  explicit RouteServer(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
76  ~RouteServer() = default;
77 
78 protected:
84  nav2_util::CallbackReturn on_configure(const rclcpp_lifecycle::State & state) override;
85 
91  nav2_util::CallbackReturn on_activate(const rclcpp_lifecycle::State & state) override;
92 
98  nav2_util::CallbackReturn on_deactivate(const rclcpp_lifecycle::State & state) override;
99 
105  nav2_util::CallbackReturn on_cleanup(const rclcpp_lifecycle::State & state) override;
106 
112  nav2_util::CallbackReturn on_shutdown(const rclcpp_lifecycle::State & state) override;
113 
117  void computeRoute();
118  void computeAndTrackRoute();
119 
137  template<typename GoalT>
139  const std::shared_ptr<const GoalT> goal,
140  ReroutingState & rerouting_info);
141 
148  template<typename GoalT>
149  Route findRoute(const std::shared_ptr<const GoalT> goal);
150 
155  template<typename ActionT>
156  void processRouteRequest(
157  std::shared_ptr<nav2_util::SimpleActionServer<ActionT>> & action_server);
158 
164  rclcpp::Duration findPlanningDuration(const rclcpp::Time & start_time);
165 
171  template<typename ActionT>
172  bool isRequestValid(std::shared_ptr<nav2_util::SimpleActionServer<ActionT>> & action_server);
173 
182  std::shared_ptr<ComputeRoute::Result> result,
183  const Route & route,
184  const nav_msgs::msg::Path & path,
185  const rclcpp::Duration & planning_duration);
186 
195  std::shared_ptr<ComputeAndTrackRoute::Result>/*result*/,
196  const Route & /*route*/,
197  const nav_msgs::msg::Path & /*path*/,
198  const rclcpp::Duration & /*planning_duration*/);
199 
206  void setRouteGraph(
207  const std::shared_ptr<rmw_request_id_t>/*request_header*/,
208  const std::shared_ptr<nav2_msgs::srv::SetRouteGraph::Request> request,
209  std::shared_ptr<nav2_msgs::srv::SetRouteGraph::Response> response);
210 
216  template<typename GoalT>
217  void exceptionWarning(const std::shared_ptr<const GoalT> goal, const std::exception & ex);
218 
219  std::shared_ptr<ComputeRouteServer> compute_route_server_;
220  std::shared_ptr<ComputeAndTrackRouteServer> compute_and_track_route_server_;
221 
222  // TF
223  std::shared_ptr<tf2_ros::Buffer> tf_;
224  std::shared_ptr<tf2_ros::TransformListener> transform_listener_;
225 
226  // Publish the route for visualization
227  rclcpp_lifecycle::LifecyclePublisher<visualization_msgs::msg::MarkerArray>::SharedPtr
228  graph_vis_publisher_;
229 
230  // Set or modify graph
231  rclcpp::Service<nav2_msgs::srv::SetRouteGraph>::SharedPtr set_graph_service_;
232 
233  // Internal tools
234  std::shared_ptr<GraphLoader> graph_loader_;
235  std::shared_ptr<RoutePlanner> route_planner_;
236  std::shared_ptr<RouteTracker> route_tracker_;
237  std::shared_ptr<PathConverter> path_converter_;
238  std::shared_ptr<GoalIntentExtractor> goal_intent_extractor_;
239 
240  std::shared_ptr<nav2_costmap_2d::CostmapSubscriber> costmap_subscriber_;
241 
242  // State Data
243  Graph graph_;
244  GraphToIDMap id_to_graph_map_;
245  std::string route_frame_, base_frame_, global_frame_;
246  double max_planning_time_;
247 };
248 
249 } // namespace nav2_route
250 
251 #endif // NAV2_ROUTE__ROUTE_SERVER_HPP_
An action server implements a Navigation Route-Graph planner to compliment free-space planning in the...
nav2_util::CallbackReturn on_deactivate(const rclcpp_lifecycle::State &state) override
Deactivate member variables.
void computeRoute()
Main route action server callbacks for computing and tracking a route.
~RouteServer()=default
A destructor for nav2_route::RouteServer.
void setRouteGraph(const std::shared_ptr< rmw_request_id_t >, const std::shared_ptr< nav2_msgs::srv::SetRouteGraph::Request > request, std::shared_ptr< nav2_msgs::srv::SetRouteGraph::Response > response)
The service callback to set a new route graph.
rclcpp::Duration findPlanningDuration(const rclcpp::Time &start_time)
Find the planning duration of the request and log warnings.
Route findRoute(const std::shared_ptr< const GoalT > goal, ReroutingState &rerouting_info)
Compute a route to the goal, incorporating rerouting information.
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_configure(const rclcpp_lifecycle::State &state) override
Configure member variables and initializes planner.
RouteServer(const rclcpp::NodeOptions &options=rclcpp::NodeOptions())
A constructor for nav2_route::RouteServer.
bool isRequestValid(std::shared_ptr< nav2_util::SimpleActionServer< ActionT >> &action_server)
Find the routing request is valid (action server OK and not cancelled)
void exceptionWarning(const std::shared_ptr< const GoalT > goal, const std::exception &ex)
Log exception warnings, templated by action message type.
void processRouteRequest(std::shared_ptr< nav2_util::SimpleActionServer< ActionT >> &action_server)
Main processing called by both action server callbacks to centralize the great deal of shared code be...
nav2_util::CallbackReturn on_cleanup(const rclcpp_lifecycle::State &state) override
Reset member variables.
void populateActionResult(std::shared_ptr< ComputeRoute::Result > result, const Route &route, const nav_msgs::msg::Path &path, const rclcpp::Duration &planning_duration)
Populate result for compute route action.
A lifecycle node wrapper to enable common Nav2 needs such as manipulating parameters.
An action server wrapper to make applications simpler using Actions.
State shared to objects to communicate important rerouting data to avoid rerouting over blocked edges...
Definition: types.hpp:264
An ordered set of nodes and edges corresponding to the planned route.
Definition: types.hpp:211