Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
following_server.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_FOLLOWING__FOLLOWING_SERVER_HPP_
17 #define OPENNAV_FOLLOWING__FOLLOWING_SERVER_HPP_
18 
19 #include <vector>
20 #include <memory>
21 #include <string>
22 #include <mutex>
23 #include <functional>
24 
25 #include "geometry_msgs/msg/pose_stamped.hpp"
26 #include "rclcpp/rclcpp.hpp"
27 #include "rclcpp_lifecycle/lifecycle_publisher.hpp"
28 #include "nav2_msgs/action/follow_object.hpp"
29 #include "nav2_util/lifecycle_node.hpp"
30 #include "nav2_util/node_utils.hpp"
31 #include "nav2_util/simple_action_server.hpp"
32 #include "nav2_util/twist_publisher.hpp"
33 #include "nav2_util/odometry_utils.hpp"
34 #include "opennav_docking/controller.hpp"
35 #include "opennav_docking/pose_filter.hpp"
36 #include "opennav_following/parameter_handler.hpp"
37 #include "tf2_ros/buffer.h"
38 #include "tf2_ros/transform_listener.h"
39 
40 namespace opennav_following
41 {
47 {
48 public:
49  using FollowObject = nav2_msgs::action::FollowObject;
51 
56  explicit FollowingServer(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
57 
61  ~FollowingServer() = default;
62 
67  void publishFollowingFeedback(uint16_t state);
68 
76  virtual bool approachObject(
77  geometry_msgs::msg::PoseStamped & object_pose,
78  const std::string & target_frame = std::string(""));
79 
86  virtual bool rotateToObject(
87  geometry_msgs::msg::PoseStamped & object_pose,
88  const std::string & target_frame = std::string(""));
89 
96  template<typename ActionT>
98  typename std::shared_ptr<const typename ActionT::Goal> goal,
99  const std::unique_ptr<nav2_util::SimpleActionServer<ActionT>> & action_server);
100 
107  template<typename ActionT>
109  std::unique_ptr<nav2_util::SimpleActionServer<ActionT>> & action_server,
110  const std::string & name);
111 
118  template<typename ActionT>
120  std::unique_ptr<nav2_util::SimpleActionServer<ActionT>> & action_server,
121  const std::string & name);
122 
128  nav2_util::CallbackReturn on_configure(const rclcpp_lifecycle::State & state) override;
129 
135  nav2_util::CallbackReturn on_activate(const rclcpp_lifecycle::State & state) override;
136 
142  nav2_util::CallbackReturn on_deactivate(const rclcpp_lifecycle::State & state) override;
143 
149  nav2_util::CallbackReturn on_cleanup(const rclcpp_lifecycle::State & state) override;
150 
156  nav2_util::CallbackReturn on_shutdown(const rclcpp_lifecycle::State & state) override;
157 
161  void publishZeroVelocity();
162 
163 protected:
167  void followObject();
168 
175  virtual bool getRefinedPose(geometry_msgs::msg::PoseStamped & pose);
176 
183  virtual bool getFramePose(geometry_msgs::msg::PoseStamped & pose, const std::string & frame_id);
184 
191  virtual bool getTrackingPose(
192  geometry_msgs::msg::PoseStamped & pose, const std::string & frame_id);
193 
201  geometry_msgs::msg::PoseStamped getPoseAtDistance(
202  const geometry_msgs::msg::PoseStamped & pose, double distance);
203 
210  bool isGoalReached(const geometry_msgs::msg::PoseStamped & goal_pose);
211 
212  // Parameter handler
213  std::unique_ptr<opennav_following::ParameterHandler> param_handler_;
214  Parameters * params_;
215 
216  // Time when object became static
217  rclcpp::Time static_object_start_time_;
218  // Flag to track if we've initialized the static timer
219  bool static_timer_initialized_;
220  // Maximum number of times the robot will retry to approach the object
221  int num_retries_;
222 
223  // Timestamp of the last time a iteration was started
224  rclcpp::Time iteration_start_time_;
225 
226  // This is a class member so it can be accessed in publish feedback
227  rclcpp::Time action_start_time_;
228 
229  // Subscribe to the dynamic pose
230  rclcpp::Subscription<geometry_msgs::msg::PoseStamped>::SharedPtr dynamic_pose_sub_;
231 
232  // Publish the filtered dynamic pose
233  rclcpp_lifecycle::LifecyclePublisher<geometry_msgs::msg::PoseStamped>::SharedPtr
234  filtered_dynamic_pose_pub_;
235 
236  // Latest message
237  geometry_msgs::msg::PoseStamped detected_dynamic_pose_;
238 
239  // Filtering of detected poses
240  std::unique_ptr<opennav_docking::PoseFilter> filter_;
241 
242  std::unique_ptr<nav2_util::TwistPublisher> vel_publisher_;
243  std::unique_ptr<nav2_util::OdomSmoother> odom_sub_;
244  std::unique_ptr<FollowingActionServer> following_action_server_;
245 
246  std::unique_ptr<opennav_docking::Controller> controller_;
247 
248  std::shared_ptr<tf2_ros::Buffer> tf2_buffer_;
249  std::unique_ptr<tf2_ros::TransformListener> tf2_listener_;
250 };
251 
252 } // namespace opennav_following
253 
254 #endif // OPENNAV_FOLLOWING__FOLLOWING_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.
An action server which implements a dynamic following behavior.
void followObject()
Main action callback method to complete following request.
void getPreemptedGoalIfRequested(typename std::shared_ptr< const typename ActionT::Goal > goal, const std::unique_ptr< nav2_util::SimpleActionServer< ActionT >> &action_server)
Gets a preempted goal if immediately requested.
bool checkAndWarnIfPreempted(std::unique_ptr< nav2_util::SimpleActionServer< ActionT >> &action_server, const std::string &name)
Checks and logs warning if action preempted.
virtual bool getFramePose(geometry_msgs::msg::PoseStamped &pose, const std::string &frame_id)
Get the pose of a specific frame in the fixed frame.
virtual bool approachObject(geometry_msgs::msg::PoseStamped &object_pose, const std::string &target_frame=std::string(""))
Use control law and perception to approach the object.
virtual bool getTrackingPose(geometry_msgs::msg::PoseStamped &pose, const std::string &frame_id)
Get the tracking pose based on the current tracking mode.
nav2_util::CallbackReturn on_deactivate(const rclcpp_lifecycle::State &state) override
Deactivate member variables.
void publishFollowingFeedback(uint16_t state)
Publish feedback from a following action.
nav2_util::CallbackReturn on_cleanup(const rclcpp_lifecycle::State &state) override
Reset member variables.
nav2_util::CallbackReturn on_shutdown(const rclcpp_lifecycle::State &state) override
Called when in shutdown state.
geometry_msgs::msg::PoseStamped getPoseAtDistance(const geometry_msgs::msg::PoseStamped &pose, double distance)
Get the pose at a distance in front of the input pose.
virtual bool getRefinedPose(geometry_msgs::msg::PoseStamped &pose)
Method to obtain the refined dynamic pose.
void publishZeroVelocity()
Publish zero velocity at terminal condition.
bool isGoalReached(const geometry_msgs::msg::PoseStamped &goal_pose)
Check if the goal has been reached.
virtual bool rotateToObject(geometry_msgs::msg::PoseStamped &object_pose, const std::string &target_frame=std::string(""))
Rotate the robot to find the object again.
bool checkAndWarnIfCancelled(std::unique_ptr< nav2_util::SimpleActionServer< ActionT >> &action_server, const std::string &name)
Checks and logs warning if action canceled.
nav2_util::CallbackReturn on_configure(const rclcpp_lifecycle::State &state) override
Configure member variables.
nav2_util::CallbackReturn on_activate(const rclcpp_lifecycle::State &state) override
Activate member variables.
~FollowingServer()=default
A destructor for opennav_following::FollowingServer.
FollowingServer(const rclcpp::NodeOptions &options=rclcpp::NodeOptions())
A constructor for opennav_following::FollowingServer.