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:
- Disconnect your controller board from the computer, close QGroundControl
- 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
- Turn on your transmitter, make sure it is talking to the receiver
- 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”
- Wait for your controller board LED light to turn a steady breathing green, it means simulated GPS signals are captured
- Launch QGroundControl, it should automatically connect to the simulation
- Press the safety switch until the positive tone is played
- Now you can arm the vehicle by using the transmitter or the button in QGroundControl
- Fly like you would in real life
HI ,
Im having a problem on running HIL . the error im getting is Error: Could not find or load main class me.drton.jmavsim.Simulator
Please help
Hi salim,
Have you initialized and updated the submodules when you clone PX4 from github? Also, make sure you build the simulation first by using “ant”.
Hi Zihao;
Thanks a lot for the replay; i managed to make it work. the problem i had was using Linux on virtual machine. it would be great if you could help of how to edit the vehicle ( bespoke sizes and configuration) . thanks again
Hi salim,
As far as I know, from my own fiddling around, all the vehicle parameters are initialized in Simulator.java in src/me/drton/jmavsim. For example if you want to change the mass of the vehicle, just ctrl-f mass and you will see where they set the mass of the vehicle. In the same class you can also change the moments of inertia and drag etc.
Hope that helps.
Very useful thanks. Running pixracer with onboard pi3. succesfully running jmavsim in HITL config. Want to run pixracer on wifi link to avoid having separate power for pi and quad, while doing sim. Cat get jmavsim gui to work properly though. have tried starting jmavsim with udp port 14550, 14555, 14556, 14560. sim is plaing running as I can arm and see take off msgs in console (via serial on droneprobe port). But jmavsim gui shows no msgs are activity. Any thoughts? Thanks in advance.
Seth
Hi Seth,
This is a weird problem, I haven’t tried doing HIL with pixracer and wifi link myself so I am not sure what exactly is the problem. But I think it is very likely coming from the wifi link. Did you start jMAVSim using the commands in this article? Because that command assumes you have usb cable attached to the pixhawk and the port ttyACM0 is open. Try start the simulation again using with the usb cable attached to the pixracer to see if the problem goes away.
Hi! I was trying HITL with Pixhawk for the first time. I followed the steps you mentioned, but the led is blinking purple and qGC gives ‘Vehicle did not respond to mission item communication’ error. What might be wrong ?
Hi Ayesha,
I have experienced this problem myself. I found that whenever I build the current stable branch and upload for HITL, the purple lights will come up. Try do the HITL with master branch and see if the problem persists.
Hello, could you solve this? We have the same problem and can’t run the simulation, sometimes it takes off and crashes randomly
Were you using the virtual joysticks in QGroundControl?
Hi Ayesha and Zihao, have you managed to solve the problem? If so, could you tell me how? I’m using the master branch but I still have the purple flashing light.
Thanks a lot !
Hello Abel,
I could not fix the purple blinking lights. I tried different versions of qGC as well as the PX4 code but finally switched to pure software simulation as this was not worth the effort.
I also have the problem with the LED blinking purple. I found that if I use version 1.4.4 of the firmware, it works as described in this page. I opened an issue for this: https://github.com/PX4/Firmware/issues/6711
This issue has been fixed. Keep in mind that in addition to the changes in the code, jMAVSim has to be started with the additional option -r 250
Hi Gabe,
Thanks for raising the awareness of this issue, I have just tried HITL using the latest master branch, and it works fine. It was initially showing purple lights because of no RC signal, as soon as I opened up QGroundControl with the virtual joystick everything went back to normal.
Hi Zihao,
First of all, I strongly do cheer your research and I appreciate your research logs.
It really helps. Thank you.
I have few problems in steps of building new model in jMAVSim
I’m trying to carry on a octocopter simulation, but the rotor position setting is quite different from the Pixhawk manual
(referring http://ardupilot.org/copter/docs/connect-escs-and-motors.html#connect-escs-and-motors)
I might miss some important setting procedure
If you have any clue regarding this, I really appreciate it
Good luck for your lab life
I haven’t really touched the jMAVsim myself, you may have better luck asking this question on Github.
Hi, Awesome website. I am looking forward to read more of your articles. Build on! Thx.
In my way of understanding it, as you have a companion, it is his job to wait until the UAV has reached the position before giving the next setpoint. For your last question, you indeed need to set some non null speed otherwise there might be undesired behavior.
Hi, really cool&usefull tutorials there. they help to understand a bit better the PX4 architecture and how to add own user functions.
I would like to build my HITL. I would like to know if it is possible to build it within Gazebo. Is there a reason of using jMAVsim?
Thanks again for the site. Keep it up.
Hello,
Thanks for this tutorial!
I tried to do HITL with different stable versions of PX4. Before arming/takeoff, my led is blinking green. When I arm/takeoff or start a mission the drone takes off but crashes just after few seconds and I got the following message: no global position (sometimes no local position) and failsafe is enabled. Do you have any idea where this problem comes from?
Thank you