When bringing up an RGB or RGB-D depth camera on a mobile robot for the first time, seeing the 3D point cloud (sensor_msgs/PointCloud2) stand vertically sideways like a wall or flip upside down in RViz2 is an almost universal rite of passage. In our previous tutorial on modeling 2D LiDAR (laser_frame) and IMU (imu_link) frames, both the sensor origins and the robot base shared the same coordinate convention where $+Z$ points straight up. Camera imaging pipelines, however, operate under a fundamentally different axis definition.
Grounded in REP-103 (Standard Units of Measure and Coordinate Conventions) and REP-105 (Coordinate Frames for Mobile Platforms), this explainer breaks down why ROS 2 uses a two-frame pattern (camera_link and camera_link_optical) for every camera, derives the exact rotation matrix behind rpy="-1.5708 0 -1.5708", and demonstrates how pinhole projection equations fail without it.
Why Two Coordinate Conventions Collide: Robot Body (REP-103) vs. Camera Optical
In mobile robotics and rigid-body dynamics, standard body frames assign $+X$ to the forward direction of travel, $+Y$ to the left, and $+Z$ upward against gravity. In computer vision libraries such as OpenCV and projective geometry frameworks, however, 2D image coordinates index columns from left to right ($+X$ right) and rows from top to bottom ($+Y$ down), leaving the forward optical axis (depth into the scene) as $+Z$.
The _optical frame suffix specification in REP-103 bridges these two domains by defining two distinct coordinate frames for a single physical camera:
| Coordinate Convention | +X Axis | +Y Axis | +Z Axis | Primary Responsibility in ROS 2 |
|---|---|---|---|---|
Robot Body Frame (camera_link) | Forward | Left | Up | Mechanical CAD mounting, bracket offsets, housing collision and inertial tags |
Camera Optical Frame (camera_link_optical) | Right | Down | Forward (Optical Depth Axis) | OpenCV pinhole projection, depth back-projection, header.frame_id for Image, CameraInfo, and PointCloud2 |
Separating the mechanical housing frame (camera_link) from the optical convention frame (camera_link_optical) decouples hardware engineering from perception software. When you adjust a camera bracket or tilt angle, you edit only camera_joint; when perception nodes project 3D points onto 2D pixels, they always operate inside camera_link_optical.
Implementing camera_link and camera_link_optical in URDF
In the myros_sketch robot model, the camera sits at the front edge of the body so the chassis does not occlude its field of view. Parenting camera_joint to base_link places camera_link at $x = 0.100\text{ m}$ forward and $z = 0.020\text{ m}$ up (or <origin xyz="0.13 0 0.05" rpy="0 0 0"/> in layouts where camera_joint attaches directly to chassis). We then attach camera_link_optical as a child of camera_link with zero translation (xyz="0 0 0") and the canonical optical rotation.
<!-- Stage 1: Mechanical housing frame (REP-103 body convention: +X forward, +Y left, +Z up) -->
<joint name="camera_joint" type="fixed">
<parent link="base_link"/>
<child link="camera_link"/>
<origin xyz="0.100 0 0.020" rpy="0 0 0"/>
</joint>
<link name="camera_link">
<visual>
<geometry>
<box size="0.025 0.030 0.025"/>
</geometry>
<material name="camera_dark">
<color rgba="0.15 0.15 0.18 1.0"/>
</material>
</visual>
<collision>
<geometry>
<box size="0.025 0.030 0.025"/>
</geometry>
</collision>
<inertial>
<mass value="0.030"/>
<inertia ixx="3.81e-6" ixy="0" ixz="0"
iyy="3.13e-6" iyz="0" izz="3.81e-6"/>
</inertial>
</link>
<!-- Stage 2: Optical convention transform (+Z forward optical axis, +X right, +Y down) -->
<joint name="camera_optical_joint" type="fixed">
<parent link="camera_link"/>
<child link="camera_link_optical"/>
<origin xyz="0 0 0" rpy="-1.5708 0 -1.5708"/>
</joint>
<!-- Massless, geometry-free dummy link used exclusively for optical TF frames -->
<link name="camera_link_optical"/>
Notice two important structural details in the XML snippet above:
- Physical properties live exclusively on
camera_link: The bounding box geometry (visualandcollision), mass ($0.030\text{ kg}$), and moment of inertia tensor (inertial) belong oncamera_link. As explained in our next tutorial on URDF collision simplification and inertial tensor calculations, assigning duplicate mass to a co-located frame distorts the robot’s center of mass, socamera_link_opticalis declared as an empty<link name="camera_link_optical"/>element. - Co-located origin with pure rotation:
camera_optical_jointsetsxyz="0 0 0"and rotates the axes viarpy="-1.5708 0 -1.5708"($-\pi/2, 0, -\pi/2\text{ rad}$).
Matrix Derivation: Why Two $-90^\circ$ Rotations (rpy="-1.5708 0 -1.5708")?
According to the ROS 2 Jazzy URDF specification, the rpy="r p y" attribute applies rotations about the parent frame’s fixed (extrinsic) axes in the sequence Roll ($r$ about fixed $X$), Pitch ($p$ about fixed $Y$), and Yaw ($y$ about fixed $Z$). Using the 3D rotation matrix formalism covered in our robot arm kinematics and minimum pick-and-place math guide, this extrinsic sequence corresponds to the matrix product:
Substituting $r = -\frac{\pi}{2}\ (-90^\circ \approx -1.5708\text{ rad})$, $p = 0$, and $y = -\frac{\pi}{2}\ (-90^\circ \approx -1.5708\text{ rad})$, where $\cos(-90^\circ) = 0$ and $\sin(-90^\circ) = -1$, gives the individual elementary rotation matrices:
Multiplying $R_z(-90^\circ)$ by $R_x(-90^\circ)$ produces the rotation matrix $R_{\text{link}}^{\text{opt}}$ that expresses the orientation of camera_link_optical relative to camera_link:
In rigid-body kinematics, the three columns of $R$ represent the child frame’s unit basis vectors ($\hat{x}{\text{opt}}, \hat{y}{\text{opt}}, \hat{z}_{\text{opt}}$) expressed in the coordinates of the parent frame (camera_link):
| Optical Basis Vector | Column of Matrix $R$ (in camera_link coordinates) | Physical Direction Relative to Robot Body |
|---|---|---|
| $\hat{x}_{\text{opt}}$ (Column 1) | $(0, -1, 0)^T = -\hat{y}_{\text{link}}$ | Opposite of robot Left ($+Y$) = Camera Right |
| $\hat{y}_{\text{opt}}$ (Column 2) | $(0, 0, -1)^T = -\hat{z}_{\text{link}}$ | Opposite of robot Up ($+Z$) = Image Down |
| $\hat{z}_{\text{opt}}$ (Column 3) | $(1, 0, 0)^T = +\hat{x}_{\text{link}}$ | Aligned with robot Forward ($+X$) = Forward Optical Axis |
Converting this rotation matrix to a unit quaternion yields $(x, y, z, w) = (-0.5, 0.5, -0.5, 0.5)$ (or its antipodal equivalent $(0.5, -0.5, 0.5, -0.5)$).
How Pinhole Projection and Depth Back-Projection Depend on the Optical Frame
To see why skipping camera_link_optical breaks computer vision math rather than merely rotating a display, examine the standard pinhole camera projection equations. A 3D point $\mathbf{P}_{\text{opt}} = (X, Y, Z)^T$ in optical coordinates projects onto pixel coordinates $(u, v)$ as:
Assume a $640 \times 480$ camera with focal lengths $f_x = f_y = 600\text{ px}$ and principal point $(c_x, c_y) = (320, 240)\text{ px}$. Consider an object located $1.0\text{ m}$ forward, $0.2\text{ m}$ to the right, and $0.1\text{ m}$ below the lens center:
| Evaluation Metric | Using camera_link_optical (Correct) | Passing camera_link Coordinates Directly (Broken) |
|---|---|---|
| 3D Point Coordinates $(X, Y, Z)$ | $(0.2, 0.1, 1.0)\text{ m}$ | $(1.0, -0.2, -0.1)\text{ m}$ |
| Point Dead Ahead on Optical Axis ($1\text{ m}$) | $(0, 0, 1.0)\text{ m} \implies Z = 1.0\text{ m}$ | $(1.0, 0, 0)\text{ m} \implies Z = 0.0\text{ m}$ |
| Projected Pixel $(u, v)$ | $u = 600(0.2/1.0)+320 = 440\text{ px}$ $v = 600(0.1/1.0)+240 = 300\text{ px}$ | Dead-ahead point divides by $Z = 0 \to \infty$ (Division-by-Zero Singularity) Off-axis points divide by vertical height ($Z_{\text{link}}$) instead of depth |
The exact same dependency applies in reverse when an RGB-D sensor or stereo pipeline back-projects a depth image pixel $(u, v)$ with depth $Z$ into a 3D PointCloud2 point:
Because these reconstructed $(X, Y, Z)$ points live in optical coordinates, tagging the PointCloud2 message with header.frame_id = "camera_link_optical" instructs ROS 2 tf2 to multiply them by $R = \begin{bmatrix} 0 & 0 & 1 \ -1 & 0 & 0 \ 0 & -1 & 0 \end{bmatrix}$ when transforming into camera_link and base_link. As explored in our analysis of LiDAR sensor fusion versus vision-only occupancy networks, multi-modal sensor fusion relies on this exact transform chain to project 3D LiDAR returns onto 2D camera images.
Isolating Mounting Pitch Angles and Setting Message frame_id Values
Suppose your robot’s camera is tilted $15^\circ$ ($0.2618\text{ rad}$) downward toward the floor to inspect near-field obstacles. Where should that $15^\circ$ angle live in URDF? It belongs in camera_joint, never in camera_optical_joint:
<!-- Physical 15-degree downward tilt goes into camera_joint; optical joint remains canonical -->
<joint name="camera_joint" type="fixed">
<parent link="base_link"/>
<child link="camera_link"/>
<origin xyz="0.100 0 0.020" rpy="0 0.2618 0"/>
</joint>
<joint name="camera_optical_joint" type="fixed">
<parent link="camera_link"/>
<child link="camera_link_optical"/>
<origin xyz="0 0 0" rpy="-1.5708 0 -1.5708"/>
</joint>
By the right-hand rule around the $+Y$ axis (which points left under REP-103), curling your fingers pitches the $+X$ nose downward for positive pitch angles and upward for negative pitch angles. Keeping the physical tilt inside camera_joint tilts camera_link’s $+X$ axis toward the floor, and camera_link_optical’s $+Z$ optical axis follows automatically without altering the standard optical rotation.
Ensure all camera driver topics reference the optical link name in their message headers:
| ROS 2 Message Type | Recommended header.frame_id | Purpose in Vision Pipeline |
|---|---|---|
sensor_msgs/msg/Image | camera_link_optical | Aligns image pixel axes $(u, v)$ with $+X_{\text{opt}}$ and $+Y_{\text{opt}}$ |
sensor_msgs/msg/CameraInfo | camera_link_optical | Ties intrinsic matrix $K$ and projection matrix $P$ to the optical frame |
sensor_msgs/msg/PointCloud2 | camera_link_optical | Anchors back-projected 3D depth points $(X, Y, Z)$ |
sensor_msgs/msg/CompressedImage | camera_link_optical | Maintains identical frame convention as raw images |
Verifying the Optical Transform with tf2_echo and Troubleshooting
After adding both camera links to your URDF, run tf2_echo to verify that the quaternion and Euler angles match the derived matrix:
# 1) Launch robot_state_publisher
ros2 launch myros_sketch display.launch.py
# 2) Inspect the rotation from camera_link to camera_link_optical
ros2 run tf2_ros tf2_echo camera_link camera_link_optical
# Translation: [0.000, 0.000, 0.000]
# Rotation: in quaternion [-0.500, 0.500, -0.500, 0.500]
# Rotation: in RPY (radian) [-1.571, -0.000, -1.571]
# Rotation: in RPY (degree) [-90.000, -0.000, -90.000]
# 3) Inspect the cumulative pose of camera_link_optical from base_link
ros2 run tf2_ros tf2_echo base_link camera_link_optical
# Translation: [0.100, 0.000, 0.020]
If your vision pipeline exhibits projection or point-cloud artifacts, cross-check the symptom against the diagnostic table below:
| Observed Symptom | Root Cause | Solution |
|---|---|---|
3D point cloud (PointCloud2) stands vertically sideways or points toward the ceiling in RViz2 | Topic frame_id is set to camera_link, or camera_optical_joint has rpy="0 0 0" | Set topic frame_id to camera_link_optical and configure rpy="-1.5708 0 -1.5708" |
| Point cloud renders upside down or mirrored left-to-right | Positive +1.5708 used instead of -1.5708, or degrees (-90) entered instead of radians | Check tf2_echo quaternion output against [-0.5, 0.5, -0.5, 0.5] |
| Flat floor appears sloped upward or downward in RViz2 | Physical camera bracket pitch tilt was omitted from camera_joint | Measure the physical tilt angle in radians and add it to camera_joint’s pitch |
TF lookup errors (frame does not exist) in image_geometry or RViz2 | Driver publishes camera_optical_frame while URDF defines camera_link_optical | Inspect ros2 topic echo /camera/image_raw --once --field header.frame_id and unify names |