Multi-Vehicle Simulation

This tutorial explains how to simulate multi UAV in Gazebo. This is a software in the loop (SITL) simulation.

Note: This is tested only in Linux.

To test an example setup, follow the below steps,

  • clone the PX4/Firmware code, then build the SITL code
  • source your environment,

    1. export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$(pwd)
    2. export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$(pwd)/Tools/sitl_gazebo

For each simulated vehicle, the following is required,

  • Gazebo model: This is defined as xacro file in Firmware/Tools/sitl_gazebo/models/rotors_description/urdf/<model>_base.xacro see . Currently, the model xacro file is assumed to end with base.xacro. This model should have an argument called mavlink_udp_port which defines the UDP port on which gazebo will communicate with PX4 node. The model’s xacro file will be used to generate an sdf model that contains UDP port that you select. To define the UDP port, set the mavlink_udp_port in the launch file, see here.
    NOTE: If you are using the same vehicle model, you don’t need a separate xacro file for each vehicle. The same xacro file is adequate.

  • PX4 node: this is the SITL PX4 app. It communicates with the simulator, Gazebo, through the same UDP port defined in the Gazebo vehicle model, i.e. mavlink_udp_port. To set the UDP port on the PX4 SITL app side, you need to set the SITL_UDP_PRT parameter in the startup file, see . You also need to specify the path of the startup file, for each vehicle, in the launch file, see here. It is recommended that you set a unique mavlink ID for each vehicle in the startup file, see .

  • MAVROS node (optional): A seperate MAVROS node can be run in the launch file, see here, in order to connect to PX4 SITL app, if you want to control your vehcile through ROS. You need to start a mavlink stream on a unique set of ports in the startup file, see . Those unique set of ports should matched when you launch MAVROS node, see here.

  • loads a world in gazebo, see .
  • for each vehicle,

    • loads gazebo model and runs PX4 SITL app instance, runs

      1. <arg name="fcu_url" default="udp://:14540:14557"/>
      2. <arg name="gcs_url" value=""/>
      3. <arg name="tgt_system" value="1"/>
      4. <arg name="rcS1" default="$(find px4)/posix-configs/SITL/init/$(arg est)/$(arg vehicle)_1"/>
      5. <arg name="ID" value="1"/>
      6. <include file="$(find px4)/launch/single_vehcile_spawn.launch">
      7. <arg name="x" value="0"/>
      8. <arg name="y" value="0"/>
      9. <arg name="z" value="0"/>
      10. <arg name="R" value="0"/>
      11. <arg name="P" value="0"/>
      12. <arg name="Y" value="0"/>
      13. <arg name="vehicle" value="$(arg vehicle)"/>
      14. <arg name="rcS" value="$(arg rcS1)"/>
      15. <arg name="mavlink_udp_port" value="14560"/>
      16. <arg name="ID" value="$(arg ID)"/>
      17. </include>
    • runs a mavros node,

  • The complete block for each vehicle looks like the following,

    1. <!-- UAV2 iris_2 -->
    2. <group ns="uav2">
    3. <arg name="fcu_url" default="udp://:14541@localhost:14559"/>
    4. <arg name="gcs_url" value=""/>
    5. <arg name="tgt_system" value="2"/>
    6. <arg name="tgt_component" value="1"/>
    7. <arg name="rcS2" default="$(find px4)/posix-configs/SITL/init/$(arg est)/$(arg vehicle)_2"/>
    8. <arg name="x" value="1"/>
    9. <arg name="y" value="0"/>
    10. <arg name="z" value="0"/>
    11. <arg name="R" value="0"/>
    12. <arg name="P" value="0"/>
    13. <arg name="Y" value="0"/>
    14. <arg name="vehicle" value="$(arg vehicle)"/>
    15. <arg name="rcS" value="$(arg rcS2)"/>
    16. <arg name="mavlink_udp_port" value="14562"/>
    17. <arg name="ID" value="$(arg ID)"/>
    18. </include>
    19. <include file="$(find mavros)/launch/node.launch">
    20. <arg name="pluginlists_yaml" value="$(arg pluginlists_yaml)" />
    21. <arg name="config_yaml" value="$(arg config_yaml)" />
    22. <arg name="fcu_url" value="$(arg fcu_url)" />
    23. <arg name="gcs_url" value="$(arg gcs_url)" />
    24. <arg name="tgt_system" value="$(arg tgt_system)" />
    25. <arg name="tgt_component" value="$(arg tgt_component)" />
    26. </include>