MAVLINKHUD

SITL Architecture

Executive Summary

Software In The Loop (SITL) allows ArduPilot to run on a standard PC (Linux/Windows/macOS) as if it were running on a flight controller.

It achieves this by replacing the hardware drivers (AP_HAL) with a simulation layer (AP_HAL_SITL) that communicates with a physics engine (libraries/SITL or external simulators like Gazebo) via TCP/UDP sockets.

Theory & Concepts

1. The HAL Layer

ArduPilot code writes to hal.gpio->write() or hal.i2c->transfer().

  • On Hardware (ChibiOS): This toggles a register on the STM32.
  • On SITL: This writes a packet to a socket or shared memory buffer. The "Physics" side reads this packet (e.g., "Motor 1 at 50%") and calculates the resulting motion.

2. The Physics Engine

The simulator calculates the vehicle's position, velocity, and sensor data based on the motor outputs.

  • Internal: Simple kinematics (F=ma) built into ArduPilot (SIM_Multicopter.cpp).
  • External: Complex physics engines like Gazebo, X-Plane, or AirSim.

Codebase Investigation

1. AP_HAL_SITL

Located in libraries/AP_HAL_SITL.

  • Scheduler.cpp: Replaces the RTOS scheduler with a standard OS thread/sleep loop.
  • UARTDriver.cpp: Redirects "Serial Ports" to TCP ports (5760 = Serial0, 5762 = Serial2).

2. SIM Libraries

Located in libraries/SITL.

  • SIM_Aircraft.cpp: The base class for all vehicle physics models.
  • SIM_Sensor.cpp: Generates fake sensor noise and drift to make the simulation realistic.

Source Code Reference

Practical Guide: Running Custom Physics

You can write your own physics backend!

  1. Inherit from SIM_Aircraft.
  2. Implement update(const struct sitl_input &input).
  3. Calculate new state (Lat/Lon/Alt/Attitude).
  4. Register it in SITL.cpp.