When integrating a 2D LiDAR and an Inertial Measurement Unit (IMU) into a mobile robot, one of the most frequent sources of mapping distortion and odometry drift is confusing a sensor’s visible housing position with its physical measurement origin. While designing controller frames for the Raspberry Pi and ESP32-S3 focuses primarily on board clearance and wiring layout, sensor frame modeling demands millimeter-level geometric fidelity: a vertical error of a few centimeters on a LiDAR or an uncompensated 90-degree rotation on an IMU PCB will directly corrupt SLAM maps and state estimation.
Following the official ROS 2 Jazzy URDF Tutorials and the coordinate conventions in REP-103 and REP-145, this guide walks through how to construct and verify the two-stage LiDAR frames (lidar_base_frame and laser_frame) and the inertial reference frame (imu_link) in the myros_sketch robot model.
Separating Mechanical Mounts from Optical Scan Planes: lidar_base_frame and laser_frame
Most 360-degree 2D laser scanners consist of a stationary lower base (containing the motor controller, interface PCB, and mounting holes) and a rotating upper turret that houses the optical emitter and receiver lens. Because mechanical CAD assemblies reference the bottom screw holes while perception algorithms reference the elevated laser beam plane, separating the LiDAR into two distinct URDF links keeps both domains clean.
| Frame Name | Physical Meaning | Parent Frame | Primary Role in ROS 2 |
|---|---|---|---|
lidar_base_frame | Mechanical bracket and screw-hole interface | rpi_frame (or chassis) | Checking standoff clearances, swapping brackets, mechanical CAD alignment |
laser_frame | Center of the 2D optical scanning plane | lidar_base_frame | header.frame_id of sensor_msgs/LaserScan, SLAM raycasting, Nav2 costmaps |
In myros_sketch, the LiDAR is stacked atop the Raspberry Pi computer board to give the laser an unobstructed 360-degree field of view. As established in the foundational base_link, chassis, and base_footprint hierarchy guide and the controller mounting layout, the center of rpi_frame sits at $Z = 0.0415\text{ m}$ relative to base_link, with its top surface at $0.0500\text{ m}$. We attach lidar_base_frame above rpi_frame via standoffs, and then place laser_frame at the exact elevation of the rotating optical plane.
<!-- Stage 1: Mechanical mounting reference (lower bracket and adapter plate) -->
<joint name="lidar_base_joint" type="fixed">
<parent link="rpi_frame"/>
<child link="lidar_base_frame"/>
<origin xyz="0 0 0.016" rpy="0 0 0"/>
</joint>
<link name="lidar_base_frame">
<visual>
<geometry>
<box size="0.060 0.060 0.015"/>
</geometry>
<material name="dark_grey">
<color rgba="0.2 0.2 0.22 1.0"/>
</material>
</visual>
<collision>
<geometry>
<box size="0.060 0.060 0.015"/>
</geometry>
</collision>
<inertial>
<mass value="0.030"/>
<inertia ixx="9.56e-6" ixy="0" ixz="0"
iyy="9.56e-6" iyz="0" izz="1.80e-5"/>
</inertial>
</link>
<!-- Stage 2: Optical measurement reference (center of the laser scan plane) -->
<joint name="laser_joint" type="fixed">
<parent link="lidar_base_frame"/>
<child link="laser_frame"/>
<origin xyz="0 0 0.030" rpy="0 0 0"/>
</joint>
<link name="laser_frame">
<visual>
<origin xyz="0 0 0.010" rpy="0 0 0"/>
<geometry>
<cylinder radius="0.035" length="0.040"/>
</geometry>
<material name="sensor_black">
<color rgba="0.08 0.08 0.10 1.0"/>
</material>
</visual>
<collision>
<origin xyz="0 0 0.010" rpy="0 0 0"/>
<geometry>
<cylinder radius="0.035" length="0.040"/>
</geometry>
</collision>
<inertial>
<mass value="0.170"/>
<inertia ixx="7.47e-5" ixy="0" ixz="0"
iyy="7.47e-5" iyz="0" izz="1.04e-4"/>
</inertial>
</link>
Note that on alternative robot layouts where the LiDAR pedestal mounts directly to the front deck of the main body rather than stacking over the Raspberry Pi, lidar_base_joint can take chassis as its parent with an explicit forward and vertical offset such as <origin xyz="0.08 0 0.06" rpy="0 0 0"/>. Regardless of whether the parent is rpi_frame or chassis, decoupling the mechanical mount from laser_frame ensures that replacing the scanner requires updating only a single joint offset.
Accumulating the Vertical Scan Height and Visual Offsets
Look closely at the <visual> and <collision> blocks inside laser_frame: both contain <origin xyz="0 0 0.010" rpy="0 0 0"/> for a cylinder of length $0.040\text{ m}$ ($40\text{ mm}$). If the optical emission window on the physical turret lies $10\text{ mm}$ above the turret base rather than at its $20\text{ mm}$ geometric midpoint, placing laser_frame at the optical plane means the cylinder geometry extends $10\text{ mm}$ below the frame origin and $30\text{ mm}$ above it. Shifting the cylinder’s visual and collision origin by $+0.010\text{ m}$ along $Z$ aligns the physical silhouette with the optical origin.
Tracing the vertical transform chain from base_link to laser_frame yields the exact scan plane elevation:
Adding the wheel radius ($0.0325\text{ m}$), which defines the elevation of base_link above base_footprint, gives the true height of the laser sheet above the floor:
As detailed in our comparison of LiDAR sensor fusion versus vision-only occupancy architectures, accurate extrinsic transforms between range sensors and the vehicle base are essential for obstacle clearing and multi-sensor registration. Even a small vertical or angular discrepancy causes projected rays to miss low obstacles or tilt wall surfaces when the robot pitches during acceleration.
Binding sensor_msgs/LaserScan to TF and Handling 180-Degree Mounting Offsets
As explained in the ROS 2 Jazzy tf2 Tutorials, a range array published on /scan carries no spatial meaning until tf2 resolves its header.frame_id against the active URDF tree. Every sensor_msgs/msg/LaserScan packet contains the following geometric metadata:
| Message Field | Type & Unit | Physical Meaning | URDF Integration Requirement |
|---|---|---|---|
header.frame_id | string | Coordinate frame in which polar ranges are measured | Must exactly match <link name="laser_frame"> |
angle_min, angle_max | float32 ($\text{rad}$) | Start and end angles of a full scan sweep | Verify where the hardware driver defines $0\text{ rad}$ (typically along $+X$) |
angle_increment | float32 ($\text{rad}$) | Angular step between consecutive rays | For a 360-point scan, $2\pi / 360 \approx 0.01745\text{ rad}$ ($1^\circ$) |
range_min, range_max | float32 ($\text{m}$) | Minimum and maximum valid distance thresholds | E.g., $0.15\text{ m}$ to $12.0\text{ m}$; out-of-range returns inf or NaN |
ranges[] | float32[] ($\text{m}$) | Array of distance measurements per angular step | Ordered counter-clockwise (CCW) around the $+Z$ axis of laser_frame |
At $360\text{ points}$ per revolution and a $10\text{ Hz}$ scan rate, a 2D LiDAR produces $3,600$ float32 ($4\text{ Byte}$) measurements per second—roughly $14.4\text{ kB/s}$ of raw payload. While this bandwidth is nearly 2,000 times smaller than an uncompressed RGB camera stream, every single scan is critical for loop closure and scan matching in SLAM.
Correcting a 180-Degree Reversed LiDAR in URDF
In compact mobile robots, routing the LiDAR’s power and UART connector cleanly often requires mounting the unit rotated by 180 degrees so the cable exits toward the rear or front without hitting a standoff. When the sensor’s zero-degree mark points toward the rear ($-X$) of the robot, raw scans appear mirrored front-to-back in RViz2.
Instead of patching driver code to reverse the ranges[] array, encode the physical mounting orientation directly in the rpy attribute of laser_joint by setting a yaw angle of $\pi\text{ rad}$ ($3.14159\text{ rad}$):
<!-- When the LiDAR's 0-degree front mark faces the rear (-X) of the robot -->
<joint name="laser_joint" type="fixed">
<parent link="lidar_base_frame"/>
<child link="laser_frame"/>
<origin xyz="0 0 0.030" rpy="0 0 3.14159"/>
</joint>
Because robot_state_publisher broadcasts this static rotation on /tf_static, RViz2, slam_toolbox, and Nav2 automatically transform every ray into the correct robot-relative direction.
IMU Modeling (imu_link): Axis Conventions and Lever-Arm Centrifugal Error
An IMU measures 3-axis angular velocity ($\boldsymbol{\omega}$) and 3-axis linear acceleration ($\mathbf{a}$). According to REP-103 and REP-145 (Conventions for IMU Sensor Drivers), the IMU frame must obey the standard right-handed ROS body convention: $+X$ forward, $+Y$ left, and $+Z$ up, with positive angular velocities defined counter-clockwise around each axis by the right-hand rule.
In myros_sketch, the center of the chassis top plate is occupied by the Raspberry Pi mounting pillars, so the IMU breakout board (imu_link) is placed $6.5\text{ cm}$ forward of base_link ($x = 0.065\text{ m}$) on the upper deck ($z = 0.0275\text{ m}$). (If your chassis layout leaves the center clear, you can alternatively parent imu_joint to chassis at <origin xyz="0 0 0.035" rpy="0 0 0"/> directly above the center of rotation.)
<joint name="imu_joint" type="fixed">
<parent link="base_link"/>
<child link="imu_link"/>
<origin xyz="0.065 0 0.0275" rpy="0 0 0"/>
</joint>
<link name="imu_link">
<visual>
<geometry>
<box size="0.020 0.020 0.003"/>
</geometry>
<material name="imu_blue">
<color rgba="0.1 0.25 0.6 1.0"/>
</material>
</visual>
<collision>
<geometry>
<box size="0.020 0.020 0.003"/>
</geometry>
</collision>
<inertial>
<mass value="0.002"/>
<inertia ixx="6.82e-8" ixy="0" ixz="0"
iyy="6.82e-8" iyz="0" izz="1.33e-7"/>
</inertial>
</link>
Calculating Centrifugal and Tangential Acceleration from Lever-Arm Offsets
While angular velocity $\boldsymbol{\omega}$ is identical everywhere on a rigid body, linear acceleration depends on where the accelerometer sits relative to the axis of rotation. When imu_link is displaced from the rotational center (base_link) by a position vector $\mathbf{r}$, any angular velocity $\boldsymbol{\omega}$ and angular acceleration $\boldsymbol{\alpha} = \dot{\boldsymbol{\omega}}$ add tangential and centrifugal terms to the measured acceleration $\mathbf{a}_{\text{meas}}$:
With myros_sketch’s forward offset of $r = 0.065\text{ m}$, spinning in place produces a radial centrifugal acceleration of magnitude $a_c = \omega^2 r$ directed along $-X$:
- Moderate yaw rate ($\omega = 1.0\text{ rad/s} \approx 57.3^\circ\text{/s}$): The centrifugal bias is $\omega^2 r = (1.0)^2 \times 0.065 = 0.065\text{ m/s}^2$, which represents roughly $0.66%$ of standard gravity ($9.81\text{ m/s}^2$).
- Aggressive yaw turn ($\omega = 3.0\text{ rad/s} \approx 171.9^\circ\text{/s}$): The centrifugal bias scales quadratically to $\omega^2 r = (3.0)^2 \times 0.065 = 0.585\text{ m/s}^2$, or nearly $6.0%$ of gravity—large enough to induce several degrees of false pitch tilt if uncompensated.
- Angular acceleration spike ($\alpha = 5.0\text{ rad/s}^2$): A lateral tangential acceleration of $\alpha r = 5.0 \times 0.065 = 0.325\text{ m/s}^2$ appears along the $Y$ axis during rotational start and stop transients.
State estimation packages such as robot_localization (ekf_node) query the TF tree for the transform between base_link and imu_link to remove lever-arm effects. Recording the exact $0.065\text{ m}$ offset in imu_joint makes this compensation automatic.
Comparing Approaches to IMU Axis Alignment
If soldering or mounting holes force you to install the IMU chip rotated by $90^\circ$ on the chassis deck, there are three places where you could compensate for the rotation:
| Compensation Method | How It Works | Advantages | Drawbacks & Risks |
|---|---|---|---|
URDF imu_joint rpy (Recommended) | Set <origin rpy="0 0 1.5708"/> on imu_joint to match physical PCB orientation | Every TF consumer (EKF, SLAM, RViz2) sees a consistent, physically accurate transform | Requires updating URDF if the board is remounted |
| Driver / Firmware Axis Swap | Swap $X$ and $Y$ channels and invert signs inside the ESP32 or ROS driver | Keeps imu_joint at rpy="0 0 0" | Disconnects raw topic channels from the silkscreen axes printed on the physical chip |
| Fusion Filter Config | Remap axes inside downstream filter configuration files | No firmware or URDF rebuild needed | Nodes subscribing directly to /imu/data outside the filter still receive rotated axes |
Keeping physical mounting geometry inside the URDF imu_joint is the cleanest architectural choice in ROS 2.
Step-by-Step Verification Routine and Troubleshooting Table
Once lidar_base_frame, laser_frame, and imu_link are added to your URDF file, execute the following four verification steps to confirm that the kinematic tree matches your live sensor streams:
- Build the package and launch
robot_state_publisher: Compile the workspace with--symlink-installand launch the visualization node. - Inspect cumulative transforms with
tf2_echo: Query the translation and rotation frombase_linktolaser_frameandimu_linkto verify that the chained offsets sum to $Z = 0.0875\text{ m}$ and $X = 0.065\text{ m}$. - Audit live topic
header.frame_idstrings: Echo a single message from/scanand/imu/datato ensure the published frame names match the URDF<link>names verbatim. - Perform a physical motion sanity check in RViz2: Place an obstacle directly in front of the robot to verify that scan points appear along $+X$, and tilt the front of the robot downward to confirm that the IMU reports a pitch change rather than roll.
# 1) Build the workspace and start the display launch file
cd ~/ros2_ws
colcon build --symlink-install --packages-select myros_sketch
source install/setup.bash
ros2 launch myros_sketch display.launch.py
# 2) Verify cumulative scan plane height from base_link (0.0415 + 0.016 + 0.030 = 0.0875 m)
ros2 run tf2_ros tf2_echo base_link laser_frame
# Translation: [0.000, 0.000, 0.088]
# Rotation: in RPY (radian) [0.000, -0.000, 0.000]
# 3) Verify IMU position relative to base_link (x = 0.065 m, z = 0.0275 m)
ros2 run tf2_ros tf2_echo base_link imu_link
# Translation: [0.065, 0.000, 0.028]
# Rotation: in RPY (radian) [0.000, -0.000, 0.000]
# 4) Confirm that live sensor messages publish exact URDF link names
ros2 topic echo /scan --once --field header.frame_id
ros2 topic echo /imu/data --once --field header.frame_id
Use the diagnostic table below to resolve common LiDAR and IMU frame failures before moving on to camera optical frames:
| Observed Symptom | Root Cause | Fix |
|---|---|---|
| LaserScan points collapse into a single dot at the origin or trigger TF errors | Mismatch between driver frame_id (e.g., laser) and URDF link (laser_frame) | Align the driver parameter frame_id: "laser_frame" with the URDF link name |
| Scan plane renders on the floor or floats above the LiDAR turret in RViz2 | Incorrect $Z$ offset in laser_joint or double-counting base_footprint elevation | Re-measure the relative vertical distance from the parent frame to the optical window |
| Obstacles in front of the robot appear behind it on the SLAM map | LiDAR mounted rotated by $180^\circ$ with laser_joint yaw left at 0 | Update laser_joint origin to rpy="0 0 3.14159" |
| Pitching the robot nose-down causes the EKF attitude to roll sideways | IMU PCB mounted at a $90^\circ$ yaw rotation without updating imu_joint | Add the physical mounting yaw ($\pm 1.5708\text{ rad}$) to imu_joint’s rpy |
| Stationary IMU reports $a_z \approx +9.81\text{ m/s}^2$ instead of $0$ | Normal specific-force measurement of the floor’s upward reaction against gravity | Expected per REP-145; enable gravity removal in your fusion filter if needed |