Go to the documentation of this file.00001
00002 #include "point_cloud_ros/occupancy_grid.h"
00003 #include "point_cloud_ros/OccupancyGrid.h"
00004 #include <ros/console.h>
00005 #include "pcl/io/pcd_io.h"
00006 #include "sensor_msgs/point_cloud_conversion.h"
00007
00008 int main(int argc, char *argv[])
00009 {
00010 ros::init(argc, argv, "occupancy_grid_node");
00011 ros::NodeHandle n;
00012
00013 ros::Publisher og_pub = n.advertise<point_cloud_ros::OccupancyGrid>("occupancy_grid", 1);
00014 ros::Publisher pc_pub = n.advertise<sensor_msgs::PointCloud2>("test_point_cloud", 1);
00015
00016 sensor_msgs::PointCloud2 cloud_blob;
00017 sensor_msgs::PointCloud cloud;
00018
00019 if (pcl::io::loadPCDFile (argv[1], cloud_blob) == -1)
00020 {
00021 ROS_ERROR ("Couldn't read file %s", argv[1]);
00022 return (-1);
00023 }
00024 ROS_INFO ("Loaded %d data points from test_pcd.pcd with the following fields: %s", (int)(cloud_blob.width * cloud_blob.height), pcl::getFieldsList (cloud_blob).c_str ());
00025
00026 cloud_blob.header.frame_id = "base_link";
00027 cloud_blob.header.stamp = ros::Time::now();
00028 sensor_msgs::convertPointCloud2ToPointCloud(cloud_blob, cloud);
00029
00030 ROS_INFO("Computing the centroid of the point cloud");
00031 float cx = 0;
00032 float cy = 0;
00033 float cz = 0;
00034 for (size_t i = 0; i < cloud.points.size(); i++)
00035 {
00036 cx += cloud.points[i].x;
00037 cy += cloud.points[i].y;
00038 cz += cloud.points[i].z;
00039 }
00040
00041 cx = cx / cloud.points.size();
00042 cy = cy / cloud.points.size();
00043 cz = cz / cloud.points.size();
00044 ROS_INFO("Centroid of the point cloud: (%.2f, %.2f, %.2f)", cx, cy, cz);
00045
00046 float rx, ry, rz;
00047 rx = 0.02;
00048 ry = 0.02;
00049 rz = 0.02;
00050
00051 float sx, sy, sz;
00052 sx = 1.5;
00053 sy = 1.5;
00054 sz = 1.5;
00055 occupancy_grid::OccupancyGrid *v = new
00056 occupancy_grid::OccupancyGrid(cx, cy, cz, sx, sy, sz, rx, ry, rz);
00057
00058 for (int j=0; j<5; j++)
00059 {
00060 for (size_t i = 0; i < cloud.points.size(); i++)
00061 {
00062 cloud.points[i].x += j*0.02;
00063 cloud.points[i].y += j*0.02;
00064 cloud.points[i].z += j*0.02;
00065 }
00066
00067 v->fillOccupancyGrid(cloud);
00068 point_cloud_ros::OccupancyGrid og_msg;
00069
00070 uint32_t* d = v->getData();
00071 int nCells = v->nX() * v->nY() * v->nZ();
00072 og_msg.data.resize(nCells);
00073 for (int i=0; i<nCells; i++)
00074 og_msg.data[i] = d[i];
00075
00076 og_msg.header.frame_id = "base_link";
00077 og_msg.header.stamp = ros::Time::now();
00078 og_msg.center.x = cx;
00079 og_msg.center.y = cy;
00080 og_msg.center.z = cz;
00081
00082 og_msg.resolution.x = rx;
00083 og_msg.resolution.y = ry;
00084 og_msg.resolution.z = rz;
00085
00086 og_msg.grid_size.x = sx;
00087 og_msg.grid_size.y = sy;
00088 og_msg.grid_size.z = sz;
00089
00090 og_msg.occupancy_threshold = 1;
00091
00092 og_pub.publish(og_msg);
00093 pc_pub.publish(cloud_blob);
00094 ros::Duration(2).sleep();
00095 }
00096
00097 }
00098
00099