Create a package in src:
catkin_create_pkg laser_pc rospy sensor_msgs laser_geometry
Run the laser scan.
Use " rostopic list" to find the configuration of laser scan.
Use “rostopic info /f_scan” to check the type of the laser scan. If it shows “Type: sensor_msgs/LaserScan”, it’s right.
Create a Python file in “src” document. I gave the name “laser2pc.py”. And Copy this program into the py file. You need to change the information of the laser scan in the program.
#!/usr/bin/env python
import rospy
from sensor_msgs.msg import PointCloud2 as pc2
from sensor_msgs.msg import LaserScan
from laser_geometry import LaserProjection
class Laser2PC():
def __init__(self):
self.laserProj = LaserProjection()
self.pcPub = rospy.Publisher("/laserPointCloud", pc2, queue_size=1)
self.laserSub = rospy.Subscriber("/f_scan", LaserScan, self.laserCallback)
def laserCallback(self,data):
cloud_out = self.laserProj.projectLaser(data)
self.pcPub.publish(cloud_out)
if __name__ == '__main__':
rospy.init_node("laser2PointCloud")
l2pc = Laser2PC()
rospy.spin()
Run the python program by using " rosrun laser_pc laser2pc.py".
open the RViz.
add the “PointCloude2” and choose the Topic.
Finished.