当前位置:   article > 正文

2D(单线)激光雷达PointCloud与LaserScan格式转换_2d激光雷达 转 点云

2d激光雷达 转 点云

sensor_msgs::LaserScan转PointCloud

  1. void LaserScanToPointCloud(sensor_msgs::LaserScan::ConstPtr _laser_scan, pcl::PointCloud<pcl::PointXYZI>& _pointcloud)
  2. {
  3. _pointcloud.clear();
  4. pcl::PointXYZI newPoint;
  5. newPoint.z = 0.0;
  6. double newPointAngle;
  7. int beamNum = _laser_scan->ranges.size();
  8. for (int i = 0; i < beamNum; i++)
  9. {
  10. newPointAngle = _laser_scan->angle_min + _laser_scan->angle_increment * i;
  11. newPoint.x = _laser_scan->ranges[i] * cos(newPointAngle);
  12. newPoint.y = _laser_scan->ranges[i] * sin(newPointAngle);
  13. newPoint.intensity = _laser_scan->intensities[i];
  14. _pointcloud.push_back(newPoint);
  15. }
  16. }

PointCloud转sensor_msgs::LaserScan

  1. sensor_msgs::LaserScan PointCloudToLaserscan(pcl::PointCloud<pcl::PointXYZI>& _pointcloud)
  2. {
  3. float angle_min, angle_max, range_min, range_max, angle_increment;
  4. //需要自行调整的参数
  5. angle_min = -3.14159;
  6. angle_max = 3.14159;
  7. range_min = 0.5;
  8. range_max = 20;
  9. //角度分辨率,分辨率越小,转换后的误差越小
  10. angle_increment = 0.005
  11. //计算扫描点个数
  12. unsigned int beam_size = ceil((angle_max - angle_min) / angle_increment);
  13. sensor_msgs::LaserScan output;
  14. output.header.stamp = ros::Time::now();
  15. output.header.frame_id = "laser";
  16. output.angle_min = angle_min;
  17. output.angle_max = angle_max;
  18. output.range_min = range_min;
  19. output.range_max = range_max;
  20. output.angle_increment = angle_increment;
  21. output.time_increment = 0.0;
  22. output.scan_time = 0.0;
  23. //先将所有数据用nan填充
  24. output.ranges.assign(beam_size, std::numeric_limits<float>::quiet_NaN());
  25. output.intensities.assign(beam_size, std::numeric_limits<float>::quiet_NaN());
  26. for (auto point : _pointcloud.points)
  27. {
  28. float range = hypot(point.x, point.y);
  29. float angle = atan2(point.y, point.x);
  30. int index = (int)((angle - output.angle_min) / output.angle_increment);
  31. if (index >= 0 && index < beam_size)
  32. {
  33. //如果当前内容为nan,则直接赋值
  34. if (isnan(output.ranges[index]))
  35. {
  36. output.ranges[index] = range;
  37. }
  38. //否则,只有距离小于当前值时,才可以重新赋值
  39. else
  40. {
  41. if (range < output.ranges[index])
  42. {
  43. output.ranges[index] = range;
  44. }
  45. }
  46. output.intensities[index] = point.intensity;
  47. }
  48. }
  49. return output;
  50. }

PointCloud转sensor_msgs::PointCloud2

pcl::toROSMsg(pcl::PointCloud<pcl::PointXYZ> pointcloud_pcl,sensor_msgs::PointCloud2 pointcloud_msg);

sensor_msgs::PointCloud2转PointCloud

pcl::fromROSMsg(sensor_msgs::PointCloud2 pointcloud_msg,pcl::PointCloud<pcl::PointXYZ> pointcloud_pcl);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/981148
推荐阅读
  

闽ICP备14008679号