---
title: "Drone Autopilot"
description: "v0.5 paper-correct skeleton. PID gains, sensor scales, failsafe thresholds, and motor mapping are starting-point values that need bench tuning + ground-tied throttle tests before a real airframe goes near them. [UNTESTED]."
source: https://docs.alplab.ai/sdk/examples/drone-autopilot
---

# `drone-autopilot`

:::danger[DO NOT FLY THIS BUILD]
v0.5 paper-correct skeleton. PID gains, sensor scales, failsafe thresholds, and motor mapping are starting-point values that need bench tuning + ground-tied throttle tests before a real airframe goes near them. **`[UNTESTED]`**.
:::

A self-contained PID-stabilised quadcopter flight controller running on an E1M-AEN module. The full real-time control stack in one app: IMU sample → attitude estimation → cascaded PID → motor mixer → ESCs.

Source: [`examples/peripheral-io/drone-autopilot/`](https://github.com/alplabai/alp-sdk/tree/main/examples/peripheral-io/drone-autopilot).

## Three deterministic control loops

| Loop | Rate | Priority | Responsibility |
|---|---|---|---|
| Rate | 1000 Hz | highest | Inner-loop angular-rate PID; 4× ESC outputs every tick. |
| Attitude | 250 Hz | middle | Outer-loop angle PID; rate setpoints fed into the inner loop. |
| Nav + battery + failsafe | 25 Hz | low | GPS hold, RTL trigger, low-battery RTL, link-loss failsafe. |

## Portable PID

The app no longer carries hand-rolled control maths. Every loop is an [`<alp/pid.h>`](https://docs.alplab.ai/sdk/api/pid) controller — the library owns the output clamp, integrator anti-windup, and derivative-on-measurement:

```c
static alp_pid_t s_rate_p, s_rate_q, s_rate_r; /* body-rate loops      */
static alp_pid_t s_atti_roll, s_atti_pitch;    /* attitude loops (dps) */
static alp_pid_t s_alt_pid;                    /* altitude-rate loop   */

alp_pid_init(&s_rate_p,
             &(alp_pid_config_t){ .kp = 0.10f, .ki = 0.05f, .kd = 0.001f,
                                  .out_min = -1.0f, .out_max = 1.0f,
                                  .deriv_on_measurement = true });

/* ... in the 1 kHz rate loop: */
const float tau_p = alp_pid_step(&s_rate_p, setp_p, s->p, dt_s);
```

The controllers live in `autopilot.c`; `main.c` never includes `pid.h`. The gains above are starting points, not tuned values — see the banner.

## `board.yaml`

```yaml
libraries:
  - name: madgwick-ahrs
    cores: [m55_hp]
  - name: pid
    cores: [m55_hp]

som:
  sku: E1M-AEN801

preset: e1m-evk

pins:
  - { e1m: E1M_I2C0, macro: EVK_I2C_BUS_SENSORS }
  - { e1m: E1M_UART0, macro: EVK_UART_PORT_DEBUG }
  - { e1m: E1M_PWM0, macro: EVK_PWM_LED_RED }    # red on the 2626-R2 E1M EVK

cores:
  a32_cluster:
    os: "off"
  m55_hp:
    app: ./src
    peripherals:
      - i2c
      - uart
      - pwm
      - gpio

chips:
  - lsm6dso
  - bmp390
  - ublox_neo_m9n
  - ina236

diagnostics:
  log_level: warn
```

## See also

- [`<alp/pid.h>`](https://docs.alplab.ai/sdk/api/pid) — the portable PID surface every loop runs on
- [`<alp/ahrs.h>`](https://docs.alplab.ai/sdk/api/ahrs) — the Madgwick fusion behind the attitude estimate
- [`drone-hud`](https://docs.alplab.ai/sdk/examples/drone-hud) — head-up display variant
- [Examples overview](https://docs.alplab.ai/sdk/examples/overview)
