An Engineer's Blog

Back

PX4 SITL in Gazebo, flown over MAVROSBlur image

Overview

Step one of the bachelor thesis drone I am building with my teammates, simulation first. Before you crash a drone you built yourself, crash it in software. Software in the loop (SITL) lets you do that for free, as many times as you want.

The video is the whole stack on my machine: PX4 simulated in Gazebo, flown in offboard mode through MAVROS. The flight is short; getting a fake drone airborne takes a surprising number of pieces.

Offboard control of a simulated iris quadcopter: arm, take off, fly, land, all from ROS.

The stack

Four pieces are running at once, each with its own terminal pane in the video:

PieceWhat it isWhy it is here
Gazebo3D physics simulatorRenders the world and simulates physics for the iris quadcopter model that ships with PX4
PX4 (SITL)The flight stack, compiled for software in the loopThe real autopilot code, but fed by simulated sensors instead of a real IMU and GPS
ROS + MAVROSThe ROS middleware and its MAVLink bridgeLets ROS nodes talk to the autopilot: read state, change modes, send setpoints
mavros_controllersOffboard controller nodesThe setpoint generators that fly the drone

I run my own forks of both: PX4-Autopilot ↗ (upstream is PX4/PX4-Autopilot ↗) and mavros_controllers ↗, forked from Jaeyoung-Lim’s version ↗. The code matches upstream for now; our own changes will land in the forks.

MAVLink is the binary protocol almost every autopilot speaks, a stream of numbered messages: heartbeats, armed/mode status, telemetry (position, attitude, battery), commands like “arm” or “return to launch”, and setpoints (“be at this position, with this velocity”).

MAVROS bridges MAVLink and ROS: a node that turns each message into a topic and each command into a service. /mavros/state shows armed and mode, /mavros/local_position/pose streams the estimated position, /mavros/setpoint_position/local accepts position targets, /mavros/cmd/arming arms the motors. On a real drone MAVROS runs on a companion computer wired to the flight controller over serial. Simulation uses the same node over a UDP port instead of a wire:

roslaunch mavros px4.launch fcu_url:="udp://:14540@localhost:14557"
sh

Port 14557 is where the SITL instance listens for companion connections; 14540 is the standard SDK port PX4 streams telemetry back to. The protocol and node are identical on hardware, so everything written against MAVROS keeps working when the drone becomes real.

The walkthrough

First, bring up the simulated autopilot and its world:

make px4_sitl gazebo
sh

PX4 boots, runs its startup scripts, starts the state estimator, and opens Gazebo with the iris quadcopter on the ground. It is a complete autopilot with imaginary sensors; nothing flies yet.

Second, connect MAVROS with the fcu_url above. The console fills with link and parameter traffic; the autopilot is now ordinary ROS topics.

Third, check the state and take over:

rostopic echo /mavros/state
rosrun mavros mavsys mode -c OFFBOARD
rosrun mavros mavsafety arm
sh

From there the flight stack stops waiting for a pilot. The offboard node publishes position targets, MAVROS translates them into MAVLink setpoints, PX4 turns them into motor commands, and Gazebo moves the model.

Finally, rqt_graph shows who talks to whom: the controller feeding /mavros/setpoint_position/local, MAVROS in the middle, everything flowing out through the single UDP link.

Team clips: hardware on the bench

While the simulation ran on laptops, the hardware side was already on a bench. We kept two short clips, in Vietnamese, from those first bench days.

The first clip is the motor test: four Emax 2300 kV motors on BLHeli 30A ESCs, powered through a Matek Mini PowerHub distribution board. Before any of them meet a frame or a flight controller, we spin each one on the bench to check direction, throttle response, and that it does not try to eat anything.

Motors on the bench: Emax 2300 kV, BLHeli 30A ESCs, Matek Mini PowerHub.

The second is less glamorous and more important: charging the 4S pack from a 220V wall socket with a charger, instead of whatever improvised arrangement we would have invented otherwise.

Charging the 4S pack from the wall, the boring and correct way.

What is next

Next in the series is autonomous indoor mapping without GPS, where this stack learns to see; after that, human-in-the-loop flying and the design work for the real airframe.

PX4 SITL in Gazebo, flown over MAVROS
https://tin.ng/blog/2020-06-26--px4-sitl-gazebo-mavros-offboard
Author Tin Nguyen
Published at June 26, 2020
Comment seems to stuck. Try to refresh?✨