The Voice-Controlled, Face Recognizing, Drone Journey – Part 2

Total Shares

Introduction to the Drone Journey

Part 2

This post is the third post in Journey to build an autonomous, voice-controlled, face recognizing drone. If you have stumbled across this I encourage you to read the following two posts first (otherwise this will not make a lot of sense):

Focus of this post

In this post I am going to pick-up where we left off and cover how you can:

  • Control the drone via a node.js application when your computer and the drone are both on the drones default wireless network
  • Install express to setup  simple web server in node-js land
  • Build a basic web application to control the drone using express js
  • Hack the drone so that it can operate on the same WPA2 secured wireless network where you have Internet access (need this for Cognitive Services)

Programmatically controlling the drone

Having interactively run the drone the next step is to build a simple node.js program you can execute.  The examples on the GitHub page do a nice job

Spin JSIf you write and save this code to a file called “Spin.js”, in your Drone development directory, then you can execute it by calling “node Spin.js“. What will happen is your drone will take off. It will start to turn clockwise for 5 seconds. Once 5 seconds is elapsed the drone will cancel the command to turn and it will land.  Press cntrl+c to exit from the node program back to your command line. Congratulations – your drone is alive!

Note: If you get no reaction when executing the Spin file check your computer has not slipped back onto your main wireless network. If it has you need to get back onto the drone network.

Running the drone from a web page

In order to control the drone from a  web page the simplest approach is to build out a web application. It turns out, thanks for the hint Lukas, that there is this great thing called express which is a minimalistic web framework for node.js.

We are going to use that to build out a simple web application. So lets get going.

  1. It is important you switch back to a network where you have Internet access first :).
  2. Go to http://www.expressjs.com/ to see what we are going to install
  3. Next go back to your command window and switch to the C.\Development\Drone directory.
  4. Now run the command “npm install express” as shown below.

Install Express for Node.Js

Congratulations. You now have the express framework installed and you are ready to get going.  It is now time to connect back to the drone network.

Next is the part is where I tell you go look at the post from Lukas in the section flying from a web page in the original blog.

There he has to code you need to essentially create two things. The first is the backend express application. It is pretty simple:

  • It listens on port 3000 when started
  • It serves a single file back to you “index.html” that exists in the same directory the application is loaded from. This means navigating to http://localhost:3000 should return you the index.html file.
  • It has two functions that can be called. The first one takes a get request that passes in takeoff and the second that takes in land. Based on what comes in the relevant command is issues just like in the program we wrote earlier.
  • Put the code shared by Lukas into a file called DroneWebServer,js and save it in c:\Development\Drone. Just remember you need to put the four lines I have below at the top, but after the line var express = require(‘express’);,  to get the library you need, to declare the drone object you will use, to define the express application object you will reference and to use the path library so you can use relative paths. None of these are in the samples from Lukas but you work it out quickly when the errors arrive.
var arDrone = require('ar-drone');
var client = arDrone.createClient();
var app = express();
var path = require('path');

The second part is the actual index.html file. Once again it is not too complex:

  • It has two basic links that when clicked fire events
  • Those events trigger the JavaScript function at the top which sends a GET request to the back-end express application and triggers the appropriate code to run.
  • Save the code shared by Lukas into a file called index.html and save it in c:\Development\Drone.

One big disclaimer here. If something goes wrong it is useful to have a second application script (not express) on standby that simply calls for the drone to land. I had situations where the app crashed and I had no way to land the drone without quickly restarting the express app 🙂

If all goes well you start up the server using “node DroneWebServer.js” and then you navigate to http://localhost:3000 and you get the web page you created as shown below.

Web Page

The next step is to stop the application and to go back onto the ardrone wireless network. Now you can run “node DroneWebServer” again and navigate to “http://localhost:3000”.

Now clicking on the Takeoff text will see the drone take off. If you then click on the Land text then it will land.  Congratulations. You can now controll your drone from a web page.

Hacking the wireless network

By now you are probably sick of constantly switching networks to get in the Internet and to interact with your drone.  If so you would not be alone.

Thankfully there is a way around this. Lukas pointed to it in his original blog and it is to make use of a script that lets you basically hack your drone to make it run on a WPA2 network of your choosing.

Obtain the script files

This changes it from serving up a wireless network to joining one.

Here are the steps to follow:

  1. Navigate to  https://github.com/daraosn/ardrone-wpa2.
  2. Look for the green button that says “Clone or download” and click on that.
  3. Select Download Zip and then “Save As”
  4. Navigate to your c:\Development\Drone directory and save the ZIP file there
  5. Assuming you have a way to unzip archives you can now right click on the zip file and select “Extract All”.
  6. When you extract the ZIP archive it will ask where you want to extract the files to. This should say “c:\Development\Drone\ardrone-wpa2-master” and if it does then edit it so it just says “c:\Development\Drone” then simply click extract.
  7. You should now have a directory called ardrone-wpa2-master which contains the directories bin and script.

DroneWPADirectory

Once you have the files extracted you can now make use of the BASH shell you already installed. Go ahead and start it up. You can do that using the command BASH.EXE in a command window or you can simply find it on your start menu.

Once opened you need to navigate to folder where you just extracted the files. A lot here depends on where your BASH shell places you when it opens but for me I need to do

cd ..
cd ..
cd mnt
cd c
cd Development
cd Drone
cd ardrone-wpa2-master

Having done that you should now be in the folder you need to be in.

Drone WAP Location
Install the hack

Now comes the tricky part. We will be first running a script that will enable you to have the drone connect to your wifi. It is important to note that this does not break anything on your drone. You will run this script likely only the once.

Here are the steps:

  1. Make sure your computer and the drone are on the same network. Normally, at this stage, this will be the drones network.
  2. Once your computer and the drone are on the same network you can go to your bash shell (assuming you are in the directory ardrone-wpa2-master as shown above) and run the command “script/install
  3. The script should then install and you should see the screen as shown below. Note that if you see no movement double check if you are on the drone network. My laptop automatically swapped me back at one stage and I could not work out why the upload was failing 🙁

Hack WPA

So now we have the hack in place we need to go ahead and make the drone connect to our network. The command to do this is shown below.

script/connect “Network_name” -p “password” -a “DroneIPAddress”

As you can see there is a need for you to know the name of your network, the password to join it and a static IP address you can assign to the Drone. There is a way to do this so you do not use a static IP address but then you need to be able to find the dynamically assigned Drone IP address and that needs router access or patience 🙂

This should execute resulting in a screen as shown below. Of course I have blanked out my network name and passwords here but I wanted to show what should happen.

Network WPA

The first time I ran this I got a telnet error and nothing else at the end. It turned out my computer had moved back to the main network not the drone network so they could nto communicate as an FYI to you.

If all goes well you should see the message “connection closed by a foreign host”. What should now have happened is the drone private network should not be showing up on your computer as the drone is now joining the network you provided.

Now you should make sure the computer and the drone are on the same network. We have one last thing to do.

  1. telnet to the drone by using the command telnet DroneIPAddress replacing DroneIPAddress with whatever IP address you used when running the previous script. In my case it was 192.168.2.170
  2. Once connected you need to update the routing table. This requires you to use the command route add default gw “192.168.2.1” ath0 where the IP Address of your router/gateway is inserted to replace the 192.168.2.1 I have used here.

An Image of me issuing these commands can be seen below.

Connect Drone To Net

At this point the drone and the computer should now hopefully both be on the same network you regularly use. This will be important when we want to call out to the Internet later from the computer.

An important point to make is that EVERYTIME you replace the battery or power down and back on the drone needs to be reconnected to your network. It does not remember the settings nor store them. You only need to run the connect script (remember that you need to be on the ardrone network to do that) and add the gateway route though. Takes 2 mins but I bet you will forget a few times 🙂

Adjusting code so that you can still fly the drone from the computer

Now that the drone is not to be found at its default location you need to make a small adjustment  to your DroneWebServer.js code.  In that code you need to replace the line:

var client = arDrone.createClient();

with one that specifies the IP address you will now find the drone at. This explains why a DHCP provided IP is no fun ;). It should look something like this in your file.

var client = arDrone.createClient({ip: ‘192.168.2.170’});

At this stage save the DroneWebServer.js file and go ahead and start it up by executing node DroneWebServer.js .. you can then navigate to http://localhost:3000 and get the same web page you had before from which you can control the drone.

Where are we and next steps

If you have got this far then you are in good shape. A lot of what we have done so far is familiarize ourselves with node.js, express, hacking the drone and taking it for a basic flight. We have one more block of things to do before we start to make the drone smart using Microsoft cognitive services.

In the next blog I will explain how I went about doing the following:

  • Developing out a better looking web page to control the drone and adding in a few additional control capabilities
  • Grabbing PNG images from the drone camera and having them fed back to a web page

Until then.. thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.