stable-flight
Flight-control core for Stable Flight: the pipeline traits, the data passed between them, copter guidance, cascaded-PID stabilizers, mixers, and FlightLoop, which sequences one control tick. It is no_std and has no simulator, I/O, or timing dependencies, so the same code runs in software-in-the-loop simulation (stable-flight-sitl) and, later, on flight hardware. See architecture.md for design rationale.
Pipeline
FlightLoop::step(dt) runs one tick through the pipeline traits (Commander, StateSource + Actuator = Vehicle, Guidance, Stabilizer, Mixer, TelemetryPublisher); the tick order and what each stage sees are specified in architecture.md. FlightLoop has no clock: a runner decides when to call step (RealTimeRunner in stable-flight-sitl today).
let mut flight_loop = new;
flight_loop.prime; // initial estimate (dt = 0) + guidance and stabilizer priming
loop
Commanders
commanders::WaypointFollower flies any waypoint list without allocating — an array or &'static slice on firmware, a Vec on a host — looping it, holding the last waypoint, or landing on it and disarming at touchdown (RouteEnd):
let mut commander = new?
.with_acceptance_radius
.with_route_end;
commanders::FailsafeCommander flies a link-fed primary (a LinkedCommander, which reports how long ago it last heard the link) and switches to a Fallback when the link goes stale, such as HoldCommander, which holds the position where it took over, or finishes a land the link dropped. It starts in the fallback and returns only after the link has been back for a while; a disarm is always honored, the fallback never arms, an arm requested during the fallback is dropped at the handover, sticks take back an armed vehicle only once they move, and a land disarms on touchdown, whichever commander flies it. Rules: architecture.md.
let mut commander = new?;
Input-device and file-based commanders (joysticks, CSV scripts) live in stable-flight-sitl.
Composition
ComposedVehicle::new(source, actuator)makes aVehiclefrom a separateStateSourceandActuator— the usual shape on hardware (IMU/estimator + ESC driver).- A tuple of telemetry publishers is a publisher:
(mavlink, (file_log, console))sends every tick to all three;()publishes nothing. FlightLoop::into_parts()hands the components back after a run.
Testing
Enable the testing feature in [dev-dependencies] for allocation-free doubles in stable_flight::testing: NullVehicle (fixed state, counts calls), ConstantCommander, and RecordingTelemetry<N> (keeps the last N ticks). settled_yaw_rate checks a body-rate config's yaw loop against a constant torque, such as an airframe's measured hover yaw imbalance.
testing::conformance checks a StateSource (simulator backend or estimator) against the VehicleState frame contract. It is a table of physical scenarios (level, banked, nose up, heading east, climbing, moving northeast, yaw and combined rates) with the state each must produce. A simulator backend or estimator adapter builds its native input for each scenario, and conformance_tests! generates one test per scenario, plus one that fails if a scenario is left uncovered:
conformance_tests!
Guidance
Guidance is the outer loop: it turns the commander's ControlInput into a Setpoint (an attitude or rate target) for the stabilizer. CopterGuidance flies waypoints with altitude and lateral position loops (the lateral loop at a configurable, slower rate, capped at each waypoint's cruise speed), turns attitude-mode yaw stick into a heading-hold target that waypoint flight also holds, flies an offboard absolute attitude (ControlInput::AbsoluteAttitude) as given, lands (ControlInput::Land: hold position, descend at a set rate), and passes rate commands through. Waypoint descents are limited (max_descent_mps), so a low waypoint descends instead of cutting thrust.
let guidance = new;
Stabilizer
Stabilizers track the guidance Setpoint: they calculate the difference between the vehicle's current state and the desired state, generating ControlOutputs that the mixer then translates into actuator commands. This component contains the core flight stabilization logic. CopterStabilizer targets multirotors. This is where you would start if you want to implement custom flight control logic.
The simplest setup uses default gains:
let stabilizer = new;
For tuned gains, set the inner loops' configs (simulator backends ship tuned sets, e.g. airframes::quadcopter_x):
let stabilizer = new;
Params
Guidance and stabilizer gains and limits can be retuned between ticks by name: a flat name → f32 view over the configs, sized for MAVLink's 16-byte param_id, with angles in degrees. A write is validated like any config change, and an invalid value leaves the config unchanged. The full name list is in the params rustdoc.
use Params;
flight_loop.set_param?;
flight_loop.visit_params;
Param metadata
The param-metadata feature adds params::metadata. For each param it gives a one-line description, units, a category (the component) and group (the loop), and the valid range, for ground stations: stable-flight-bridge serves it to QGC. A config describes its params by implementing ParamMetadata: a METADATA table next to its ParamTable, with the same names in the same order. Ranges restate what validate accepts, and tests check each one against it. The feature is host-side data only; firmware leaves it off, so it costs no flash.
use ParamMetadata;
use CopterGuidanceConfig;
for meta in METADATA
Serialization
The serde feature derives Serialize/Deserialize on the pipeline data (Command, Setpoint, VehicleState, ControlOutput, ActuatorCommand, Saturation, and their parts) without std, so the same types cross a wire. stable-flight-link carries them between the flight controller and a companion. TelemetryRecord borrows its parts, so the link sends an owned copy.
Mixer
Mixers convert control outputs into an ActuatorCommand: generic motors (0–1) and servos (-1–1) slots whose meaning the mixer sets, and which the backend maps to output channels:
let mixer = default;