Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
simple_non_charging_dock.hpp
1 // Copyright (c) 2024 Open Navigation LLC
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 OPENNAV_DOCKING__SIMPLE_NON_CHARGING_DOCK_HPP_
16 #define OPENNAV_DOCKING__SIMPLE_NON_CHARGING_DOCK_HPP_
17 
18 #include <string>
19 #include <memory>
20 #include <vector>
21 
22 #include "std_srvs/srv/trigger.hpp"
23 #include "geometry_msgs/msg/pose_stamped.hpp"
24 #include "sensor_msgs/msg/battery_state.hpp"
25 #include "sensor_msgs/msg/joint_state.hpp"
26 #include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
27 #include "tf2/utils.hpp"
28 #include "nav2_ros_common/tf2_factories.hpp"
29 #include "nav2_ros_common/lifecycle_node.hpp"
30 
31 #include "opennav_docking_core/non_charging_dock.hpp"
32 #include "opennav_docking/pose_filter.hpp"
33 
34 namespace opennav_docking
35 {
36 
38 {
39 public:
44  : NonChargingDock()
45  {}
46 
52  virtual void configure(
53  const nav2::LifecycleNode::WeakPtr & parent,
54  const std::string & name, nav2::TransformBuffer::SharedPtr tf);
55 
59  void cleanup() override;
60 
64  void activate() override;
65 
69  void deactivate() override;
70 
79  virtual geometry_msgs::msg::PoseStamped getStagingPose(
80  const geometry_msgs::msg::Pose & pose, const std::string & frame);
81 
87  virtual bool getRefinedPose(geometry_msgs::msg::PoseStamped & pose, std::string id);
88 
92  virtual bool isDocked();
93 
97  bool startDetectionProcess() override;
98 
102  bool stopDetectionProcess() override;
103 
104 protected:
105  void jointStateCallback(const sensor_msgs::msg::JointState::ConstSharedPtr & state);
106 
107  // Optionally subscribe to a detected dock pose topic
108  nav2::Subscription<geometry_msgs::msg::PoseStamped>::SharedPtr dock_pose_sub_;
109  nav2::Publisher<geometry_msgs::msg::PoseStamped>::SharedPtr dock_pose_pub_;
110  nav2::Publisher<geometry_msgs::msg::PoseStamped>::SharedPtr filtered_dock_pose_pub_;
111  nav2::Publisher<geometry_msgs::msg::PoseStamped>::SharedPtr staging_pose_pub_;
112  // If subscribed to a detected pose topic, will contain latest message
113  geometry_msgs::msg::PoseStamped detected_dock_pose_;
114  // This is the actual dock pose once it has the specified translation/rotation applied
115  // If not subscribed to a topic, this is simply the database dock pose
116  geometry_msgs::msg::PoseStamped dock_pose_;
117 
118  // Optionally subscribe to joint state message, used to determine if stalled
119  nav2::Subscription<sensor_msgs::msg::JointState>::SharedPtr joint_state_sub_;
120  std::vector<std::string> stall_joint_names_;
121  double stall_velocity_threshold_, stall_effort_threshold_;
122  bool is_stalled_;
123 
124  // An external reference (such as image_proc::TrackMarkerNode) can be used to detect dock
125  bool use_external_detection_pose_;
126  double external_detection_timeout_;
127  tf2::Quaternion external_detection_rotation_;
128  double external_detection_translation_x_;
129  double external_detection_translation_y_;
130 
131  // Filtering of detected poses
132  std::shared_ptr<PoseFilter> filter_;
133 
134  // If not using an external pose reference, this is the distance threshold
135  double docking_threshold_;
136  std::string base_frame_id_;
137  // Offset for staging pose relative to dock pose
138  double staging_x_offset_;
139  double staging_yaw_offset_;
140 
141  nav2::LifecycleNode::SharedPtr node_;
142  nav2::TransformBuffer::SharedPtr tf2_buffer_;
143 
144  // Detector control parameters
145  std::string detector_service_name_;
146  double detector_service_timeout_{5.0};
147  bool subscribe_toggle_{false};
148 
149  // Client used to call the Trigger service
150  nav2::ServiceClient<std_srvs::srv::Trigger>::SharedPtr detector_client_;
151 
152  // Detection state flags
153  bool detection_active_{false};
154  bool initial_pose_received_{false};
155 };
156 
157 } // namespace opennav_docking
158 
159 #endif // OPENNAV_DOCKING__SIMPLE_NON_CHARGING_DOCK_HPP_
bool stopDetectionProcess() override
Stop external detection process.
virtual geometry_msgs::msg::PoseStamped getStagingPose(const geometry_msgs::msg::Pose &pose, const std::string &frame)
Method to obtain the dock's staging pose. This method should likely be using TF and the dock's pose i...
virtual bool isDocked()
Have we made contact with dock? This can be implemented in a variety of ways: by establishing communi...
virtual void configure(const nav2::LifecycleNode::WeakPtr &parent, const std::string &name, nav2::TransformBuffer::SharedPtr tf)
bool startDetectionProcess() override
Start external detection process (service call + subscribe).
void cleanup() override
Method to cleanup resources used on shutdown.
void activate() override
Method to active Behavior and any threads involved in execution.
virtual bool getRefinedPose(geometry_msgs::msg::PoseStamped &pose, std::string id)
Method to obtain the refined pose of the dock, usually based on sensors.
void deactivate() override
Method to deactivate Behavior and any threads involved in execution.
Abstract interface for a non-charging dock for the docking framework.