Up to now the route was known before the run started. Here it is not: the program probes, reads, and picks its next move from what came back. The flying is the same. The control flow is what changes.
Commands
Flight
await drone.turn(degrees)
- Yaw in place, without moving.
await drone.moveForward(distance)
- Fly `distance` meters along the drone's current heading, holding altitude.
await drone.moveTo(x, y, z)
- Fly to an absolute point in the world, in one straight line.
drone.setSpeed(speed)
- Set the cruise speed used by every move that follows.
Sensors
await drone.getDistanceAhead()
- Rangefinder reading straight along the nose.
await drone.scan(range?)
- Sweep the object-detection suite and list everything around the drone.
await drone.getPosition()
- Where the drone is now, in world coordinates.
await drone.getHeading()
- The airframe's yaw, in degrees.
await drone.getAltitude()
- Current height above the field.
Worked examples
Probing left and right
The rangefinder only looks along the nose, so measuring in another direction means pointing the nose there. Turning back by the same amount afterwards leaves the drone as it was found, which keeps the probe from becoming part of the route.
async function main(drone) {
await drone.takeoff(2.5);
async function probe(degrees) {
await drone.turn(degrees);
const distance = await drone.getDistanceAhead();
await drone.turn(-degrees);
return distance;
}
const left = await probe(-90);
const right = await probe(90);
console.log('left', left, 'right', right);
// Take whichever side has more room.
await drone.turn(left > right ? -90 : 90);
await drone.moveForward(Math.min(left, right, 8) - 1);
await drone.land();
}
Reading the contact list
`scan()` returns everything within range, nearest first, with a bearing already relative to the nose. Logging the raw list once is the fastest way to learn what a mission has actually put around you.
async function main(drone) {
await drone.takeoff(4);
const contacts = await drone.scan(25);
for (const contact of contacts) {
console.log(
contact.kind,
contact.label ?? '(unlabelled)',
`${contact.distance}m`,
`bearing ${contact.bearingDeg}`,
);
}
console.log('nearest is', contacts[0]?.kind ?? 'nothing in range');
await drone.land();
}
Flying to something you found
A contact carries both a bearing and absolute coordinates, which gives you two ways to reach it. Turning by the bearing and flying the distance is the one that generalises. It works the same whether the target moved or you did.
async function main(drone) {
await drone.takeoff(3);
const contacts = await drone.scan(30);
const target = contacts.find((c) => c.label === 'north-tower');
if (!target) {
console.warn('nothing labelled north-tower in range');
await drone.land();
return;
}
// bearingDeg is already relative to the nose: 0 is straight
// ahead, positive is to the right. That is exactly what
// turn() wants, so it can be passed straight through.
await drone.turn(target.bearingDeg);
// Stop short of it rather than on top of it.
drone.setSpeed(3);
await drone.moveForward(target.distance - 4);
await drone.land();
}
A loop that always ends
A sensor loop that never satisfies its condition runs until the mission clock stops it, and the run is scored as a timeout. Counting the passes costs one variable and turns a hang into a log line you can read.
async function main(drone) {
await drone.takeoff(2);
const maxSteps = 40;
let steps = 0;
while ((await drone.getDistanceAhead()) > 2) {
if (steps++ >= maxSteps) {
console.warn('gave up after', maxSteps, 'steps');
break;
}
await drone.moveForward(1);
}
await drone.land();
}