Oct 1, 2024

Software in the Loop Testing for PX4

Software in the Loop Testing for PX4

Software-in-the-Loop (SITL) testing is a crucial component in the development and validation of drone systems, particularly those using the PX4 autopilot.

Software-in-the-Loop (SITL) testing is a crucial component in developing and validating drone systems, particularly those using the PX4 autopilot. This article explores the setup and configuration of SITL testing for PX4, providing insights into how to effectively simulate and test drone behavior without needing physical hardware.

In this month’s Drone Software Meetup, Godfrey Nolan, President of RIIS, went through the process of setting up SITL environments for PX4 drones. As usual, you can watch along in the video or follow the written tutorial. If you are looking for Linux-specific instructions, you will only find them in the written version.

Understanding PX4 and Its Ecosystem

PX4 is an open-source autopilot system widely used in the drone industry. It provides a flexible and powerful platform for controlling various types of unmanned vehicles, including multicopters, fixed-wing aircraft, and even ground robots.

Benefits of Using PX4 for Drone Projects

  1. Open-source nature allows for customization and community support

  2. Supports a wide range of vehicle types and configurations

  3. Extensive documentation and active development community

  4. Integration with popular ground control stations like QGroundControl

Introduction to SITL (Software in the Loop)

SITL testing allows developers to run the PX4 flight stack on their development computer without the need for actual drone hardware. This approach offers several advantages including rapid prototyping, a safe environment for testing risky maneuvers, simulating varied environmental conditions, and cost reduction.

Components of the SITL Setup

Our setup was completed on Windows, but it can be accomplished on Linux with some tweaks. To create a functional SITL environment for PX4, several key components are required:

PX4 Firmware: The core software that runs on the simulated flight controller.

  1. Simulator: In this case, we’re using jMAVSim and running it on top of the Windows Subsystem for Linux (WSL), though alternatives like Gazebo and AirSim exist.

  2. Ground Control Station: QGroundControl is the recommended option for PX4, but note we will not be running it on WSL.

  3. Development Environment: Windows Subsystem for Linux (WSL) is used to bridge the gap between Windows and Linux-based tools.

Installation Process

The installation process involves several steps:

  1. If on Windows open up Powershell and issue a wsl command.

  2. Clone the PX4 repository:

    git clone https://github.com/PX4/PX4-Autopilot.git --recursive
  3. Set up the Ubuntu environment within WSL:

    cd
    
    
  4. Install jMAVSim

    ./Tools/simulation/jmavsim_run.sh -I
  5. Restart after the script completes.

    1. Windows

      exit wsl --shutdown wsl
    2. Linux

      sudo reboot
  6. Navigate back to the PX4 directory:

    cd ~/PX4-Autopilot
  7. If on Windows find your IP because we will need to configure it later. The default PX4 configuration for SITL is usually sufficient for Linux setups.

    c:\ipconfig

Running the Simulation

Once the setup is complete, you can start the simulation:

  1. Navigate to the PX4 directory and run the below code.

    make px4_sitl jmavsim

    This will also take a few minutes 😁

  2. Wait for the “ready for takeoff” message in the console.

Connecting to QGroundControl.

  1. If on Windows we will need to configure the IP now by opening px4-rc.mavlink.

  2. Within the file you’ll see the line mavlink start -x -u $udp_gcs_port_local -r 40000000 -f in between the 40000000 and the f add a -t flag followed by your IP address.

  3. Run make px4_sitl jmavsim one more time

  4. Launch QGroundControl.

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

Troubleshooting Common Issues

As you work with SITL, you may encounter some common issues. Here are a few troubleshooting tips:

Network Configuration

If you’re having trouble connecting QGroundControl to your simulated drone, double-check your network configuration:

  1. Ensure that the IP address in your mavlink_router.conf file matches your current Windows IP.

  2. Check that no firewalls are blocking the communication between WSL and Windows.

  3. Try restarting both the simulation and QGroundControl.

Graphics Performance

If you’re experiencing lag or stuttering in the simulation:

  1. Update your graphics drivers to the latest version.

  2. Close unnecessary background applications to free up system resources.

  3. Consider using a less resource-intensive simulator like jMAVSim instead of Gazebo for basic testing.

WSL Compatibility

Some users may encounter issues specific to the Windows Subsystem for Linux:

  1. Ensure you’re using the latest version of WSL.

  2. If you’re having persistent problems, consider using a native Linux installation or a virtual machine for a more stable environment.

Custom Plugins and Modules

PX4 allows for the development of custom plugins and modules to extend its functionality. You can test these custom components in the SITL environment before deploying them to real hardware.

To create and test a custom module:

  1. Develop your module following PX4 coding standards and guidelines.

  2. Integrate your module into the PX4 build system.

  3. Run the SITL simulation with your custom module enabled.

  4. Use QGroundControl or custom scripts to interact with and validate your module’s functionality.

Conclusion

Software in the Loop testing is an invaluable tool for drone developers working with the PX4 autopilot system. By setting up a robust SITL environment, you can rapidly iterate on your designs, test complex scenarios, and validate your software without the risks associated with physical flight testing.

While the initial setup process may present some challenges, especially on Windows-based systems, the benefits of SITL far outweigh the setup complexities. As you become more proficient with SITL testing, you’ll find that it significantly accelerates your development cycle and improves the overall quality of your drone projects.

Remember that SITL is just one part of a comprehensive testing strategy. While it provides an excellent platform for early-stage development and testing, it’s essential to validate your results with real-world flight tests when appropriate. By combining SITL with other testing methodologies, you can create robust, reliable, and innovative drone solutions using the PX4 ecosystem.

To learn more about drone application development join the Drone Software Meetup group for monthly tutorials and networking.