PX4 SITL Simulation¶
PX4¶
PX4 is an OSS project which develops flight controllers and firmware for UAVs.
Setup Environment¶
The following instructions are based on Development Environment on Linux - PX4 Developer Guide. If you have any problems, please refer the instruction on the official website.
Setup¶
The user must be the member of dialout
group to communicate serial devices. To make user the member of dialout
, run the following command.
sudo usermod -a -G dialout $USER
After running the command, log out from the current user and log in again to enable group setting.
Installation¶
You can setup the environment for PX4 simulation with the script provided by the official repo. Firstly, download ubuntu_sim_ros_gazebo.sh.
You can’t install the ROS package with repository key in downloaded script. To install ROS package, fix the following line in ubuntu_sim_ros_gazebo.sh
.
sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116
The following command fixes the key for the repository.
sudo apt-key adv --keyserver 'hkp://ha.pool.sks-keyservers.net:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
Run the following command in the directory which contains ubuntu_sim_ros_gazebo.sh
.
source ubuntu_sim_ros_gazebo.sh
Packages are automatically installed with the command. ROS will be installed if ROS is not installed in your system. It will take a while to install ROS.
Update Gazebo installed by PX4 installation script. The following command is based on Install Gazebo using Ubuntu packages. Firstly, remove installed Gazebo.
sudo apt purge gazebo* libgazebo*
Then add setting to accept packages from packages.osrfoundation.org
.
sudo sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable `lsb_release -cs` main" > /etc/apt/sources.list.d/gazebo-stable.list'
Then add repository key.
wget https://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -
Finally, update Gazebo7.
sudo apt update
sudo apt install gazebo7
sudo apt install libgazebo7-dev
After running the script, PX4 firmware is downloaded into ~/src/Firmware
.
Simulation using Gazebo¶
To run Gazebo simulation, run the following command in ~/src/Firmware
.
make posix_sitl_default gazebo
It takes a while to start Gazebo if it is first time to run. The window like the following image appears. UAV is in the red rectangle.

There are other airframes available for simulation. You can find available airframes here.
Run the following command in the terminal of SITL simulator to get UAV take off.
commander takeoff

You can try the other commands. For the list of available commands, run help
command.
Simulation with Gazebo and ROS¶
mavros¶
We will use mavros to exchange messages between ROS nodes and vehicles in the simulator.
The vehicles in the simulator follows Mavlink protocol to communicate. MAVROS is the wrapper package of Mavlink protocol. You can read sensor measurements and send controll messages like take off, landing, etc. For the list of available services and topics, see here.

Communicate with vehicle via mavros¶
Let’s communicate with the vehicle in the simulator using mavros. Run the simulator by following the instruction described in Simulation using Gazebo .
After starting up the simulator, run mavros by the following command. See Appendix: fcu_url argument of mavros for the details of fcu_url parameter.
roslaunch mavros px4.launch fcu_url:="udp://:14540@127.0.0.1:14557"
After starting up mavros, run the following command in the other terminal.
rostopic list
You can see topics like /mavros/...
when mavros communicates successfully.
Let’s use services which mavros provides. You can start motors on the UAV by sending “value: true” message to /mavros/cmd/arming
service.
rosservice call /mavros/cmd/arming "value: true"
You can try other services such as taking off, landing, etc. Tapping TAB key twise after entering /mavros
gives you the list of available services. This feature is called “Tab Completion”.
Using tab completion after entering service name, you can get the template of required ROS message of the service. You need to change the values in the template of message, because they are set to default value. After starting up the motors by the command above, run the following command to get UAV take off to 5m from the ground.
rosservice call /mavros/cmd/takeoff "{min_pitch: 0.0, yaw: 0.0, latitude: 47.3977506, longitude: 8.5456074, altitude: 5}"

The command get UAV to the point of latitude 47.3977506, longitude 8.5456074, and attitude 5.
Launch simulator with ROS wrapper¶
You can run PX4 SITL simulator by roslaunch
command. The following launch file will run the simulator.
- posix_sitl.launch
Launch SITL Simulator
- mavros_posix_sitl.launch
Launch SITL and Mavros
Mavros, ROS node, and simulator communicate like the following in this case.

