@@ -26,6 +26,8 @@ using namespace std;
2626ros::Publisher pcl_pub;
2727ros::Publisher bbox_pub;
2828ros::Publisher template_pub;
29+ ros::Publisher pose_pub;
30+
2931Eigen::Matrix4d icp_transform;
3032string template_cuboid_filename;
3133sensor_msgs::PointCloud2 bbox_msg;
@@ -49,6 +51,41 @@ tf::Transform get_tf_from_stamped_tf(tf::StampedTransform sTf)
4951 return tf;
5052}
5153
54+ void publish_pose (Eigen::Matrix4d H)
55+ {
56+ // Set translation
57+ tf::Vector3 origin;
58+ origin.setValue (H (0 , 3 ), H (1 , 3 ), H (2 , 3 ));
59+
60+ // Set rotation
61+ tf::Quaternion quaternion;
62+ tf::Matrix3x3 rotation_matrix;
63+ rotation_matrix.setValue (H (0 , 0 ), H (0 , 1 ), H (0 , 2 ),
64+ H (1 , 0 ), H (1 , 1 ), H (1 , 2 ),
65+ H (2 , 0 ), H (2 , 1 ), H (2 , 2 ));
66+ rotation_matrix.getRotation (quaternion);
67+
68+ // Make tf transform message
69+ tf::Transform transform;
70+ transform.setOrigin (origin);
71+ transform.setRotation (quaternion);
72+
73+ // Make pose message
74+ geometry_msgs::Pose p;
75+ p.position .x = H (0 , 3 );
76+ p.position .y = H (1 , 3 );
77+ p.position .z = H (2 , 3 );
78+ p.orientation .x = quaternion.x ();
79+ p.orientation .y = quaternion.y ();
80+ p.orientation .z = quaternion.z ();
81+ p.orientation .w = quaternion.w ();
82+
83+ // Broadcast the transforms
84+ static tf::TransformBroadcaster br;
85+ br.sendTransform (tf::StampedTransform (transform, ros::Time::now (), " camera_depth_optical_frame" , " icp_cuboid_frame" ));
86+ pose_pub.publish (p);
87+ }
88+
5289void publish_bounding_box (Eigen::Matrix4d H)
5390{
5491 // Extract cuboid dimensions
@@ -98,14 +135,15 @@ void pose_callback(const geometry_msgs::Pose::ConstPtr& msg)
98135void icp_callback (const sensor_msgs::PointCloud2::ConstPtr& msg)
99136{
100137 // Compute ICP only once
101- // if (ICP_SUCCESS)
102- // {
103- // pcl_pub.publish(output_msg);
104- // template_msg.header.frame_id = "camera_depth_optical_frame";
105- // template_pub.publish(template_msg);
106- // publish_bounding_box(icp_transform);
107- // return;
108- // }
138+ if (ICP_SUCCESS)
139+ {
140+ pcl_pub.publish (output_msg);
141+ template_msg.header .frame_id = " camera_depth_optical_frame" ;
142+ template_pub.publish (template_msg);
143+ publish_bounding_box (icp_transform);
144+ publish_pose (icp_transform);
145+ return ;
146+ }
109147
110148 // Point cloud containers
111149 pcl::PointCloud<pcl::PointXYZ>::Ptr template_input (new pcl::PointCloud<pcl::PointXYZ>);
@@ -117,15 +155,15 @@ void icp_callback(const sensor_msgs::PointCloud2::ConstPtr& msg)
117155 pcl::fromROSMsg (*msg, *input_cuboid);
118156
119157 // Read template point cloud
120- if (pcl::io::loadPCDFile<pcl::PointXYZ>(template_cuboid_filename, *template_input ) == -1 )
158+ if (pcl::io::loadPCDFile<pcl::PointXYZ>(template_cuboid_filename, *template_cuboid ) == -1 )
121159 {
122160 PCL_ERROR (" Couldn't read the template PCL file" );
123161 return ;
124162 }
125163
126164 // Transform the template point cloud to the estimated frame
127- if (!POSE_FLAG) return ;
128- pcl::transformPointCloud (*template_input, *template_cuboid, pose_transform.cast <float >());
165+ // if (!POSE_FLAG) return;
166+ // pcl::transformPointCloud(*template_input, *template_cuboid, pose_transform.cast<float>());
129167
130168 // Run ICP
131169 pcl::IterativeClosestPoint<pcl::PointXYZ, pcl::PointXYZ> icp;
@@ -143,22 +181,23 @@ void icp_callback(const sensor_msgs::PointCloud2::ConstPtr& msg)
143181 if (icp.hasConverged ())
144182 {
145183 // Display ICP results
146- ICP_SUCCESS = true ;
147184 if (DEBUG || !ICP_SUCCESS)
148185 {
149186 cerr << " \n ICP Score: " << icp.getFitnessScore () << endl;
150187 cerr << " ICP Transform:\n " << icp_transform << endl;
151188 }
189+ ICP_SUCCESS = true ;
190+
191+ // Convert to ROS data type
192+ pcl::toROSMsg (*output_cloud, output_msg);
193+ pcl::toROSMsg (*template_cuboid, template_msg);
152194
153195 // Publish template point cloud
154196 template_msg.header .frame_id = " camera_depth_optical_frame" ;
155197 pcl_pub.publish (output_msg);
156198 template_pub.publish (template_msg);
157199 publish_bounding_box (icp_transform);
158-
159- // Convert to ROS data type
160- pcl::toROSMsg (*output_cloud, output_msg);
161- pcl::toROSMsg (*template_cuboid, template_msg);
200+ publish_pose (icp_transform);
162201 }
163202}
164203
@@ -188,6 +227,7 @@ int main(int argc, char **argv)
188227 pcl_pub = nh.advertise <sensor_msgs::PointCloud2>(" /icp/aligned_points" , 1 );
189228 bbox_pub = nh.advertise <sensor_msgs::PointCloud2>(" /icp/bbox_points" , 1 );
190229 template_pub = nh.advertise <sensor_msgs::PointCloud2>(" /icp/template" , 1 );
230+ pose_pub = nh.advertise <geometry_msgs::Pose>(" /icp/pose" , 1 );
191231
192232 // Spin
193233 ros::spin ();
0 commit comments