ROBOTICS AND SIMULATION · COMING IN 1.3.7
Drone flight
A quadcopter mission written in Go: arm, climb to 12 m, fly the corners of a 40 m square in a 3 m/s crosswind with gusts, come home and land. Run it and the recorded flight replays under Output.
Offline: bundled gopherforge.app/drone simulator; Run replays the flight under Output
What it is
gopherforge.app/drone is a small simulator written for learning: a point-mass quadcopter with thrust, drag, wind and gusts and a draining battery, an autopilot with take-off, hold, go-to, return-to-home and land, and geofence and low-battery failsafes. It is deterministic for a given seed, so a test can check where the drone lands.
sim.Save() writes the flight to flight.gfflight. GopherForge animates it under Output: the trail on a map with the geofence and waypoints, an altitude strip, and speed, battery and flight mode as it plays. The browser Playground shows the same replay.
mission.go keeps the mission as plain data, so mission_test.go can check its geometry and fly it in a crosswind to prove the drone lands within 2 m of home. The Drones with Go course builds from here to PID control, failsafes, swarms and telemetry.
Every template's Go program was compiled for GOOS=wasip1 GOARCH=wasm with Go 1.27, then run, vetted and tested before it was added to the app.
Files
drone-flight/
├── vendor/
│ ├── gopherforge.app/drone/
│ │ ├── README.md
│ │ └── drone.go
│ └── modules.txt
├── go.mod
├── main.go
├── mission.go
└── mission_test.goThe code
main.go
sim := drone.New(drone.Config{
Wind: drone.Vec3{X: 3, Y: -1}, // 3 m/s from the west, a little from the north
Gust: 1.5,
Geofence: []drone.Vec3{
{X: -60, Y: -60}, {X: 60, Y: -60}, {X: 60, Y: 60}, {X: -60, Y: 60},
},
Ceiling: 50,
Seed: 42,
})main.go
for i, p := range Square(40, 12) {
if err := d.GoTo(p); err != nil {
log.Fatal(err)
}
if !sim.RunUntil(90, func(float64) bool { return d.Arrived() }) {
log.Fatalf("waypoint %d not reached: %+v", i+1, d.State())
}
st := d.State()
fmt.Printf("waypoint %d %v at %.1f s, battery %.0f%%\n", i+1, p, sim.Time(), st.Battery)
}mission.go
// Square returns the corners of a square with the given side, centred on
// home, flown counter-clockwise at one altitude.
func Square(side, alt float64) []drone.Vec3 {
h := side / 2
return []drone.Vec3{
{X: h, Y: -h, Z: alt},
{X: h, Y: h, Z: alt},
{X: -h, Y: h, Z: alt},
{X: -h, Y: -h, Z: alt},
}
}mission_test.go
func TestMissionLandsAtHomeInWind(t *testing.T) {
sim := drone.New(drone.Config{Wind: drone.Vec3{X: 3}, Gust: 1.5, Seed: 42})
d := sim.Drone()
d.Arm()
d.TakeOff(12)
sim.RunUntil(30, func(float64) bool { return d.State().Mode == drone.Hold })
for _, p := range Square(40, 12) {
d.GoTo(p)
if !sim.RunUntil(90, func(float64) bool { return d.Arrived() }) {
t.Fatalf("did not reach %v", p)
}
}
d.ReturnHome()
if !sim.RunUntil(120, func(float64) bool { return d.State().Landed }) {
t.Fatalf("did not land: %+v", d.State())
}
if off := d.State().Pos.Horizontal().Len(); off > 2 {
t.Fatalf("landed %.1f m from home", off)
}
}What you learn
- Drive an autopilot from Go: arm, take off, go to waypoints, return home and land.
- Step a simulation with sim.RunUntil and a condition instead of sleeping.
- Express positions in local east-north-up metres with a Vec3 type.
- See how wind and gusts push a multicopter off its line, and how the autopilot corrects.
- Keep a mission as data and test it deterministically with a fixed seed.
- Record a flight and read it back as an animated replay.
Run it on a server
The simulator is plain Go with no dependencies and is vendored under vendor/gopherforge.app/drone, so the project builds anywhere with go run . and go test ./...; Save writes flight.gfflight, a JSON recording, next to the program. Flying real hardware is a different step: flight controllers such as PX4 and ArduPilot speak MAVLink, which Go programs use through libraries like github.com/bluenviron/gomavlib, and both autopilots ship a software-in-the-loop simulator to test against first.
go test ./...
go run .
ls -l flight.gfflightRelated: Drones with Go course · Go fundamentals