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
00032
00033
00034
00035
00036
00037
00046
00047 #include <ros/ros.h>
00048 #include <pcl/io/io.h>
00049 #include <pcl/io/pcd_io.h>
00050 #include <pcl/point_types.h>
00051
00052 #include "pcl_ros/publisher.h"
00053
00054 using namespace std;
00055
00056 class PCDGenerator
00057 {
00058 protected:
00059 string tf_frame_;
00060 ros::NodeHandle nh_;
00061 ros::NodeHandle private_nh_;
00062 public:
00063
00064
00065 sensor_msgs::PointCloud2 cloud_;
00066
00067 string file_name_, cloud_topic_;
00068 double wait_;
00069
00070 pcl_ros::Publisher<sensor_msgs::PointCloud2> pub_;
00071
00073 PCDGenerator () : tf_frame_ ("/base_link"), private_nh_("~")
00074 {
00075
00076
00077 cloud_topic_ = "cloud_pcd";
00078 pub_.advertise (nh_, cloud_topic_.c_str (), 1);
00079 private_nh_.param("frame_id", tf_frame_, std::string("/base_link"));
00080 ROS_INFO ("Publishing data on topic %s with frame_id %s.", nh_.resolveName (cloud_topic_).c_str (), tf_frame_.c_str());
00081 }
00082
00084
00085 int
00086 start ()
00087 {
00088 if (file_name_ == "" || pcl::io::loadPCDFile (file_name_, cloud_) == -1)
00089 return (-1);
00090 cloud_.header.frame_id = tf_frame_;
00091 return (0);
00092 }
00093
00095
00096 bool spin ()
00097 {
00098 int nr_points = cloud_.width * cloud_.height;
00099 string fields_list = pcl::getFieldsList (cloud_);
00100 double interval = wait_ * 1e+6;
00101 while (nh_.ok ())
00102 {
00103 ROS_DEBUG_ONCE ("Publishing data with %d points (%s) on topic %s in frame %s.", nr_points, fields_list.c_str (), nh_.resolveName (cloud_topic_).c_str (), cloud_.header.frame_id.c_str ());
00104 cloud_.header.stamp = ros::Time::now ();
00105
00106 if (pub_.getNumSubscribers () > 0)
00107 {
00108 ROS_DEBUG ("Publishing data to %d subscribers.", pub_.getNumSubscribers ());
00109 pub_.publish (cloud_);
00110 }
00111 else
00112 {
00113
00114 ros::Duration (1).sleep ();
00115 continue;
00116 }
00117
00118 usleep (interval);
00119
00120 if (interval == 0)
00121 {
00122
00123 ros::Duration (3.0).sleep ();
00124 break;
00125 }
00126 }
00127 return (true);
00128 }
00129
00130
00131 };
00132
00133
00134 int
00135 main (int argc, char** argv)
00136 {
00137 if (argc < 2)
00138 {
00139 std::cerr << "Syntax is: " << argv[0] << " <file.pcd> [publishing_interval (in seconds)]" << std::endl;
00140 return (-1);
00141 }
00142
00143 ros::init (argc, argv, "pcd_to_pointcloud");
00144
00145 PCDGenerator c;
00146 c.file_name_ = string (argv[1]);
00147
00148 if (argc == 2)
00149 {
00150 c.wait_ = 0;
00151 }
00152 else
00153 {
00154 c.wait_ = atof (argv[2]);
00155 }
00156
00157 if (c.start () == -1)
00158 {
00159 ROS_ERROR ("Could not load file %s. Exiting.", argv[1]);
00160 return (-1);
00161 }
00162 ROS_INFO ("Loaded a point cloud with %d points (total size is %zu) and the following channels: %s.", c.cloud_.width * c.cloud_.height, c.cloud_.data.size (), pcl::getFieldsList (c.cloud_).c_str ());
00163 c.spin ();
00164
00165 return (0);
00166 }
00167