Control a Parrot AR Drone with Linino

Performing the control of the AR.Drone with the iOS or Android apps is really difficult. The controls are very unstable because the app virtual joystick hasn’t a good sensitivity response. The idea was to use a real joystick instead of the Parrot application itself :

Ingredients:

So, to create an application to control the quadcopter, you need Linino with Node.js and the Ideino development environment on-board, that include a library for managing the pins of the board in Node.js. For installing Node.js and Ideino on the Yun, please refer to this guide: http://wiki.linino.org/doku.php?id=wiki:nodejscript

After setting up Node.js and Ideino, we looked around for a node module implementing all the necessary commands to pilot the AR.Drone. We found that the most used is ar-drone. The source code of the ar-drone module is available on GitHub( https://github.com/felixge/node-ar-drone). ar-drone module come with a detailed documentation from which i deduced that the main commands for controlling the AR.Drone:

  • take-off

  • land

  • stop

  • left / right

  • front / back

  • up /down

  • clockwise / counter clockwise

The following picture show how we associate the ar-drone commands with the joystick shield controls

joystick_commandsNote that the Take Off / Landing commands are performed by a single button switch, located under the two potentiometers of the joystick

To start write the Node.js code in Ideino, we need to setup our environment. The First step is to setup the mcu with a sketch, generally to work with ideino and the ideino-linino-lib you need to load the firmata sketch that allows Linino to comunicate with the board. With ideino-linino-lib are distributed  two .hex files

  • firmata_spi.hex

  • firmata_spi_pullup_enabled.hex

The most used is the first one, but in this case, for the Joystick Shield, we need to use the second one which enables the pullup resistor in the Arduino board. See the product page for more details https://www.sparkfun.com/products/9760. To load the .hex file you need to open an ssh connection to your Arduino Yun and execute the commands below in the linux shell :

1
2
3
$ ssh root@linino.local
$ cd /opt/ideino-linino/node_modules/ideino-linino-lib/ext/firmata/firmata_spi_pullup_enabled
$ run-avrdude firmata_spi_pullup_enabled.hex

AVRDUDE

Then go to the Ideino worspace page via web browser(http://linino.local:2424) (after the root login) and create new project called “drone”, inside that we write our code to pilot the ar.drone.

WORKSPACE

Before start coding, we need to install the ar-drone module. To do that open the package.json file inside the drone project you have just created, and add the dependence of the ar-drone module. This is the default package.json file :

1
2
3
4
5
6
7
8
{
  "name": "hello-world",
  "version": "0.0.1",
  "description": "Ideino example project",
  "author": {
  "name": "Ideino Team"
  }
}

Your file should look like this :

1
2
3
4
5
6
7
8
9
10
11
{
  "name": "Drone",
  "version": "0.0.1",
  "description": "Controlling Parrot AR.Drone with Linino on Arduino Yun and Joystick Shield",
  "author": {
    "name": "John Doe"
  },
  "dependencies": {
    "ar-drone": "*"
  }
}

you can also write other informations like the name and description of the project. Finally save the package.json file, press the right mouse button on it and click Install. Ideino will run npm command for you and will install the ar-drone module inside the project directory.

PROJECT-DRONE

Finally, we are able to write the code to controls the drone: inside the “drone” project, create a new file and name it drone-joystick.js:

CODE

Import the ar-drone module and create drone object :

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

Create a board object with the ideino-linino-lib :

4
5
var linino = require('ideino-linino-lib'),
    board = new linino.Board();

Create variables that representing both the joystick buttons and controls, and board pins :

7
8
9
10
11
12
13
14
var btnUP = { pin: board.pin.digital.D4, value : 0 },
    btnDOWN = { pin: board.pin.digital.D5, value : 0 },
    btnCTNCLKWISE = { pin: board.pin.digital.D3, value : 0 },
    btnCLKWISE = { pin: board.pin.digital.D6, value : 0 },
    btnFLY = { pin: board.pin.digital.D2, value : 0 },
    btnLEFTRIGHT = { pin: board.pin.analog.A0, value : 0 },
    btnBACKFRONT = { pin: board.pin.analog.A1, value : 0 },
    led = { pin: board.pin.digital.D13, value : 0 };

Create variables for controlling the AR.Drone :

16
17
18
19
20
var flying = false,
    LRValue = 0,
    BFValue = 0,
    S1 = 0.47,
    S2 = 0.52;

Connection to the Arduino Board :

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
board.connect( function(){
 
 //Setting the pins...
 board.pinMode(btnUP.pin, board.MODES.INPUT);
 board.pinMode(btnDOWN.pin, board.MODES.INPUT);
 board.pinMode(btnCTNCLKWISE.pin, board.MODES.INPUT);
 board.pinMode(btnCLKWISE.pin, board.MODES.INPUT);
 board.pinMode(btnFLY.pin, board.MODES.INPUT);
 board.pinMode(led.pin, board.MODES.OUTPUT);
 
    //The following analogRead functions have callback that trigger when the joystick  is moved
    //LEFT and RIGHT
    board.analogRead(btnLEFTRIGHT.pin,function(data){
       LRValue = data.value / 1000;
 
       if(LRValue >= S1 && LRValue <= S2 ){
           drone.stop();
           board.digitalWrite(led.pin,board.LOW);
       }
       else{
           if(LRValue < S1 )
               drone.left( (S1 - LRValue).toFixed(2));
           else
               drone.right((LRValue - S2).toFixed(2));
 
            board.digitalWrite(led.pin,board.HIGH);      
       }
    });
 
    //BACK and FRONT
    board.analogRead(btnBACKFRONT.pin,function(data){
       BFValue = data.value / 1000;
 
       if(BFValue >= S1 && BFValue <= S2 ){
           drone.stop();
           board.digitalWrite(led.pin,board.LOW);
       }
       else{
           if(BFValue < S1 )
               drone.back((S1 - BFValue).toFixed(2));
           else
               drone.front((BFValue - S2).toFixed(2));
 
            board.digitalWrite(led.pin,board.HIGH);
       }
    });
 
 
    //The following digitalRead functions are associated with the shield Buttons switch.        
    //TAKE OFF and LANDING
    board.digitalRead(btnFLY.pin,function(data){
        try{
            if(data.value == 0 ){
                board.digitalWrite(led.pin,board.HIGH);
 
                //console.log('cmd');
                if(flying == false){
                    console.log("go take off");
                    drone.takeoff();
 
                }
                else{
                    console.log("go landing");
                    drone.stop();    
                    drone.land(); 
                }
                flying = !flying;
            }
            else
                board.digitalWrite(led.pin,board.LOW);
        }
        catch(err){
            console.log(err.message);
        }
    });
 
    //CLOCKWISE
    board.digitalRead(btnCLKWISE.pin,function(data){
        if(data.value == 0){
            board.digitalWrite(led.pin,board.HIGH);
            drone.clockwise(0.3);            
        }
        else{
            drone.stop();
            board.digitalWrite(led.pin,board.LOW);
        }
    });
 
    //COUNTER CLOCKWISE
    board.digitalRead(btnCTNCLKWISE.pin,function(data){
         if(data.value == 0){
             board.digitalWrite(led.pin,board.HIGH);
            drone.counterClockwise(0.3);
         }
        else{
            board.digitalWrite(led.pin,board.LOW);
            drone.stop();
        }         
    });
 
    //UP
    board.digitalRead(btnUP.pin,function(data){
        if(data.value == 0){
            board.digitalWrite(led.pin,board.HIGH);
            drone.up(0.3);
        }
        else{
            board.digitalWrite(led.pin,board.LOW);
            drone.stop();
        }        
    });
 
    //DOWN
    board.digitalRead(btnDOWN.pin,function(data){
         if(data.value == 0){
             board.digitalWrite(led.pin,board.HIGH);
            drone.down(0.3);
         }
        else{
            board.digitalWrite(led.pin,board.LOW);
            drone.stop();
        }         
    });
 
});

Now that the code is ready to run, you need to put the application in autorun mode, this means that the application will run automatically at the next startup/reboot of Linino. Right mouse click on drone-joystick.js  and click “Autorun”.

AUTORUN

Finally you need to tell your Arduino Yun to connect to the AR.Drone wifi hotspot, to do that connect to the luci administration panel of you Arduino Yun via web browser. Wait a few minute for the wifi reconfiguration, system restart and for autorun your Node.js file. When your file is started you will see a 1 second fast blink of the on board led (L13) of the Arduino Yun.

Go Flying !