Introduction: Autonomous AR Parrot Drone 2.0 Flying

About: I hope to help people with the things I make.

This instructable will give you an EXTREMELY simple and quick way (< 15 minutes) to have an AR Parrot Drone 2.0 fly autonomously with code written by you! The best part of this project is it only requires the drone and a laptop (mac or windows). No modifications to the drone. I will also show you how to stream the video from the drone onto your laptop. Check out a sample program I wrote for my drone:
 


People have done CRAZY stuff with this, including having a drone that can follow peoples faces or be attracted to objects that are red. This guide is a step-by-step on how to use Felixge's node library to control your drone. Definitely check out what he's done. I did this through trial and error and pared it down to the essentials, best of luck!

Step 1: The Drone

To program your drone, you will first need a drone! I am using an AR Parrot Drone 2.0. The first version may work, but no promises. You can find them at Radioshack, Amazon, etc.

First you will need to get comfortable just controlling the drone with you smartphone/tablet. Download one of the apps such as 'AR.FreeFlight 2.4' to control it. It might be a good idea to leave the indoor bumper hull on at first to minimize damage from collisions. I also suggest flying in an open field or park. 

Step 2: Node JS

The first thing needed for autonomously programming our drone is to download Node JS. It's awesome for web applications with JavaScript and if you are interested you can learn a bit more about it. For our purposes though just download the file. I recommend making a new folder on your computer in your 'C' drive and naming it something like 'Drone'. Move the program into this new folder.

You are halfway to controlling your drone with your laptop!

Step 3: Command Line

Now we will do a bit of work with the command line. Follow the instructions for your type of computer.

FOR WINDOWS:
The command line program is called 'Command Prompt' and on recent windows versions can be opened by going to 'Start' and searching through the programs or viewing 'all apps'. If confused refer to this help. Once you have the program open type the following line of code and then hit enter:

npm install ar-drone

This will give you access to Felixge’s node.js library so that you can use high level commands such as 'takeoff()' and 'leftFlip' to control the drone. As long as the Command Prompt doesn't throw any errors you are all set to start controlling the drone with your computer! For help using the Command Prompt check this resource.


FOR MAC:
The command line program on a mac is called Terminal. The easiest way to open it is to click in the Spotlight search bar in the upper right hand of your desktop screen and type 'Terminal'. Then just click on the program called Terminal. Once open type the following line of code and hit enter:

sudo npm install ar-drone

This will give you access to Felixge’s node.js library so that you can use high level commands such as 'takeoff()' and 'leftFlip' to control the drone. As long as Terminal doesn't throw any errors you are all set to start controlling the drone with your computer! For help using Terminal check this resource.

Step 4: Testing

Now that you have Node.js installed with the ar-drone library we can control it with your laptop! To do this, first turn the drone on by plugging in the battery.

Next connect you laptop to the Wi-Fi network named something similar to 'drone######'.

Open up the node.js program. 

Type the next four lines of code into the node.js window and hit enter after each line. (WARNING: as soon as you hit enter on the third line the drone will take off and hover a few feet off the ground until you type the fourth line and hit enter again). Please make sure the drone is in an open space.

var arDrone = require('ar-drone');
var client = arDrone.createClient();
client.takeoff();
client.land();


It works! Woohoo! This is a good way to check that everything was installed correctly. If you come across any errors you may need to go back to the last step and download the 'ar-drone' library again. 

Step 5: Autonomous Program

Now the exciting part! We will make some autonomous programs for the drone. This is a huge advantage over what we just did because rather than typing the code line by line we can send a whole program to the drone and it will execute it sequentially for some flawless and exciting flight. 

To do this we will need to write programs in JavaScript. Don't fear if you don't know JavaScript we will mostly be using the same cut and paste commands. If you are interested you can read up on it HERE. We will need a text editor to write our code in, I recommend Sublime Text. Just download it and then upon opening it will open a new file we will type all the lines of our code directly into Sublime Text, as shown in the picture for this step. Then when saving the program make sure to save the file with '.js' at the end of the name and it will automatically become a JavaScript file! 

For now copy my simple program in the picture and save it as 'test.js'. This will have the drone takeoff, hover for a few seconds, and then land. Check the next step on how to run this program.

Step 6: Running Programs

