Programming the Parrot AR 2.0 Drone – Beginners Guide 1

Welcome to my first beginners guide on programming and flying the Parrot AR 2.0 drone! The guides I’ll be writing are based on my own experience of learning and working with the AR 2.0 drone. The aim is to write these in a way that I would’ve found useful and easily understandable when I was getting started.

Although there are other great resources out there such as this Instructables guide and this video, I wanted to write a guide for absolute beginners – explaining as much as possible as I go along. This way there is absolutely no expectation to have any prior understanding of programming or working with technology. I hope to assume as little knowledge as possible so please feel free to comment on this post if you have any questions or issues while following along.

What are we going to do?

In this guide we are going to achieve the following:

  1. Create a folder on the Desktop to work in
  2. Install an application that will allow us to send and receive information to and from our drone (NodeJS)
  3. Connect to the drone using Wi-Fi
  4. Use NodeJS to tell the drone to take off and land from our laptop/PC
  5. Write some simple JavaScript programs to save us typing lots of commands each time we want to fly the drone
  6. Add some more functionality into our JavaScript programs and understanding the use of these to control the movement of the drone

In writing this guide, I’ve used my experience of going through a fantastic guide from the Instructables website. This contains many of the commands that we’ll use, however I’ve tailored this guide to fill in any gaps in understanding that I came across, as well as explaining a little bit about the technologies we’ll need to use along the way.

Stuff you’ll need

Before we get into anything, these are the things you’ll need to get going with this guide.

1. Windows 10 PC/laptop with Wi-Fi

For these tutorials, I am using a Windows 10 PC with a USB Wi-Fi adapter to connect to the drone, if you don’t have Wi-Fi capability on your PC, something like this USB Wi-Fi adapter should solve that for you.

If you have a Windows 10 laptop, you should be all set (built-in Wi-Fi) 🙂

Windows: Other versions of Windows should be fine, I originally got this all working on a Windows 7 laptop so that should be fine, Windows 8 (and 8.1) are notoriously awkward operating systems however so there may be a need to Google some bits and pieces if you run into issues. Feel free to comment on this post with any issues as well though and we’ll try to work through them.

Linux/Mac: At the moment I’m looking into getting a MacBook of my own so one day I should be able to try this all out on Mac and write a guide on that if needed. Might as well wait to do Linux until that point as well!

2. A drone!

You’ll also need a Parrot AR 2.0 drone for this guide. You can find these on Amazon for under £200 brand new (at the time of writing), but check the used/new options from other sellers as I managed to get it even cheaper through Amazon Warehouse Deals, just with a slightly damaged box! Click this link to see the current cost of these drones on Amazon (UK).

3. Spare battery (optional, but recommended)

I would definitely recommend getting hold of an additional battery for the drone as the flight time on the standard battery (1000mAh Li-ion polymer) is around 10-12 minutes, give or take. I got hold of a 1500mAh battery to use while the other one is charging which worked out quite nicely. The 1500mAh I bought can be found here and will allow for about 18 minutes of flight time. This will cost around £40 which seems like a bit of a sting to the wallet but it with save you twiddling your thumbs for around an hour while you’re waiting for a battery to charge!

Third-party batteries: If you’re going to get a spare battery, I would absolutely recommend getting an official Parrot one rather than from another manufacturer. Although these batteries may well be a bit cheaper and probably absolutely fine for use, I remember reading some horror stories online when I originally started looking for a spare battery.

4. Notepad++

Aside from the above, I would also recommend downloading Notepad++ as we’ll be fiddling with some code in JavaScript. Notepad++ is a nice, basic text editor that understands code formatting so will highlight elements of code for us, which will really help anyone get to grips with basic coding.

Let’s get to it!

1. Create a folder to work from

Just before we get into installing NodeJS, the first thing I did was create a folder on the Desktop called ‘Drone’ which contained an empty folder called ‘nodejs’. When we install NodeJS below we’ll change the default install location from C:\Program Files\nodejs\ to C:\Users\Mark\Desktop\Drone\nodejs\ (just change ‘Mark’ to the user name on your PC!)

folderondesktop.png

2. Download and Install NodeJS

Now we’ve got a place for NodeJS and our other files to live, click this link to view the available NodeJS downloads.

The version you download will ultimately depend on the operating system you are running, in Windows you can check this in the following ways:

  • Windows 7 – right click the ‘Computer’ option in the start menu and click the ‘Properties’ option
  • Windows 8 and 10 – hold down the windows key (key in-between CTRL and ALT on the bottom left of keyboard) and press E to open the Windows File Explorer, then right click ‘This PC’ on the left hand side of the file explorer window and click ‘Properties’.

The resulting window should contain the ‘System’ section as shown below.

system64bit.png

According to the result given above for my PC, I’m running a 64-bit operating system (‘System type’ field) so I’ll need to download the 64-bit version of NodeJS (use the .msi installer) as shown by the red box in the next screenshot.

nodewebsite.png

Clicking the link in the red box above will start to download NodeJS, once the file has downloaded double-click it in your Downloads folder to start the installation process. Accept the license agreement and click Next on each screen if you’re happy with the defaults, apart from the screen that includes the below (install location of NodeJS).

installingnode.png

Click the ‘change’ button and in the new window that opens, type  ‘C:\Users\<yoursystemusername>\Desktop\Drone\nodejs\’ as the location to install NodeJS, click OK to confirm and close the new window and then click Next on the original window for all further steps of the installation.

We now have NodeJS installed on our machine! There’s just one last step to complete which will make sure that NodeJS knows what a drone is and how to work with it. To do this we need to use the windows command prompt within the nodejs folder – do this by holding down the left ‘shift’ key on your keyboard and right click an empty area inside the nodejs folder, then choose ‘Open command window here’ as shown below (ignore any additional files you can see in the background, you’ll create these soon!).

openingcmdfromlocation.png

At this point you should have a command window open much like the one below.

cmdwindowopen.png

Type in npm install ar-drone and press enter.

The result should look something like the output shown below, ignore the yellow warning (WARN) messages.

installingardrone.png

What this has done is tell the Node Package Manager that we need a new set of ‘tools’ (or ‘package’ of tools), the tool in question is one created by Felixge which enables the NodeJS application to work with our drone and perform commands such as take-off, land etc.

If you head to the npm website, you can search for available npm packages for a range of uses, below I’ve entered ‘ar-drone’ to see the packages available for our drone.

npmsite.png

3. Connect to the Drone

First things’ first, if you’re using a desktop PC like me – make sure you have a wireless network adapter (see recommended USB Wi-Fi device mentioned earlier in this guide), and be sure to test it works by connecting to your Wi-Fi at home and accessing the internet through your web browser.

Attach a charged battery to the drone and set it on the side for now, the lights should come on and the propellers should perform a quick test (small jolt of each propeller, don’t worry it won’t take off until you tell it!).

Assuming your Wi-Fi is working as expected on your PC, a new connection should appear if you click the Network icon in the system tray (bottom right of your screen), ‘ardrone2_062272’ in my case. Click on your ardrone2_xxxxxx network, and then click the ‘Connect’ button to establish the connection between your laptop/PC and the drone.

connectingbywifi.png

Note: I’m not under police surveillance (promise), my Wi-Fi network was named by my housemate!

When you’ve connected to the drones’ Wi-Fi it will say ‘No internet, open’ regarding the connection to your drone, don’t worry about this at all, it’s only showing this as your PC would normally expect a connection to a router/hub that provides internet access to your machine, our drone isn’t that clever unfortunately.

4. Running NodeJS and controlling the drone (takeoff and land)

Once you’re on the Wi-Fi of your drone, open the node program that we installed into ‘…Desktop\Drone\nodejs\’ earlier, double-click node.exe (green hexagon-shaped icon with a white ‘n’) to open the NodeJS terminal window.

NodeJS will be your means of sending commands to the drone and receiving data back (such as receiving a live stream from the drone to your PC, which we’ll get into in the next guide). Now that we have the NodeJS terminal window open, we can start typing in some commands to explicitly establish our connection between the NodeJS application and the drone to start sending it commands.

At this point I’m going to borrow some bits from the Instructables guide as they do an amazing job of going over the commands. To save you having to jump between different guides, I thought it would be better to copy and paste a couple of images from their guide (all credit for the below images of code to Instructables!)

In case you’d like to use the guide though, the Instructables guide can be found here.

If you’re still with me, we’re going to start entering some stuff into the NodeJS command window we recently opened (node.exe). The image below shows four lines that each needs to be entered into the NodeJS command window, one-by-one. Before we enter these commands, let’s go over each one briefly:

  • var arDrone = require(‘ar-drone’); this command lets NodeJS know that it needs to use the library of tools called ar-drone that we downloaded earlier to complete some or all of the commands that follow.
  • var client = arDrone.createClient(); we’re now getting a bit into networking, NodeJS is what’s called a ‘server-side’ language so its job is to provide stuff (commands, data, files etc.) to ‘clients’. In this case we’re telling Node that our drone is the client and we’re about to provide it some instructions, simple as that!
  • client.takeoff(); so we’ve told Node that our ‘client’ is the drone, this line is telling the drone to go ahead and run a command from the ar-drone library (see first command) called takeoff(). No prizes for guessing what happens when this command is executed!
  • client.land(); same idea as client.takeoff(); – it makes the drone land with the command called land() in the ar-drone library.

instructablescodescreenshot.png

So now that we’ve gone over each command hopefully you have a better idea of how everything we’ve done up to this point fits together. We need NodeJS to send data to and from our drone, but to do that we need the package of tools called ‘ar-drone’ to ensure NodeJS understands what a drone is and how to communicate with it.

Once you’re happy with the above ideas, go ahead and enter each command. I’ve written them out below so you can copy and paste them into the NodeJS window a line at a time if you like. Word of warning, when the drone takes off it will hover about a meter off the ground and stay roughly there until you tell it to land. Just in case you need to stop/land it quickly for whatever reason, I would make sure you copy the command to land the drone (highlight command with cursor, right click, copy) before pressing enter on the take-off command. This will make sure that you can paste the land command into the NodeJS command window (right click NodeJS window and choose Paste, ctrl+v doesn’t work!) and press enter to stop the drone whenever you need.

var arDrone = require(‘ar-drone’);

var client = arDrone.createClient();

client.takeoff();

client.land();

If you run into any errors, make sure each line ends with a semicolon (;) and you’ve run each line separately by pressing enter after each semicolon at the end of the line. If it presents you with an error about not knowing what ar-drone is, make sure you’ve used the npm install ar-drone command as shown earlier in the guide. When all is said and done your NodeJS window should look more or less exactly like mine below:

runningnodecommands.png

5. Running commands as a simple, repeatable JavaScript program instead

To write our JavaScript files I installed Notepad++ as this is a fantastic, free and simple text editor that comes equipped with code assistance tools which will help us more easily visualise the structure of our code. In case you skipped the link above, you can download Notepad++ here. You can just use Notepad it you like, but Notepad++ does make the code a little more understandable.

First thing’s first, let’s make a new Notepad++ file for our program. Open Notepad++ from wherever you installed it and enter the code shown below.

firstjsprogram.png

Now save the file as firstflight.js in the same folder as your node.exe application (e.g. …\Drone\nodejs\firstflight.js). To do this click File -> Save As…, enter firstflight.js as the ‘File name‘ and change the ‘Save as type’ option to JavaScript file (*.js) as shown below.

savingfirstjsprogram.png

If you don’t fancy writing this file for yourself, firstflight.js and some other JavaScript files (which we’ll be going over soon) can be found in my GitHub repository, feel free to download/clone this repository and mess around with the code! Alternatively, if you’re not familiar with working with GitHub/Git, typing out the files will achieve the same result.

GitHub: In case you’ve not bumped into these terms before, I’ll give you a quick explanation. GitHub is a website which software developers use to store and maintain code on. If they choose to store their code in public repositories then me or you can find it on the GitHub site, download it, play with it etc. The main major reason people use Git(Hub) as well is that it offers ‘Version Control’. This means that whenever updates are made to code,the previous versions of that code are still kept in case the creator needs to roll back to a previous version for any reason (say, the new code has a massive bug in it, but the old version didn’t).

Git: This is the application you install on your laptop or PC to manage the use of code repositories, enabling you to clone (make a copy of) someone’s code from the GitHub site (remote repository) onto your own machine (local repository) to mess around with. If you choose to set up a GitHub account, you too can create repositories of code that you can share with others and utilise Version Control.

To complete this guide, you don’t need to use GitHub at all if you don’t want to, nor do you need to install Git so don’t worry about these for now. In the next guide we will need to though so I’ll point you at some resources later on that will get you up to speed on what you need to know.

To run our program we’ll need to open the Windows Command Prompt (not the NodeJS command window) in the location of your firstflight.js file and enter the following command. Remember, to open the command window in a location in your file explorer you can hold left-shift and right click a blank area within the nodejs folder, then choose ‘Open command window here’.

node firstflight.js

Your drone should now take-off, and about 5 seconds after starting to take off it will start to land. So it’ll take-off, hover for a couple of seconds, then land.

Note: if your drone just sits on the ground with its lights flashing, it is likely you’ve got a NodeJS window open somewhere, make sure all NodeJS windows are closed! The above command calls node and tells it to run a particular program – node firstflight.js essentially means “computer, I want you to use a program called node [node.exe as we know it] to run a JavaScript program called firstflight.js, simple as that!

Another note: I asked you to open command prompt in the directory of the firstflight.js program you wrote, this isn’t mandatory, it just saves you having to type out the whole path to your program like node C:\Users\Mark\Desktop\Drone\nodejs\firstflight.js – that would be a pain to enter every time compared to node firstflight.js, much more typing involved each time!

6. Adding more commands in JavaScript

We now have a simple program that makes the drone take-off, float about a bit, then land. Altogether not too exciting, but we can add some more commands into the program to make it do a bit more for us.

When making this guide, I created a JavaScript file called secondflight.js to demonstrate a few more simple commands, these commands are described nicely in the Instructables guide so I’ve used their examples along with my own description of what each one does below.

Any reference to (speed) is a value between 0 (no speed at all) to 1 (max speed), you can replace the word ‘speed’ inside the brackets with any value from 0.1 to 1 to set the speed of the given action. (E.g. front(0.1) will make the drone go forward very slowly but front(0.9) will make it go forward at 90% of its maximum speed).

  • up(speed) – drone will gain altitude (go up)
  • down(speed) – drone reduce altitude (go down)
  • clockwise(speed) – drone spins clockwise at a given speed
  • 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

The file secondflight.js in the screenshot below demonstrates us adding a bit more functionality to our drone program. In this file we’re telling the drone to take off, go forward a bit (front) at 20% (0.2) of maximum speed, then stop after 2 seconds/2000 milliseconds of going forward (lines 10 and 11, .after(2000, function() {this.stop()}. The same is then repeated but instead of going forward again (front), we’re telling it to go backwards (back), before stopping and then landing.

secondjsprogram.png

That was pretty straightforward, so just to concrete the idea in our minds about how these commands are built and work with the drone, thirdflight.js (below) demonstrates the concept of ‘yaw’ using the clockwise and counterClockwise commands.

thirdjsprogram.png

Yaw is a movement where the drone will stay at the same height/altitude, but rotate mid-air without going forward or backwards – like putting a pen flat on its side on a table and rotate it, this is yaw! The image below shows the concept of yaw in terms of an aeroplane, linear rotation on the perpendicular axis of the object.

pitchrollyawimage.gif

Above image source – http://machinedesign.com/site-files/machinedesign.com/files/uploads/2014/06/PRY.gif

Another example of using navigational controls in NodeJS is making the drone perform a simple route around an object. This is exactly what I did when I was learning about the use of NodeJS with the drone as shown in this video..

Feel free to copy the code I wrote to perform the square route in the video linked above, this too can be found in my GitHub repository.

That’s all for now

With any luck you now have a drone flying about breaking every lampshade in its vicinity! If you ran into any issues that you couldn’t figure out please comment below and we’ll have a go at fixing it together. Similarly, if you ran into issues using this guide and were able to overcome them, please comment as well so I can update this guide with your input!

In the next guide, I’ll be taking you through how to use an amazing open-source (free) application called WebFlight (all credit to Laurent Eschenauer) to enable us to control the drone in real time using the keyboard as well as allowing us to view a live feed from the drones’ camera through our web browser.

Thanks for reading,

Mark

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s