PX4 - Simulation

Introduction

Simulators allow PX4 flight code to control a computer modeled vehicle in a simulated environment, with the same level of software interactability as the real drone. PX4 supports both Software in the Loop (SITL) and Hardware in the Loop (HITL).

Simulators

Since the PX4 is an important component in the system's design, it is used as the starting point for this search. While simulation can take place with a wide variety of softwares and physics environments, it is best to start from a point where PX4 is supported out-of-the-box. PX4's dev documentation lists the following simulation options:

  1. Gazebo
  2. jMAVSim
  3. FlightGear
  4. Microsoft AirSim

Gazebo

Access documentation here.

jMAVSim

Access documentation here.

FlightGear

Access documentation here.

Microsoft AirSim

Access documentation here.

Software-in-the-Loop (SITL)

Access reference

Set-Up

To run the simulations, you need to first enter the PX4-Autopilot toolchain directory that was cloned. This is explained in this section.

cd ./PX4-Autopilot

Launch Simulation

From there, we can build the code for which ever simulation environment that we want as long as it exists within the build tags.

# For Gazebo Simulation
make px4_sitl gz_x500

# For jMAVSim simulation
make px4_sitl jmavsim

From here, in a separate terminal, if you run QGroundControl, you should be able to control the drone through that.

Low-Level Control

The out-of-the-box set-up for SITL does not provide access to PWM inputs into the actuators. This is because the simulation does not have an ESC model. As a result direct control of the simulated motors is not possible.

There is a repository that offers a way to achieve this low-level control: SaxionMechatronics/px4_offboard_lowlevel

In this code, they are using a commonly used "quadratic approximation" that maps RPM to PWM:

PWM=c2RPM2+c1RPM+c0

In this application:

# For the holybro x500
ros__parameters:
   uav_parameters:
       mass: 2.0                  # Kg
       arm_length: 0.25             # m
       num_of_arms: 4
       inertia:                     # Kg.m^2
           x: 0.08612
           y: 0.08962
           z: 0.16088
       moment_constant: 0.016     # m
       thrust_constant: 8.54858e-06   # N.s^2/rad^2
       max_rotor_speed: 1000         # rad/s
       gravity: 9.81                # m/s^2
       omega_to_pwm_coefficient:
           x_2: 0.001142
           x_1: 0.2273
           x_0: 914.2
       PWM_MIN: 1075
       PWM_MAX: 1950
       input_scaling: 1000
       zero_position_armed: 10

Hardware-in-the-Loop (HITL)

Access reference

Set-Up with Saluki V2

In order to connect via ethernet, need to provide the following details for eth0 connection:

IPv4 Method: Manual
Addresses:

The drone should now connect to /dev/ttyACM1 after rebooting drone. Can confirm in QGroundControl by connecting to that channel.

Launch Simulation

Then, to launch HITL, run:

# From px4-firmware root
# Confirmed to work with github.com/tiiuae/px4-firmware -b faulty-controller-hitl
./Tools/simulation/jmavsim/jmavsim_run.sh -q -s -d /dev/ttyACM1 -b 921600 -r 250

Access ROS 2 Topics

To launch the DDS-Agent for accessing ROS 2 Topics:

docker pull ghcr.io/tiiuae/tii-microxrce-agent:sha-9e67fa5
docker run -it --rm --network=host ghcr.io/tiiuae/tii-microxrce-agent:sha-9e67fa5

Also possible to run from local installation with open-source MicroXRCE-DDS Agent:

MicroXRCEAgent udp4 --port 2020 --send_port 2019 --refs ./agent.refs

Where agents.refs file is defined:

<profiles>
   <participant profile_name="default_xrce_participant">
       <domainId>0</domainId>
       <rtps>
           <name>default_xrce_participant</name>
       </rtps>
   </participant>
</profiles>

Containerised Deployment

It is possible to deploy the simulation in a containerised environment. The benefit of this is that you no longer need to build or install dependencies in the local machine.

Pre-requisites to this require you to clone the firmware directory:

git clone --recursive git@github.com:tiiuae/px4-firmware.git -b faulty-controller

To build and run the simulation in a container:

xhost +

docker run -it --privileged --rm \
   -v </path/to>/px4-firmware:/home/user/Firmware:rw \
   -v /tmp/.X11-unix:/tmp/.X11-unix:ro \
   -e DISPLAY=${DISPLAY} \
   -e LOCAL_USER_ID="$(id -u)" \
   -w /home/user/Firmware \
   --network=host  \
   --name=container_name px4io/px4-dev-simulation-jammy \
   "./Tools/simulation/sitl_docker_run.sh"

The DDS topics can be exposed to ros2 through the same MicroXRCEAgent from before as --network=host.