Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix namespacing for multiple instances of gazebo_ros2_control plugin #181

Merged
merged 13 commits into from
Dec 23, 2024
Merged
32 changes: 32 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,38 @@ The following is a basic configuration of the controllers:
.. literalinclude:: ../gazebo_ros2_control_demos/config/cart_controller.yaml
:language: yaml


Multiple Namespaces
-----------------------------------------------------------
The gazebo_ros2_control plugin can be launched in multiple namespaces. This is useful if it is desired to have multiple robots of the same description running at the same time.
If the namespace is to be defined in the URDF file, then you can use the ``<namespace>`` tag as detailed below:

.. code-block:: xml

<gazebo>
<plugin name="gazebo_ros2_control" filename="libgazebo_ros2_control.so">
<parameters> $(arg control_yaml) </parameters>
<ros>
<namespace>r1</namespace>
<remapping>/tf:=tf</remapping>
<remapping>/tf_static:=tf_static</remapping>
<remapping>diffdrive_controller/odom:=odom</remapping>
</ros>
</plugin>
</gazebo>

Where ``r1`` is the namespace that the plugin nodes will be run in. Please note that the remapping of ``/tf`` and ``/tf_static`` is recommended only if you want to have a separate TF buffer for each namespace, e.g ``/r1/tf``

A second method for launching in namespaces is by using the Gazebo ``spawn_entity.py`` tool and setting the ``-robot_namespace`` parameter to the script:

.. code-block:: bash

ros2 run gazebo_ros spawn_entity.py -entity robot_1 -robot_namespace r1 -topic robot_description

The script calls the ``spawn_entity`` service to spawn an entity named ``robot_1`` in the namespace ``r1``. It is assumed that a robot description is being published by a ``robot_state_publisher`` node on the topic ``robot_description``, this will also be used for the ``gazebo_ros2_control`` plugin.

The ``spawn_entity.py`` script will rewrite the robot description to include a namespace for every plugin tag. Please note that this method will apply a namespace to every plugin in the robot description. It will also overwrite any existing namespaces set in the description file.

gazebo_ros2_control_demos
==========================================

Expand Down
14 changes: 0 additions & 14 deletions gazebo_ros2_control/src/gazebo_ros2_control_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,24 +233,10 @@ void GazeboRosControlPlugin::Load(gazebo::physics::ModelPtr parent, sdf::Element

if (sdf->HasElement("ros")) {
sdf = sdf->GetElement("ros");

// Set namespace if tag is present
if (sdf->HasElement("namespace")) {
std::string ns = sdf->GetElement("namespace")->Get<std::string>();
// prevent exception: namespace must be absolute, it must lead with a '/'
if (ns.empty() || ns[0] != '/') {
ns = '/' + ns;
}
std::string ns_arg = std::string("__ns:=") + ns;
arguments.push_back(RCL_REMAP_FLAG);
arguments.push_back(ns_arg);
}

// Get list of remapping rules from SDF
if (sdf->HasElement("remapping")) {
sdf::ElementPtr argument_sdf = sdf->GetElement("remapping");

arguments.push_back(RCL_ROS_ARGS_FLAG);
while (argument_sdf) {
std::string argument = argument_sdf->Get<std::string>();
arguments.push_back(RCL_REMAP_FLAG);
Expand Down