Add sensors
A sensor in Gazebo has two halves. A world system does the simulating, and your model says where the sensor is and what to publish. The world already has its half; this page is about yours.
What the world provides
World system in |
Simulates |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
Rendered sensors: |
The world also sets <spherical_coordinates>: where on Earth the origin is
(Portuguese Ledge, Monterey Bay). The GPS reports positions relative to that
spot, and the magnetometer computes Earth’s magnetic field there.
The pattern
Every sensor follows the same four steps:
A frame in the URDF where the sensor sits, such as
imu_link(Sensor frames).A tiny link in the Gazebo model, placed at that frame and fixed to
base_link, holding the<sensor>(Sensors).<frame_id>set to${name}/plus the URDF frame, so the messages name the framerobot_state_publisherpublishes for this instance.A bridge entry for its topic (The bridge configuration).
The IMU is shown in Compose the Gazebo model.
The other three sensors on the custom USV differ only in their <sensor>
element:
<xacro:sensor_link sensor_name="magnetometer" frame="imu_link">
<sensor name="magnetometer" type="magnetometer">
<frame_id>${name}/imu_link</frame_id>
<topic>${name}/magnetometer</topic>
<update_rate>50</update_rate>
<always_on>true</always_on>
</sensor>
</xacro:sensor_link>
<xacro:sensor_link sensor_name="navsat" frame="gps_link">
<sensor name="navsat" type="navsat">
<frame_id>${name}/gps_link</frame_id>
<topic>${name}/navsat</topic>
<update_rate>5</update_rate>
<always_on>true</always_on>
</sensor>
</xacro:sensor_link>
The anemometer is a custom sensor that the wind system reads. It reports the
apparent wind, the wind at the sensor less the boat’s own velocity, in the
sensor frame, as the linear part of a gz.msgs.Twist, bridged to
geometry_msgs/msg/TwistStamped:
<xacro:sensor_link sensor_name="anemometer" frame="gps_link">
<sensor name="anemometer" type="custom" gz:type="anemometer">
<frame_id>${name}/gps_link</frame_id>
<topic>${name}/anemometer</topic>
<update_rate>10</update_rate>
<always_on>true</always_on>
<gz:anemometer>
<noise type="gaussian">
<mean>0</mean>
<stddev>0.1</stddev>
</noise>
</gz:anemometer>
</sensor>
</xacro:sensor_link>
It is a gz-sensors custom sensor, so it takes <topic>, <frame_id>,
<update_rate> and <pose> like any other, and the standard <noise>
block inside <gz:anemometer>, applied to each axis on its own and seeded
from Gazebo’s seed (gz sim --seed). Without one its readings are exact.
A camera
The custom USV has no camera, but adding one follows the same pattern. With
a camera_link frame in your URDF:
<link name="camera_sensor">
<pose relative_to="camera_link">0 0 0 0 0 0</pose>
<inertial>
<mass>1e-5</mass>
<inertia><ixx>1e-8</ixx><iyy>1e-8</iyy><izz>1e-8</izz></inertia>
</inertial>
<sensor name="camera" type="camera">
<frame_id>${name}/camera_link</frame_id>
<topic>${name}/camera/image</topic>
<update_rate>15</update_rate>
<always_on>true</always_on>
<camera>
<horizontal_fov>1.2</horizontal_fov>
<image><width>640</width><height>480</height></image>
<clip><near>0.05</near><far>200</far></clip>
</camera>
</sensor>
</link>
<joint name="camera_sensor_joint" type="fixed">
<parent>base_link</parent>
<child>camera_sensor</child>
</joint>
Bridge it as sensor_msgs/msg/Image to gz.msgs.Image, from Gazebo to ROS.
Warning
open_water.sdf lights its scene with a cyan ambient light
(<scene><ambient>0.0 1.0 1.0</ambient>) for its deep-ocean look. Camera
sensors render with that light, so objects look green and blue in their
images. The Gazebo GUI uses its own light and isn’t affected. If colors
matter to you, use a copy of the world with a neutral ambient light, such as
0.4 0.4 0.4 (Use your own world).
An underwater camera
A camera under water sees through the water: put the underwater camera
plugin inside an rgbd_camera and it publishes what the camera sees through
the world’s water. See See through the water.
Check a sensor
ros2 topic echo /custom_usv/imu --once
Look for frame_id: custom_usv/imu_link in the header. If the command
waits forever, check the bridge entry and that the world runs the matching
system.
A sonar is a custom sensor driven by the world’s sonar system rather than a stock gz-sim sensor: see Add a sonar.
Next: Use your own world.