Swarm-SLAM  1.0.0
C-SLAM Framework
rgbd_handler.h
Go to the documentation of this file.
1 #ifndef _RGBDHANDLER_H_
2 #define _RGBDHANDLER_H_
3 
4 #include <rclcpp/rclcpp.hpp>
5 
6 #include <rtabmap_msgs/msg/rgbd_image.hpp>
7 
8 #include <rtabmap/core/Compression.h>
9 #include <rtabmap/core/Memory.h>
10 #include <rtabmap/core/RegistrationVis.h>
11 #include <rtabmap/core/Rtabmap.h>
12 #include <rtabmap/core/SensorData.h>
13 #include <rtabmap/core/VWDictionary.h>
14 #include <rtabmap/core/util2d.h>
15 #include <rtabmap/core/util3d.h>
16 #include <rtabmap/utilite/UStl.h>
17 
18 #include <message_filters/subscriber.h>
19 #include <message_filters/sync_policies/approximate_time.h>
20 #include <message_filters/synchronizer.h>
21 #include <message_filters/time_synchronizer.h>
22 
23 #include <image_transport/image_transport.hpp>
24 #include <image_transport/subscriber_filter.hpp>
25 
26 #include <tf2_ros/buffer.h>
27 #include <tf2_ros/transform_listener.h>
28 
29 #include <cv_bridge/cv_bridge.h>
30 
31 #include <chrono>
32 #include <cslam_common_interfaces/msg/keyframe_odom.hpp>
33 #include <cslam_common_interfaces/msg/keyframe_rgb.hpp>
34 #include <cslam_common_interfaces/msg/viz_point_cloud.hpp>
35 #include <cslam_common_interfaces/msg/inter_robot_loop_closure.hpp>
36 #include <cslam_common_interfaces/msg/local_descriptors_request.hpp>
37 #include <cslam_common_interfaces/msg/local_image_descriptors.hpp>
38 #include <cslam_common_interfaces/msg/local_keyframe_match.hpp>
39 #include <cslam_common_interfaces/msg/intra_robot_loop_closure.hpp>
40 #include <diagnostic_msgs/msg/key_value.hpp>
41 #include <sensor_msgs/msg/nav_sat_fix.hpp>
42 #include <deque>
43 #include <functional>
44 #include <nav_msgs/msg/odometry.hpp>
45 #include <thread>
46 
47 #include <memory>
48 
49 #include <rtabmap_conversions/MsgConversion.h>
52 
53 namespace cslam
54 {
55 
56  class RGBDHandler : public ISensorHandler
57  {
58  public:
64  RGBDHandler(std::shared_ptr<rclcpp::Node> &node);
66 
71  virtual void process_new_sensor_data();
72 
79  cslam_common_interfaces::msg::LocalDescriptorsRequest::
80  ConstSharedPtr request);
81 
88  cslam_common_interfaces::msg::LocalKeyframeMatch::ConstSharedPtr
89  msg);
90 
97  const std::shared_ptr<
98  cslam_common_interfaces::msg::LocalImageDescriptors>
99  msg);
100 
106  void
107  compute_local_descriptors(std::shared_ptr<rtabmap::SensorData> &frame_data);
108 
116  const std::shared_ptr<
117  cslam_common_interfaces::msg::LocalImageDescriptors>
118  msg,
119  rtabmap::SensorData &sensor_data);
120 
128  const std::shared_ptr<rtabmap::SensorData> sensor_data,
129  rtabmap_msgs::msg::RGBDImage &msg_data);
130 
138  bool generate_new_keyframe(std::shared_ptr<rtabmap::SensorData> &keyframe);
139 
145  void send_keyframe(const std::pair<std::shared_ptr<rtabmap::SensorData>, std::shared_ptr<const nav_msgs::msg::Odometry>> &keypoints_data);
146 
153  void send_keyframe(const std::pair<std::shared_ptr<rtabmap::SensorData>, std::shared_ptr<const nav_msgs::msg::Odometry>> &keypoints_data, const sensor_msgs::msg::NavSatFix& gps_data);
154 
160  virtual void send_visualization(const std::pair<std::shared_ptr<rtabmap::SensorData>, std::shared_ptr<const nav_msgs::msg::Odometry>> &keypoints_data);
161 
167  void send_visualization_keypoints(const std::pair<std::shared_ptr<rtabmap::SensorData>, std::shared_ptr<const nav_msgs::msg::Odometry>> &keypoints_data);
168 
174  void send_visualization_pointcloud(const std::shared_ptr<rtabmap::SensorData> &sensor_data);
175 
185  void rgbd_callback(
186  const sensor_msgs::msg::Image::ConstSharedPtr image_rect_rgb,
187  const sensor_msgs::msg::Image::ConstSharedPtr image_rect_depth,
188  const sensor_msgs::msg::CameraInfo::ConstSharedPtr camera_info_rgb,
189  const nav_msgs::msg::Odometry::ConstSharedPtr odom);
190 
196  void clear_sensor_data(std::shared_ptr<rtabmap::SensorData> &sensor_data);
197 
203  void gps_callback(const sensor_msgs::msg::NavSatFix::ConstSharedPtr msg);
204 
211  sensor_msgs::msg::PointCloud2 visualization_pointcloud_voxel_subsampling(
212  const sensor_msgs::msg::PointCloud2 &input_cloud);
213 
214  protected:
215  std::deque<std::pair<std::shared_ptr<rtabmap::SensorData>,
216  nav_msgs::msg::Odometry::ConstSharedPtr>>
218 
219  std::shared_ptr<rtabmap::SensorData> previous_keyframe_;
220 
221  std::map<int, std::shared_ptr<rtabmap::SensorData>> local_descriptors_map_;
222 
223  std::shared_ptr<rclcpp::Node> node_;
224 
227 
228  message_filters::Subscriber<nav_msgs::msg::Odometry> sub_odometry_;
229 
230  rclcpp::Subscription<
231  cslam_common_interfaces::msg::LocalDescriptorsRequest>::SharedPtr
233 
234  rclcpp::Publisher<
235  cslam_common_interfaces::msg::LocalImageDescriptors>::SharedPtr
238 
239  rclcpp::Publisher<cslam_common_interfaces::msg::KeyframeRGB>::SharedPtr
241 
242  rclcpp::Publisher<cslam_common_interfaces::msg::KeyframeOdom>::SharedPtr
244 
245  rclcpp::Publisher<cslam_common_interfaces::msg::VizPointCloud>::SharedPtr
247 
248  rclcpp::Subscription<
249  cslam_common_interfaces::msg::LocalKeyframeMatch>::SharedPtr
251 
252  rclcpp::Subscription<
253  cslam_common_interfaces::msg::LocalImageDescriptors>::SharedPtr
255 
256  rtabmap::RegistrationVis registration_;
257 
258  rclcpp::Publisher<
259  cslam_common_interfaces::msg::InterRobotLoopClosure>::SharedPtr
261 
262  rclcpp::Publisher<
263  cslam_common_interfaces::msg::IntraRobotLoopClosure>::SharedPtr
265 
266  rclcpp::Publisher<
267  diagnostic_msgs::msg::KeyValue>::SharedPtr
271 
272  std::shared_ptr<tf2_ros::Buffer>
274  std::shared_ptr<tf2_ros::TransformListener> tf_listener_;
275 
276  std::string base_frame_id_;
279 
283 
285  std::string gps_topic_;
286  rclcpp::Subscription<sensor_msgs::msg::NavSatFix>::SharedPtr gps_subscriber_;
287  sensor_msgs::msg::NavSatFix latest_gps_fix_;
288  std::deque<sensor_msgs::msg::NavSatFix>
290 
291  private:
292  image_transport::SubscriberFilter sub_image_color_;
293  message_filters::Subscriber<sensor_msgs::msg::CameraInfo> sub_camera_info_color_;
294  image_transport::SubscriberFilter sub_image_depth_;
295  message_filters::Subscriber<sensor_msgs::msg::CameraInfo> sub_camera_info_depth_;
296  typedef message_filters::sync_policies::ApproximateTime<
297  sensor_msgs::msg::Image, sensor_msgs::msg::Image,
298  sensor_msgs::msg::CameraInfo,
299  nav_msgs::msg::Odometry>
300  RGBDSyncPolicy;
301  message_filters::Synchronizer<RGBDSyncPolicy> *rgbd_sync_policy_;
302 
303  sensor_msgs::msg::PointCloud2::SharedPtr cloud_msg_;
304  };
305 } // namespace cslam
306 #endif
Interface class for handling sensor data.
std::shared_ptr< rtabmap::SensorData > previous_keyframe_
Definition: rgbd_handler.h:219
void gps_callback(const sensor_msgs::msg::NavSatFix::ConstSharedPtr msg)
GPS data callback.
void send_keyframe(const std::pair< std::shared_ptr< rtabmap::SensorData >, std::shared_ptr< const nav_msgs::msg::Odometry >> &keypoints_data)
Function to send the image to the python node.
std::shared_ptr< rclcpp::Node > node_
Definition: rgbd_handler.h:223
rclcpp::Publisher< cslam_common_interfaces::msg::VizPointCloud >::SharedPtr keyframe_pointcloud_publisher_
Definition: rgbd_handler.h:246
rclcpp::Subscription< cslam_common_interfaces::msg::LocalImageDescriptors >::SharedPtr local_descriptors_subscriber_
Definition: rgbd_handler.h:254
bool generate_new_keyframe(std::shared_ptr< rtabmap::SensorData > &keyframe)
Generate a new keyframe according to the policy.
void sensor_data_to_rgbd_msg(const std::shared_ptr< rtabmap::SensorData > sensor_data, rtabmap_msgs::msg::RGBDImage &msg_data)
converts sensor data to descriptor msg
rclcpp::Subscription< sensor_msgs::msg::NavSatFix >::SharedPtr gps_subscriber_
Definition: rgbd_handler.h:286
rclcpp::Publisher< cslam_common_interfaces::msg::IntraRobotLoopClosure >::SharedPtr intra_robot_loop_closure_publisher_
Definition: rgbd_handler.h:264
void receive_local_keyframe_match(cslam_common_interfaces::msg::LocalKeyframeMatch::ConstSharedPtr msg)
Receives a local match and tries to compute a local loop closure.
rclcpp::Subscription< cslam_common_interfaces::msg::LocalDescriptorsRequest >::SharedPtr send_local_descriptors_subscriber_
Definition: rgbd_handler.h:232
unsigned int min_inliers_
Definition: rgbd_handler.h:225
unsigned int nb_local_keyframes_
Definition: rgbd_handler.h:226
void send_visualization_pointcloud(const std::shared_ptr< rtabmap::SensorData > &sensor_data)
Send colored pointcloud for visualizations.
std::shared_ptr< tf2_ros::TransformListener > tf_listener_
Definition: rgbd_handler.h:274
sensor_msgs::msg::PointCloud2 visualization_pointcloud_voxel_subsampling(const sensor_msgs::msg::PointCloud2 &input_cloud)
Subsample pointcloud to reduce size for visualization.
void compute_local_descriptors(std::shared_ptr< rtabmap::SensorData > &frame_data)
Computes local 3D descriptors from frame data and store them.
void rgbd_callback(const sensor_msgs::msg::Image::ConstSharedPtr image_rect_rgb, const sensor_msgs::msg::Image::ConstSharedPtr image_rect_depth, const sensor_msgs::msg::CameraInfo::ConstSharedPtr camera_info_rgb, const nav_msgs::msg::Odometry::ConstSharedPtr odom)
Callback receiving sync data from camera.
void send_visualization_keypoints(const std::pair< std::shared_ptr< rtabmap::SensorData >, std::shared_ptr< const nav_msgs::msg::Odometry >> &keypoints_data)
Send keypoints for visualizations.
rtabmap::RegistrationVis registration_
Definition: rgbd_handler.h:256
rclcpp::Publisher< diagnostic_msgs::msg::KeyValue >::SharedPtr log_publisher_
Definition: rgbd_handler.h:268
unsigned int max_queue_size_
Definition: rgbd_handler.h:225
sensor_msgs::msg::NavSatFix latest_gps_fix_
Definition: rgbd_handler.h:287
message_filters::Subscriber< nav_msgs::msg::Odometry > sub_odometry_
Definition: rgbd_handler.h:228
float visualization_max_range_
Definition: rgbd_handler.h:282
float keyframe_generation_ratio_threshold_
Definition: rgbd_handler.h:277
rclcpp::Publisher< cslam_common_interfaces::msg::InterRobotLoopClosure >::SharedPtr inter_robot_loop_closure_publisher_
Definition: rgbd_handler.h:260
std::string gps_topic_
Definition: rgbd_handler.h:285
std::map< int, std::shared_ptr< rtabmap::SensorData > > local_descriptors_map_
Definition: rgbd_handler.h:221
unsigned int log_total_local_descriptors_cumulative_communication_
Definition: rgbd_handler.h:269
std::string base_frame_id_
Definition: rgbd_handler.h:276
std::deque< std::pair< std::shared_ptr< rtabmap::SensorData >, nav_msgs::msg::Odometry::ConstSharedPtr > > received_data_queue_
Definition: rgbd_handler.h:217
std::deque< sensor_msgs::msg::NavSatFix > received_gps_queue_
Definition: rgbd_handler.h:289
bool generate_new_keyframes_based_on_inliers_ratio_
Definition: rgbd_handler.h:278
rclcpp::Publisher< cslam_common_interfaces::msg::KeyframeOdom >::SharedPtr keyframe_odom_publisher_
Definition: rgbd_handler.h:243
unsigned int robot_id_
Definition: rgbd_handler.h:225
rclcpp::Subscription< cslam_common_interfaces::msg::LocalKeyframeMatch >::SharedPtr local_keyframe_match_subscriber_
Definition: rgbd_handler.h:250
unsigned int max_nb_robots_
Definition: rgbd_handler.h:225
float visualization_voxel_size_
Definition: rgbd_handler.h:282
virtual void send_visualization(const std::pair< std::shared_ptr< rtabmap::SensorData >, std::shared_ptr< const nav_msgs::msg::Odometry >> &keypoints_data)
Send keypoints for visualizations.
rclcpp::Publisher< cslam_common_interfaces::msg::KeyframeRGB >::SharedPtr keyframe_data_publisher_
Definition: rgbd_handler.h:240
unsigned int visualization_period_ms_
Definition: rgbd_handler.h:280
void receive_local_image_descriptors(const std::shared_ptr< cslam_common_interfaces::msg::LocalImageDescriptors > msg)
Message callback to receive descriptors and compute.
rclcpp::Publisher< cslam_common_interfaces::msg::LocalImageDescriptors >::SharedPtr visualization_local_descriptors_publisher_
Definition: rgbd_handler.h:237
rclcpp::Publisher< cslam_common_interfaces::msg::LocalImageDescriptors >::SharedPtr local_descriptors_publisher_
Definition: rgbd_handler.h:236
std::shared_ptr< tf2_ros::Buffer > tf_buffer_
Definition: rgbd_handler.h:273
void clear_sensor_data(std::shared_ptr< rtabmap::SensorData > &sensor_data)
Clear images and large data fields in sensor data.
virtual void local_descriptors_msg_to_sensor_data(const std::shared_ptr< cslam_common_interfaces::msg::LocalImageDescriptors > msg, rtabmap::SensorData &sensor_data)
converts descriptors to sensore data
void local_descriptors_request(cslam_common_interfaces::msg::LocalDescriptorsRequest::ConstSharedPtr request)
Service callback to publish local descriptors.
virtual void process_new_sensor_data()
Processes Latest received image.
RGBDHandler(std::shared_ptr< rclcpp::Node > &node)
Initialization of parameters and ROS 2 objects.
Definition: __init__.py:1