Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #pragma once
00032 #ifndef REALSENSE_CAMERA_BASE_NODELET_H
00033 #define REALSENSE_CAMERA_BASE_NODELET_H
00034
00035 #include <iostream>
00036 #include <boost/thread.hpp>
00037 #include <boost/algorithm/string/join.hpp>
00038 #include <thread>
00039 #include <cstdlib>
00040 #include <cctype>
00041 #include <algorithm>
00042 #include <sstream>
00043 #include <memory>
00044 #include <map>
00045 #include <queue>
00046 #include <unistd.h>
00047 #include <signal.h>
00048 #include <string>
00049 #include <vector>
00050
00051 #include <opencv2/core/core.hpp>
00052 #include <cv_bridge/cv_bridge.h>
00053 #include <opencv2/highgui/highgui.hpp>
00054
00055 #include <ros/ros.h>
00056 #include <nodelet/nodelet.h>
00057 #include <sensor_msgs/PointCloud2.h>
00058 #include <sensor_msgs/image_encodings.h>
00059 #include <sensor_msgs/point_cloud2_iterator.h>
00060 #include <std_msgs/String.h>
00061 #include <std_msgs/Float32MultiArray.h>
00062 #include <image_transport/image_transport.h>
00063 #include <camera_info_manager/camera_info_manager.h>
00064 #include <librealsense/rs.hpp>
00065 #include <pluginlib/class_list_macros.h>
00066 #include <tf/transform_broadcaster.h>
00067 #include <tf2_ros/static_transform_broadcaster.h>
00068
00069 #include <realsense_camera/CameraConfiguration.h>
00070 #include <realsense_camera/IsPowered.h>
00071 #include <realsense_camera/SetPower.h>
00072 #include <realsense_camera/ForcePower.h>
00073 #include <realsense_camera/constants.h>
00074
00075 namespace realsense_camera
00076 {
00077 class BaseNodelet: public nodelet::Nodelet
00078 {
00079 public:
00080
00081 virtual void onInit();
00082 virtual ~BaseNodelet();
00083 virtual void setDepthEnable(bool &enable_depth);
00084 virtual bool getCameraOptionValues(realsense_camera::CameraConfiguration::Request & req,
00085 realsense_camera::CameraConfiguration::Response & res);
00086 virtual bool setPowerCameraService(realsense_camera::SetPower::Request & req,
00087 realsense_camera::SetPower::Response & res);
00088 virtual bool forcePowerCameraService(realsense_camera::ForcePower::Request & req,
00089 realsense_camera::ForcePower::Response & res);
00090 virtual bool isPoweredCameraService(realsense_camera::IsPowered::Request & req,
00091 realsense_camera::IsPowered::Response & res);
00092
00093 protected:
00094
00095 ros::NodeHandle nh_;
00096 ros::NodeHandle pnh_;
00097 ros::Time camera_start_ts_;
00098 ros::Publisher pointcloud_publisher_;
00099 ros::ServiceServer get_options_service_;
00100 ros::ServiceServer set_power_service_;
00101 ros::ServiceServer force_power_service_;
00102 ros::ServiceServer is_powered_service_;
00103 rs_error *rs_error_ = NULL;
00104 rs_context *rs_context_ = NULL;
00105 rs_device *rs_device_;
00106 std::string nodelet_name_;
00107 std::string serial_no_;
00108 std::string usb_port_id_;
00109 std::string camera_type_;
00110 std::string mode_;
00111 bool enable_[STREAM_COUNT] = {false};
00112 int width_[STREAM_COUNT];
00113 int height_[STREAM_COUNT];
00114 int fps_[STREAM_COUNT];
00115 rs_format format_[STREAM_COUNT];
00116 std::string encoding_[STREAM_COUNT];
00117 int cv_type_[STREAM_COUNT];
00118 int unit_step_size_[STREAM_COUNT];
00119 int step_[STREAM_COUNT];
00120 double ts_[STREAM_COUNT];
00121 std::string frame_id_[STREAM_COUNT];
00122 std::string optical_frame_id_[STREAM_COUNT];
00123 cv::Mat image_[STREAM_COUNT] = {};
00124 image_transport::CameraPublisher camera_publisher_[STREAM_COUNT] = {};
00125 sensor_msgs::CameraInfoPtr camera_info_ptr_[STREAM_COUNT] = {};
00126 std::string base_frame_id_;
00127 float max_z_ = -1.0f;
00128 bool enable_pointcloud_;
00129 bool enable_tf_;
00130 bool enable_tf_dynamic_;
00131 double tf_publication_rate_;
00132 const uint16_t *image_depth16_;
00133 cv::Mat cvWrapper_;
00134 std::mutex frame_mutex_[STREAM_COUNT];
00135
00136 boost::shared_ptr<boost::thread> transform_thread_;
00137 ros::Time transform_ts_;
00138 tf2_ros::StaticTransformBroadcaster static_tf_broadcaster_;
00139 tf::TransformBroadcaster dynamic_tf_broadcaster_;
00140 rs_extrinsics color2depth_extrinsic_;
00141 rs_extrinsics color2ir_extrinsic_;
00142 rs_source rs_source_ = RS_SOURCE_VIDEO;
00143 bool start_camera_ = true;
00144 bool start_stop_srv_called_ = false;
00145
00146 struct CameraOptions
00147 {
00148 rs_option opt;
00149 double min, max, step, value;
00150 };
00151 std::vector<CameraOptions> camera_options_;
00152
00153 std::queue<pid_t> system_proc_groups_;
00154
00155
00156 virtual void getParameters();
00157 virtual bool connectToCamera();
00158 virtual std::vector<int> listCameras(int num_of_camera);
00159 virtual void advertiseTopics();
00160 virtual void advertiseServices();
00161 virtual std::vector<std::string> setDynamicReconfServer() { return {}; }
00162 virtual void startDynamicReconfCallback() { return; }
00163 virtual void getCameraOptions();
00164 virtual void setStaticCameraOptions(std::vector<std::string> dynamic_params);
00165 virtual void setStreams();
00166 virtual void enableStream(rs_stream stream_index, int width, int height, rs_format format, int fps);
00167 virtual void getStreamCalibData(rs_stream stream_index);
00168 virtual void disableStream(rs_stream stream_index);
00169 virtual std::string startCamera();
00170 virtual std::string stopCamera();
00171 virtual ros::Time getTimestamp(rs_stream stream_index, double frame_ts);
00172 virtual void publishTopic(rs_stream stream_index, rs::frame & frame);
00173 virtual void setImageData(rs_stream stream_index, rs::frame & frame);
00174 virtual void publishPCTopic();
00175 virtual void getCameraExtrinsics();
00176 virtual void publishStaticTransforms();
00177 virtual void publishDynamicTransforms();
00178 virtual void prepareTransforms();
00179 virtual void checkError();
00180 virtual bool checkForSubscriber();
00181 virtual void wrappedSystem(const std::vector<std::string>& string_argv);
00182 virtual void setFrameCallbacks();
00183 virtual std::string checkFirmwareValidation(const std::string& fw_type,
00184 const std::string& current_fw,
00185 const std::string& camera_name,
00186 const std::string& camera_serial_number);
00187 std::function<void(rs::frame f)> depth_frame_handler_, color_frame_handler_, ir_frame_handler_;
00188 };
00189 }
00190 #endif // REALSENSE_CAMERA_BASE_NODELET_H