在ros中,对于3D点,可以直接给sensor_msgs/PointCloud 赋值并发布消息,比较方便。
1.创建工程
mkdir -p pointcloud/src
cd src
catkin_create_pkg pointcloud roscpp rospy sensor_msgs std_msgs
cd ..
catkin_make
2.编辑主函数
pointcloud.cpp
#include <ros/ros.h>
#include <sensor_msgs/PointCloud.h>
int main(int argc, char** argv){
ros::init(argc, argv, "point_cloud_publisher");
ros::NodeHandle n;
ros::Publisher cloud_pub = n.advertise<sensor_msgs::PointCloud>("cloud", 50);
unsigned int num_points = 100;
ros::Rate r(1.0);
while(n.ok()){
sensor_msgs::PointCloud cloud;
cloud.header.stamp = ros::Time::now();
cloud.header.frame_id = "sensor_frame";
cloud.points.resize(num_points);
//we'll also add an intensity channel to the cloud
cloud.channels.resize(1);
cloud.channels[0].name = "rgb";
cloud.channels[0].values.resize(num_points);
//generate some fake data for our point cloud
for(unsigned int i = 0; i < num_points; ++i){
cloud.points[i].x = 0.1*i;
cloud.points[i].y = 0.1*i;
cloud.points[i].z = 5;
cloud.channels[0].values[i] = 255;
}
cloud_pub.publish(cloud);
r.sleep();
}
}
此程序对wiki上http://wiki.ros.org/cn/navigation/Tutorials/RobotSetup/Sensors的例子进行了修改。
cloud.channels[0].name = “rgb”,将名字改为rgb。另外固定点云位置为一条直线。
3.编辑CMakeLists.txt
在最后加入
add_executable(pointcloud src/pointcloud.cpp)
target_link_libraries(pointcloud ${catkin_LIBRARIES})
4.rviz显示
在rviz 中fix frame 设置为sensor_frame
其他设置及运行效果如图
参考文献
http://wiki.ros.org/cn/navigation/Tutorials/RobotSetup/Sensors
http://docs.ros.org/api/sensor_msgs/html/msg/PointCloud.html