Oct 29, 2024

Harware in the Loop Testing for PX4

Harware in the Loop Testing for PX4

Hardware-in-the-Loop testing for PX4 drones: Setup guide for realistic simulations using flight controllers, QGroundControl, + simulators like jMAVSim + Gazebo.

Introduction

Hardware-in-the-Loop (HITL) testing is a crucial step in developing and validating autonomous drone systems. This article explores the process of setting up and running HITL simulations for PX4-powered drones, providing valuable insights into configuring hardware, running simulations, and troubleshooting common issues. By the end of this guide, you’ll have a solid understanding of how to enhance your drone testing capabilities using HITL.

Understanding HITL and Its Importance

Hardware-in-the-Loop testing differs from Software-in-the-Loop (SITL) simulations in that it incorporates real flight controller hardware into the simulation environment. This approach offers several advantages:

  1. Testing actual flight code on real hardware

  2. Validating hardware-specific behaviors and interactions

  3. Identifying potential issues that may not be apparent in pure software simulations

HITL testing bridges the gap between software simulations and real-world flight tests, providing a more comprehensive validation of your drone system.

Required Hardware and Setup Process

To get started with HITL testing for PX4, you’ll need the following components:

  • A compatible flight controller (e.g., Cube Orange, Pixhawk 4, or Holybro Kakute F7)

    • We’re going to use the Cube Orange in this tutorial.

  • A computer running Ubuntu Linux (recommended for optimal performance)

  • USB cable for connecting the flight controller to the computer

  • PX4 Autopilot firmare

  • QGroundControl ground control station software

  • A simulator (jMAVSim or Gazebo Classic)

The setup process involves several key steps:

  1. Installing PX4 Autopilot software

  2. Configuring the flight controller for HITL mode

  3. Setting up QGroundControl

  4. Preparing the simulation environment

Let’s dive into each of these steps in more detail.

Installing and Configuring PX4 Autopilot

To ensure stability and compatibility, it’s recommended to use the latest stable release of PX4 Autopilot. We’re going to be using stable release 1.15 in this tutorial. Here’s how to set it up:

  1. Clone the PX4 Autopilot repository:

    git clone <https://github.com/PX4/PX4-Autopilot.git>

  2. Navigate to the PX4-Autopilot directory:

    cd PX4-Autopilot

  3. Checkout the latest stable release:

    git checkout <COMMIT-HASH>

  4. Update submodules:

    git submodule update --init --recursive

  5. Edit the default board configuration file for your specific flight controller. For example, for the Cube Orange:

    nano boards/cubepilot/cubeorange/default.px4board

  6. Add the following line to enable HITL simulation:

    CONFIG_MODULE_SIMULATION_PWM_OUT_SIM=y

  7. Compile the PX4 firmware for your board:

    make cubepilot_cubeorange

  8. Now that all of that is done, we can start uploading the firmware to our flight controller using QGroundControl.

Configuring QGroundControl for HITL

QGroundControl (QGC) is the ground control station software used for interacting with PX4-powered drones. To set up QGC for HITL:

  1. Connect your flight controller to your computer via USB.

  2. Launch QGroundControl.

  3. Go to “Vehicle Setup” > “Safety” section.

  4. Enable HITL mode by selecting “Enabled” from the HITL Enabled list.

  5. Go to “Vehicle Setup” > “Airframe” and select the appropriate airframe (e.g., quadcopter).

  6. Apply changes and reboot the flight controller.

  7. In QGC settings, uncheck all auto-connect options except for UDP.

Setting Up the Simulation Environment

There are multiple simulation environments that are compatible with PX4 projects, but we’re going to walk you through two possible options today, jMavSim and Gazebo. First, we’ll focus on using jMAVSim as our simulator and you can jump ahead if you are interested in using Gazebo.

To run jMAVSim with HITL:

  1. Open a terminal in the PX4-Autopilot directory.

  2. Run the following command:

    ./Tools/simulation/jmavsim/jmavsim_run.sh -d /dev/ttyACM0 -b 921600 -r 250


    Note: The device path (/dev/ttyACM0) may vary depending on your system. Use sudo dmesg | grep tty to find the correct device path.

  3. Once jMAVSim is running, open QGroundControl. It should automatically connect to the simulated drone.

