tango_ros_nodelet.h
Go to the documentation of this file.
1 // Copyright 2016 Intermodalics All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #ifndef TANGO_ROS_NODELET_H_
15 #define TANGO_ROS_NODELET_H_
16 #include <atomic>
17 #include <chrono>
18 #include <condition_variable>
19 #include <mutex>
20 #include <string>
21 #include <thread>
22 #include <time.h>
23 
27 
28 #include <opencv2/core/core.hpp>
29 
31 #include <cv_bridge/cv_bridge.h>
32 #include <geometry_msgs/TransformStamped.h>
35 #include <nav_msgs/OccupancyGrid.h>
36 #include <nodelet/nodelet.h>
37 #include <ros/ros.h>
38 #include <ros/node_handle.h>
39 #include <sensor_msgs/CameraInfo.h>
40 #include <sensor_msgs/LaserScan.h>
41 #include <sensor_msgs/PointCloud2.h>
42 #include <tango_ros_messages/GetMapName.h>
43 #include <tango_ros_messages/GetMapUuids.h>
44 #include <tango_ros_messages/LoadOccupancyGrid.h>
45 #include <tango_ros_messages/SaveMap.h>
46 #include <tango_ros_messages/TangoConnect.h>
49 #include <visualization_msgs/MarkerArray.h>
50 
51 #include "tango_ros_native/PublisherConfig.h"
52 
53 namespace tango_ros_native {
54 // See laser scan message doc for definition of laser scan constants:
55 // http://docs.ros.org/api/sensor_msgs/html/msg/LaserScan.html
56 const float LASER_SCAN_ANGLE_MIN = 0.;
57 const float LASER_SCAN_ANGLE_MAX = 3.1415;
58 const float LASER_SCAN_ANGLE_INCREMENT = 3.1415 / 360;
59 const float LASER_SCAN_TIME_INCREMENT = 0.0;
60 const float LASER_SCAN_SCAN_TIME= 0.3333;
61 const std::string LASER_SCAN_FRAME_ID = "laser";
62 
63 const std::string DATASET_DEFAULT_DIRECTORY = "/sdcard/tango_ros_streamer/datasets/";
64 const std::string OCCUPANCY_GRID_DEFAULT_DIRECTORY = "/sdcard/tango_ros_streamer/occupancy_grids/";
66 
67 const std::string TANGO_STATUS_TOPIC_NAME = "status";
68 const std::string POINT_CLOUD_TOPIC_NAME = "point_cloud";
69 const std::string LASER_SCAN_TOPIC_NAME = "laser_scan";
70 const std::string FISHEYE_IMAGE_TOPIC_NAME = "camera/fisheye_1/image_raw";
71 const std::string FISHEYE_RECTIFIED_IMAGE_TOPIC_NAME = "camera/fisheye_1/image_rect";
72 const std::string COLOR_IMAGE_TOPIC_NAME = "camera/color_1/image_raw";
73 const std::string COLOR_RECTIFIED_IMAGE_TOPIC_NAME = "camera/color_1/image_rect";
74 const std::string COLOR_MESH_TOPIC_NAME = "reconstruction/mesh";
75 const std::string OCCUPANCY_GRID_TOPIC_NAME = "reconstruction/occupancy_grid";
76 const std::string STATIC_OCCUPANCY_GRID_TOPIC_NAME = "static_occupancy_grid";
77 const std::string START_OF_SERVICE_T_DEVICE_TOPIC_NAME = "transform/start_of_service_T_device";
78 const std::string AREA_DESCRIPTION_T_START_OF_SERVICE_TOPIC_NAME = "transform/area_description_T_start_of_service";
79 
80 const std::string CREATE_NEW_MAP_PARAM_NAME = "create_new_map";
81 const std::string LOCALIZATION_MODE_PARAM_NAME = "localization_mode";
82 const std::string LOCALIZATION_MAP_UUID_PARAM_NAME = "localization_map_uuid";
83 const std::string DATASET_DIRECTORY_PARAM_NAME = "dataset_directory";
84 const std::string DATASET_UUID_PARAM_NAME = "dataset_uuid";
85 const std::string ENABLE_DEPTH_PARAM_NAME = "enable_depth";
86 const std::string ENABLE_COLOR_CAMERA_PARAM_NAME = "enable_color_camera";
87 const std::string PUBLISH_POSE_ON_TF_PARAM_NAME = "publish_pose_on_tf";
88 const std::string ENABLE_3DR_MESH_PARAM_NAME = "enable_3dr_mesh";
89 const std::string ENABLE_3DR_OCCUPANCY_GRID_PARAM_NAME = "enable_3dr_occupancy_grid";
90 const std::string USE_TF_STATIC_PARAM_NAME = "use_tf_static";
91 const std::string START_OF_SERVICE_FRAME_ID_PARAM_NAME = "start_of_service_frame_id";
92 const std::string AREA_DESCRIPTION_FRAME_ID_PARAM_NAME = "area_description_frame_id";
93 const std::string OCCUPANCY_GRID_DIRECTORY_PARAM_NAME = "occupancy_grid_directory";
94 
95 const std::string GET_MAP_NAME_SERVICE_NAME = "get_map_name";
96 const std::string GET_MAP_UUIDS_SERVICE_NAME = "get_map_uuids";
97 const std::string SAVE_MAP_SERVICE_NAME = "save_map";
98 const std::string LOAD_OCCUPANCY_GRID_SERVICE_NAME = "load_occupancy_grid";
99 const std::string CONNECT_SERVICE_NAME = "connect";
100 
101 // Localization mode values.
102 // See http://developers.google.com/tango/overview/area-learning to know more
103 // about Tango localization
105  // No map required. Internally runs VIO (Visual Inertial Odometry) from Tango.
106  ODOMETRY = 1,
107  // No map required. Internally runs COM (Concurrent Odometry and Mapping,
108  // also mentioned as drift correction) from Tango.
110  // Map required. Internally runs Tango localization on ADF (Area Description File).
112 };
113 
114 enum class TangoStatus {
115  UNKNOWN = 0,
119 };
120 
122  std::thread publish_thread;
124  std::condition_variable data_available;
125 };
126 
127 // Node collecting tango data and publishing it on ros topics.
129  public:
130  TangoRosNodelet();
131  ~TangoRosNodelet();
132  // Initialization function called when plugin is loaded.
133  void onInit();
134  // Gets the full list of map Uuids (Universally Unique IDentifier)
135  // available on the device.
136  // @param uuid_list to fill in
137  // @return false if list could not be requested.
138  bool GetAvailableMapUuidsList(std::vector<std::string>& uuid_list);
139  // Gets the map name corresponding to a given map uuid.
140  // @param map_uuid UUID to lookup
141  // @param map_name to fill in
142  // @return false if the uuid was not found
143  bool GetMapNameFromUuid(const std::string& map_uuid, std::string& map_name);
144 
145  // Tries to get a first valid pose and sets the camera intrinsics.
146  // @return returns success if it ended successfully.
147  TangoErrorType OnTangoServiceConnected();
148  // Disconnects from the tango service.
149  void TangoDisconnect();
150  // Starts the threads that publish data.
151  void StartPublishing();
152  // Stops the threads that publish data.
153  // Will not return until all the internal threads have exited.
154  void StopPublishing();
155 
156  // Function called when a new device pose is available.
157  void OnPoseAvailable(const TangoPoseData* pose);
158  // Function called when a new point cloud is available.
159  void OnPointCloudAvailable(const TangoPointCloud* point_cloud);
160  // Function called when a new camera image is available.
161  void OnFrameAvailable(TangoCameraId camera_id, const TangoImageBuffer* buffer);
162 
163  private:
164  // Sets the tango config to be able to collect all necessary data from tango.
165  // @return returns TANGO_SUCCESS if the config was set successfully.
166  TangoErrorType TangoSetupConfig();
167  // Connects to the tango service and to the necessary callbacks.
168  // @return returns TANGO_SUCCESS if connecting to tango ended successfully
169  // or if service was already connected.
170  TangoErrorType TangoConnect();
171  // Sets up config, connect to Tango service, starts publishing threads and
172  // publishes first static transforms.
173  TangoErrorType ConnectToTangoAndSetUpNode();
174  // Helper function to update and publish the Tango status. Publishes always
175  // for convenience, even if Tango status did not change.
176  void UpdateAndPublishTangoStatus(const TangoStatus& status);
177  // Publishes the necessary static transforms (device_T_camera_*).
178  void PublishStaticTransforms();
179  // Publishes the available data (device pose, point cloud, images, ...).
180  void PublishDevicePose();
181  void PublishPointCloud();
182  void PublishLaserScan();
183  void PublishFisheyeImage();
184  void PublishColorImage();
185  void PublishMesh();
186  // Runs ros::spinOnce() in a loop to trigger subscribers callbacks (e.g. dynamic reconfigure).
187  void RunRosSpin();
188  // Function called when one of the dynamic reconfigure parameter is changed.
189  // Updates the publisher configuration consequently.
190  void DynamicReconfigureCallback(PublisherConfig &config, uint32_t level);
191  // ROS service callback to get the user readable name from a given UUID.
192  bool GetMapNameServiceCallback(const tango_ros_messages::GetMapName::Request& req,
193  tango_ros_messages::GetMapName::Response& res);
194  // ROS service callback to get a list of available ADF UUIDs and corresponding
195  // user readable map names.
196  bool GetMapUuidsServiceCallback(const tango_ros_messages::GetMapUuids::Request& req,
197  tango_ros_messages::GetMapUuids::Response& res);
198  // Function called when the LoadOccupancyGrid service is called.
199  // Load the requested occupancy grid and publish it.
200  bool LoadOccupancyGridServiceCallback(const tango_ros_messages::LoadOccupancyGrid::Request& req,
201  tango_ros_messages::LoadOccupancyGrid::Response& res);
202  // Function called when the SaveMap service is called.
203  // Save the current map (ADF) to disc with the given name.
204  bool SaveMapServiceCallback(const tango_ros_messages::SaveMap::Request& req,
205  tango_ros_messages::SaveMap::Response& res);
206  // ROS service callback to connect or disconnect from Tango Service.
207  bool TangoConnectServiceCallback(
208  const tango_ros_messages::TangoConnect::Request& request,
209  tango_ros_messages::TangoConnect::Response& response);
210 
213 
220  std::thread ros_spin_thread_;
221  std::atomic_bool run_threads_;
223 
226 
227  bool tango_data_available_ = true;
228  double time_offset_ = 0.; // Offset between tango time and ros time in s.
229  bool publish_pose_on_tf_ = true;
230  bool use_tf_static_ = true;
231  bool enable_depth_ = true;
232  bool enable_color_camera_ = true;
233  bool enable_3dr_mesh_ = true;
234  bool enable_3dr_occupancy_grid_ = true;
235  bool fisheye_camera_available_ = true;
238 
242  geometry_msgs::TransformStamped start_of_service_T_device_;
243  geometry_msgs::TransformStamped area_description_T_start_of_service_;
245  geometry_msgs::TransformStamped device_T_camera_depth_;
247  geometry_msgs::TransformStamped device_T_camera_fisheye_;
248  geometry_msgs::TransformStamped device_T_camera_color_;
249 
251  sensor_msgs::PointCloud2 point_cloud_;
252 
254  sensor_msgs::LaserScan laser_scan_;
255  double laser_scan_max_height_ = 1.0;
256  double laser_scan_min_height_ = -1.0;
257  double laser_scan_max_range_ = 4.0;
258  double laser_scan_min_range_ = 0.3;
259 
260  std::shared_ptr<image_transport::ImageTransport> image_transport_;
262  std_msgs::Header fisheye_image_header_;
263  sensor_msgs::CameraInfo fisheye_camera_info_;
264  std::shared_ptr<camera_info_manager::CameraInfoManager> fisheye_camera_info_manager_;
266  cv::Mat fisheye_image_;
267  cv::Mat cv_warp_map_x_;
268  cv::Mat cv_warp_map_y_;
270 
272  std_msgs::Header color_image_header_;
273  sensor_msgs::CameraInfo color_camera_info_;
274  std::shared_ptr<camera_info_manager::CameraInfoManager> color_camera_info_manager_;
276  cv::Mat color_image_;
279 
282  nav_msgs::OccupancyGrid occupancy_grid_;
283  // Context for a 3D Reconstruction. Maintains the state of a single
284  // mesh being reconstructed.
286  TangoSupport_PointCloudManager* point_cloud_manager_;
288  TangoSupport_ImageBufferManager* image_buffer_manager_;
293 
295 
301 };
302 } // namespace tango_ros_native
303 #endif // TANGO_ROS_NODELET_H_
const std::string GET_MAP_UUIDS_SERVICE_NAME
TangoSupport_ImageBufferManager * image_buffer_manager_
const float LASER_SCAN_ANGLE_MIN
ros::Publisher area_description_T_start_of_service_publisher_
std::shared_ptr< image_transport::ImageTransport > image_transport_
image_transport::Publisher fisheye_rectified_image_publisher_
image_transport::CameraPublisher fisheye_camera_publisher_
const std::string DATASET_DIRECTORY_PARAM_NAME
const std::string AREA_DESCRIPTION_FRAME_ID_PARAM_NAME
TangoErrorType
const std::string ENABLE_DEPTH_PARAM_NAME
const std::string OCCUPANCY_GRID_DIRECTORY_PARAM_NAME
const std::string LASER_SCAN_FRAME_ID
const std::string LOCALIZATION_MAP_UUID_PARAM_NAME
const std::string PUBLISH_POSE_ON_TF_PARAM_NAME
TangoSupport_PointCloudManager * point_cloud_manager_
const std::string ENABLE_COLOR_CAMERA_PARAM_NAME
image_transport::Publisher color_rectified_image_publisher_
const std::string COLOR_MESH_TOPIC_NAME
const std::string CONNECT_SERVICE_NAME
const std::string STATIC_OCCUPANCY_GRID_TOPIC_NAME
sensor_msgs::PointCloud2 point_cloud_
std::shared_ptr< camera_info_manager::CameraInfoManager > color_camera_info_manager_
const std::string SAVE_MAP_SERVICE_NAME
const std::string COLOR_IMAGE_TOPIC_NAME
nav_msgs::OccupancyGrid occupancy_grid_
TangoCameraId
sensor_msgs::CameraInfo color_camera_info_
const std::string CREATE_NEW_MAP_PARAM_NAME
const std::string DATASET_DEFAULT_DIRECTORY
geometry_msgs::TransformStamped area_description_T_start_of_service_
const std::string USE_TF_STATIC_PARAM_NAME
Tango3DR_CameraCalibration t3dr_color_camera_intrinsics_
geometry_msgs::TransformStamped device_T_camera_depth_
image_transport::CameraPublisher color_camera_publisher_
const float LASER_SCAN_TIME_INCREMENT
const std::string TANGO_STATUS_TOPIC_NAME
std::atomic_bool new_point_cloud_available_for_t3dr_
const std::string AREA_DESCRIPTION_T_START_OF_SERVICE_TOPIC_NAME
sensor_msgs::CameraInfo fisheye_camera_info_
const std::string START_OF_SERVICE_T_DEVICE_TOPIC_NAME
const std::string GET_MAP_NAME_SERVICE_NAME
const float LASER_SCAN_ANGLE_MAX
geometry_msgs::TransformStamped device_T_camera_fisheye_
const std::string LASER_SCAN_TOPIC_NAME
sensor_msgs::LaserScan laser_scan_
const std::string DATASET_UUID_PARAM_NAME
const std::string FISHEYE_RECTIFIED_IMAGE_TOPIC_NAME
const std::string START_OF_SERVICE_FRAME_ID_PARAM_NAME
const std::string LOCALIZATION_MODE_PARAM_NAME
const std::string ENABLE_3DR_MESH_PARAM_NAME
const std::string POINT_CLOUD_TOPIC_NAME
image_geometry::PinholeCameraModel color_camera_model_
const std::string OCCUPANCY_GRID_DEFAULT_DIRECTORY
Tango3DR_ReconstructionContext t3dr_context_
std::condition_variable data_available
ros::ServiceServer load_occupancy_grid_service_
ros::Publisher start_of_service_T_device_publisher_
geometry_msgs::TransformStamped device_T_camera_color_
const float LASER_SCAN_SCAN_TIME
struct _Tango3DR_ReconstructionContext * Tango3DR_ReconstructionContext
const std::string FISHEYE_IMAGE_TOPIC_NAME
tf::TransformBroadcaster tf_broadcaster_
std::shared_ptr< camera_info_manager::CameraInfoManager > fisheye_camera_info_manager_
const std::string COLOR_RECTIFIED_IMAGE_TOPIC_NAME
const std::string ENABLE_3DR_OCCUPANCY_GRID_PARAM_NAME
const float LASER_SCAN_ANGLE_INCREMENT
tf::StampedTransform camera_depth_T_laser_
void * TangoConfig
geometry_msgs::TransformStamped start_of_service_T_device_
const int NUMBER_OF_STATIC_TRANSFORMS
const std::string LOAD_OCCUPANCY_GRID_SERVICE_NAME
const std::string OCCUPANCY_GRID_TOPIC_NAME
tf2_ros::StaticTransformBroadcaster tf_static_broadcaster_


tango_ros_native
Author(s):
autogenerated on Mon Jun 10 2019 15:37:51