Programming the Parrot AR 2.0 Drone – Beginners Guide 2

In the previous guide we used NodeJS and some basic JavaScript to control the drone, however this is all a bit static – what if we want to control the drone in real-time during flight, rather than having to pre-define a route for it to fly? Or what if we want to view a live feed from the drone? This is where WebFlight comes in. WebFlight is a superb open-source project created by Laurent Eschenauer that can be downloaded/cloned for free from Laurent’s GitHub repository.

What are we going to do?

  1. Go over thenewboston tutorial on installing Git bash
  2. Download (Clone) WebFlight and install required components
  3. Configure WebFlight
  4. Download and install a program to render the feed from the drone’s camera as a live stream in our web browser (FFMPEG)
  5. Start flying our drone in real time using our laptop/PC keyboard!

Stuff you’ll need

Same as last time (mostly)

In this guide you’ll need exactly the same bits and pieces as in the previous guide  (Win10 laptop/PC with Wi-Fi, drone, Notepad++). You’ll also need to have completed the previous guide to ensure you have NodeJS installed as well.

Aside from that we also need an application called Git installed. We will need to use ‘Git Bash’ in order to get all the bits and pieces we need set up.

Git

Git is a piece of software that falls under the bracket of Version Control Software (VCS). Git’s job is to make sure files are up to date as well as keeping a history of the different versions of files up to the current point, just in case you need to revert to an older version.

Going into detail about Git and its uses is a little out of the scope of this guide, but you absolutely need Git installed on your laptop/PC to get WebFlight installed correctly. Bucky Roberts over at thenewboston.com has put together a great series of easy to understand video tutorials on getting up and running with Git so I would recommend you go through at least the first tutorial on installing Git before continuing. The tutorials after the first one are great if you have a GitHub account and want to understand a bit more about Git and its uses. For the sake of this guide, we only need Git to do some very basic stuff so it’s not totally necessary to know loads about it.

Also, have a look around the tutorials on thenewboston.com, there are absolutely loads of really well put together and clearly explained tutorials on programming. It’s an absolute goldmine of programming knowledge!

Let’s get to it!

1. Go over thenewboston.com’s tutorial on installing Git Bash

Make sure you’re familiar with Git (Git Bash) before going any further; this will ensure that you can get everything set up ready to use WebFlight! Head over to thenewboston’s Git tutorials here .

2. Download (Clone) WebFlight and install required components

Scrolling down a bit on the WebFlight GitHub page you’ll see the below instructions for installation. The yellow highlighted line has been added by me as I found that bower wasn’t recognised by the computer without this (bower install will fail, unknown command).

If you still have your ‘Drone’ folder from the previous guide and you have Git Bash installed, open the Drone folder and right click an empty area in the folder, now choose ‘Git Bash Here’ to open the Git Bash command window. Now run the below commands in the Git Bash window one at a time.

installCommands.PNG

What we are doing by entering these commands is explained below:

  1. “git clone https://github.com/xyz…”  “Grab me my very own copy of some software stored on GitHub”
  2. “cd ardrone-webflight” – change from the current directory into the (newly cloned) ardrone-webflight directory (cd = change directory, cd )
  3. “npm install” – get the Node Package Manager to auto-detect any dependencies within the current directory and install them
  4. “npm install bower -g” – , Bower is another package manager (handles any required packages that NPM doesn’t look after) so needs to be installed seperately and given global (-g) access so you can run it from anywhere in the system
  5. “bower install” – now Bower is installed we can get it to auto-detect any dependencies within the current directory (ardrone-webflight) and install them.

Above I mentioned ‘dependencies’ a couple of times. As I described in the previous guide dependecies can be though of as tools to complete a job. In the last guide we used the command ‘require(‘ar-drone’); this is saying that in order to complete some or all of the commands in the program, it requires (or depends on) the ar-drone library being installed. As programs get bigger and bigger, they may likely use many dependencies, hence the need for dependency managers and the ability for them to automatically detect any required dependencies and get them installed ready for you. Imagine if you had a program with 50 dependencies, it would take a loooooong time to type ‘npm install abc’ 50 times manually!

The three screenshots below demonstrate the process of installing everything, and installing bower using NPM, using the –g tag to set a global path variable to it as well (this just means that you can run the command ‘bower’ anywhere and it’ll be recognised, rather than having to run it from the directory it lives in).

Clone the WebFlight repo from GitHub, then using npm install:

cloneinstallwebflight.PNG

At first your computer will not understand what bower ‘is’

bowerinstall.PNG

Use NPMto install bower and use the -g tag to set it to the computers’ global path, bower is now recognised and you can run bower install

installbower.PNG

Random error I ran into

When testing the installation of WebFlight a second time for this guide, I ran into an error when running the bower install command (last line). Unfortunately I closed the window before I thought about screenshotting it so I apologise for that. However, part of it included “exit code #128 fatal” so if you see that at all, it’s the same error.

While I was googling the error and trying to figure out how to fix it, the gremlin in the system causing this issue magically disappeared and I was able to run the bower install command without any errors at all. I literally did nothing to my PC, nadda, it just started working all of a sudden!

3. Configure WebFlight

Now that everything is installed we need to make sure that Webflight is configured as we like it. To get a basic level of operation running we won’t need to do much so we’ll do the bare minimum for now to get up and running.

If you navigate to the ardrone-webflight folder that you recently cloned from GitHub, you’ll find a file called config.js.EXAMPLE – simply put, this is the configuration file that tells Webflight which plugins to use etc. For now, rename the file to config.js (remove the ‘.EXAMPLE’ bit at the end), this will make sure that this file is recognised as the correct file for Webflight to use.

Aside from config.js, there is one other file in the same directory that is very important, that being app.js. App.js is the main, central JavaScript program for controlling the drone. This file includes links to the config.js file (hence why renaming this a minute ago was important!) as well as bindings from the keyboard to the basic drone controls etc. There’s no need to edit this right now, that’s the basic configuration done!

If you now open a command prompt window in the ardrone-webflight directory and type ‘node app.js’ you will initialise the WebFlight application! At the moment you will see a couple of errors pop up under the ‘node app.js’ command to do with something called ‘ffmpeg’ not being installed, for now don’t worry about that, simply open up your web browser and type  localhost:3000 in the URL bar at the top – press enter and you should see the following.

stockimagewf.png

You’ll notice that your drone is (probably) not pointed at a nice view of clouds, this is a stock image. The reason it displays this stock image is due to FFMPEG not being installed, this is the program used to render any images and videos from the drone to a format our browser can understand and output, so until we get that set up we’re stuck staring at the stock image!

Saying that though, it’s not all bad! We’ve got some nice HUD (Heads Up Display) elements including a compass along the bottom of the screen and some subtle instruments in the centre of the screen so we must be doing something right!

4. Download and configure FFMPEG

This section is a bit fiddly so take your time and go through it carefully!

In the previous guide I took you through how to find out if your PC/laptop is 32 or 64-bit, the same rules apply to downloading FFMPEG. If you get stuck, go back to section 2 of the previous guide for a reminder. You can download FFMPEG here.

When the download has finished, navigate to your Downloads folder and copy your new FFMPEG ZIP file (notice it has ‘.zip’ after the file name) from the Downloads folder, into the ardrone-webflight folder, inside the Drone folder (Drone\ardrone-webflight\). As the folder is a zip file, we need to ‘extract’ it to use the contents of it. You can do this by right-clicking the zip file and then selecting ‘extract here’. Rename the extracted folder to ‘ffmpeg’ for easier referencing later on.

I found FFMPEG to be a bit of a pain to install overall, however after Googling around a bit I found this guide which helped me eventually. If you run into issues installing FFMPEG, I would recommend having a look at the guide linked above, or Googling any specific errors you run into.

Within the ffmpeg folder you’ll find another folder called ‘bin’, enter this folder and copy the location of this. To do this, click on the address bar (the bit that says ‘Drone -> ardrone-webflight > ffmpeg > bin’ in the image below) and copy the whole path that shows up. See the two images below, first image is before the address bar is clicked on my computer, second image shows it when it is clicked. In my example below, I copied all of the path highlighted in blue, you need to copy the whole path shown on yours as well.

ffmpeginstall.png

getpath.png

Now we need to edit the environment variables for your computer so you can use ffmpeg from anywhere. It would make sense that you could use the –g tag like we did earlier for bower, but I haven’t figured out why that doesn’t work in this case so I’m just putting it down to FFMPEG being awkward and doing it the good old fashioned wcortana.pngay.

In Windows 10 all you need to do is start typing ‘env’ in the ‘Ask me anything’ search bar in the bottom left of your screen (Cortana I think it’s called?), you should see an option to edit the system environment variables as shown in the screenshot to the right, click this.

The next bit is a little more fiddly so I’ve included three windows in one image to try to explain the process as clearly as possible!

You should now be presented with the top left window shown in the screenshot below, click the Environment Variables… button to be taken to open the top right window from the screenshot below. Click on the Path item (PATH, path – whichever way it’s shown on your computer), then click on the Edit button to reach the window shown at the bottom of the screenshot below. Click New and then paste in the path you copied earlier from the ffmpeg/bin folder.

envvar.png

Now if we close any command prompt windows and reopen them, typing ‘ffmpeg –version’ you should see an output similar to below, if so, FFMPEG is installed and ready to be used with WebFlight!

ffmpeginstalled.png

If you now navigate back to your ardrone-webflight folder and run ‘node app.js’ in command line, you should notice that the FFMPEG errors have gone and if you open ‘localhost:3000’ in your browser you are receiving a feed live from your drone! Below is my feed while writing this guide, the drone is currently looking out of the window enjoying a view of perfect English summer weather.

notstockimagewf.png

5. Controlling the drone using the keyboard

Seeing the live feed from your drone’s camera is all good and well but what about flying it? As long as your drone has at least 20% battery life (see top left of WebFlight display in your browser), you can press ‘T’ on your keyboard to take off and ‘L’ to land!

However you might notice that other commands seem to be missing. The up and down arrows seem to make drone go up and down, but what about forwards and backwards? By default the config.js file is set to use a different keyboard layout from QWERTY (UK/US layout) so we just need to change one line in there first.

Open the config.js file, you’ll notice on line 13 there is a keyboard variable set to ‘azerty’, simply change this to ‘qwerty’ to use WebFlight with a standard UK/US keyboard layout. Save and exit the file and restart WebFlight for the changes to take effect (close browser window/tab running WebFlight and use ctrl+c to stop it in the command prompt window – then re-run node app.js in command prompt and reopen localhost:3000 in your browser).

editconfig.png

Once you’ve reconfigured Webflight in the config.js file, start Webflight up again with ‘node app.js’ (make sure you’ve closed down all other windows running Webflight first). You’ll notice that you can run the following commands in the browser window:

  • W – Forward (pitch)
  • A – Left (roll)
  • S – Backward (pitch)
  • D – Right (roll)
  • Up – Increase height
  • Down – Decrease height
  • Left – Rotate left (yaw)
  • Right – Rotate right (yaw)
  • F – Flip (make sure you’ve got lots of room around the drone!)

That’s all for now

With any luck you now have a drone flying around according to your controls, as well as a feed from the drones’ camera in the web browser (localhost:3000)! Check out Laurent Eschenauer’s WebFlight repository on GitHub for more controls, plugins etc. As always, if you ran into any issues getting up and running with WebFlight please feel free to comment below and we’ll look into it together, alternatively head over to the WebFlight repository linked above and click the ‘issues’ tab to search any issues/fixes other users ran into!

In the next guide we’ll look into installing and some additional plugins for Webflight to extend its functionality even further!

Thanks for reading,

Mark

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