Introduction
Now that you have programmed the drone to fly through the REPL we can move on to writing a script which will fly the drone autonomously. In other words, it will follow a set of instructions from start to finish, without needing user intervention.
Autonomous Flight Script
This is done in a very similar way to using the REPL. Create a new file in your drone project directory called autonomous-demo.js
. Include the following code within the file:
var arDrone = require('ar-drone'); var client = arDrone.createClient(); client.takeoff(); client .after(5000, function() { this.clockwise(0.5); }) .after(3000, function() { this.stop(); this.land(); });
The first two lines should be familiar - including the library, and creating a client (drone) object. The script then instructs the drone to take-off. Next, the .after
function is called on the client
. This delays the following code (contained within an anonymous function) for a certain period of time. In the case of the first instruction (to turn clockwise) that will only occur after 5 seconds (5000 milliseconds). The next instruction (to stop turning, and to land) will occur 3 seconds after the previous instruction.
It is important to remember that JavaScript and Node.js are non-blocking, meaning that if you do not enclose the instructions in delayed constructs then they will execute and return immediately, meaning that they will not have sufficient time to execute.
Student Activity: Up, Across, Down
Let's return to out old friend - the Up, Across, Down activity. This time you need to write a script to complete the flight manoeuvres completely autonomously!
To make things more interesting, you can only have two test flights before the real (assessed) flights - so make sure that your code is top-notch!
Place two cones 5m apart. Write the commands in the REPL to lift-off, fly to an altitude of around 3m, then fly 5m sideways (keeping the nose pointing away, tail facing towards the pilot), and then descend and land.
Once you are done compared your code with other students in the class.