In most cases, Software In the Loop (SITL) is sufficient for testing your flight controller code. But in other cases, you may want to use Hardware In the Loop (HITL) simulation to validate that your code can actually run on a controller board.

The HITL differs from SITL in a few ways:

  • All sensor raw datas are generated by the computer, but they are fed to the controller and all estimations, control commands etc. are calculated using the controller board processor
  • You can use an actual transmitter and receiver pair to send manual control signals
  • You will see the LED light status as if it is in real flight
  • You will need to operate the safety switch (although you can disable it from parameters)

1. Preparation, Equipment

For HITL using jMAVSim, you need a similar setup as the SITL. Follow these guides to install QGroundControl Daily build, PX4 development environment etc.

Although daily build of QGroundControl is optional, make sure you have another version of the QGroundControl on your computer.

This version of the HITL is tested on HKPilot 32 and Pixfalcon. If you are using Pixfalcon, you need to connect your receiver. If you are using HKPilot 32, you will need to connect the safety switch and buzzer as well.

2. Build jMAVSim

Since the PX4 team has included jMAVSim as a submodule in the PX4/Firmware repository, as you clone PX4 you have also cloned jMAVSim. In the Firmware directory, make sure you have initialized and updated all the submodules first, by using “git submodule update –init –recursive” command. To build jMAVSim you will need to use ant. “sudo apt-get install ant” first if you haven’t got ant. Now change directory to “Firmware/Tools/jMAVSim” and enter “ant” in the terminal. You should see “BUILD SUCCESSFUL” in your terminal window.

3. Setup HIL Airframe

For HITL, you need to trick the controller board into believing it has all the sensors calibrated and it is receiving the sensor readings. To do that open QGroundControl, connect your controller board and change your airframe type to “HILQuadcopter X”. There are two ways to this:

  • Click cog wheels, then click Airframe, in the Simulation block, select “HIL Quadcopter X” from the drop down list
  • Click cog wheels, then click Parameters, choose System from the list of parameter groups, change the value of “SYS_AUTOSTART” to 1001.

Both methods require a reboot of the controller board.

Next, you will need to calibrate the radio if you are using real receiver and transmitter. Alternatively, you can also use a joystick if you have QGroundControl daily build. But you can’t use both at the same time.

You will also need to fix the number of battery cells in the Power tab under the cog wheels, if you are not sure just use 3. You should also assign the flight modes you want.

4. Start Simulation

You must do these steps in the correct order for the simulation to work properly:

  1. Disconnect your controller board from the computer, close QGroundControl
  2. Connect your controller board to the computer, wait for the LED light to turn into a steady breathing blue, it means the start-up procedure is finished
  3. Turn on your transmitter, make sure it is talking to the receiver
  4. Open a terminal, change directory to “Firmware/Tools/jMAVSim” and enter the following commands “java -Djava.ext.dirs= -cp lib/*:out/production/jmavsim.jar me.drton.jmavsim.Simulator -serial /dev/ttyACM0 921600 -qgc”
  5. Wait for your controller board LED light to turn a steady breathing green, it means simulated GPS signals are captured
  6. Launch QGroundControl, it should automatically connect to the simulation
  7. Press the safety switch until the positive tone is played
  8. Now you can arm the vehicle by using the transmitter or the button in QGroundControl
  9. Fly like you would in real life