Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
controller_server.hpp
1 // Copyright (c) 2019 Intel Corporation
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_CONTROLLER__CONTROLLER_SERVER_HPP_
16 #define NAV2_CONTROLLER__CONTROLLER_SERVER_HPP_
17 
18 #include <memory>
19 #include <string>
20 #include <thread>
21 #include <unordered_map>
22 #include <vector>
23 #include <mutex>
24 
25 #include "nav2_core/controller.hpp"
26 #include "nav2_core/progress_checker.hpp"
27 #include "nav2_core/goal_checker.hpp"
28 #include "nav2_core/path_handler.hpp"
29 #include "nav2_costmap_2d/costmap_2d_ros.hpp"
30 #include "nav2_ros_common/tf2_factories.hpp"
31 #include "nav2_msgs/action/follow_path.hpp"
32 #include "nav2_msgs/msg/tracking_feedback.hpp"
33 #include "nav2_msgs/msg/speed_limit.hpp"
34 #include "nav2_ros_common/lifecycle_node.hpp"
35 #include "nav2_ros_common/simple_action_server.hpp"
36 #include "nav2_util/robot_utils.hpp"
37 #include "nav2_util/odometry_utils.hpp"
38 #include "nav2_util/twist_publisher.hpp"
39 #include "pluginlib/class_loader.hpp"
40 #include "pluginlib/class_list_macros.hpp"
41 #include "nav2_controller/parameter_handler.hpp"
42 
43 namespace nav2_controller
44 {
45 
46 class ProgressChecker;
53 {
54 public:
55  using ControllerMap = std::unordered_map<std::string, nav2_core::Controller::Ptr>;
56  using GoalCheckerMap = std::unordered_map<std::string, nav2_core::GoalChecker::Ptr>;
57  using ProgressCheckerMap = std::unordered_map<std::string, nav2_core::ProgressChecker::Ptr>;
58  using PathHandlerMap = std::unordered_map<std::string, nav2_core::PathHandler::Ptr>;
59 
64  explicit ControllerServer(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
69 
70 protected:
81  nav2::CallbackReturn on_configure(const rclcpp_lifecycle::State & state) override;
90  nav2::CallbackReturn on_activate(const rclcpp_lifecycle::State & state) override;
99  nav2::CallbackReturn on_deactivate(const rclcpp_lifecycle::State & state) override;
108  nav2::CallbackReturn on_cleanup(const rclcpp_lifecycle::State & state) override;
114  nav2::CallbackReturn on_shutdown(const rclcpp_lifecycle::State & state) override;
115 
116  using Action = nav2_msgs::action::FollowPath;
118 
124  bool goalReceived(std::shared_ptr<const Action::Goal> goal);
125 
126  // Our action server implements the FollowPath action
127  typename ActionServer::SharedPtr action_server_;
128 
138  void computeControl();
139 
147  bool findControllerId(const std::string & c_name, std::string & name);
148 
156  bool findGoalCheckerId(const std::string & c_name, std::string & name);
157 
165  bool findProgressCheckerId(const std::string & c_name, std::string & name);
166 
174  bool findPathHandlerId(const std::string & c_name, std::string & name);
175 
180  void setPlannerPath(const nav_msgs::msg::Path & path);
181 
187  void transformedPlanAndGoal(const geometry_msgs::msg::PoseStamped & current_robot_pose);
192  void computeAndPublishVelocity(const geometry_msgs::msg::PoseStamped & current_robot_pose);
197  void updateGlobalPath();
202  void publishVelocity(const geometry_msgs::msg::TwistStamped & velocity);
206  void publishZeroVelocity();
210  void onGoalExit(bool force_stop);
216  double waitForCostmap();
222  bool isGoalReached(const geometry_msgs::msg::PoseStamped & current_robot_pose);
228  geometry_msgs::msg::PoseStamped getCurrentRobotPose();
229 
236  double getThresholdedVelocity(double velocity, double threshold)
237  {
238  return (std::abs(velocity) > threshold) ? velocity : 0.0;
239  }
240 
246  geometry_msgs::msg::Twist getThresholdedTwist(const geometry_msgs::msg::Twist & twist)
247  {
248  geometry_msgs::msg::Twist twist_thresh;
249  twist_thresh.linear.x = getThresholdedVelocity(twist.linear.x,
250  params_->min_x_velocity_threshold);
251  twist_thresh.linear.y = getThresholdedVelocity(twist.linear.y,
252  params_->min_y_velocity_threshold);
253  twist_thresh.angular.z = getThresholdedVelocity(twist.angular.z,
254  params_->min_theta_velocity_threshold);
255  return twist_thresh;
256  }
257 
258  // The controller needs a costmap node
259  std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros_;
260  std::unique_ptr<nav2::NodeThread> costmap_thread_;
261 
262  // Publishers and subscribers
263  std::unique_ptr<nav2_util::OdomSmoother> odom_sub_;
264  std::unique_ptr<nav2_util::TwistPublisher> vel_publisher_;
265  nav2::Subscription<nav2_msgs::msg::SpeedLimit>::SharedPtr speed_limit_sub_;
266  nav2::Publisher<nav2_msgs::msg::TrackingFeedback>::SharedPtr tracking_feedback_pub_;
267 
268  // Progress Checker Plugin
269  pluginlib::ClassLoader<nav2_core::ProgressChecker> progress_checker_loader_;
270  ProgressCheckerMap progress_checkers_;
271  std::string progress_checker_ids_concat_, current_progress_checker_;
272 
273  // Goal Checker Plugin
274  pluginlib::ClassLoader<nav2_core::GoalChecker> goal_checker_loader_;
275  GoalCheckerMap goal_checkers_;
276  std::string goal_checker_ids_concat_, current_goal_checker_;
277 
278  // Controller Plugins
279  pluginlib::ClassLoader<nav2_core::Controller> lp_loader_;
280  ControllerMap controllers_;
281  std::string controller_ids_concat_, current_controller_;
282 
283  // Path Handler Plugins
284  pluginlib::ClassLoader<nav2_core::PathHandler> path_handler_loader_;
285  PathHandlerMap path_handlers_;
286  std::string path_handler_ids_concat_, current_path_handler_;
287 
288  size_t start_index_;
289  geometry_msgs::msg::PoseStamped end_pose_;
290  geometry_msgs::msg::PoseStamped transformed_end_pose_;
291 
292  // Last time the controller generated a valid command
293  rclcpp::Time last_valid_cmd_time_;
294 
295  // Current path container
296  nav_msgs::msg::Path current_path_;
297  nav_msgs::msg::Path transformed_global_plan_;
298  std::unique_ptr<nav2_controller::ParameterHandler> param_handler_;
299  Parameters * params_;
300  nav2::Publisher<nav_msgs::msg::Path>::SharedPtr transformed_plan_pub_;
301  double transform_tolerance_;
302 
303 private:
308  void speedLimitCallback(const nav2_msgs::msg::SpeedLimit::ConstSharedPtr & msg);
309 };
310 
311 } // namespace nav2_controller
312 
313 #endif // NAV2_CONTROLLER__CONTROLLER_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.
This class hosts variety of plugins of different algorithms to complete control tasks from the expose...
nav2::CallbackReturn on_cleanup(const rclcpp_lifecycle::State &state) override
Calls clean up states and resets member variables.
double waitForCostmap()
Wait for costmap to become current, with timeout.
bool isGoalReached(const geometry_msgs::msg::PoseStamped &current_robot_pose)
Checks if goal is reached.
void publishVelocity(const geometry_msgs::msg::TwistStamped &velocity)
Calls velocity publisher to publish the velocity on "cmd_vel" topic.
ControllerServer(const rclcpp::NodeOptions &options=rclcpp::NodeOptions())
Constructor for nav2_controller::ControllerServer.
double getThresholdedVelocity(double velocity, double threshold)
get the thresholded velocity
void onGoalExit(bool force_stop)
Called on goal exit.
nav2::CallbackReturn on_configure(const rclcpp_lifecycle::State &state) override
Configures controller parameters and member variables.
void computeControl()
FollowPath action server callback. Handles action server updates and spins server until goal is reach...
geometry_msgs::msg::Twist getThresholdedTwist(const geometry_msgs::msg::Twist &twist)
get the thresholded Twist
~ControllerServer()
Destructor for nav2_controller::ControllerServer.
nav2::CallbackReturn on_deactivate(const rclcpp_lifecycle::State &state) override
Deactivates member variables.
bool findGoalCheckerId(const std::string &c_name, std::string &name)
Find the valid goal checker ID name for the specified parameter.
void updateGlobalPath()
Calls setPlannerPath method with an updated path received from action server.
bool goalReceived(std::shared_ptr< const Action::Goal > goal)
Goal received callback to validate a new goal before acceptance.
void setPlannerPath(const nav_msgs::msg::Path &path)
Assigns path to controller.
nav2::CallbackReturn on_shutdown(const rclcpp_lifecycle::State &state) override
Called when in Shutdown state.
void transformedPlanAndGoal(const geometry_msgs::msg::PoseStamped &current_robot_pose)
Refreshes transformed_global_plan_ and transformed_end_pose_ for the current cycle.
nav2::CallbackReturn on_activate(const rclcpp_lifecycle::State &state) override
Activates member variables.
void publishZeroVelocity()
Calls velocity publisher to publish zero velocity.
geometry_msgs::msg::PoseStamped getCurrentRobotPose()
Obtain the current pose of the robot in the costmap frame.
bool findControllerId(const std::string &c_name, std::string &name)
Find the valid controller ID name for the given request.
bool findProgressCheckerId(const std::string &c_name, std::string &name)
Find the valid progress checker ID name for the specified parameter.
bool findPathHandlerId(const std::string &c_name, std::string &name)
Find the valid path handler ID name for the specified parameter.
void computeAndPublishVelocity(const geometry_msgs::msg::PoseStamped &current_robot_pose)
Calculates velocity and publishes to "cmd_vel" topic.