You are here:-, PX4-PX4 Research Log [8] – Hardware In the Loop (HITL) Simulation using jMAVsim on Ubuntu 14.04

PX4 Research Log [8] – Hardware In the Loop (HITL) Simulation using jMAVsim on Ubuntu 14.04

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
By |2017-05-22T05:52:57+00:00August 31st, 2016|Categories: Flight Control, PX4|21 Comments

About the Author:

21 Comments

  1. salim September 13, 2016 at 4:09 pm - Reply

    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

    • zihao September 14, 2016 at 12:32 am - Reply

      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”.

  2. salim September 16, 2016 at 7:36 pm - Reply

    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

    • zihao September 19, 2016 at 12:29 am - Reply

      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.

  3. Seth Neiman November 4, 2016 at 8:58 pm - Reply

    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

    • zihao November 7, 2016 at 1:28 am - Reply

      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.

  4. Ayesha January 2, 2017 at 1:49 pm - Reply

    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 ?

    • zihao January 2, 2017 at 11:32 pm - Reply

      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.

    • Damian February 2, 2017 at 12:41 am - Reply

      Hello, could you solve this? We have the same problem and can’t run the simulation, sometimes it takes off and crashes randomly

      • zihao February 7, 2017 at 1:08 am - Reply

        Were you using the virtual joysticks in QGroundControl?

    • Abel February 9, 2017 at 7:02 pm - Reply

      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 !

      • Ayesha February 17, 2017 at 1:15 pm - Reply

        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.

    • Gabe March 2, 2017 at 7:03 pm - Reply

      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

      • Gabe March 8, 2017 at 2:49 pm - Reply

        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

      • zihao March 9, 2017 at 2:05 am - Reply

        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.

  5. Junyoung Han March 23, 2017 at 7:53 pm - Reply

    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

    • zihao April 1, 2017 at 5:18 am - Reply

      I haven’t really touched the jMAVsim myself, you may have better luck asking this question on Github.

  6. Abel Retana April 27, 2017 at 10:07 pm - Reply

    Hi, Awesome website. I am looking forward to read more of your articles. Build on! Thx.

  7. Virtual Private Servers August 14, 2017 at 1:25 pm - Reply

    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.

  8. Albert November 9, 2017 at 12:54 pm - Reply

    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.

  9. Mehdi May 28, 2018 at 10:07 am - Reply

    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

Leave A Comment