---
# System prepended metadata

title: PX4 HITL(hardware in the loop) simulation and telemetry of IMU data

---


# PX4 HITL(hardware in the loop) simulation and telemetry of IMU data

## Some benefits of HITL simulation : 
1.Simular computing performance to real flights situations the SITL(software in the loop) simulation.

2.Able to test some external devices which connects to pixhawk. eg. Raspberry Pi, GPS, Radio, Distance Sensors.

---

# Steps to setup a simulation environment

---

## 1. Setting up a PX4 Developer environment (Toolchain) ###

```
sudo apt update
sudo apt install git -y
```

Download PX4 Source Code:  
``git clone https://github.com/PX4/PX4-Autopilot.git --recursive`` 

Run ubuntu.sh in a bash shell to install everything:
`bash ./PX4-Autopilot/Tools/setup/ubuntu.sh`

## 2. Download and Install QGroundControl, a ground station software
http://qgroundcontrol.com/

```
sudo usermod -a -G dialout $USER
sudo apt-get remove modemmanager -y
sudo apt install gstreamer1.0-plugins-bad gstreamer1.0-libav gstreamer1.0-gl -y
```


## 3. Setting up HITL, PX4 Configuration
 ![](https://i.imgur.com/wyGhXVG.png)
 ![](https://i.imgur.com/lYaFSVr.png)
 ![](https://i.imgur.com/qPKvZPH.png)
 1.Update PX4 firmare
 
 2.Connect the autopilot directly to QGroundControl via USB.
 
 3.Open Setup > Safety section.
 
 4.Enable HITL mode by selecting Enabled from the HITL Enabled list
 
 5.Select Airframe
 Open Setup > Airframes > HIL Quadcopter X
 ![](https://i.imgur.com/VeUt7po.png)

 
 6.Setup UDP
 Under the General tab of the settings menu, uncheck all AutoConnect boxes except for UDP.
 ![](https://i.imgur.com/l0Vk0rb.png) 

---

# (Optional) Setting up MAVSDK, a set of libraries providing a high-level API to MAVLink.
---


###### The prebuilt C++ library can be downloaded as a .deb from releases
 
```
wget https://github.com/mavlink/MAVSDK/releases/download/v0.37.0/mavsdk_0.37.0_ubuntu20.04_amd64.deb
```
```
sudo dpkg -i mavsdk_0.37.0_ubuntu20.04_amd64.deb
```


---

# HITL Simulation using jMAVSim


---

1.Connect the flight controller to the computer and wait for it to boot.
2.Run jMAVSim in HITL mode:
`cd ~/PX4-Autopilot `
`./Tools/jmavsim_run.sh -q -s -d /dev/ttyACM0 -b 921600 -r 250`

> **Note:** Replace the serial port name /dev/ttyACM0 as appropriate. On macOS this port would be /dev/tty.usbmodem1. On Windows (including Cygwin) it would be the COM1 or another port - check the connection in the Windows Device Manager.

![](https://i.imgur.com/GnUgwAE.jpg)

### Switch flight mode to "Altitude mode" in Qgroundccontrol GUI, so the position estimation only depend on IMU

![](https://i.imgur.com/Jtg76LR.png)

arm the vehicle, control it before disarming

control the quadrotor manually by two virtual stick in QGroundControl 
![](https://i.imgur.com/2gKdtkz.png)

### Run the telemetry program integrated by MAVSDK API, automatically log the IMU data.

Install MAVSDK-Python
```
pip3 install mavsdk
python3 ./position_ned.py
```
*vehicle must be still before launch the program 
1. input 1~4 to change the waypoint position
2. assign the x value of the waypoint
3. assign the y value of the waypoint
4. back to step 1. or input "0" to quit flight

![](https://i.imgur.com/alpUWEj.jpg)

---

# Reference: jMAVSIM Keyboard Commands

---

Views:

F - First-person-view camera.
S - Stationary ground camera.
G - Gimbal camera.
Z - Toggle auto-zoom for Stationary camera.
+/- - Zoom in/out
0/ENTER - Reset zoom to default.

Actions:

Q - Disable sim on MAV.
I - Enable sim on MAV.
H - Toggle HUD overlay.
C - Clear all messages on HUD.
R - Toggle data report sidebar.
T - Toggle data report updates.
D - Toggle sensor parameter control sidebar.
F1 - Show this key commands reference.
P - Pause.
ESC - Exit jMAVSim.
SPACE - Reset vehicle & view to start position.

Manipulate Vehicle:

ARROW KEYS - Rotate around pitch/roll.
END/PG-DN - Rotate CCW/CW around yaw.
SHIFT + ARROWS - Move N/S/E/W.
SHIFT + INS/DEL - Move Up/Down.
NUMPAD 8/2/4/6 - Start/increase rotation rate around pitch/roll axis.
NUMPAD 1/3 - Start/increase rotation rate around yaw axis.
NUMPAD 5 - Stop all rotation.
CTRL + NUMPAD 5 - Reset vehicle attitude, velocity, & accelleration.
Manipulate Environment:

**ALT +**
ARROW KEYS - Increase wind deviation in N/S/E/W direction.
INS/DEL - Increase wind deviation in Up/Down direction.
NUMPAD 8/2/4/6 - Increase wind speed in N/S/E/W direction.
NUMPAD 7/1 - Increase wind speed in Up/Down direction.
NUMPAD 5 - Stop all wind and deviations.
CTRL+ Manipulate - Rotate/move/increase at a higher/faster rate.