Running Your First HITL Simulation

With everything set up, you’re ready to run your first HITL simulation:

  1. In QGroundControl, arm the drone by clicking the “Arm” button.

  2. Use the controls in QGC to takeoff, fly, and land the simulated drone.

  3. Monitor the drone’s behavior and telemetry data in both QGC and jMAVSim.

During the simulation, you can test various flight modes, mission planning, and even simulate different environmental conditions to validate your drone’s performance.

Setting Up PX4 and Gazebo Simulation for Flight Controllers

While we previously discussed using jMAVSim, Gazebo is another powerful simulation tool that can be used for HITL testing with PX4. Gazebo offers more advanced simulation capabilities, including better graphics and more realistic physics.

First thing we’re going to need to do is install our dependencies:

sudo apt remove gz-harmonic
sudo apt install aptitude
sudo aptitude install gazebo libgazebo11 libgazebo-dev

Then we need to compile Gazebo classic:

DONT_RUN=1 make px4_sitl_default gazebo-classic

Then we want to find our vehicle models sdf file. Like we noted before, you can sudo dmesg | grep tty to find the correct device path, and if it it’s not ttyACM0, you’ll have to go to your iris_hitl.sdf file and edit to change it to the correct path.

Next, open up a new terminal and run the following setup tool:

source Tools/simulation/gazebo-classic/setup_gazebo.bash $(pwd) $(pwd)/build/px4_sitl_default

Then finally to run Gazebo

gazebo Tools/simulation/gazebo-classic/sitl_gazebo-classic/worlds/hitl_iris.world

With Gazebo running, you can now connect to the simulated drone using QGroundControl, just as you would with jMAVSim.

Demonstration of UAV Control Software and Flight Simulation

Now that we have our HITL environment set up, let’s demonstrate how to control the simulated UAV using QGroundControl.

  1. In QGroundControl, you should see the simulated drone connected and ready for flight.

  2. Use the “Arm” button to arm the drone. You’ll notice the propellers start spinning in the simulation.

  3. Use the joystick or on-screen controls to take off and maneuver the drone.

  4. Try out different flight modes, such as Position Hold, Altitude Hold, or Mission mode.

  5. Create a simple mission by adding waypoints on the map in QGroundControl.

  6. Execute the mission and observe how the drone behaves in the simulation.

Throughout this process, pay attention to how the simulated drone responds to your inputs and how closely it mirrors real-world behavior. This is where the power of HITL testing becomes apparent, as you can test complex scenarios and edge cases without risking real hardware.

Lessons Learned and Future Plans

As we wrap up this demonstration, let’s reflect on some key lessons learned from HITL testing:

  1. HITL provides a more accurate representation of real-world behavior compared to pure software simulations.

  2. It allows for testing of hardware-specific features and interactions that may not be apparent in SITL.

  3. HITL can help identify potential issues before real-world flight tests, saving time and reducing risks.

Looking ahead, there are several exciting developments in the world of drone simulation and testing:

  1. Integration of more advanced sensors and payloads into HITL simulations.

  2. Improved physics models for even more realistic simulations.

  3. Better integration with ROS (Robot Operating System) for more complex autonomous behaviors.

Conclusion

Hardware-in-the-Loop testing is a crucial step in the development and validation of autonomous drone systems. By following the setup process and demonstration outlined in this article, you now have the knowledge to implement HITL testing in your own PX4-based drone projects.

HITL bridges the gap between software simulations and real-world flight tests, providing a safe and controlled environment to validate your drone’s behavior. As drone technology continues to advance, HITL testing will play an increasingly important role in ensuring the safety and reliability of autonomous systems.

Remember that while HITL is a powerful tool, it’s just one part of a comprehensive testing strategy. Combine HITL with Software-in-the-Loop simulations and real-world flight tests to achieve the most robust and reliable drone systems possible.

As you continue your journey in drone development, keep exploring new tools, techniques, and technologies. The field of autonomous systems is rapidly evolving, and staying up-to-date with the latest advancements will help you create more capable and innovative drone solutions.