Drone software: architecture, MAVLink and telemetry · 6 MIN READ
Drone software architecture: flight controller, companion computer, ground station and fleet backend
Place each piece of drone software on the right computer, explain why the inner control loops stay in real-time firmware, and fly a first simulated take-off, hold and landing from Go.
A drone that flies itself is several computers that trust each other only so far. A microcontroller runs the flight controller firmware and keeps the aircraft upright hundreds of times a second; a Linux computer on board may run mission logic, cameras and networking; a ground control station shows the operator what is happening; and a backend may coordinate many vehicles. Go is a strong fit for the last three and a poor fit for the first, and the reasons explain most design choices in this course.
drones · A MAP OF THE IDEA
- Firmware closes loops
- Companion sends setpoints
- GCS monitors and commands
- Backend plans and stores
Useful when
- Each layer can fail without taking down the one below it.
- Go fits the networked layers with one static binary per target.
Watch out
- Latency and jitter grow with every layer above the firmware.
- Disabling a failsafe in shipped software moves risk to people on the ground.
Implementation & operation cost: A clear contract per layer: which messages cross each link, at what rate, and what happens when they stop.
Four computers, four time scales
The flight controller is a microcontroller board, such as a Pixhawk-class STM32, running PX4 on the NuttX RTOS or ArduPilot (on ChibiOS for most boards), written in C and C++. It samples the IMU at hundreds of hertz to kilohertz, runs the state estimator (an EKF), the rate, attitude and position controllers and the motor mixer, and owns the failsafes. The companion computer is a Linux single-board computer, such as a Raspberry Pi or an NVIDIA Jetson, wired to the flight controller by a fast serial or Ethernet link and speaking MAVLink (or uXRCE-DDS for ROS 2 with PX4); it runs vision, mission logic, video and the connection to the outside world, often through a MAVLink router. The ground control station, such as QGroundControl or Mission Planner, talks MAVLink over a telemetry radio or IP. A fleet backend collects telemetry and logs from many vehicles and plans their work. Deadlines go from microseconds in the firmware to minutes in the backend.
Why the inner loops stay in firmware
A multicopter is unstable: if the rate loop misses its deadlines for a few tens of milliseconds, the aircraft can tip over. Flight firmware runs on an RTOS with fixed priorities, no paging and no garbage collector, with the IMU on a short SPI bus next to the processor. A Go process on Linux adds scheduler jitter, garbage-collection pauses that are usually short but have no hard bound, and milliseconds of serial or USB latency; and if the companion crashes or reboots, something must still be flying the aircraft. So companions send setpoints (a position, a velocity, an attitude) and let the flight controller close the loops. PX4 makes the contract explicit: Offboard mode requires a continuous proof-of-life stream of at least 2 Hz and triggers the configured failsafe when it stops for longer than COM_OF_LOSS_T. Every layer should survive the failure of the layer above it.
A first flight from Go
The program plays the companion computer: it sends high-level commands and watches the mode, while the simulator plays the flight controller that runs the loops and decides when a manoeuvre is complete. It first asks for a take-off before arming, then arms, climbs to 10 m, holds for five seconds in a 5 m/s wind with 2 m/s gusts and lands. A watch function prints every mode change with the simulated time, the altitude and the armed flag, and sim.Save writes the recording for the replay.
package main
import (
"errors"
"fmt"
"log"
"gopherforge.app/drone"
)
// A first flight. This program plays the companion computer: it sends
// high-level commands (arm, take off, land) and watches the mode. The
// simulator plays the flight controller: it runs the control loops, decides
// when a manoeuvre is finished and changes the mode itself.
func main() {
sim := drone.New(drone.Config{Wind: drone.Vec3{X: 5}, Gust: 2, Seed: 1})
d := sim.Drone()
last := d.State().Mode
fmt.Printf("t=%6.2f s mode %s\n", sim.Time(), last)
watch := func() {
st := d.State()
if st.Mode != last {
fmt.Printf("t=%6.2f s mode %-7s -> %-7s alt %5.2f m armed=%t\n",
sim.Time(), last, st.Mode, st.Pos.Z, st.Armed)
last = st.Mode
}
}
// The flight controller is the authority: it refuses unsafe commands.
if err := d.TakeOff(10); errors.Is(err, drone.ErrNotArmed) {
fmt.Println("take-off before arming refused:", err)
}
must(d.Arm())
watch()
must(d.TakeOff(10))
watch()
climbed := sim.RunUntil(30, func(float64) bool {
watch()
return d.State().Mode == drone.Hold
})
if !climbed {
log.Fatalf("did not reach 10 m: %+v", d.State())
}
// Hold for five seconds in a 5 m/s wind with 2 m/s gusts: the position
// loop tilts into the wind and pulls the drone back after each gust.
drift := 0.0
sim.Run(5, func(float64) {
watch()
drift = max(drift, d.State().Pos.Horizontal().Len())
})
fmt.Printf("held 5 s at %.2f m, largest drift %.2f m\n", d.State().Pos.Z, drift)
must(d.Land())
watch()
landed := sim.RunUntil(60, func(float64) bool {
watch()
return d.State().Landed
})
if !landed {
log.Fatalf("did not land: %+v", d.State())
}
st := d.State()
fmt.Printf("flight %.2f s, battery %.1f%%, %.2f m from home\n",
sim.Time(), st.Battery, st.Pos.Horizontal().Len())
if err := sim.Save(); err != nil {
log.Fatal(err)
}
}
func must(err error) {
if err != nil {
log.Fatal(err)
}
}The early TakeOff returns "drone: not armed": the autopilot, not the caller, enforces preconditions. Arm and TakeOff change the mode at t=0.00 s; the autopilot itself switches TAKEOFF -> HOLD at t=4.64 s with the altitude at 9.72 m, because it ends the climb within 0.3 m of the target, and the hold settles at 10.00 m; the gusts never push it more than 0.16 m from home, because the position loop tilts into the wind and pulls it back. LAND starts at 9.64 s and LANDED follows at 19.18 s with armed=false, because the autopilot disarms on touchdown. The flight used 4.4% of the battery and touched down 0.27 m from home, and the last line is sim.Save reporting flight.gfflight.
Safety and the rules, as principles
This is not legal advice; rules differ by country and change, so check your national aviation authority before flying. The common principles: keep the aircraft in visual line of sight (VLOS) unless you hold an authorisation for more; stay below the altitude limit, 120 m above the ground in the EU open category and 400 ft under the US Part 107 rules; register where required; and broadcast Remote ID where required. In the US, drones that must be registered have to comply with the Remote ID rule through built-in broadcast, a broadcast module, or by flying at an FAA-recognized identification area; in the EU many class-marked drones must broadcast direct remote identification. Controlled airspace near airports and published geographical zones need permission or are closed. For software this means geofences, altitude ceilings, link-loss and low-battery failsafes are requirements, not options: never ship code that disables them, and test in simulation, then with propellers removed, then in an open field.
Where Go fits
Go suits the layers that are mostly networking and concurrency. On the companion computer a single static binary, cross-compiled with GOOS=linux GOARCH=arm64, can read MAVLink from the flight controller, forward telemetry, run mission logic and talk to the cloud, with a goroutine per link. github.com/bluenviron/gomavlib implements MAVLink in Go and powers the mavp2p router; Gobot drives small drones such as the Tello. Ground-station services, telemetry pipelines and fleet backends are ordinary Go servers. The flight controller is the exception: TinyGo can run on microcontrollers, but the flight stacks people actually fly are C and C++ with years of flight testing, and your Go code should treat them as the authority.
Fly it in GopherForge
In the app, choose New project > Robotics and simulation > Drone flight. Replace main.go with this lesson's program and keep mission.go: this program does not call Square, and the file still compiles next to it. Tap Run to see the mode changes in the console, then open Output: the replay of flight.gfflight shows the climb, the hold and the landing on the altitude strip, the wind arrow and the HUD with mode and battery.
TAKE THIS WITH YOU
Keep the loops that hold the aircraft in the air on the flight controller, where an RTOS meets every deadline. Put Go where it is strong: companion computers, ground stations and fleet backends that send setpoints and missions, watch modes and telemetry, and survive being disconnected.
Check your understanding
A team wants to move the multicopter attitude and rate controllers from PX4 into a Go service on the Linux companion computer. Where should those loops run?
Go to the source
Progress stays in this browser. No account needed.