base_realsense_node.h
Go to the documentation of this file.
00001 // License: Apache 2.0. See LICENSE file in root directory.
00002 // Copyright(c) 2018 Intel Corporation. All Rights Reserved
00003 
00004 #pragma once
00005 
00006 #include "../include/realsense_node_factory.h"
00007 #include <ddynamic_reconfigure/ddynamic_reconfigure.h>
00008 
00009 #include <diagnostic_updater/diagnostic_updater.h>
00010 #include <diagnostic_updater/update_functions.h>
00011 #include <sensor_msgs/CameraInfo.h>
00012 #include <sensor_msgs/PointCloud2.h>
00013 #include <sensor_msgs/point_cloud2_iterator.h>
00014 #include <sensor_msgs/Imu.h>
00015 #include <nav_msgs/Odometry.h>
00016 #include <tf/transform_broadcaster.h>
00017 #include <tf2_ros/static_transform_broadcaster.h>
00018 
00019 #include <queue>
00020 #include <mutex>
00021 #include <atomic>
00022 
00023 namespace realsense2_camera
00024 {
00025     struct FrequencyDiagnostics
00026     {
00027       FrequencyDiagnostics(double expected_frequency, std::string name, std::string hardware_id) :
00028         expected_frequency_(expected_frequency),
00029         frequency_status_(diagnostic_updater::FrequencyStatusParam(&expected_frequency_, &expected_frequency_)),
00030         diagnostic_updater_(ros::NodeHandle(), ros::NodeHandle("~"), ros::this_node::getName() + "_" + name)
00031       {
00032         ROS_INFO("Expected frequency for %s = %.5f", name.c_str(), expected_frequency_);
00033         diagnostic_updater_.setHardwareID(hardware_id);
00034         diagnostic_updater_.add(frequency_status_);
00035       }
00036 
00037       void update()
00038       {
00039         frequency_status_.tick();
00040         diagnostic_updater_.update();
00041       }
00042 
00043       double expected_frequency_;
00044       diagnostic_updater::FrequencyStatus frequency_status_;
00045       diagnostic_updater::Updater diagnostic_updater_;
00046     };
00047     typedef std::pair<image_transport::Publisher, std::shared_ptr<FrequencyDiagnostics>> ImagePublisherWithFrequencyDiagnostics;
00048 
00049     class NamedFilter
00050     {
00051         public:
00052             std::string _name;
00053             std::shared_ptr<rs2::filter> _filter;
00054 
00055         public:
00056             NamedFilter(std::string name, std::shared_ptr<rs2::filter> filter):
00057             _name(name), _filter(filter)
00058             {}
00059     };
00060 
00061         class PipelineSyncer : public rs2::asynchronous_syncer
00062         {
00063         public: 
00064                 void operator()(rs2::frame f) const
00065                 {
00066                         invoke(std::move(f));
00067                 }
00068         };
00069 
00070     class SyncedImuPublisher
00071     {
00072         public:
00073             SyncedImuPublisher() {_is_enabled=false;};
00074             SyncedImuPublisher(ros::Publisher imu_publisher, std::size_t waiting_list_size=1000);
00075             ~SyncedImuPublisher();
00076             void Pause();   // Pause sending messages. All messages from now on are saved in queue.
00077             void Resume();  // Send all pending messages and allow sending future messages.
00078             void Publish(sensor_msgs::Imu msg);     //either send or hold message.
00079             uint32_t getNumSubscribers() { return _publisher.getNumSubscribers();};
00080             void Enable(bool is_enabled) {_is_enabled=is_enabled;};
00081         
00082         private:
00083             void PublishPendingMessages();
00084 
00085         private:
00086             std::mutex                    _mutex;
00087             ros::Publisher                _publisher;
00088             bool                          _pause_mode;
00089             std::queue<sensor_msgs::Imu>  _pending_messages;
00090             std::size_t                     _waiting_list_size;
00091             bool                          _is_enabled;
00092     };
00093 
00094     class BaseRealSenseNode : public InterfaceRealSenseNode
00095     {
00096     public:
00097         BaseRealSenseNode(ros::NodeHandle& nodeHandle,
00098                           ros::NodeHandle& privateNodeHandle,
00099                           rs2::device dev,
00100                           const std::string& serial_no);
00101 
00102         void toggleSensors(bool enabled);
00103         virtual void publishTopics() override;
00104         virtual void registerDynamicReconfigCb(ros::NodeHandle& nh) override;
00105         virtual ~BaseRealSenseNode() {}
00106 
00107     public:
00108         enum imu_sync_method{NONE, COPY, LINEAR_INTERPOLATION};
00109 
00110     protected:
00111         class float3
00112         {
00113             public:
00114                 float x, y, z;
00115 
00116             public:
00117                 float3& operator*=(const float& factor)
00118                 {
00119                     x*=factor;
00120                     y*=factor;
00121                     z*=factor;
00122                     return (*this);
00123                 }
00124                 float3& operator+=(const float3& other)
00125                 {
00126                     x+=other.x;
00127                     y+=other.y;
00128                     z+=other.z;
00129                     return (*this);
00130                 }
00131         };
00132 
00133         std::string _base_frame_id;
00134         std::string _odom_frame_id;
00135         std::map<stream_index_pair, std::string> _frame_id;
00136         std::map<stream_index_pair, std::string> _optical_frame_id;
00137         std::map<stream_index_pair, std::string> _depth_aligned_frame_id;
00138         ros::NodeHandle& _node_handle, _pnh;
00139         bool _align_depth;
00140 
00141         virtual void calcAndPublishStaticTransform(const stream_index_pair& stream, const rs2::stream_profile& base_profile);
00142         rs2::stream_profile getAProfile(const stream_index_pair& stream);
00143         tf::Quaternion rotationMatrixToQuaternion(const float rotation[9]) const;
00144         void publish_static_tf(const ros::Time& t,
00145                                const float3& trans,
00146                                const tf::Quaternion& q,
00147                                const std::string& from,
00148                                const std::string& to);
00149 
00150 
00151     private:
00152         class CIMUHistory
00153         {
00154             public:
00155                 enum sensor_name {mGYRO, mACCEL};
00156                 class imuData
00157                 {
00158                     public:
00159                         imuData(const imuData& other):
00160                             imuData(other.m_reading, other.m_time)
00161                             {};
00162                         imuData(const float3 reading, double time):
00163                             m_reading(reading),
00164                             m_time(time)
00165                             {};
00166                         imuData operator*(const double factor);
00167                         imuData operator+(const imuData& other);
00168                     public:
00169                         BaseRealSenseNode::float3 m_reading;
00170                         double                    m_time;
00171                 };
00172                 
00173             private:
00174                 size_t m_max_size;
00175                 std::map<sensor_name, std::list<imuData> > m_map;
00176 
00177             public:
00178                 CIMUHistory(size_t size);
00179                 void add_data(sensor_name module, imuData data);
00180                 bool is_all_data(sensor_name);
00181                 bool is_data(sensor_name);
00182                 const std::list<imuData>& get_data(sensor_name module);
00183                 imuData last_data(sensor_name module);
00184         };
00185 
00186         static std::string getNamespaceStr();
00187         void getParameters();
00188         void setupDevice();
00189         void setupErrorCallback();
00190         void setupPublishers();
00191         void enable_devices();
00192         void setupFilters();
00193         void setupStreams();
00194         void setBaseTime(double frame_time, bool warn_no_metadata);
00195         cv::Mat& fix_depth_scale(const cv::Mat& from_image, cv::Mat& to_image);
00196         void clip_depth(rs2::depth_frame depth_frame, float clipping_dist);
00197         void updateStreamCalibData(const rs2::video_stream_profile& video_profile);
00198         void publishStaticTransforms();
00199         void publishIntrinsics();
00200         void publishPointCloud(rs2::points f, const ros::Time& t, const rs2::frameset& frameset);
00201         Extrinsics rsExtrinsicsToMsg(const rs2_extrinsics& extrinsics, const std::string& frame_id) const;
00202 
00203         IMUInfo getImuInfo(const stream_index_pair& stream_index);
00204         void publishFrame(rs2::frame f, const ros::Time& t,
00205                           const stream_index_pair& stream,
00206                           std::map<stream_index_pair, cv::Mat>& images,
00207                           const std::map<stream_index_pair, ros::Publisher>& info_publishers,
00208                           const std::map<stream_index_pair, ImagePublisherWithFrequencyDiagnostics>& image_publishers,
00209                           std::map<stream_index_pair, int>& seq,
00210                           std::map<stream_index_pair, sensor_msgs::CameraInfo>& camera_info,
00211                           const std::map<stream_index_pair, std::string>& optical_frame_id,
00212                           const std::map<rs2_stream, std::string>& encoding,
00213                           bool copy_data_from_frame = true);
00214         bool getEnabledProfile(const stream_index_pair& stream_index, rs2::stream_profile& profile);
00215 
00216         void publishAlignedDepthToOthers(rs2::frameset frames, const ros::Time& t);
00217         double FillImuData_Copy(const stream_index_pair stream_index, const CIMUHistory::imuData imu_data, sensor_msgs::Imu& imu_msg);
00218         double FillImuData_LinearInterpolation(const stream_index_pair stream_index, const CIMUHistory::imuData imu_data, sensor_msgs::Imu& imu_msg);
00219         void imu_callback(rs2::frame frame);
00220         void imu_callback_sync(rs2::frame frame, imu_sync_method sync_method=imu_sync_method::COPY);
00221         void pose_callback(rs2::frame frame);
00222         void multiple_message_callback(rs2::frame frame, imu_sync_method sync_method);
00223         void frame_callback(rs2::frame frame);
00224         void registerDynamicOption(ros::NodeHandle& nh, rs2::options sensor, std::string& module_name);
00225         rs2_stream rs2_string_to_stream(std::string str);
00226 
00227         rs2::device _dev;
00228         std::map<stream_index_pair, rs2::sensor> _sensors;
00229         std::map<std::string, std::function<void(rs2::frame)>> _sensors_callback;
00230         std::vector<std::shared_ptr<ddynamic_reconfigure::DDynamicReconfigure>> _ddynrec;
00231 
00232         std::string _json_file_path;
00233         std::string _serial_no;
00234         float _depth_scale_meters;
00235         float _clipping_distance;
00236         bool _allow_no_texture_points;
00237 
00238         double _linear_accel_cov;
00239         double _angular_velocity_cov;
00240         bool  _hold_back_imu_for_frames;
00241 
00242         std::map<stream_index_pair, rs2_intrinsics> _stream_intrinsics;
00243         std::map<stream_index_pair, int> _width;
00244         std::map<stream_index_pair, int> _height;
00245         std::map<stream_index_pair, int> _fps;
00246         std::map<stream_index_pair, bool> _enable;
00247         std::map<rs2_stream, std::string> _stream_name;
00248         tf2_ros::StaticTransformBroadcaster _static_tf_broadcaster;
00249 
00250         std::map<stream_index_pair, ImagePublisherWithFrequencyDiagnostics> _image_publishers;
00251         std::map<stream_index_pair, ros::Publisher> _imu_publishers;
00252         std::shared_ptr<SyncedImuPublisher> _synced_imu_publisher;
00253         std::map<rs2_stream, int> _image_format;
00254         std::map<stream_index_pair, ros::Publisher> _info_publisher;
00255         std::map<stream_index_pair, cv::Mat> _image;
00256         std::map<rs2_stream, std::string> _encoding;
00257 
00258         std::map<stream_index_pair, int> _seq;
00259         std::map<rs2_stream, int> _unit_step_size;
00260         std::map<stream_index_pair, sensor_msgs::CameraInfo> _camera_info;
00261         std::atomic_bool _is_initialized_time_base;
00262         double _camera_time_base;
00263         std::map<stream_index_pair, std::vector<rs2::stream_profile>> _enabled_profiles;
00264 
00265         ros::Publisher _pointcloud_publisher;
00266         ros::Time _ros_time_base;
00267         bool _sync_frames;
00268         bool _pointcloud;
00269         bool _publish_odom_tf;
00270         imu_sync_method _imu_sync_method;
00271         std::string _filters_str;
00272         stream_index_pair _pointcloud_texture;
00273         PipelineSyncer _syncer;
00274         std::vector<NamedFilter> _filters;
00275         std::vector<rs2::sensor> _dev_sensors;
00276         std::map<rs2_stream, std::shared_ptr<rs2::align>> _align;
00277 
00278         std::map<stream_index_pair, cv::Mat> _depth_aligned_image;
00279         std::map<stream_index_pair, cv::Mat> _depth_scaled_image;
00280         std::map<rs2_stream, std::string> _depth_aligned_encoding;
00281         std::map<stream_index_pair, sensor_msgs::CameraInfo> _depth_aligned_camera_info;
00282         std::map<stream_index_pair, int> _depth_aligned_seq;
00283         std::map<stream_index_pair, ros::Publisher> _depth_aligned_info_publisher;
00284         std::map<stream_index_pair, ImagePublisherWithFrequencyDiagnostics> _depth_aligned_image_publishers;
00285         std::map<stream_index_pair, ros::Publisher> _depth_to_other_extrinsics_publishers;
00286         std::map<stream_index_pair, rs2_extrinsics> _depth_to_other_extrinsics;
00287 
00288         const std::string _namespace;
00289 
00290     };//end class
00291 
00292 }
00293 


realsense2_camera
Author(s): Sergey Dorodnicov , Doron Hirshberg
autogenerated on Sat Jul 6 2019 03:30:09