PX4 Simulation

PX4 Gazebo Simulation in ROS

ROS (Robot Operating System) can be used with PX4 and simulate it in Gazebo simulator. it uses the MAVROS MAVLINK node to communicate with PX4.

The ROS/Gazebo integration with PX4 follows the pattern in the diagram below (this shows the generic PX4 simulation environment). PX4 communicates with the simulator (e.g. Gazebo) to receive sensor data from the simulated world and send motor and actuator values with the TCP communication. It communicates with the GCS and an Offboard API (e.g. ROS) to send telemetry from the simulated environment and receive commands.

1

Note

The only slight difference to “normal behavior” is that ROS initiates the connection on port 14557, while it is more typical for an offboard API to listen for connections on UDP port 14540.

Install ROS and Gazebo

I hope that all of you have already installed ROS and Gazebo correctly with the official guide. And the following commands will help you to install the MAVROS.

1
sudo apt-get install ros-noetic-mavros ros-noetic-mavros-extras
1
sudo /opt/ros/noetic/lib/mavros/install_geographiclib_datasets.sh

Launching ROS/Simulation

The command below can be used to launch the simulation and connect ROS to it via MAVROS, where fcu_url is the IP / port of the computer running the simulation:

1
roslaunch mavros px4.launch fcu_url:="udp://:14540@192.168.1.36:14557"

To connect to localhost, use this URL:

1
roslaunch mavros px4.launch fcu_url:="udp://:14540@127.0.0.1:14557"

Launching Gazebo with ROS Wrappers

The Gazebo simulation can be modified to integrate sensors publishing directly to ROS topics e.g. the Gazebo ROS laser plugin. To support this feature, Gazebo must be launched with the appropriate ROS wrappers.

There are ROS launch scripts available to run the simulation wrapped in ROS, to run SITL wrapped in ROS the ROS environment needs to be updated, the launch as usual:

1
2
3
4
5
6
cd <PX4-Autopilot_clone>
DONT_RUN=1 make px4_sitl_default gazebo
source Tools/setup_gazebo.bash $(pwd) $(pwd)/build/px4_sitl_default
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$(pwd)
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$(pwd)/Tools/sitl_gazebo
roslaunch px4 posix_sitl.launch

PX4 Simulation
http://mingshanhe.github.io/2022/05/06/ROS/PX4_1/
Author
Mingshan He
Posted on
May 6, 2022
Licensed under