ISAAC  0.2.11
Flight software for the ISAAC project, adding functionality to the Astrobee robot, operating inside the International Space Station.
All Classes Functions Variables Pages
dense_map_ros_utils.h
1 /* Copyright (c) 2021, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The "ISAAC - Integrated System for Autonomous and Adaptive Caretaking
7  * platform" software is licensed under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with the
9  * License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16  * License for the specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef DENSE_MAP_ROS_UTILS_H_
21 #define DENSE_MAP_ROS_UTILS_H_
22 
23 // ROS includes
24 #include <ros/ros.h>
25 #include <rosbag/view.h>
26 #include <sensor_msgs/point_cloud2_iterator.h>
27 #include <tf2/LinearMath/Matrix3x3.h>
28 #include <tf2_geometry_msgs/tf2_geometry_msgs.h>
29 #include <tf2_ros/transform_broadcaster.h>
30 #include <tf2_ros/transform_listener.h>
31 
32 #include <opencv2/core/core.hpp>
33 
34 #include <Eigen/Core>
35 #include <Eigen/Geometry>
36 
37 #include <dense_map_utils.h>
38 
39 #include <map>
40 #include <string>
41 #include <vector>
42 
43 // Forward declaration
44 namespace pcl {
45  class PointXYZ;
46  template<class T>
47  class PointCloud;
48 }
49 
50 namespace dense_map {
51 
52 // Publish a given pose
53 void PublishTF(const Eigen::Affine3d& publish_tf, std::string const& parent_frame, std::string const& child_frame,
54  ros::Time const& timestamp, tf2_ros::TransformBroadcaster& tf_publisher);
55 
56 void readBagPoses(std::string const& bag_file, std::string const& topic, StampedPoseStorage& poses);
57 
58 void readBagImageTimestamps(std::string const& bag_file, std::string const& topic, std::vector<double>& timestamps);
59 
60 // Given a bag view, for each topic in the view read the vector of
61 // messages for that topic, sorted by message header timestamp. Only
62 // the following sensor types are supported: sensor_msgs::Image,
63 // sensor_msgs::CompressedImage, and sensor_msgs::PointCloud2.
64 void indexMessages(rosbag::View& view, // view can't be made const
65  std::map<std::string, std::vector<rosbag::MessageInstance>>& bag_map);
66 
67 void readExifFromBag(std::vector<rosbag::MessageInstance> const& bag_msgs, std::map<double, std::vector<double>>& exif);
68 
69 // Find an image at the given timestamp or right after it. We assume
70 // that during repeated calls to this function we always travel
71 // forward in time, and we keep track of where we are in the bag using
72 // the variable bag_pos that we update as we go.
73 bool lookupImage(double desired_time, std::vector<rosbag::MessageInstance> const& bag_msgs, bool save_grayscale,
74  cv::Mat& image, int& bag_pos, double& found_time, std::string image_dir = "");
75 
76 // Find the closest depth cloud to given timestamp. Return an empty
77 // cloud if one cannot be found closer in time than max_time_diff.
78 // Store it as a cv::Mat of vec3f values. We assume that during
79 // repeated calls to this function we always travel forward in time,
80 // and we keep track of where we are in the bag using the variable
81 // bag_pos that we update as we go.
82 bool lookupCloud(double desired_time, std::vector<rosbag::MessageInstance> const& bag_msgs,
83  double max_time_diff, cv::Mat& cloud, int& bag_pos, double& found_time);
84 
85 // A wrapper around a function in pcl_ros/point_cloud.h to avoid
86 // including that header all over the place as it creates an annoying
87 // warning.
88 void msgToPcl(sensor_msgs::PointCloud2::ConstPtr pc_msg, pcl::PointCloud<pcl::PointXYZ> & pc);
89 
90 // Read the list of topics in a bag while avoiding repetitions
91 void readTopicsInBag(std::string const& bag_file, std::vector<std::string>& topics);
92 
93 // A small struct in which to store an opened ROS bag and the vector of its messages
94 // that we will use later to quickly navigate through it while going forward in time.
95 struct RosBagHandle {
96  RosBagHandle() = delete; // The rosbag API prevents anything else than initialization
97  RosBagHandle(std::string const& bag_file, std::string const& topic) {
98  bag_msgs.clear();
99  bag.open(bag_file, rosbag::bagmode::Read);
100  std::vector<std::string> topics;
101  topics.push_back(topic);
102  view = boost::shared_ptr<rosbag::View>(new rosbag::View(bag, rosbag::TopicQuery(topics)));
103  for (rosbag::MessageInstance const m : *view) bag_msgs.push_back(m);
104  }
105  rosbag::Bag bag;
106  boost::shared_ptr<rosbag::View> view;
107  std::vector<rosbag::MessageInstance> bag_msgs;
108 };
109 
110 } // end namespace dense_map
111 
112 #endif // DENSE_MAP_ROS_UTILS_H_
pcl::PointCloud
Definition: dense_map_ros_utils.h:47
dense_map::RosBagHandle
Definition: dense_map_ros_utils.h:95