Programming a Voice Controlled Drone Using Node and ARDrone

Quick summary: In this tutorial, we are going to program a voice-controlled drone using Node.js and Web speech API. We will use Parrot ARDrone 2.0 as our drone and Node.js + web speech API to build our application.

Introduction

Drones are amazing. I personally love the ability to sit down and fly the drone to either capture pictures/videos or just having fun.

Drones are not just for fun though. Drones are used heavily in the movie production industry, glacier exploration, military, agriculture and many more.

Consumer Drones are getting popular due to ease in regulation by many countries and great quality drones manufactured by DJI and other providers.

In this tutorial, we are going to create a program that will allow us to control and maneuver our drone using voice commands. That’s right, the drone will do as you command.

Before diving into development, do have a look at this one minute video where I am flying the drone in my hotel room (Not safe at all).

Let’s look at the things you require beforehand.

Source code and demo

LIVE DEMO

DOWNLOAD

Hardware

You need the following hardware:

You need to have a standard laptop running Windows/Mac/Ubuntu. I have tested it on Mac and Ubuntu 18.04.

Software

You must have the latest version of Node.js installed in your system. Visit this site to download Node.js.

You need to have the latest version of Google Chrome installed in your system. Visit this site to download chrome.

Understanding the drone

Let’s try to understand the built and structure of the Parrot ARDrone.

The Parrot ARDrone is a quadcopter i.e there are 4 rotating motors attached to each corner.

Each pair of opposite rotors is turning the same way. One pair is turning clockwise and the other anti-clockwise.

The drone movements are obtained by the changing pitch, roll and yaw angles.

As you can observe in the diagram above, by changing the left and right rotors speed the opposite way yields roll movement. This allows the drone to go forth and back.

By changing front and rear rotors speeds the opposite way yields pitch movement.

By changing each rotor pair speed the opposite way yields yaw movement. This allows turning left and right.

You really don’t need to learn aerodynamics to fly the drone, this is just for the basic information about the drone.

How Parrot ARDrone works

Parrot ARDrone provides its own WiFi access point. One needs to connect to it in order to send the command to the drone.

There are various mobile apps which you can use to fly the drone using your mobile. We will also use the WiFi access point to connect to the drone.

Let’s do the basic connection first. Connect the battery in the drone and it will boot the drone system.

Check your WiFi connection. You should an access point naming similar to one shown in the screenshot below.

Connect to the WiFi network. Once connected, open up the terminal and try to do a telnet to 192.168.1.1 — this is the IP address of the drone.

You should see successful telnet into the drone operating system running Linux Busybox.

The App Architecture

Our app codebase is divided into the following section:

  • User interface with Web speech API to detect voice.
  • Command filtering and matching.
  • Sending commands to the drone.
  • Live video streaming

The web speech API works with an internet connection. The reason why we are adding ethernet connection because we will connect to the drone WiFi access point and we need internet to detect our voice using web speech API.

Let’s build our app.

Coding the app

Let’s code our app. First, create a new folder and switch to it using the terminal.

Create a new Node project using the following command.

Let’s install the required dependency.

npm install — save express body-parser ar-drone

We are going to support the following commands.

  • Takeoff
  • Land
  • Up — go up about half meter distance then stops.
  • Down — go down about half meter distance then stops.
  • Go left — go left about half meter distance then stops.
  • Go right — go right about half meter distance then stops.
  • Turn — turn clockwise 90 degrees.
  • Go forward — go forward about a meter distance then stops.
  • Go backward — go backward about a meter distance then stops.
  • stop

Let’s code our server.

Here is our code to receive the command, filter it and control the drone.

Here is the HTML and JavaScript code to detect speech and send a command to Node server.

And here is the JavaScript code to handle the voice commands and sending it back to the Node server.

Let’s run the code.

Running the app

Run the app using the following command. Make sure you are connected to the drone WiFi and ethernet cable is connected to the computer as well.

Open localhost:3000 in the browser and click on Start Recognition.

Say Takeoff and see the drone taking off from the ground. Try other commands too and have fun.

Streaming live video from the drone

Create a new file inside the project folder and copy/paste this code.

const http = require(“http”);
const drone = require(“dronestream”);
const server = http.createServer(function(req, res) {
require(“fs”).createReadStream(__dirname + “/public/video.html”).pipe(res);
});
drone.listen(server);
server.listen(4000);

Here is the HTML code. Place it inside public folder.

<!doctype html>
<html>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8">
<title>Stream as module</title>
<script src=”/dronestream/nodecopter-client.js” type=”text/javascript” charset=”utf-8"></script>
</head>
<body>
<h1 id=”heading”>Drone video stream</h1>
<div id=”droneStream” style=”width: 640px; height: 360px”> </div>
<script type=”text/javascript” charset=”utf-8">
new NodecopterStream(document.getElementById(“droneStream”));
</script>
</body>
</html>

Run the code and view localhost:8080 to view the live video feed from the front camera.

Tips

  • Make sure the ground over which drone is flying is free from bug obstacles.
  • Please fly this drone indoors.
  • Always put the protective cover on the drone before a takeoff.
  • Make sure the battery is charged.
  • In case of drone acts weird, hold the drone from the bottom and flip it. It will put the drone in emergency mode and rotors will stop immediately.

Conclusion

Writing code and seeing the machine acts feels awesome and when that machine is a drone it’s extraordinary feeling. We learned how to code a program that can control the drone using our voice commands. We can code tons of awesome stuff such as face recognition using drone camera, autonomous flights, gesture recognition etc.

Let me know what you are going to build next?


Originally published at codeforgeek.com on March 27, 2019.