0
$\begingroup$

Rosanswers logo

Hi all, I'm trying to run multiple independent Gazebo-ROS instances on the same machine so two or more remotely connected users can use it at the same time. I've read this answer about multiple ros masters and this one about multiple Gazebo instances.I've been following them with no success.

I've tried running two ros masters on different ports and launch a gazebo empty_world.launch on each one with different group namespaces as follows:

terminal 1: Run the first ros master: roscore -p 11312

terminal 2: Run test_A.launch: Where test_A.launch is the same as gazebo_ros empty_world.launch but adding a group ns="ns_A" and changing the node names as follows:

<launch>

  <!-- these are the arguments you can pass this launch file, for example paused:=true -->
  <arg name="paused" default="false"/>
  <arg name="use_sim_time" default="true"/>
  <arg name="gui" default="true"/>
  <arg name="headless" default="false"/>
  <arg name="debug" default="false"/>
  <arg name="verbose" default="false"/>
  <arg name="world_name" default="worlds/empty.world"/> <!-- Note: the world_name is with respect to GAZEBO_RESOURCE_PATH environmental variable -->

  <!-- set use_sim_time flag -->
  <group if="$(arg use_sim_time)">
    <param name="/use_sim_time" value="true" />
  </group>

  <!-- set command arguments -->
  <arg unless="$(arg paused)" name="command_arg1" value=""/>
  <arg     if="$(arg paused)" name="command_arg1" value="-u"/>
  <arg unless="$(arg headless)" name="command_arg2" value=""/>
  <arg     if="$(arg headless)" name="command_arg2" value="-r"/>
  <arg unless="$(arg verbose)" name="command_arg3" value=""/>
  <arg     if="$(arg verbose)" name="command_arg3" value="--verbose"/>
  <arg unless="$(arg debug)" name="script_type" value="gzserver"/>
  <arg     if="$(arg debug)" name="script_type" value="debug"/>

    <group ns="ns_A">
        
      <!-- start gazebo server-->
      <node name="gazebo_A" pkg="gazebo_ros" type="gzserver" respawn="false" output="screen"
        args="$(arg command_arg1) $(arg command_arg2) $(arg command_arg3) $(arg world_name)" />
        
      <!-- start gazebo client -->
      <node name="gazebo_gui_A" pkg="gazebo_ros" type="gzclient" respawn="false" output="screen"></node>

    </group>

</launch>

terminal 3: Run the second ros master: roscore -p 11313

terminal 4: Run test_B.launch: Where test_B.launch is the same as gazebo_ros empty_world.launch but adding a group ns="ns_B" and changing the node names as follows:

<launch>

  <!-- these are the arguments you can pass this launch file, for example paused:=true -->
  <arg name="paused" default="false"/>
  <arg name="use_sim_time" default="true"/>
  <arg name="gui" default="true"/>
  <arg name="headless" default="false"/>
  <arg name="debug" default="false"/>
  <arg name="verbose" default="false"/>
  <arg name="world_name" default="worlds/empty.world"/> <!-- Note: the world_name is with respect to GAZEBO_RESOURCE_PATH environmental variable -->

  <!-- set use_sim_time flag -->
  <group if="$(arg use_sim_time)">
    <param name="/use_sim_time" value="true" />
  </group>

  <!-- set command arguments -->
  <arg unless="$(arg paused)" name="command_arg1" value=""/>
  <arg     if="$(arg paused)" name="command_arg1" value="-u"/>
  <arg unless="$(arg headless)" name="command_arg2" value=""/>
  <arg     if="$(arg headless)" name="command_arg2" value="-r"/>
  <arg unless="$(arg verbose)" name="command_arg3" value=""/>
  <arg     if="$(arg verbose)" name="command_arg3" value="--verbose"/>
  <arg unless="$(arg debug)" name="script_type" value="gzserver"/>
  <arg     if="$(arg debug)" name="script_type" value="debug"/>

    <group ns="ns_B">
        
      <!-- start gazebo server-->
      <node name="gazebo_B" pkg="gazebo_ros" type="gzserver" respawn="false" output="screen"
        args="$(arg command_arg1) $(arg command_arg2) $(arg command_arg3) $(arg world_name)" />
        
      <!-- start gazebo client -->
      <node name="gazebo_gui_B" pkg="gazebo_ros" type="gzclient" respawn="false" output="screen"></node>

    </group>

</launch>

When I execute all this I get one instance of Gazebo running, in which I insert a box. Then, when I launch the second instance (test_B.launch) I get a new Gazebo window in which I see the box inserted on the first. In other words, as I understand it, I got another client connected to the same server, even running from different ros masters. This is the exception the second instance trows:

Exception [Master.cc:50] Unable to start server[Address already in use]. There is probably another Gazebo process running.

¿Has anyone been able to do this? ¿Can you spot any error? I'm using ROS Hydro on Ubuntu 12.04 LTS.

Thank you in advance for your help.


Originally posted by Sergio MP on ROS Answers with karma: 182 on 2014-09-19

Post score: 3

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Ok. I finally managed to solve this. I can run now multiple instances of rosmaster and Gazebo!

Since the envioremental variables are "local" to each shell (terminal instance), you need to be careful with this. First, for running different rosmaster instances you need to change the ROS_MASTER_URI enviorement variable for each one. You also need to change the GAZEBO_MASTER_URI for each INDEPENDENT Gazebo instance you want to run. I tried it, and at first it didn't work. That's because according to THIS, you have to edit your /usr/share/gazebo/setup.sh and replace

export GAZEBO_MASTER_URI=http://localhost:11345

for

export GAZEBO_MASTER_URI=${GAZEBO_MASTER_URI:-"http://localhost:11345"}

Then save. If you dont do this change, the GAZEBO_MASTER_URI won't chance properly!!

Now, for running different instances of rosmaster:

First shell:

export ROS_MASTER_URI=http://localhost:11315 11315 is just an example. The default is 11311.
roscore -p 11315 or roslaunch some package. It'll run on the specified port ( localhost:11315 )

Second shell:

export ROS_MASTER_URI=http://localhost:11316 A different one from before!
roscore -p 11316 or roslaunch some package. It'll run on the specified port ( localhost:11316 )

Now, for running multiple Gazebo instances

Third shell:

export GAZEBO_MASTER_URI=http://localhost:11355 11355 is just an example. The default is 11345.

gazebo

Fourth shell:

export GAZEBO_MASTER_URI=http://localhost:11356 A different one from before!

gazebo

And voilà!


Originally posted by Sergio MP with karma: 182 on 2014-09-24

This answer was ACCEPTED on the original site

Post score: 6


Original comments

Comment by RainMan on 2018-03-05:
Hello sir, By following your description I am now able to open multiple gazebo instances. But since we are using multiple master node, what is the most effective way to subscribe/publish topics for different gazebo instances?

Comment by alidemir on 2018-05-25:
you can check this repo: https://github.com/alidemir1/RosGazeboParallelSimulations

Comment by shonigmann on 2022-03-31:
Adding an update to this, as a number of people have still had issues with the Gazebo client's using the wrong scene. To fix this, as mentioned here and here, you need to set a unique IGN_PARTITION environment variable value. More information here.

Comment by retinfai on 2023-02-16:
For Gazebo Garden the environment variables to change is GZ_PARTITION, and for ROS2 Humble, it is ROS_DOMAIN_ID

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.