Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
regulated_pure_pursuit_controller.hpp
1 // Copyright (c) 2020 Shrijit Singh
2 // Copyright (c) 2020 Samsung Research America
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef NAV2_REGULATED_PURE_PURSUIT_CONTROLLER__REGULATED_PURE_PURSUIT_CONTROLLER_HPP_
17 #define NAV2_REGULATED_PURE_PURSUIT_CONTROLLER__REGULATED_PURE_PURSUIT_CONTROLLER_HPP_
18 
19 #include <string>
20 #include <vector>
21 #include <memory>
22 #include <algorithm>
23 #include <mutex>
24 
25 #include "nav2_core/controller.hpp"
26 #include "nav2_ros_common/lifecycle_node.hpp"
27 #include "pluginlib/class_loader.hpp"
28 #include "pluginlib/class_list_macros.hpp"
29 #include "geometry_msgs/msg/pose.hpp"
30 #include "std_msgs/msg/bool.hpp"
31 #include "nav2_regulated_pure_pursuit_controller/collision_checker.hpp"
32 #include "nav2_regulated_pure_pursuit_controller/parameter_handler.hpp"
33 #include "nav2_regulated_pure_pursuit_controller/regulation_functions.hpp"
34 #include "nav2_regulated_pure_pursuit_controller/dynamic_window_pure_pursuit_functions.hpp"
35 #include "nav2_ros_common/tf2_factories.hpp"
36 
37 namespace nav2_regulated_pure_pursuit_controller
38 {
39 
45 {
46 public:
51 
55  ~RegulatedPurePursuitController() override = default;
56 
64  void configure(
65  const nav2::LifecycleNode::WeakPtr & parent,
66  std::string name, nav2::TransformBuffer::SharedPtr tf,
67  std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros) override;
68 
72  void cleanup() override;
73 
77  void activate() override;
78 
82  void deactivate() override;
83 
93  geometry_msgs::msg::TwistStamped computeVelocityCommands(
94  const geometry_msgs::msg::PoseStamped & pose,
95  const geometry_msgs::msg::Twist & velocity,
96  nav2_core::GoalChecker * /*goal_checker*/,
97  const nav_msgs::msg::Path & transformed_global_plan,
98  const geometry_msgs::msg::PoseStamped & global_goal) override;
99 
100  bool cancel() override;
101 
106  void newPathReceived(const nav_msgs::msg::Path & raw_global_path) override;
107 
115  void setSpeedLimit(const double & speed_limit, const bool & percentage) override;
116 
117  void reset() override;
118 
119 protected:
125  double getLookAheadDistance(const geometry_msgs::msg::Twist &);
126 
132  std::unique_ptr<geometry_msgs::msg::PointStamped> createCarrotMsg(
133  const geometry_msgs::msg::PoseStamped & carrot_pose);
134 
142  bool shouldRotateToPath(
143  const geometry_msgs::msg::PoseStamped & carrot_pose, double & angle_to_path,
144  double & x_vel_sign);
145 
156  nav2_core::GoalChecker * goal_checker,
157  const geometry_msgs::msg::PoseStamped & robot_pose,
158  const geometry_msgs::msg::PoseStamped & goal_pose,
159  const geometry_msgs::msg::Twist & speed,
160  const nav_msgs::msg::Path & transformed_plan);
161 
169  void rotateToHeading(
170  double & linear_vel, double & angular_vel,
171  const double & angle_to_path, const geometry_msgs::msg::Twist & curr_speed);
172 
181  void applyConstraints(
182  const double & curvature, const geometry_msgs::msg::Twist & speed,
183  const double & pose_cost, const nav_msgs::msg::Path & path,
184  double & linear_vel, double & sign);
185 
186  nav2::LifecycleNode::WeakPtr node_;
187  nav2::TransformBuffer::SharedPtr tf_;
188  std::string plugin_name_;
189  std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros_;
190  nav2_costmap_2d::Costmap2D * costmap_;
191  rclcpp::Logger logger_ {rclcpp::get_logger("RegulatedPurePursuitController")};
192 
193  Parameters * params_;
194  double control_duration_;
195  bool cancelling_ = false;
196  bool finished_cancelling_ = false;
197  bool is_rotating_to_heading_ = false;
198  geometry_msgs::msg::Twist last_command_velocity_;
199 
200  nav2::Publisher<geometry_msgs::msg::PointStamped>::SharedPtr carrot_pub_;
201  nav2::Publisher<geometry_msgs::msg::PointStamped>::SharedPtr curvature_carrot_pub_;
202  nav2::Publisher<std_msgs::msg::Bool>::SharedPtr is_rotating_to_heading_pub_;
203  nav2::Publisher<nav_msgs::msg::Path>::SharedPtr carrot_arc_pub_;
204  std::unique_ptr<nav2_regulated_pure_pursuit_controller::ParameterHandler> param_handler_;
205  std::unique_ptr<nav2_regulated_pure_pursuit_controller::CollisionChecker> collision_checker_;
206 };
207 
208 } // namespace nav2_regulated_pure_pursuit_controller
209 
210 #endif // NAV2_REGULATED_PURE_PURSUIT_CONTROLLER__REGULATED_PURE_PURSUIT_CONTROLLER_HPP_
controller interface that acts as a virtual base class for all controller plugins
Definition: controller.hpp:60
Function-object for checking whether a goal has been reached.
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:69
bool shouldRotateToPath(const geometry_msgs::msg::PoseStamped &carrot_pose, double &angle_to_path, double &x_vel_sign)
Whether robot should rotate to rough path heading.
double getLookAheadDistance(const geometry_msgs::msg::Twist &)
Get lookahead distance.
void newPathReceived(const nav_msgs::msg::Path &raw_global_path) override
nav2_core newPathReceived - Receives a new plan from the Planner Server
void rotateToHeading(double &linear_vel, double &angular_vel, const double &angle_to_path, const geometry_msgs::msg::Twist &curr_speed)
Create a smooth and kinematically smoothed rotation command.
bool shouldRotateToGoalHeading(nav2_core::GoalChecker *goal_checker, const geometry_msgs::msg::PoseStamped &robot_pose, const geometry_msgs::msg::PoseStamped &goal_pose, const geometry_msgs::msg::Twist &speed, const nav_msgs::msg::Path &transformed_plan)
Whether robot should rotate to final goal orientation.
~RegulatedPurePursuitController() override=default
Destrructor for nav2_regulated_pure_pursuit_controller::RegulatedPurePursuitController.
geometry_msgs::msg::TwistStamped computeVelocityCommands(const geometry_msgs::msg::PoseStamped &pose, const geometry_msgs::msg::Twist &velocity, nav2_core::GoalChecker *, const nav_msgs::msg::Path &transformed_global_plan, const geometry_msgs::msg::PoseStamped &global_goal) override
Compute the best command given the current pose and velocity, with possible debug information.
RegulatedPurePursuitController()=default
Constructor for nav2_regulated_pure_pursuit_controller::RegulatedPurePursuitController.
void setSpeedLimit(const double &speed_limit, const bool &percentage) override
Limits the maximum linear speed of the robot.
void applyConstraints(const double &curvature, const geometry_msgs::msg::Twist &speed, const double &pose_cost, const nav_msgs::msg::Path &path, double &linear_vel, double &sign)
apply regulation constraints to the system
std::unique_ptr< geometry_msgs::msg::PointStamped > createCarrotMsg(const geometry_msgs::msg::PoseStamped &carrot_pose)
Creates a PointStamped message for visualization.
void configure(const nav2::LifecycleNode::WeakPtr &parent, std::string name, nav2::TransformBuffer::SharedPtr tf, std::shared_ptr< nav2_costmap_2d::Costmap2DROS > costmap_ros) override
Configure controller state machine.
void reset() override
Reset the state of the controller if necessary after task is exited.