## SITUATION REPORT 4 (16/12/2022)
# How to setup a PX4 simulation using SITL
## Introduction
This report aims to describe the steps to setup a simulation environment for the *Pixhawk* board. The simulation will be done using the *PX4* software, which is the most common software used in the *Pixhawk* board.
Simulators allow PX4 flight code to control a computer modeled vehicle in a simulated world. It is possible to interact with this vechicle just as you might with a real vehicle, using *QGroundControl*, an offboard API, or a radio controller.
PX4 supports both SITL (Software in the loop) and HITL (Hardware in the loop) simulations. This guide is targeted for SITL simulation, where the flight code runs on the same computer as the simulator.
---
## Supported Simulators
While PX4 supports other simulators, the following are the most relevant for our use case:
| Simulator | Description | Supported Vehicles |
|---|---|---|
| jMAVSim | A simple multirotor simulator that allows you to fly copter-type vehicles around a simulated world. It is easy to set up and can be used to test if the vehicle can take-off, fly, land, and responds approprietly to various fail conditions (e.g GPS failure). Multi-vehicle simulation is also supported. | Quad |
| Gazebo | A powerful 3D simulation environment that is particularly suitable for testing object-avoidance and computer vision. It can also be used for multi-vehicle simulation and is commonly used with ROS, a collection of tools for automating vehicle control. | Quad (Iris and Solo, Hex (Typhoon H480), Generic quad delta VTOL, Tailsitter, Plane, Rover, Submarine |
| FlightGear | A simulator that provides physically and visually realistic simulations. In particular it can simulate many weather conditions, including thunderstorms, snow, rain and hail, and can also simulate thermals and different types of atmospheric flows. Multi-vehicle simulation is also supported. | Plane, Autogyro, Rover |
**PX4** contains documentation regarding the set up of each simulator. The following sections will focus on the *jMAVSim* simulator, as it is the simplest to set up and, as of right now, seems to contain all the features required for our use case.
### Simulator MAVLink API
All simulators communicate with PX4 using the *Simulator MAVLink API*. This API defines a set of messages that **supply sensor data** from the simulated world to PX4 and return motor and actuator values from the flight code that will be applied to the simulated vehicle. The image below depicts the flow of information between the simulator and PX4:
![MavLink Message Flow](assets/Pixhawk/px4_mavlink_msg_flow.png)
Note: A SITL build of PX4 uses [SimulatorMavlink.cpp](https://github.com/PX4/PX4-Autopilot/blob/main/src/modules/simulation/simulator_mavlink/SimulatorMavlink.cpp) to handle these messages. To see the messages that are exchanged in more detail, visit the [documentation page](https://docs.px4.io/main/en/simulation/#simulator-mavlink-api).
#### Default PX4 MAVLink UDP Ports
By default, PX4 uses commonly established ports for MAVLink communication with Ground Control Stations (e.g QGroundControl), offboard APIs (e.G MAVSDK) and simulator APIs (e.g jMAVSim). The following table shows the default ports used by PX4:
| Port | Description |
|---|---|
| UDP 14550 | Communication with Ground Control Stations, such as QGroundControl. |
| UDP 14540 | Communication with offboard APIs. |
| TCP 4560 | Communication with PX4 (flight code). The simulator listens to this port and PX4 initiates a connection to it. |
## SITL Simulation Environment
The diagram below shows a typical SITL simulation environment:
![PX4 Simulation Environment](assets/Pixhawk/px4_sitl_environment.png)
The different parts of the system connect via UDP, and can be run on either the same computer or another computer on the same network.
PX4 uses a simulation-specific module to connect to the simulator's local TCP port 4560. Simulators then exchange data with PX4 using the MAVLink API described above. PX4 on SITL and the simulator can run on either the same machine or on different machines on the same network.
## jMavSim Simulator
https://docs.px4.io/main/en/simulation/jmavsim.html#jmavsim-with-sitl
- Talk about jmavSim
- Coding Environment (Works on Linux & Windows) but with specific configurations.
### Ground Control Station
Now focusing on the Ground Control Station:
![Ground Control Station](assets/Pixhawk/groundStationOverview.png)
Fig. 4 - Ground Control Station
In terms of hardware, the ground station just needs a telemetry module, identical to the USB module represented in the figure, that will communicate with the antenna in the rocket, using the *MAVLINK* protocol.
In the software layer, resides the user interfaces. The software represented here are essentially GUIs that utilize the *MAVLINK* protocol. Namely, *QGroundControl*, *APM Planner* and *Mission Planner*. The first 2 are for Linux, while the last one is exclusively for Windows.
Additionally, this layer also contains packages that can control vehicles, with the *MAVLINK* protocol through a coding language. In other words, these packages allow you to treat *Ardupilot* and *PX4* as an API to control the vehicle. Here we have represented the package **DroneKit**.
---
## PX4 Deep Dive
As mentioned above, PX4 is one of the most popular *Flight Controller Software*. For that reason, we will explore it in more detail since its supported features are very similar to *Ardupilot* and the documentation is easier to follow.
### Supported Vehicles
Although PX4 supports a high variety of vehicles, model rockets are not part of that list. Nonetheless, I believe that is not an issue since this list is targeted for the usage of the software as a *Professional Autopilot*, that is, controlling the vehicle's trajectory and moving it as desired. In our use case, that feature isn't required so it should be fine as long as the features mentioned in the [Introduction](##Introduction) are supported.
### System Architecture
The *PX4* documentation provides a high-level overview of a typical PX4 system based around a flight controller.
![PX4 System Architecture](assets/Pixhawk/px4SimpleDiagram.png)
Fig. 5 - PX4 System Architecture
Note that this architecture is similar to what we wish to achieve, as it contains most of the sensors required for the model rocket. The missing pieces in the diagram are the [*parachute triggering*](https://docs.px4.io/main/en/peripherals/parachute.html) and [*persistent storage*](https://docs.px4.io/main/en/dev_log/logging.html), which are also supported and documented.
### Flight Controllers
There are many *Pixhawk* boards to choose from, such as the [Pixhawk 4](https://docs.px4.io/main/en/flight_controller/pixhawk4.html). The comparison and analysis of the most suitable board should be made in collaboration with the *Electrical Engineering Department*.
### Supported Sensors
It is confirmed that the Pixhawk + PX4 combination supports the sensors and radio systems we are looking for. Anyhow, there are a lot of options to choose from and, once again, contact with the *Electrical Engineering Department* is needed to reach a consensus.
---
## Conclusion and Next Steps
After analyzing this information, *Pixhawk* seems to be a promising board to introduce in our model rocket. However, the current information isn't enough to reach a conclusion. To do so, we must convert this theoretical information into a practical example. Thankfully, the *Software in the Loop* solution mentioned in this report provides a way to simulate the *Pixhawk* and the rocket model. Thus, the next steps involve testing this *PX4* and *Ardupilot* software, to ensure they fit our needs and are capable of adapting to our scenario.
## References
\[1\] [PX4 Docs](https://docs.px4.io/main/en)
\[2\] [PX4 User Guide](https://docs.px4.io/main/en/)
\[3\] [Ardupilot](https://ardupilot.org/)