How to Add a Depth Camera to an SDF File for Gazebo

depth-camera

In the cover image, you can see a depth camera that was added to a simulated robot in Gazebo. An depth camera is useful for performing tasks like object recognition, facial recognition, obstacle avoidance, and more.

Adding a depth camera is important if you’re planning to build a robust robot for the real-world that will use the ROS 2 Navigation stack.

To add a simulated depth camera to your SDF file, you will need to add code that looks like this:

  <!-- *********************** DEPTH CAMERA ******************************  -->
  <!-- The depth camera (e.g. Intel Realsense camera). -->
  <link name="camera_depth_frame">
    <pose>0.12 0 0.65 -1.5708 0 -1.5708</pose>
  </link>
  
  <link name="camera_link">
    <pose>0.12 0 0.65 0 0 0</pose>
    
    <visual name="camera_visual">
      <pose>-0.005 0 0 0 0 0</pose>
      <geometry>
        <box>
          <size>0.015 0.08 0.022</size>
        </box>
      </geometry>
      <material>
        <ambient>0 0 0 1.0</ambient>
        <diffuse>0 0 0 1.0</diffuse>
        <specular>0.0 0.0 0.0 1.0</specular>
        <emissive>0.0 0.0 0.0 1.0</emissive>
      </material>
    </visual>    
    
    <sensor name="depth_camera" type="camera">
      <always_on>true</always_on>
      <visualize>false</visualize>
      <update_rate>5</update_rate>
      <camera name="camera">
        <horizontal_fov>1.02974</horizontal_fov>
        <image>
          <width>640</width>
          <height>480</height>
          <format>R8G8B8</format>
        </image>
        <clip>
          <near>0.02</near>
          <far>10</far>
        </clip>
        <noise>
          <type>gaussian</type>
          <!-- Noise is sampled independently per pixel on each frame.
               That pixel's noise value is added to each of its color
                channels, which at that point lie in the range [0,1]. -->
          <mean>0.0</mean>
          <stddev>0.007</stddev>
        </noise>
      </camera>
      <plugin name="depth_camera_controller" filename="libgazebo_ros_camera.so">
        <camera_name>depth_camera</camera_name>
        <frame_name>camera_depth_frame</frame_name>
        <hack_baseline>0</hack_baseline>
        <min_depth>0.001</min_depth>
      </plugin>
    </sensor>
  </link>

When you launch RViz along with Gazebo, you will need to add the Image sensor option so that you can visualize the depth camera output. Be sure to select “Best Effort” for the reliability policy.