Setup¶
You need to set the environment variable to run simulator using these launch files. Firstly, navigate to ~/src/Firmware
.
cd ~/src/Firmware
If you didn’t do the procedure in Simulation using Gazebo , run the following command. The following command generates files required to run simulator. You need to run the following command in ~/src/Firmware
.
make posix_sitl_default gazebo
Then, run the scripts to configure settings. You don’t need to run the first line if ROS environment is already prepared.
source ~/catkin_ws/devel/setup.bash
source Tools/setup_gazebo.bash $(pwd) $(pwd)/build/posix_sitl_default
Then, set environment variables.
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$(pwd)
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$(pwd)/Tools/sitl_gazebo
You need to run the commands everytime you run the simulator. Instead of running the command, you can write the following commands to ~/.bashrc
to run commands automatically.
source $HOME/src/Firmware/Tools/setup_gazebo.bash $HOME/src/Firmware $HOME/src/Firmware/build/posix_sitl_default > /dev/null
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$HOME/src/Firmware
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$HOME/src/Firmware/Tools/sitl_gazebo
Run simulator¶
The following command activates simulator
roslaunch px4 posix_sitl.launch
Use mavros_posix_sitl.launch
to start mavros with simulator.
roslaunch px4 mavros_posix_sitl.launch
By the following command, you can use airframes listed in Gazebo Simulation - PX4 Developer Guide.
roslaunch px4 mavros_posix_sitl.launch vehicle:=vehicle_name
Appendix: fcu_url argument of mavros¶
roslaunch command¶
In ROS, you can set values to variables in launch file in the following way.
roslaunch package file.launch arg:=value
The following is the content of px4.launch
. You can set the value of fcu_url
with the command like roslaunch mavros px4.launch fcu_url:="udp://:14540@127.0.0.1:14557"
.
<launch>
<!-- vim: set ft=xml noet : -->
<!-- example launch script for PX4 based FCU's -->
<arg name="fcu_url" default="/dev/ttyACM0:57600" />
<arg name="gcs_url" default="" />
<arg name="tgt_system" default="1" />
<arg name="tgt_component" default="1" />
<arg name="log_output" default="screen" />
<arg name="fcu_protocol" default="v2.0" />
<arg name="respawn_mavros" default="false" />
<include file="$(find mavros)/launch/node.launch">
<arg name="pluginlists_yaml" value="$(find mavros)/launch/px4_pluginlists.yaml" />
<arg name="config_yaml" value="$(find mavros)/launch/px4_config.yaml" />
<arg name="fcu_url" value="$(arg fcu_url)" />
<arg name="gcs_url" value="$(arg gcs_url)" />
<arg name="tgt_system" value="$(arg tgt_system)" />
<arg name="tgt_component" value="$(arg tgt_component)" />
<arg name="log_output" value="$(arg log_output)" />
<arg name="fcu_protocol" value="$(arg fcu_protocol)" />
<arg name="respawn_mavros" default="$(arg respawn_mavros)" />
</include>
</launch>
fcu_url¶
fcu_url
is the address of flight controller (FCU). If you use serial port such as USB connection for FCU connection, the address of FCU will be like /dev/ttyACM0
. The address slightly changes depending on the environment. The number after colon is baudrate.
In case of simulation, we use UDP for communication. Set fcu_url like the following when using UDP.
udp://[bind_host][:port]@[remote_host][:port]
bind_host
defines IP address of FCU. By default, it isINADDR_ANY(0.0.0.0)
and mavros receives data from any IP adress.port
is listening port of ROS node and it is14540
by default.remote_host
is IP address of remote PC. In case of simulation, setlocalhost
or127.0.0.1
.The next
port
is the port of FCU. It is14557
by default.
fcu_url
will be like the following by assigning the values above.
udp://:14540@127.0.0.1:14557
For port settings, see ROS with Gazebo Simulation - PX4 Developer Guide. See mavros - ROS Wiki for the detailes of fcu_url
.
References¶
- PX4 Developer Guide
Developer wiki of PX4. You can find basic knowledge here
- The following instructions are based on Development Environment on Linux - PX4 Developer Guide. If you have any problems, please refer the instruction on the official website.
Installation Guide of PX4 Firmware for Linux
- By the following command, you can use airframes listed in Gazebo Simulation - PX4 Developer Guide.
Simulation with Gazebo
- ROS with Gazebo Simulation
Simulation with ROS and Gazebo
- Linuxシステムの仕組みと運用、管理
About serial devices
- Understanding ROS Services and Parameters
About ROS Service