ROS 2 Lyrical Luth · Lesson 47 of 86
LaserScan to point cloud conversion
Polar to Cartesian conversion, PointCloud2 and the laser_geometry package.
Estimated time · 45–75 min
Learning goals
What you will understand
- Explain laserscan to point cloud conversion 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
A LaserScan is polar: each ray has a distance and an angle. To use it with 3D tools you convert it to Cartesian points: x = r cos θ, y = r sin θ (z = 0 for a 2D lidar). The result is a sensor_msgs/msg/PointCloud2 in the sensor's frame.
The laser_geometry package does this conversion (projectLaser) including handling of invalid rays, and can also transform the cloud into another frame. Keep the header (frame id and stamp) so TF can place the cloud.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the laserscan to point cloud conversion 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
#include "laser_geometry/laser_geometry.hpp"
// Library that converts scans to clouds.
laser_geometry::LaserProjection projector_;
// A projector object kept as a member.
sensor_msgs::msg::PointCloud2 cloud;
// The output message.
projector_.projectLaser(*scan, cloud);
// Convert every valid ray to an (x, y, z) point.
cloud_pub_->publish(cloud);
// Publish; the header keeps the lidar frame and time.
// Manual version: x = r * cos(theta), y = r * sin(theta)
// The formula projectLaser applies to each ray.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 LaserScan to point cloud conversion. Expected result: (1, 0) (0, 2) (-3, 0)
Practice code
Now type it yourself
Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice
#include <cmath>
#include <cstdio>
#include <vector>
int main() {
const double angle_min = 0.0, increment = M_PI / 2; // rays at 0, 90 and 180 degrees
const std::vector<double> ranges{1.0, 2.0, 3.0};
for (std::size_t i = 0; i < ranges.size(); ++i) {
const double theta = angle_min + i * increment;
// TODO: compute x = r*cos(theta) and y = r*sin(theta)
x = std::round(x * 1000) / 1000 + 0.0;
y = std::round(y * 1000) / 1000 + 0.0;
std::printf("(%g, %g)%s", x, y, i + 1 < ranges.size() ? " " : "\n");
}
return 0;
}Expected output
(1, 0) (0, 2) (-3, 0)Stuck? Show the full solution (try it yourself first)
#include <cmath>
#include <cstdio>
#include <vector>
int main() {
const double angle_min = 0.0, increment = M_PI / 2; // rays at 0, 90 and 180 degrees
const std::vector<double> ranges{1.0, 2.0, 3.0};
for (std::size_t i = 0; i < ranges.size(); ++i) {
const double theta = angle_min + i * increment;
double x = ranges[i] * std::cos(theta);
double y = ranges[i] * std::sin(theta);
x = std::round(x * 1000) / 1000 + 0.0;
y = std::round(y * 1000) / 1000 + 0.0;
std::printf("(%g, %g)%s", x, y, i + 1 < ranges.size() ? " " : "\n");
}
return 0;
}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
Convert a /scan to a PointCloud2 with laser_geometry, view it in RViz2 with the PointCloud2 display, then implement the polar conversion yourself and compare a few points.
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.