tango_ros_nodelet.h
Go to the documentation of this file.
00001 // Copyright 2016 Intermodalics All Rights Reserved.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      http://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 #ifndef TANGO_ROS_NODELET_H_
00015 #define TANGO_ROS_NODELET_H_
00016 #include <atomic>
00017 #include <chrono>
00018 #include <condition_variable>
00019 #include <mutex>
00020 #include <string>
00021 #include <thread>
00022 #include <time.h>
00023 
00024 #include <tango_3d_reconstruction/tango_3d_reconstruction_api.h>
00025 #include <tango_client_api/tango_client_api.h>
00026 #include <tango_support/tango_support.h>
00027 
00028 #include <opencv2/core/core.hpp>
00029 
00030 #include <camera_info_manager/camera_info_manager.h>
00031 #include <cv_bridge/cv_bridge.h>
00032 #include <geometry_msgs/TransformStamped.h>
00033 #include <image_geometry/pinhole_camera_model.h>
00034 #include <image_transport/image_transport.h>
00035 #include <nav_msgs/OccupancyGrid.h>
00036 #include <nodelet/nodelet.h>
00037 #include <ros/ros.h>
00038 #include <ros/node_handle.h>
00039 #include <sensor_msgs/CameraInfo.h>
00040 #include <sensor_msgs/LaserScan.h>
00041 #include <sensor_msgs/PointCloud2.h>
00042 #include <tango_ros_messages/GetMapName.h>
00043 #include <tango_ros_messages/GetMapUuids.h>
00044 #include <tango_ros_messages/LoadOccupancyGrid.h>
00045 #include <tango_ros_messages/SaveMap.h>
00046 #include <tango_ros_messages/TangoConnect.h>
00047 #include <tf/transform_broadcaster.h>
00048 #include <tf2_ros/static_transform_broadcaster.h>
00049 #include <visualization_msgs/MarkerArray.h>
00050 
00051 #include "tango_ros_native/PublisherConfig.h"
00052 
00053 namespace tango_ros_native {
00054 // See laser scan message doc for definition of laser scan constants:
00055 // http://docs.ros.org/api/sensor_msgs/html/msg/LaserScan.html
00056 const float LASER_SCAN_ANGLE_MIN = 0.;
00057 const float LASER_SCAN_ANGLE_MAX = 3.1415;
00058 const float LASER_SCAN_ANGLE_INCREMENT = 3.1415 / 360;
00059 const float LASER_SCAN_TIME_INCREMENT = 0.0;
00060 const float LASER_SCAN_SCAN_TIME= 0.3333;
00061 const std::string LASER_SCAN_FRAME_ID = "laser";
00062 
00063 const std::string DATASET_DEFAULT_DIRECTORY = "/sdcard/tango_ros_streamer/datasets/";
00064 const std::string OCCUPANCY_GRID_DEFAULT_DIRECTORY = "/sdcard/tango_ros_streamer/occupancy_grids/";
00065 const int NUMBER_OF_STATIC_TRANSFORMS = 5;
00066 
00067 const std::string TANGO_STATUS_TOPIC_NAME = "status";
00068 const std::string POINT_CLOUD_TOPIC_NAME = "point_cloud";
00069 const std::string LASER_SCAN_TOPIC_NAME = "laser_scan";
00070 const std::string FISHEYE_IMAGE_TOPIC_NAME = "camera/fisheye_1/image_raw";
00071 const std::string FISHEYE_RECTIFIED_IMAGE_TOPIC_NAME = "camera/fisheye_1/image_rect";
00072 const std::string COLOR_IMAGE_TOPIC_NAME = "camera/color_1/image_raw";
00073 const std::string COLOR_RECTIFIED_IMAGE_TOPIC_NAME = "camera/color_1/image_rect";
00074 const std::string COLOR_MESH_TOPIC_NAME = "reconstruction/mesh";
00075 const std::string OCCUPANCY_GRID_TOPIC_NAME = "reconstruction/occupancy_grid";
00076 const std::string STATIC_OCCUPANCY_GRID_TOPIC_NAME = "static_occupancy_grid";
00077 const std::string START_OF_SERVICE_T_DEVICE_TOPIC_NAME = "transform/start_of_service_T_device";
00078 const std::string AREA_DESCRIPTION_T_START_OF_SERVICE_TOPIC_NAME = "transform/area_description_T_start_of_service";
00079 
00080 const std::string CREATE_NEW_MAP_PARAM_NAME = "create_new_map";
00081 const std::string LOCALIZATION_MODE_PARAM_NAME = "localization_mode";
00082 const std::string LOCALIZATION_MAP_UUID_PARAM_NAME = "localization_map_uuid";
00083 const std::string DATASET_DIRECTORY_PARAM_NAME = "dataset_directory";
00084 const std::string DATASET_UUID_PARAM_NAME = "dataset_uuid";
00085 const std::string ENABLE_DEPTH_PARAM_NAME = "enable_depth";
00086 const std::string ENABLE_COLOR_CAMERA_PARAM_NAME = "enable_color_camera";
00087 const std::string PUBLISH_POSE_ON_TF_PARAM_NAME = "publish_pose_on_tf";
00088 const std::string ENABLE_3DR_MESH_PARAM_NAME = "enable_3dr_mesh";
00089 const std::string ENABLE_3DR_OCCUPANCY_GRID_PARAM_NAME = "enable_3dr_occupancy_grid";
00090 const std::string USE_TF_STATIC_PARAM_NAME = "use_tf_static";
00091 const std::string START_OF_SERVICE_FRAME_ID_PARAM_NAME = "start_of_service_frame_id";
00092 const std::string AREA_DESCRIPTION_FRAME_ID_PARAM_NAME = "area_description_frame_id";
00093 const std::string OCCUPANCY_GRID_DIRECTORY_PARAM_NAME = "occupancy_grid_directory";
00094 
00095 const std::string GET_MAP_NAME_SERVICE_NAME = "get_map_name";
00096 const std::string GET_MAP_UUIDS_SERVICE_NAME = "get_map_uuids";
00097 const std::string SAVE_MAP_SERVICE_NAME = "save_map";
00098 const std::string LOAD_OCCUPANCY_GRID_SERVICE_NAME = "load_occupancy_grid";
00099 const std::string CONNECT_SERVICE_NAME = "connect";
00100 
00101 // Localization mode values.
00102 // See http://developers.google.com/tango/overview/area-learning to know more
00103 // about Tango localization
00104 enum LocalizationMode {
00105   // No map required. Internally runs VIO (Visual Inertial Odometry) from Tango.
00106   ODOMETRY = 1,
00107   // No map required. Internally runs COM (Concurrent Odometry and Mapping,
00108   // also mentioned as drift correction) from Tango.
00109   ONLINE_SLAM = 2,
00110   // Map required. Internally runs Tango localization on ADF (Area Description File).
00111   LOCALIZATION = 3
00112 };
00113 
00114 enum class TangoStatus {
00115   UNKNOWN = 0,
00116   SERVICE_NOT_CONNECTED,
00117   NO_FIRST_VALID_POSE,
00118   SERVICE_CONNECTED
00119 };
00120 
00121 struct PublishThread {
00122   std::thread publish_thread;
00123   std::mutex data_available_mutex;
00124   std::condition_variable data_available;
00125 };
00126 
00127 // Node collecting tango data and publishing it on ros topics.
00128 class TangoRosNodelet : public ::nodelet::Nodelet {
00129  public:
00130   TangoRosNodelet();
00131   ~TangoRosNodelet();
00132   // Initialization function called when plugin is loaded.
00133   void onInit();
00134   // Gets the full list of map Uuids (Universally Unique IDentifier)
00135   // available on the device.
00136   // @param uuid_list to fill in
00137   // @return false if list could not be requested.
00138   bool GetAvailableMapUuidsList(std::vector<std::string>& uuid_list);
00139   // Gets the map name corresponding to a given map uuid.
00140   // @param map_uuid UUID to lookup
00141   // @param map_name to fill in
00142   // @return false if the uuid was not found
00143   bool GetMapNameFromUuid(const std::string& map_uuid, std::string& map_name);
00144 
00145   // Tries to get a first valid pose and sets the camera intrinsics.
00146   // @return returns success if it ended successfully.
00147   TangoErrorType OnTangoServiceConnected();
00148   // Disconnects from the tango service.
00149   void TangoDisconnect();
00150   // Starts the threads that publish data.
00151   void StartPublishing();
00152   // Stops the threads that publish data.
00153   // Will not return until all the internal threads have exited.
00154   void StopPublishing();
00155 
00156   // Function called when a new device pose is available.
00157   void OnPoseAvailable(const TangoPoseData* pose);
00158   // Function called when a new point cloud is available.
00159   void OnPointCloudAvailable(const TangoPointCloud* point_cloud);
00160   // Function called when a new camera image is available.
00161   void OnFrameAvailable(TangoCameraId camera_id, const TangoImageBuffer* buffer);
00162 
00163  private:
00164   // Sets the tango config to be able to collect all necessary data from tango.
00165   // @return returns TANGO_SUCCESS if the config was set successfully.
00166   TangoErrorType TangoSetupConfig();
00167   // Connects to the tango service and to the necessary callbacks.
00168   // @return returns TANGO_SUCCESS if connecting to tango ended successfully
00169   // or if service was already connected.
00170   TangoErrorType TangoConnect();
00171   // Sets up config, connect to Tango service, starts publishing threads and
00172   // publishes first static transforms.
00173   TangoErrorType ConnectToTangoAndSetUpNode();
00174   // Helper function to update and publish the Tango status. Publishes always
00175   // for convenience, even if Tango status did not change.
00176   void UpdateAndPublishTangoStatus(const TangoStatus& status);
00177   // Publishes the necessary static transforms (device_T_camera_*).
00178   void PublishStaticTransforms();
00179   // Publishes the available data (device pose, point cloud, images, ...).
00180   void PublishDevicePose();
00181   void PublishPointCloud();
00182   void PublishLaserScan();
00183   void PublishFisheyeImage();
00184   void PublishColorImage();
00185   void PublishMesh();
00186   // Runs ros::spinOnce() in a loop to trigger subscribers callbacks (e.g. dynamic reconfigure).
00187   void RunRosSpin();
00188   // Function called when one of the dynamic reconfigure parameter is changed.
00189   // Updates the publisher configuration consequently.
00190   void DynamicReconfigureCallback(PublisherConfig &config, uint32_t level);
00191   // ROS service callback to get the user readable name from a given UUID.
00192   bool GetMapNameServiceCallback(const tango_ros_messages::GetMapName::Request& req,
00193                                  tango_ros_messages::GetMapName::Response& res);
00194   // ROS service callback to get a list of available ADF UUIDs and corresponding
00195   // user readable map names.
00196   bool GetMapUuidsServiceCallback(const tango_ros_messages::GetMapUuids::Request& req,
00197                                   tango_ros_messages::GetMapUuids::Response& res);
00198   // Function called when the LoadOccupancyGrid service is called.
00199   // Load the requested occupancy grid and publish it.
00200   bool LoadOccupancyGridServiceCallback(const tango_ros_messages::LoadOccupancyGrid::Request& req,
00201                              tango_ros_messages::LoadOccupancyGrid::Response& res);
00202   // Function called when the SaveMap service is called.
00203   // Save the current map (ADF) to disc with the given name.
00204   bool SaveMapServiceCallback(const tango_ros_messages::SaveMap::Request& req,
00205                tango_ros_messages::SaveMap::Response& res);
00206   // ROS service callback to connect or disconnect from Tango Service.
00207   bool TangoConnectServiceCallback(
00208           const tango_ros_messages::TangoConnect::Request& request,
00209           tango_ros_messages::TangoConnect::Response& response);
00210 
00211   TangoConfig tango_config_;
00212   ros::NodeHandle node_handle_;
00213 
00214   PublishThread device_pose_thread_;
00215   PublishThread point_cloud_thread_;
00216   PublishThread laser_scan_thread_;
00217   PublishThread fisheye_image_thread_;
00218   PublishThread color_image_thread_;
00219   PublishThread mesh_thread_;
00220   std::thread ros_spin_thread_;
00221   std::atomic_bool run_threads_;
00222   std::atomic_bool new_point_cloud_available_for_t3dr_;
00223 
00224   ros::Publisher tango_status_publisher_;
00225   TangoStatus tango_status_;
00226 
00227   bool tango_data_available_ = true;
00228   double time_offset_ = 0.; // Offset between tango time and ros time in s.
00229   bool publish_pose_on_tf_ = true;
00230   bool use_tf_static_ = true;
00231   bool enable_depth_ = true;
00232   bool enable_color_camera_ = true;
00233   bool enable_3dr_mesh_ = true;
00234   bool enable_3dr_occupancy_grid_ = true;
00235   bool fisheye_camera_available_ = true;
00236   std::string start_of_service_frame_id_;
00237   std::string area_description_frame_id_;
00238 
00239   tf::TransformBroadcaster tf_broadcaster_;
00240   ros::Publisher start_of_service_T_device_publisher_;
00241   ros::Publisher area_description_T_start_of_service_publisher_;
00242   geometry_msgs::TransformStamped start_of_service_T_device_;
00243   geometry_msgs::TransformStamped area_description_T_start_of_service_;
00244   tf2_ros::StaticTransformBroadcaster tf_static_broadcaster_;
00245   geometry_msgs::TransformStamped device_T_camera_depth_;
00246   tf::StampedTransform camera_depth_T_laser_;
00247   geometry_msgs::TransformStamped device_T_camera_fisheye_;
00248   geometry_msgs::TransformStamped device_T_camera_color_;
00249 
00250   ros::Publisher point_cloud_publisher_;
00251   sensor_msgs::PointCloud2 point_cloud_;
00252 
00253   ros::Publisher laser_scan_publisher_;
00254   sensor_msgs::LaserScan laser_scan_;
00255   double laser_scan_max_height_ = 1.0;
00256   double laser_scan_min_height_ = -1.0;
00257   double laser_scan_max_range_ = 4.0;
00258   double laser_scan_min_range_ = 0.3;
00259 
00260   std::shared_ptr<image_transport::ImageTransport> image_transport_;
00261   image_transport::CameraPublisher fisheye_camera_publisher_;
00262   std_msgs::Header fisheye_image_header_;
00263   sensor_msgs::CameraInfo fisheye_camera_info_;
00264   std::shared_ptr<camera_info_manager::CameraInfoManager> fisheye_camera_info_manager_;
00265   image_transport::Publisher fisheye_rectified_image_publisher_;
00266   cv::Mat fisheye_image_;
00267   cv::Mat cv_warp_map_x_;
00268   cv::Mat cv_warp_map_y_;
00269   cv::Mat fisheye_image_rect_;
00270 
00271   image_transport::CameraPublisher color_camera_publisher_;
00272   std_msgs::Header color_image_header_;
00273   sensor_msgs::CameraInfo color_camera_info_;
00274   std::shared_ptr<camera_info_manager::CameraInfoManager> color_camera_info_manager_;
00275   image_transport::Publisher color_rectified_image_publisher_;
00276   cv::Mat color_image_;
00277   image_geometry::PinholeCameraModel color_camera_model_;
00278   cv::Mat color_image_rect_;
00279 
00280   ros::Publisher mesh_marker_publisher_;
00281   ros::Publisher occupancy_grid_publisher_;
00282   nav_msgs::OccupancyGrid occupancy_grid_;
00283   // Context for a 3D Reconstruction. Maintains the state of a single
00284   // mesh being reconstructed.
00285   Tango3DR_ReconstructionContext t3dr_context_;
00286   TangoSupport_PointCloudManager* point_cloud_manager_;
00287   Tango3DR_Pose last_camera_depth_pose_;
00288   TangoSupport_ImageBufferManager* image_buffer_manager_;
00289   Tango3DR_Pose last_camera_color_pose_;
00290   Tango3DR_CameraCalibration t3dr_color_camera_intrinsics_;
00291   double t3dr_resolution_;
00292   uint8_t t3dr_occupancy_grid_threshold_;
00293 
00294   ros::Publisher static_occupancy_grid_publisher_;
00295 
00296   ros::ServiceServer get_map_name_service_;
00297   ros::ServiceServer get_map_uuids_service_;
00298   ros::ServiceServer save_map_service_;
00299   ros::ServiceServer load_occupancy_grid_service_;
00300   ros::ServiceServer tango_connect_service_;
00301 };
00302 }  // namespace tango_ros_native
00303 #endif  // TANGO_ROS_NODELET_H_


tango_ros_native
Author(s):
autogenerated on Thu Jun 6 2019 19:49:54