ROS 2 Lyrical Luth · Lesson 51 of 86
Point cloud processing (PCL)
PointCloud2 to PCL, voxel-grid downsampling, passthrough filtering and plane removal.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain point cloud processing (pcl) in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
The Point Cloud Library (PCL) provides filters and algorithms for 3D data. In ROS 2 a PointCloud2 message converts to a pcl::PointCloud with pcl::fromROSMsg. Typical first steps: a PassThrough filter (keep a range on one axis), a VoxelGrid (downsample so each cell has one point) and a segmentation step that removes the floor plane.
Cloud processing is expensive: downsample first, filter to the region of interest, then run heavy steps. Keep the frame id of the cloud and transform it to base_link with tf2 before making decisions about obstacles.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the point cloud processing (pcl) example and identify what each line does.
- 3.
Build only the affected package, source the workspace overlay, and run the example.
- 4.
Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.
Worked example
Read it before you run it
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
// A PCL cloud container.
pcl::fromROSMsg(*msg, *cloud);
// Convert the ROS message to PCL.
pcl::VoxelGrid<pcl::PointXYZ> vg;
// A downsampling filter.
vg.setInputCloud(cloud);
// Give it the input.
vg.setLeafSize(0.05f, 0.05f, 0.05f);
// Keep one point per 5 cm cube.
pcl::PointCloud<pcl::PointXYZ> filtered;
// Output cloud.
vg.filter(filtered);
// Apply the filter.
RCLCPP_INFO(get_logger(), "%zu -> %zu points", cloud->size(), filtered.size());
// Show how much the cloud shrank.What the example is doing
- 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
- 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
- 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.
Watch it run · Sensor view
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for Point cloud processing (PCL). Expected result: Point count drops after filtering
Practice code
Now type it yourself
Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.
pcl::VoxelGrid<pcl::PointXYZ> vg;
vg.setInputCloud(cloud);
// TODO: set the leaf size to 5 cm in each axis
vg.filter(filtered);Expected output
Point count drops after filteringStuck? Show the full solution (try it yourself first)
pcl::VoxelGrid<pcl::PointXYZ> vg;
vg.setInputCloud(cloud);
vg.setLeafSize(0.05f, 0.05f, 0.05f);
vg.filter(filtered);Expected result
How you know it worked
- ROS_DISTRO is lyrical in every terminal used for the exercise.
- The complete package from the exercise builds and the expected graph entity appears in a second terminal.
Practical exercise
Apply the lesson
Subscribe to a PointCloud2, downsample it with a 5 cm voxel grid, keep only points with 0.1 < z < 1.5 using a PassThrough filter, and publish the result. Report the point counts.
Before you continue
- ROS_DISTRO reports lyrical in every terminal you used
- The example builds and runs without errors
- You checked the result with a ros2 command, RViz2 or a test, not just by reading the code
If something goes wrong
Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.
Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.