Advertisement
Guest User

Laser Scan To Point Cloud Transform

a guest
May 3rd, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.   * Script to transform sensor_msgs::LaserScan data
  3.   * from Lidar to sensor_msgs::PointCloud data.
  4.   * @Author Sonia Mannan
  5.   * @Version 4/25/2015
  6.  */
  7.  
  8. #include "ros/ros.h"
  9. #include "std_msgs/String.h"
  10. #include "sensor_msgs/PointCloud.h"
  11. #include "laser_geometry/laser_geometry.h"
  12. #include <tf/transform_listener.h>
  13.  
  14. //create a LaserProjection object
  15. //to project laserScan data to point_cloud
  16. laser_geometry::LaserProjection projector;
  17.  
  18. //create a Transform Listener object to
  19. //receive transforms
  20. tf::TransformListener listen_scan;
  21.  
  22. //create a PointCloud object to project data onto.
  23. sensor_msgs::PointCloud cloud;
  24.  
  25. /**
  26.   * Callback to process LaserScan data from Lidar.
  27.   * @param scan_input a sensor_msgs::LaserScan pointer
  28.   * i.e. pointer to LaserScan data from Lidar
  29.   * Will take in LaserScan data, transform using a
  30.   * TransformListener and project data using LaserProjection
  31.   * to a point cloud.
  32. */
  33. void Process_Lidar_Data(const sensor_msgs::LaserScan::ConstPtr& scan_input)
  34. {
  35.    /**
  36.      * Wait until a Transform becomes available.
  37.      *
  38.      * First param: frame to transform from
  39.      * scan_input->header.frame_id = frame of the
  40.      * current LaserScan input
  41.      *
  42.           *
  43.      * First param: frame to transform from
  44.      * scan_input->header.frame_id = frame of the
  45.      * current LaserScan input
  46.      *
  47.      * Second param: frame to transform to
  48.      * "/base_link" = frame to transform to
  49.      *
  50.      * Third param: when to transform
  51.      * scan_input->header.stamp = time first frame is received
  52.      * ros::Duration().fromSec = ros Duration is a time
  53.      * period, fromSec is a function to convert from Seconds
  54.      * scan_input->ranges.size() = size of data array received
  55.      * scan_input->time_increment = time between measurements
  56.      * in seconds.
  57.      * Transforms at time = (when the first frame is received +
  58.      * data size*time between measurements) seconds
  59.      *
  60.      * Fourth param: when to time out
  61.      * ros::Duration(1.0) = 1 second
  62.      * Do not wait longer than 1 second.
  63.    */
  64.  
  65.    if(!listen_scan.waitForTransform
  66.          (scan_input->header.frame_id, "/base_link",
  67.           scan_input->header.stamp+ros::Duration().
  68.           fromSec(scan_input->ranges.size()*scan_input
  69.           ->time_increment),ros::Duration(1.0)))
  70.    {
  71.            
  72.          return;
  73.    }
  74.  
  75.    //Transform LaserScan to PointCloud data
  76.    projector.transformLaserScanToPointCloud("/base_link", *scan_input,
  77.       cloud, listen_scan);
  78. }
  79.  
  80. void printPointCloudData(sensor_msgs::PointCloud cloud)
  81. {
  82.    for(unsigned int i = 0; i < cloud.points.size(); i++)
  83.    {
  84.       ROS_INFO("X Coord: %f", cloud.points[i].x);
  85.       ROS_INFO("Y Coord: %f", cloud.points[i].y);
  86.       ROS_INFO("Z Coord: %f", cloud.points[i].z);
  87.    }
  88. }
  89.  
  90. int main (int argc, char **argv)
  91. {
  92.    //Initialize the ROS lidar listener
  93.    //Must be called before using ROS System
  94.  
  95.    ros::init(argc, argv, "lidar_listener");
  96.  
  97.    //Create ROS NodeHandle to communicate with
  98.    //the ROS System
  99.  
  100.    ros::NodeHandle n_lidar;
  101.  
  102.    //Subscribe to _scan topic (broadcasts Lidar data)
  103.    //Tells ROS you want to receive messages from Lidar
  104.    //Messages passed to Proess_Lidar_Data callback (above)
  105.  
  106.    ros::Subscriber lidar_sub = n_lidar.subscribe("_scan",
  107.       1000, Process_Lidar_Data);
  108.  
  109.    printPointCloudData(cloud);
  110.                                      
  111.    ros::spin();
  112.  
  113.    return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement