@@ -13,8 +13,10 @@ DepthImageProjectorNode::DepthImageProjectorNode(const ros::NodeHandle& nh, cons
1313 colorSubscriber_ = it_.subscribeCamera (" color_in" , 1 , &DepthImageProjectorNode::colorCallback, this );
1414 depthSubscriber_ = it_.subscribeCamera (" depth_in" , 1 , &DepthImageProjectorNode::depthCallback, this );
1515
16- // TODO change to use TF and remove dependency on realsense2_camera package
17- depthToColorSubscriber_ = nh_.subscribe <realsense2_camera::Extrinsics>(" depth_to_color" , 1 , &DepthImageProjectorNode::depthToColorCallback, this );
16+ // frame ids for tf which associates color frame with depth frame
17+ nh_.param <std::string>(" color_frame_id" , color_frame_id_, " d435_color_optical" );
18+ nh_.param <std::string>(" depth_frame_id" , depth_frame_id_, " d435_depth_optical" );
19+ nh_.param <double >(" tf_wait_duration" , tf_wait_duration_, 0.1 );
1820
1921 int colorWidth, colorHeight;
2022 nh_.param <int >(" colorWidth" , colorWidth , 640 );
@@ -92,7 +94,9 @@ void DepthImageProjectorNode::depthCallback(const sensor_msgs::Image::ConstPtr&
9294 return ;
9395 }
9496
95- if (!depthToColorArrived_)
97+ // Update & check whether depthToColor is valid.
98+ bool is_conversion_valid = updateDepthToColor ();
99+ if (!is_conversion_valid)
96100 {
97101 ROS_WARN_STREAM (" extrinsics parameter not arrivied yet" );
98102 return ;
@@ -122,21 +126,42 @@ void DepthImageProjectorNode::depthCallback(const sensor_msgs::Image::ConstPtr&
122126 imagePublisher_.publish (outImage.toImageMsg ());
123127}
124128
125- void DepthImageProjectorNode::depthToColorCallback ( const realsense2_camera::Extrinsics::ConstPtr& depthToColorMsg )
129+ bool DepthImageProjectorNode::updateDepthToColor ( )
126130{
127- auto & r = depthToColorMsg->rotation ;
128- auto & t = depthToColorMsg->translation ;
129- latestDepthToColor_ = {
130- static_cast <float >(r[0 ]), static_cast <float >(r[1 ]), static_cast <float >(r[2 ]), 0 ,
131- static_cast <float >(r[3 ]), static_cast <float >(r[4 ]), static_cast <float >(r[5 ]), 0 ,
132- static_cast <float >(r[6 ]), static_cast <float >(r[7 ]), static_cast <float >(r[8 ]), 0 ,
133- static_cast <float >(t[0 ]), static_cast <float >(t[1 ]), static_cast <float >(t[2 ]), 1 ,
134- }; // column major order
135-
136- depthToColorArrived_ = true ;
131+ // Calculation is performed only once since depthToColor does not change.
132+ if (depthToColorArrived_)
133+ {
134+ return true ;
135+ }
136+
137+ try
138+ {
139+ if (tfListener_.waitForTransform (color_frame_id_, depth_frame_id_, ros::Time (0 ), ros::Duration (tf_wait_duration_)))
140+ {
141+ tf::StampedTransform transform;
142+ tfListener_.lookupTransform (color_frame_id_, depth_frame_id_, ros::Time (0 ), transform);
143+
144+ const tf::Matrix3x3& R = transform.getBasis ();
145+ const tf::Vector3& t = transform.getOrigin ();
146+
147+ latestDepthToColor_ = {
148+ static_cast <float >(R[0 ][0 ]), static_cast <float >(R[1 ][0 ]), static_cast <float >(R[2 ][0 ]), 0.0 ,
149+ static_cast <float >(R[0 ][1 ]), static_cast <float >(R[1 ][1 ]), static_cast <float >(R[2 ][1 ]), 0.0 ,
150+ static_cast <float >(R[0 ][2 ]), static_cast <float >(R[1 ][2 ]), static_cast <float >(R[2 ][2 ]), 0.0 ,
151+ static_cast <float >(t[0 ]), static_cast <float >(t[1 ]), static_cast <float >(t[2 ]), 1.0 ,
152+ }; // column major order
153+ depthToColorArrived_ = true ;
154+ }
155+ }
156+ catch (tf::TransformException& ex)
157+ {
158+ ROS_WARN (" Transform Exception in updateDepthToColor(). %s" , ex.what ());
159+ }
160+
161+ return depthToColorArrived_;
137162}
138163
139164void DepthImageProjectorNode::run ()
140165{
141166 ros::spin ();
142- }
167+ }
0 commit comments