Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
controller.hpp
1 // Copyright (c) 2024 Open Navigation LLC
2 // Copyright (c) 2024 Alberto J. Tudela Roldán
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 OPENNAV_DOCKING__CONTROLLER_HPP_
17 #define OPENNAV_DOCKING__CONTROLLER_HPP_
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "geometry_msgs/msg/pose.hpp"
24 #include "geometry_msgs/msg/twist.hpp"
25 #include "nav2_costmap_2d/costmap_subscriber.hpp"
26 #include "nav2_costmap_2d/footprint_subscriber.hpp"
27 #include "nav2_costmap_2d/costmap_topic_collision_checker.hpp"
28 #include "nav2_graceful_controller/smooth_control_law.hpp"
29 #include "nav_msgs/msg/path.hpp"
30 #include "nav2_ros_common/lifecycle_node.hpp"
31 
32 namespace opennav_docking
33 {
39 {
40 public:
49  Controller(
50  const nav2::LifecycleNode::SharedPtr & node, std::shared_ptr<tf2_ros::Buffer> tf,
51  std::string fixed_frame, std::string base_frame);
52 
56  ~Controller();
57 
67  const geometry_msgs::msg::Pose & pose, geometry_msgs::msg::Twist & cmd, bool is_docking,
68  bool backward = false);
69 
77  geometry_msgs::msg::Twist computeRotateToHeadingCommand(
78  const double & angular_distance_to_heading,
79  const geometry_msgs::msg::Twist & current_velocity,
80  const double & dt);
81 
82 protected:
92  const geometry_msgs::msg::Pose & target_pose, bool is_docking, bool backward = false);
93 
99  rcl_interfaces::msg::SetParametersResult
100  dynamicParametersCallback(std::vector<rclcpp::Parameter> parameters);
101 
111  const nav2::LifecycleNode::SharedPtr & node,
112  std::string costmap_topic, std::string footprint_topic, double transform_tolerance);
113 
114  // Dynamic parameters handler
115  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr dyn_params_handler_;
116  std::mutex dynamic_params_lock_;
117 
118  rclcpp::Logger logger_{rclcpp::get_logger("Controller")};
119  rclcpp::Clock::SharedPtr clock_;
120 
121  // Smooth control law
122  std::unique_ptr<nav2_graceful_controller::SmoothControlLaw> control_law_;
123  double k_phi_, k_delta_, beta_, lambda_;
124  double slowdown_radius_, v_linear_min_, v_linear_max_, v_angular_max_;
125  double rotate_to_heading_angular_vel_, rotate_to_heading_max_angular_accel_;
126 
127  // The trajectory of the robot while dock / undock for visualization / debug purposes
128  rclcpp::Publisher<nav_msgs::msg::Path>::SharedPtr trajectory_pub_;
129 
130  // Used for collision checking
131  bool use_collision_detection_;
132  double projection_time_;
133  double simulation_time_step_;
134  double dock_collision_threshold_;
135  double transform_tolerance_;
136  std::shared_ptr<tf2_ros::Buffer> tf2_buffer_;
137  std::unique_ptr<nav2_costmap_2d::CostmapSubscriber> costmap_sub_;
138  std::unique_ptr<nav2_costmap_2d::FootprintSubscriber> footprint_sub_;
139  std::shared_ptr<nav2_costmap_2d::CostmapTopicCollisionChecker> collision_checker_;
140  std::string fixed_frame_, base_frame_;
141 };
142 
143 } // namespace opennav_docking
144 
145 #endif // OPENNAV_DOCKING__CONTROLLER_HPP_
Default control law for approaching a dock target.
Definition: controller.hpp:39
Controller(const nav2::LifecycleNode::SharedPtr &node, std::shared_ptr< tf2_ros::Buffer > tf, std::string fixed_frame, std::string base_frame)
Create a controller instance. Configure ROS 2 parameters.
Definition: controller.cpp:27
bool isTrajectoryCollisionFree(const geometry_msgs::msg::Pose &target_pose, bool is_docking, bool backward=false)
Check if a trajectory is collision free.
Definition: controller.cpp:152
bool computeVelocityCommand(const geometry_msgs::msg::Pose &pose, geometry_msgs::msg::Twist &cmd, bool is_docking, bool backward=false)
Compute a velocity command using control law.
Definition: controller.cpp:118
~Controller()
A destructor for opennav_docking::Controller.
Definition: controller.cpp:109
geometry_msgs::msg::Twist computeRotateToHeadingCommand(const double &angular_distance_to_heading, const geometry_msgs::msg::Twist &current_velocity, const double &dt)
Perform a command for in-place rotation.
Definition: controller.cpp:127
void configureCollisionChecker(const nav2::LifecycleNode::SharedPtr &node, std::string costmap_topic, std::string footprint_topic, double transform_tolerance)
Configure the collision checker.
Definition: controller.cpp:225
rcl_interfaces::msg::SetParametersResult dynamicParametersCallback(std::vector< rclcpp::Parameter > parameters)
Callback executed when a parameter change is detected.
Definition: controller.cpp:237