To run the program 'test.js' we first need to know where we saved the file. Move the program into the 'Drone' folder you made in your 'C' drive. Once again we will need to use the command line, for Windows open Command Prompt, and for Mac open Terminal. 

Turn your drone on and connect the laptop to its Wi-Fi connection. Type the following line of code and hit enter:
node c:\Drone\test.js

What this is doing is having the node.js program run the file test.js which is stored in the 'Drone' folder in the 'C' directory. By looking at the line of code hopefully this makes sense. When running a different program only the name 'test.js' needs to be changed as long as the program is located in the 'Drone' folder.

If the drone took off, hovered, and landed you did it! 

Step 7: Functions

Now you have everything you need to autonomously fly your drone. To write more complicated programs you will just have to know a few functions that the drone is capable of. Below I show the name and describe what they do. You should also check out Felixge's README file that was included with the ar-drone library as it has tons of beneficial information.

takeoff() - has the drone takeoff and hover above the ground
land() - has the drone land
up(speed) - has the drone gain altitude at a speed between 1 (max speed) and 0 (still).
down(speed) - makes the drone reduce altitude
clockwise(speed) - drone spins clockwise
counterClockwise(speed) - drone spins counter-clockwise
front(speed)/back(speed) - changes the pitch causing horizontal movement
left(speed)/right(speed) - changes the roll causing horizontal movement
stop() - keeps the drone hovering in place
(for complex functions like flips and manuevers, check out the README file linked above)

All of these functions can be used in a long list with a dedicated time between them by using the format:

client
    .after(5000, function() {
      this.clockwise(0.5);
    })
    .after(3000, function() {
      this.stop();
    });

The amount 5000 is the time in milliseconds that the drone will turn clockwise for at 0.5 of its top turning rate (1 being max and 0 being still). We can see for the stop function that it doesn't have a speed because it takes care of that itself. By making a long list of commands like this we can set up an autonomous task list to carry out. I have copied the code that my drone ran, in the video on the first page, below:

var arDrone = require('ar-drone');
var client  = arDrone.createClient();

client.takeoff();

client
  .after(2000, function() {
    this.up(1);
  })
  .after(2000, function() {
    this.animate('flipAhead',500);
  })
  .after(1000, function() {
    this.animate('flipBehind', 500);
  })
  .after(1000, function() {
    this.animate('flipLeft',500);
  })
  .after(1000, function() {
    this.animate('flipRight', 500);
  })
  .after(5000, function() {
    this.front(1.0);
  })
  .after(2000, function() {
    this.clockwise(0.5);
  })
  .after(5000, function() {
    this.back(0.8);
  })
  .after(2000, function() {
    this.land();
  });

Hopefully this will give you a good idea of how your program outline will look.

Step 8: Streaming Video

For all the super awesome applications like face tracking you will have to stream the video from the ar drone. One easy way to do it is to download ffmpeg. After downloading go ahead and run it. A command line type window will pop open (you can see a picture of mine on this step). You will need to make a new JavaScript program using the Sublime Text editor as described in step 5. Copy and paste the code below into a file and save it as 'video.js' in your 'Drone' folder (Sorry I couldn't upload any '.js' files).

var arDrone = require('ar-drone');
var http    = require('http');

console.log('Connecting png stream ...');

var pngStream = arDrone.createClient().getPngStream();

var lastPng;
pngStream
  .on('error', console.log)
  .on('data', function(pngBuffer) {
    lastPng = pngBuffer;
  });

var server = http.createServer(function(req, res) {
  if (!lastPng) {
    res.writeHead(503);
    res.end('Did not receive any png data yet.');
    return;
  }

  res.writeHead(200, {'Content-Type': 'image/png'});
  res.end(lastPng);
});

server.listen(8080, function() {
  console.log('Serving latest png on port 8080 ...');
});

Now run this program by typing the following line into the command line window that opened when you ran ffmpeg. Don't forget to hit enter.
node c:\Drone\video.js

Go to your web browser (any one will work) and go to the following website (you can just copy/paste it):
http://localhost:8080/

You will now be getting a stream of the video that your drone is viewing through the front camera! You can see a picture of what mine was viewing along with some of my bookmarks :)

CONGRATS! You are now set to begin doing some awesome stuff with you drone. Feel free to check out Felixge's library if you are interested in using the video feed from the drone to do things such as track and follow people's faces (this uses a program called OpenCV). Best of luck.