Compose the Gazebo model
The URDF says what the vehicle is. The Gazebo model adds how it is
simulated: what floats it, what drives it, how the water slows it down,
and what it senses. It is one short file, model.sdf.xacro, that includes
your URDF and adds the rest. The custom USV’s is
kai_custom_vehicle/models/custom_usv/model.sdf.xacro, and every snippet
on this page is taken from it.
flowchart LR
U["<b>custom_usv.urdf</b><br/>links, joints, mass,<br/>contact shapes, frames"]
M["<b>model.sdf</b><br/>the URDF, plus:<br/>marked displacement volume<br/>Thruster × 2: propulsion<br/>Hydrodynamics: damping<br/>IMU, magnetometer, GPS<br/>JointStatePublisher"]
U -->|merged into| M
The wrapper
<sdf version="1.12" xmlns:xacro="http://ros.org/wiki/xacro"
xmlns:gz="http://gazebosim.org/schema">
<xacro:arg name="name" default="custom_usv"/>
<xacro:arg name="urdf_uri" default="model://custom_usv/custom_usv.urdf"/>
<xacro:property name="name" value="$(arg name)"/>
<xacro:include filename="../../urdf/dimensions.xacro"/>
<model name="${name}">
<include merge="true">
<uri>$(arg urdf_uri)</uri>
</include>
<!-- displacement, plugins and sensors go here -->
</model>
</sdf>
Three things to notice:
merge="true"puts the URDF’s links and joints directly into this model, so what follows can refer to them by name:base_link,motor_port_jointand so on.xmlns:gzon the root declares the namespace the displacement mark below uses. Without it the file doesn’t parse.nameis the instance name. It becomes the model name, the prefix of every topic and the prefix of every sensor frame, so one file serves any number of boats. The spawn launch passes it, together with aurdf_uripointing at the URDF it rendered for the same name (One name per instance).
The include of dimensions.xacro, by a path relative to this file, gives it
the same hull sizes the URDF was built from, so the displacement boxes below
cannot drift from the hulls.
What survives the conversion
Gazebo converts the URDF when it loads it, and in doing so merges every
link attached by a fixed joint into its parent. On the custom USV,
imu_link and gps_link stop being links, but survive as frames with the
same names. That has two consequences:
A plugin that needs a link must name one that survives: here
base_linkor a propeller.A sensor can still be placed at
imu_link, because poses can be given relative to a frame.
To see exactly what survives, print the converted model:
SDF_PATH=$GZ_SIM_RESOURCE_PATH \
gz sdf -p $(ros2 pkg prefix --share kai_custom_vehicle)/models/custom_usv/model.sdf
gz sdf looks for model:// URIs in SDF_PATH, not in
GZ_SIM_RESOURCE_PATH, hence the variable.
Mark the collisions that displace
Every gz-maritime vehicle needs this step. Add a link fixed to base_link
whose collision shapes are the volume that floats the vehicle, and mark each
of them with gz:buoyancy="true". The custom USV’s macro makes one box per
hull:
<xacro:macro name="pontoon" params="side y">
<collision name="pontoon_${side}" gz:buoyancy="true" gz:wind="true">
<pose>0 ${y} ${hull_z} 0 0 0</pose>
<geometry>
<box><size>${hull_length} ${hull_width} ${hull_height}</size></box>
</geometry>
<surface>
<contact><collide_bitmask>0x00</collide_bitmask></contact>
</surface>
</collision>
</xacro:macro>
and puts the two on a link of their own:
<link name="hull_displacement">
<pose relative_to="base_link">0 0 0 0 0 0</pose>
<inertial>
<mass>1e-5</mass>
<inertia>
<ixx>1e-8</ixx><ixy>0</ixy><ixz>0</ixz>
<iyy>1e-8</iyy><iyz>0</iyz><izz>1e-8</izz>
</inertia>
</inertial>
<xacro:pontoon side="port" y="${hull_spacing / 2}"/>
<xacro:pontoon side="stbd" y="${-hull_spacing / 2}"/>
</link>
<joint name="hull_displacement_joint" type="fixed">
<parent>base_link</parent>
<child>hull_displacement</child>
</joint>
The rules:
A marked collision floats; an unmarked one doesn’t. The world floats nothing it isn’t told about, and this is how it is told. A link that marks any of its collisions floats by those alone; its other collisions stay contact geometry.
Use shapes buoyancy can slice: box, sphere, cylinder, capsule, ellipsoid and cone. Meshes and planes are skipped, with a warning. A box per hull is a good start; the custom USV’s are the same size and place as the URDF’s hulls, from the shared
dimensions.xacro.Give a mark a zero
collide_bitmask. A marked collision is still a collision to the physics engine. With the bitmask it touches nothing, and the URDF’s hulls do the bumping.Put the marks on their own link. They could sit on
base_linktoo, butbase_linkcomes from the URDF, and a merged include can’t add elements to it. A separate link, fixed-jointed, is one rigid body to physics and keeps contact and displacement visibly apart.Get the centre of buoyancy above the centre of mass, or at least the waterplane wide, or the vehicle capsizes. The custom USV floats its 12 kg on two boxes centred 3.6 cm above the waterline, well above its centre of mass.
Important
Without a marked collision your vehicle sinks. The mark is a plain SDF attribute, so it travels with the model file: nothing in the world and nothing in a launch file refers to it.
Thrusters
Add one Thruster plugin per propeller joint. It is gz-maritime’s thruster
system, gz_thruster: Gazebo’s own with a normalized command mode, the
interface every vehicle here is driven with:
<xacro:macro name="thruster" params="side coefficient">
<plugin filename="gz-maritime-thruster-system"
name="gz::sim::maritime::Thruster">
<joint_name>motor_${side}_joint</joint_name>
<namespace></namespace>
<topic>${name}/motor_${side}/cmd</topic>
<use_normalized_cmd>true</use_normalized_cmd>
<thrust_coefficient>${coefficient}</thrust_coefficient>
<fluid_density>${water_density}</fluid_density>
<propeller_diameter>${prop_diameter}</propeller_diameter>
<velocity_control>true</velocity_control>
<!-- The thrust limits [N]: what +1 and -1 command. -->
<max_thrust_cmd>20.0</max_thrust_cmd>
<min_thrust_cmd>-15.0</min_thrust_cmd>
</plugin>
</xacro:macro>
Commands are a fraction of full thrust, in [-1, 1], on the topic: 1 gives
max_thrust_cmdahead, -1 givesmin_thrust_cmdastern, 0 stops. Those two limits, in newtons, are the only place a force appears. That is how a real thruster is driven: an autopilot or a controller produces a normalized output and the ESC maps it onto the motor, so a controller written against this boat drives the BlueBoat unchanged. The propeller speed comes back on the same topic followed by/ang_vel.Leave
<namespace>empty and put the name in the topic yourself. Otherwise the plugin prefixes the topic with the model name on its own, and the bridge configuration can’t know what to expect.Counter-rotating propellers get opposite signs of
thrust_coefficient: the macro is called with0.02for port and-0.02for starboard.fluid_densityis seawater, andpropeller_diametercomes from the shared dimensions, so it matches the propeller in the URDF.
gz_thruster is on its way upstream. When a Gazebo release carries the
mode, only the plugin’s filename and name change.
Hydrodynamic damping
Water resists motion. The Hydrodynamics plugin applies that resistance to one link:
<plugin filename="gz-maritime-hydrodynamics-system"
name="gz::sim::maritime::Hydrodynamics">
<link_name>base_link</link_name>
<xU>-20</xU> <yV>-40</yV> <zW>-30</zW>
<kP>-20</kP> <mQ>-20</mQ> <nR>-15</nR>
<xUabsU>-30</xUabsU>
<yVabsV>-90</yVabsV>
<zWabsW>-100</zWabsW>
<kPabsP>-40</kPabsP>
<mQabsQ>-40</mQabsQ>
<nRabsR>-30</nRabsR>
</plugin>
Each pair of coefficients resists one kind of motion:
Coefficients |
Motion |
|
|---|---|---|
|
Surge |
forwards and backwards |
|
Sway |
sideways |
|
Heave |
up and down |
|
Roll |
tipping to the side |
|
Pitch |
nose up and down |
|
Yaw |
turning |
The first of each pair grows with speed, the abs one with speed squared. A
boat should resist sideways motion much more than forward motion, or it
slides out of its turns. The custom USV’s values are placeholders borrowed
from the BlueBoat; tune them for your vehicle.
The plugin is gz-maritime-hydrodynamics-system, from gz_hydrodynamics:
Gazebo’s Hydrodynamics with a few changes. The main one: it damps against the
water rather than the ground, so the boat drifts with the world’s ocean
current. gz-sim’s
own gz-sim-hydrodynamics-system takes the same coefficients but doesn’t see
the world’s current, so a boat that names it sits still in one. Put added
mass in the plugin’s <xDotU> and the like, not in <fluid_added_mass>,
which only DART implements.
Note
The damping acts in the air too, so a boat spawned well above the water falls slowly. Spawn it at the waterline.
By default the plugin takes the hull’s velocity and applies the drag at the
origin of its link. When the thrust acts on another line, say below
base_link, thrust and drag make a couple and the vehicle pitches or rolls
under way. The coefficients are only valid about the point they were defined
at, so name that point, the centre of pressure, in the link frame: where
the coefficients were identified or, for coefficients without coupling terms,
where the drag acts on the hull. The plugin then takes the velocity and the
current there and applies the drag there.
<center_of_pressure>0 0 -0.07</center_of_pressure>
Added mass
When a hull speeds up it also drags some water along, which makes it feel
heavier than it is. Gazebo models this with <fluid_added_mass> inside a
link’s <inertial>. A URDF can’t express that element, and a merged include
can’t add it to a link that comes from the URDF. For a small, slow boat it is
safe to leave out.
Sensors
Each sensor sits on a tiny link fixed to base_link, placed at a frame the
URDF declared. The custom USV’s macro builds the link and the joint and
takes the <sensor> as a block:
<xacro:macro name="sensor_link" params="sensor_name frame *sensor">
<link name="${sensor_name}_sensor">
<pose relative_to="${frame}">0 0 0 0 0 0</pose>
<inertial>
<mass>1e-5</mass>
<inertia>
<ixx>1e-8</ixx><ixy>0</ixy><ixz>0</ixz>
<iyy>1e-8</iyy><iyz>0</iyz><izz>1e-8</izz>
</inertia>
</inertial>
<xacro:insert_block name="sensor"/>
</link>
<joint name="${sensor_name}_sensor_joint" type="fixed">
<parent>base_link</parent>
<child>${sensor_name}_sensor</child>
</joint>
</xacro:macro>
The IMU is then:
<xacro:sensor_link sensor_name="imu" frame="imu_link">
<sensor name="imu" type="imu">
<frame_id>${name}/imu_link</frame_id>
<topic>${name}/imu</topic>
<update_rate>50</update_rate>
<always_on>true</always_on>
</sensor>
</xacro:sensor_link>
Why a separate link:
base_linkcomes from the URDF, and a merged include can’t add a sensor inside it.Why
frame_idcarries the name: messages then say they are inboat_a/imu_link, which is the framerobot_state_publisherpublishes for the instanceboat_a(Spawn and drive).
Add sensors covers the other sensor types.
Joint states
<plugin filename="gz-sim-joint-state-publisher-system"
name="gz::sim::systems::JointStatePublisher">
<topic>${name}/joint_states</topic>
<joint_name>motor_port_joint</joint_name>
<joint_name>motor_stbd_joint</joint_name>
</plugin>
The bridge turns this into /<name>/joint_states, which the instance’s
robot_state_publisher reads to spin the propellers in RViz.
Build it
The package looks like this:
kai_custom_vehicle/
├── CMakeLists.txt
├── package.xml
├── urdf/custom_usv.urdf.xacro, dimensions.xacro
├── models/custom_usv/model.sdf.xacro, model.config
├── config/ros_gz_bridge.yaml.in
├── rviz/custom_usv.rviz
├── launch/sim.launch.xml, rviz.launch.xml, display.launch.xml
├── scripts/instance_rviz.py
└── hooks/resource_paths.dsv.in, resource_paths.sh.in
CMakeLists.txt expands the URDF and the model for the default instance,
installs them with a rendered bridge config and model.config, and adds
the models directory to Gazebo’s resource path, so that model://custom_usv
resolves:
# Put the models directory on GZ_SIM_RESOURCE_PATH so model://custom_usv
# resolves. The model is deliberately not named after the package: Gazebo
# tries model:// names relative to the current directory first, and a source
# checkout has a directory called kai_custom_vehicle.
ament_environment_hooks("hooks/resource_paths.dsv.in")
ament_environment_hooks("hooks/resource_paths.sh.in")
# The default instance, model://custom_usv, for plain `gz sim` and for world
# files: the URDF and the model expanded with the default name, the bridge
# config rendered from its template, and model.config. kai_bringup's
# spawn_vehicle.launch.xml renders the same three sources for any other name.
set(model_dir models/custom_usv)
xacro_add_files(
urdf/custom_usv.urdf.xacro
${model_dir}/model.sdf.xacro
INSTALL DESTINATION ${model_dir})
set(name custom_usv)
configure_file(config/ros_gz_bridge.yaml.in
${CMAKE_CURRENT_BINARY_DIR}/ros_gz_bridge.yaml @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ros_gz_bridge.yaml
DESTINATION share/${PROJECT_NAME}/${model_dir})
install(DIRECTORY urdf models config rviz launch
DESTINATION share/${PROJECT_NAME})
install(PROGRAMS scripts/instance_rviz.py scripts/pinger_homing.py
DESTINATION lib/${PROJECT_NAME})
Any other instance is rendered on demand by the spawn launch, which runs the same xacros with another name and fills the same bridge template (Spawn and drive). You can run that step yourself:
ros2 run kai_bringup instantiate_vehicle.py --name boat_b --out-dir ~/boat_b \
--xacro $(ros2 pkg prefix --share kai_custom_vehicle)/models/custom_usv/model.sdf.xacro \
--bridge $(ros2 pkg prefix --share kai_custom_vehicle)/config/ros_gz_bridge.yaml.in \
--urdf $(ros2 pkg prefix --share kai_custom_vehicle)/urdf/custom_usv.urdf.xacro
That writes model.sdf, robot.urdf and ros_gz_bridge.yaml for boat_b.
Tip
Don’t name the model directory after the package. Gazebo first looks for
model:// names relative to the current directory, and a source checkout
contains a directory called kai_custom_vehicle. Hence models/custom_usv.
Marking from a URDF
If you would rather not keep a separate SDF file, the mark can travel in the
URDF, but only one way. Gazebo converts a URDF when it loads it, and the
conversion rebuilds every <collision> from the parsed geometry, so an
attribute on a URDF collision is lost, and so is one inside a
<gazebo reference="..."> block. What does come through unchanged is a
model-level <gazebo> block: everything inside it is copied into the
converted model as-is. Put the displacement link and its joint there, in SDF:
<robot name="my_boat" xmlns:gz="http://gazebosim.org/schema">
<link name="base_link"> ... contact shapes ... </link>
<gazebo>
<link name="hull_displacement">
<pose relative_to="base_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>
<collision name="pontoon_port" gz:buoyancy="true">
<pose>0 0.25 0.036 0 0 0</pose>
<geometry><box><size>1.0 0.15 0.15</size></box></geometry>
<surface><contact><collide_bitmask>0x00</collide_bitmask></contact></surface>
</collision>
</link>
<joint name="hull_displacement_joint" type="fixed">
<parent>base_link</parent>
<child>hull_displacement</child>
</joint>
</gazebo>
</robot>
The xmlns:gz declaration goes on <robot>. Thrusters, damping and sensors
can sit in the same block as <plugin> and <sensor> elements, and the file
spawns directly, with ros_gz_sim create -file my_boat.urdf or from the
robot_description topic. check_urdf and RViz ignore the block.
It is quicker to start with, but it ties the URDF to Gazebo, and the block is SDF inside a URDF, with no xacro sharing of the hull dimensions. The separate model file above scales better, which is why the custom USV uses it.
Next: Spawn and drive.