Everything in this course is built from five commands and one loop. Get comfortable here and the rest of the catalog is the same ideas flown to tighter tolerances.
Commands
Flight
await drone.takeoff(altitude?)
- Spool the motors and climb straight up from the pad.
await drone.land()
- Descend straight down from wherever the drone currently is and settle on its landing gear.
await drone.hover(seconds)
- Hold station for a fixed time.
await drone.moveForward(distance)
- Fly `distance` meters along the drone's current heading, holding altitude.
await drone.turn(degrees)
- Yaw in place, without moving.
await drone.moveTo(x, y, z)
- Fly to an absolute point in the world, in one straight line.
await drone.setAltitude(altitude)
- Climb or descend to an absolute height, holding position over the ground.
Sensors
await drone.getDistanceAhead()
- Rangefinder reading straight along the nose.
Worked examples
The shape of every program
Your code is one async function named `main`, and it is handed a `drone`. Every command is awaited, because each one resolves when the manoeuvre has actually been flown. Drop an `await` and the whole script runs in a single frame.
async function main(drone) {
await drone.takeoff();
await drone.hover(2);
await drone.land();
}
A square, with a loop
`moveForward()` goes wherever the nose points, so turning between legs is what draws a shape. Four identical legs means a loop rather than four pairs of lines, and changing the 4 to a 6 flies a hexagon.
async function main(drone) {
await drone.takeoff(3);
const sides = 4;
for (let i = 0; i < sides; i++) {
await drone.moveForward(6);
await drone.turn(360 / sides);
}
await drone.land();
}
Waypoints as data
Waypoint coordinates from a briefing are absolute, so they are flown with `moveTo()`. Keeping them in an array separates the route from the flying: to change the mission you edit the list, not the loop.
async function main(drone) {
// Lift off first. moveTo() will happily fly you into
// the ground if you hand it a low y.
await drone.takeoff();
const route = [
{ x: 8, y: 3, z: -6 },
{ x: 14, y: 3, z: -14 },
{ x: 2, y: 3, z: -18 },
];
for (const point of route) {
await drone.moveTo(point.x, point.y, point.z);
console.log('reached', point);
}
await drone.land();
}
Creeping up on an obstacle
The rangefinder looks straight along the nose and reports 20 when it sees nothing. Testing against a threshold, rather than against 20, is what makes this read as "keep going while the way is clear".
async function main(drone) {
await drone.takeoff(2);
// Nudge forward a meter at a time and re-check, so the
// drone never commits to a leg longer than it can see.
while ((await drone.getDistanceAhead()) > 2) {
await drone.moveForward(1);
}
console.log('stopped at', await drone.getDistanceAhead(), 'm');
// Something is in the way: go over it rather than through it.
await drone.setAltitude(6);
await drone.moveForward(6);
await drone.land();
}