Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
graceful_controller.hpp
1 // Copyright (c) 2023 Alberto J. Tudela Roldán
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_GRACEFUL_CONTROLLER__GRACEFUL_CONTROLLER_HPP_
16 #define NAV2_GRACEFUL_CONTROLLER__GRACEFUL_CONTROLLER_HPP_
17 
18 #include <string>
19 #include <limits>
20 #include <vector>
21 #include <memory>
22 #include <algorithm>
23 #include <mutex>
24 
25 #include "nav2_core/controller.hpp"
26 #include "nav2_costmap_2d/footprint_collision_checker.hpp"
27 #include "rclcpp/rclcpp.hpp"
28 #include "pluginlib/class_loader.hpp"
29 #include "pluginlib/class_list_macros.hpp"
30 #include "nav2_graceful_controller/parameter_handler.hpp"
31 #include "nav2_graceful_controller/smooth_control_law.hpp"
32 #include "nav2_graceful_controller/utils.hpp"
33 #include "nav2_ros_common/tf2_factories.hpp"
34 
35 namespace nav2_graceful_controller
36 {
37 
43 {
44 public:
48  GracefulController() = default;
49 
53  ~GracefulController() override = default;
54 
62  void configure(
63  const nav2::LifecycleNode::WeakPtr & parent,
64  std::string name, nav2::TransformBuffer::SharedPtr tf,
65  std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros) override;
66 
70  void cleanup() override;
71 
75  void activate() override;
76 
80  void deactivate() override;
81 
91  geometry_msgs::msg::TwistStamped computeVelocityCommands(
92  const geometry_msgs::msg::PoseStamped & pose,
93  const geometry_msgs::msg::Twist & velocity,
94  nav2_core::GoalChecker * goal_checker,
95  const nav_msgs::msg::Path & transformed_global_plan,
96  const geometry_msgs::msg::PoseStamped & global_goal) override;
97 
102  void newPathReceived(const nav_msgs::msg::Path & raw_global_path) override;
103 
111  void setSpeedLimit(const double & speed_limit, const bool & percentage) override;
112 
113 protected:
123  bool validateTargetPose(
124  geometry_msgs::msg::PoseStamped & target_pose,
125  double dist_to_target,
126  nav_msgs::msg::Path & trajectory,
127  geometry_msgs::msg::TransformStamped & costmap_transform,
128  geometry_msgs::msg::TwistStamped & cmd_vel);
129 
141  geometry_msgs::msg::PoseStamped & target_pose,
142  double dist_to_target,
143  double dist_to_goal,
144  nav_msgs::msg::Path & trajectory,
145  geometry_msgs::msg::TransformStamped & costmap_transform,
146  geometry_msgs::msg::TwistStamped & cmd_vel);
147 
159  geometry_msgs::msg::PoseStamped & target_pose,
160  double dist_to_target,
161  geometry_msgs::msg::TransformStamped & costmap_transform,
162  double safety_cost,
163  nav_msgs::msg::Path & best_trajectory,
164  geometry_msgs::msg::TwistStamped & best_cmd_vel);
165 
177  bool simulateTrajectory(
178  const geometry_msgs::msg::PoseStamped & motion_target,
179  const geometry_msgs::msg::TransformStamped & costmap_transform,
180  nav_msgs::msg::Path & trajectory,
181  geometry_msgs::msg::TwistStamped & cmd_vel,
182  bool backward);
183 
190  geometry_msgs::msg::Twist rotateToTarget(double angle_to_target);
191 
200  bool inCollision(
201  const double & x, const double & y, const double & theta,
202  double inflation_scale = 1.0);
203 
210  double getMaxCost(
211  const nav_msgs::msg::Path & path,
212  geometry_msgs::msg::TransformStamped & costmap_transform);
213 
220  const std::vector<geometry_msgs::msg::PoseStamped> & poses,
221  std::vector<double> & distances);
222 
227  void validateOrientations(std::vector<geometry_msgs::msg::PoseStamped> & path);
228 
229  nav2::TransformBuffer::SharedPtr tf_buffer_;
230  std::string plugin_name_;
231  std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros_;
232  std::unique_ptr<nav2_costmap_2d::FootprintCollisionChecker<nav2_costmap_2d::Costmap2D *>>
233  collision_checker_;
234  rclcpp::Logger logger_{rclcpp::get_logger("GracefulController")};
235 
236  Parameters * params_;
237 
238  // True from the time a new path arrives until we have completed an initial rotation
239  bool do_initial_rotation_;
240 
241  std::optional<double> safe_approach_angle_;
242 
243  nav2::Publisher<nav_msgs::msg::Path>::SharedPtr local_plan_pub_;
244  nav2::Publisher<geometry_msgs::msg::PoseStamped>::SharedPtr motion_target_pub_;
245  nav2::Publisher<visualization_msgs::msg::Marker>::SharedPtr slowdown_pub_;
246  std::unique_ptr<nav2_graceful_controller::ParameterHandler> param_handler_;
247  std::unique_ptr<nav2_graceful_controller::SmoothControlLaw> control_law_;
248 };
249 
250 } // namespace nav2_graceful_controller
251 
252 #endif // NAV2_GRACEFUL_CONTROLLER__GRACEFUL_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.
void activate() override
Activate controller state machine.
~GracefulController() override=default
Destructor for nav2_graceful_controller::GracefulController.
void computeDistanceAlongPath(const std::vector< geometry_msgs::msg::PoseStamped > &poses, std::vector< double > &distances)
Compute the distance to each pose in a path.
bool validateTargetPose(geometry_msgs::msg::PoseStamped &target_pose, double dist_to_target, nav_msgs::msg::Path &trajectory, geometry_msgs::msg::TransformStamped &costmap_transform, geometry_msgs::msg::TwistStamped &cmd_vel)
Validate a given target pose for calculating command velocity.
bool validateTargetPoseOnApproach(geometry_msgs::msg::PoseStamped &target_pose, double dist_to_target, double dist_to_goal, nav_msgs::msg::Path &trajectory, geometry_msgs::msg::TransformStamped &costmap_transform, geometry_msgs::msg::TwistStamped &cmd_vel)
Validate a given target pose for calculating command velocity on approach to goal.
void deactivate() override
Deactivate controller state machine.
bool inCollision(const double &x, const double &y, const double &theta, double inflation_scale=1.0)
Checks if the robot is in collision.
bool findBestApproachTrajectory(geometry_msgs::msg::PoseStamped &target_pose, double dist_to_target, geometry_msgs::msg::TransformStamped &costmap_transform, double safety_cost, nav_msgs::msg::Path &best_trajectory, geometry_msgs::msg::TwistStamped &best_cmd_vel)
Find the best approach trajectory by searching multiple orientations.
bool simulateTrajectory(const geometry_msgs::msg::PoseStamped &motion_target, const geometry_msgs::msg::TransformStamped &costmap_transform, nav_msgs::msg::Path &trajectory, geometry_msgs::msg::TwistStamped &cmd_vel, bool backward)
Simulate trajectory calculating in every step the new velocity command based on a new curvature value...
GracefulController()=default
Constructor for nav2_graceful_controller::GracefulController.
geometry_msgs::msg::TwistStamped computeVelocityCommands(const geometry_msgs::msg::PoseStamped &pose, const geometry_msgs::msg::Twist &velocity, nav2_core::GoalChecker *goal_checker, 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.
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 cleanup() override
Cleanup controller state machine.
geometry_msgs::msg::Twist rotateToTarget(double angle_to_target)
Rotate the robot to face the motion target with maximum angular velocity.
void newPathReceived(const nav_msgs::msg::Path &raw_global_path) override
nav2_core newPathReceived - Receives a new plan from the Planner Server
void setSpeedLimit(const double &speed_limit, const bool &percentage) override
Limits the maximum linear speed of the robot.
void validateOrientations(std::vector< geometry_msgs::msg::PoseStamped > &path)
Control law requires proper orientations, not all planners provide them.
double getMaxCost(const nav_msgs::msg::Path &path, geometry_msgs::msg::TransformStamped &costmap_transform)
Get the maximum cost of a path.