สถาปัตยกรรม PX4 และ ArduPilot
Software structure, parameters, flight modes, failsafes and geofences
Overview: two systems, one set of ideas
An autopilot is the software on a flight controller that reads the sensors, estimates position and attitude, computes commands and drives the motors hundreds of times a second. The two open-source autopilots most used in education and industry are PX4 and ArduPilot. Both support multirotors, fixed-wing aircraft, VTOL and ground vehicles, and both talk to a ground control station (GCS) using the MAVLink protocol.
Operators need to understand this structure for two reasons. Most flight problems are solved quickly once you know which layer the symptom comes from: sensors, estimation, control loops or failsafe settings. And the aircraft's behaviour when something goes wrong is set by parameters that we choose; it does not happen by itself.
From sensors to motors
Both systems follow a similar chain:
- Sensor drivers read the IMU, barometer, compass, GNSS and any extra sensors.
- The state estimator fuses all sources with an EKF into best estimates of position, velocity and attitude. PX4 uses EKF2; ArduPilot uses EKF3.
- Navigation and flight modes turn a mission or stick input into targets.
- Cascaded controllers run the position, velocity, attitude and rate loops.
- The mixer converts the required thrust and moments into individual motor commands for the ESCs.
They differ in how the software is organised. PX4 is split into independent modules such as the estimator, controllers and the commander that manages vehicle state. Modules exchange data through uORB, a publish/subscribe messaging system: one module publishes a topic and any interested module subscribes. Modules can be replaced or added without touching the others. PX4 runs on the NuttX real-time operating system.
ArduPilot has vehicle-specific code such as ArduCopter and ArduPlane on top of shared libraries, with a hardware abstraction layer (HAL) that hides board differences. Work is organised by a scheduler inside a main loop: urgent tasks such as the rate controller run every cycle, others run at rates listed in a task table. Copter's main loop defaults to 400 Hz.
Parameters: what actually defines behaviour
Parameters are settings stored in the flight controller's non-volatile memory. Each system has thousands, covering tuning gains, limits, port assignments and failsafes. Names usually start with a group prefix: in ArduPilot, ATC_ for attitude control and FENCE_ for geofencing; in PX4, MC_ for multicopter control and COM_ for the commander.
Three habits keep parameters under control. Save the complete parameter file before any change. Change one value or one group at a time, then test. Record the firmware version alongside the file, because names, units and defaults change between releases, so a file from another aircraft or another version should never be loaded wholesale.
Flight modes
A flight mode decides at which loop the pilot or the automation gives commands. Names differ slightly between the two systems; the ideas match.
| Level | PX4 | ArduPilot Copter | What the pilot commands |
|---|---|---|---|
| Attitude | Stabilized | Stabilize | Lean angle and throttle |
| Altitude hold | Altitude | Alt Hold | Lean angle and climb rate |
| Position hold | Position | Loiter | Desired velocity; centred sticks hold position |
| Automatic | Mission, Hold, Return, Land | Auto, Guided, RTL, Land | The system generates its own targets |
Modes that rely on GNSS are unavailable when the position estimate is poor. The autopilot will refuse the mode or fall back to a lower one, which is why pilots must stay proficient in attitude modes.
Failsafes and geofences
A failsafe is a rule the autopilot applies on its own when an abnormal condition occurs: RC signal loss, GCS link loss, low battery or a failed estimator. Each has a trigger (for example, no heartbeat from the GCS for longer than a set time) and an action (RTL, land, or continue the mission). A heartbeat is a MAVLink message sent periodically to show that the other side is still present.
Some failsafes are disabled by default, including GCS link-loss failsafe in both systems (check the documentation for the version you fly). If you do not configure it, an aircraft flying an automatic mission keeps going after the station has lost contact.
A geofence is a virtual boundary that the autopilot checks continuously: an altitude ceiling, a circle around home, or polygons. ArduPilot distinguishes inclusion fences, which the aircraft must stay inside, from exclusion fences, which it must not enter. On a breach the aircraft performs the configured action such as Hold, RTL or Land. Because it needs distance to stop, the fence must sit inside the edge of the approved area.
Worked example: which failsafe fires first?
Example: link lost while flying outbound
Assume an approved area that is a 300 m circle around home. The drone flies outbound at 8 m/s at 40 m height, with 1 s response time, 3 m/s² braking and 3 m GNSS error. The GCS failsafe waits 5 s and then commands RTL. Average power is 400 W and the battery is 14.8 V, 5,000 mAh. All values are assumed.
- Fence placement: stopping distance = 8 × 1 + 8²/(2 × 3) = 8 + 10.7 = 18.7 m, plus 3 m GNSS error = 21.7 m. The fence must be no further than 300 − 21.7 = 278.3 m, so set it at 275 m.
- Link lost at 250 m: the GCS failsafe would fire after 5 s, by which time the drone would be 40 m further out at 290 m. But it reaches the 275 m fence in 25/8 = 3.1 s, so the fence acts first.
- Furthest point: 275 + 18.7 ≈ 293.7 m, or 296.7 m including position error, still inside 300 m.
- RTL energy: return 293.7/8 = 36.7 s, descend 40 m at 1.5 m/s = 26.7 s, loiter 5 s; total 68.4 s. Energy 400 × 68.4/3600 = 7.6 Wh out of 14.8 × 5 = 74 Wh, or 10.3%.
Failsafe layers back each other up, and which one fires first depends on position and speed at the moment of failure. Every value must be confirmed in SITL and in real tests on the firmware you fly, not taken from the calculation alone.
Common pitfalls
- Assuming the drone will come home on link loss without checking that the failsafe is enabled.
- Loading a whole parameter file from another aircraft or firmware version.
- Changing many parameters at once, then not knowing which one changed the behaviour.
- Drawing the fence exactly on the approved boundary with no allowance for stopping distance.
- Keeping the default RTL altitude where buildings or trees are taller.
- Raising the loop rate beyond what the board can handle without checking scheduler load.
- Testing in SITL with a different version or parameter set from the real aircraft.
Continue in the lessons
- UAT 206 Module 4: Autopilots and flight modes: which loops each mode uses, and managing parameter files.
- UAT 302 Module 2: Installing the FC, GNSS and sensors: mounting orientation and sensor placement.
- UAT 305 Module 1: Autopilot architecture: the scheduler, uORB and loop latency.
- UAT 305 Module 5: Failsafes and geofences: inclusion and exclusion fences, and proving failsafes from logs.
- UAT 311 Module 3: Control, navigation and communication: architecture overview and navigation sources.
- UAT 312 Module 2: Automated operations: mission commands, failsafe defaults and RTL energy.
- UAT 321 Module 1: Selecting and installing equipment: wiring the flight controller to its peripherals.
- UAT 322 Module 1: Integrating autonomy: dividing duties between the autopilot and the companion computer.
- UAT 454 Module 7: Integrating cameras, sensors, communications and the control centre: the autopilot within a complete surveillance system.
Key references
- PX4 Autopilot. PX4 architectural overview. PX4 user guide (main). link
- PX4 Autopilot. Safety (failsafe) configuration. PX4 user guide (main). link
- ArduPilot Dev Team. Flight modes. ArduPilot Copter documentation. link
- ArduPilot Dev Team. GCS failsafe. ArduPilot Copter documentation. link
- ArduPilot Dev Team. Inclusion and exclusion fences. ArduPilot Copter documentation. link
- MAVLink Development Team. Heartbeat/connection protocol. MAVLink developer guide. link