-
Notifications
You must be signed in to change notification settings - Fork 0
/
drone-pilot.js
206 lines (138 loc) · 5.33 KB
/
drone-pilot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'use strict'
var QRAR = require('qrar');
var drone = require('ar-drone');
var Parser = require('ar-drone/lib/video/PaVEParser.js');
var ardrondeConstants = require('ar-drone/lib/constants');
var fs = require('fs');
var _ = require('lodash');
var videoRecord = require('./video.js');
var commands = require('./command.js');
// TODO Read from command line
var videoFolder = './video';
var logFolder = './log';
// Cliente para acceder al dron
var client = drone.createClient();
client.config('general:navdata_demo', 'TRUE');
client.config('video:video_channel', 0); // La camara 0 es la frontal, la 3 es la de abajo
client.config('general:navdata_options', 1 << ardrondeConstants.options.MAGNETO); // Abilita la recopilación de los datos del magnetometro
// Crea las variables para guardar los datos de vuelo
var fligthName = new Date(Date.now()).toISOString();
var navdataHistoryStream = fs.createWriteStream(`${logFolder}/history-navdata-${fligthName}.csv`, {flags: 'a'});
var navdataMagnetoStream = fs.createWriteStream(`${logFolder}//history-magneto-${fligthName}.csv`, {flags: 'a'});
// Imprime las cabeceras de los ficheros csv con los datos de vuelo
navdataHistoryStream.write('timestamp,navdata\n');
navdataMagnetoStream.write('time,mx,my,mz,rawx,rawy,rawz\n');
// Cuando hay datos de navegación, se guardan en los ficheros
client.on('navdata', function(data) {
var time = Date.now();
var navdatatxt = JSON.stringify(data);
var csvRowHistory = `${time},${navdatatxt}\n`;
navdataHistoryStream.write(csvRowHistory);
// Check if magneto data is available
if( data.magneto ) {
var csvRowMagneto = `${time},${data.magneto.mx},${data.magneto.my},${data.magneto.mz},${data.magneto.raw.x},${data.magneto.raw.y},${data.magneto.raw.z}\n`;
navdataMagnetoStream.write(csvRowMagneto);
}
});
// Empezar a grabar
videoRecord.record(client, {
videoFolder: videoFolder,
name: `video-${fligthName}.m4v`
});
// Programa que escanea los codigos QR
var codes = new QRAR(client);
var lastExecutedCode = '';
var isFirstQRDetected = true;
var isReady = true;
function iterateArrayPausable(array, cb) {
let i = 0;
function loop () {
let value = array[i];
let wait = _.startsWith(value, 'wait') ? parseInt(value.replace('wait(', '').replace(')', '')) : 0;
let operation = _.startsWith(value, 'wait') ? ()=>{} : console.log;
setTimeout(function () {
operation.call(null, value);
i++;
if (i < array.length) {
loop();
} else {
if( cb)
_.defer(cb.bind(null));
}
}, wait);
}
loop();
}
function executeCodeAfter(code, time, cb) {
setTimeout(() => {
if( ! commands.isValidCode(code) ) {
throw new Error('Code not valid: ' + code);
return;
}
var selectedCommands = commands.getCommand(code);
iterateArrayPausable(selectedCommands, cb);
}, time);
}
// Cuando se detecta un QR se procesa
codes.on('qrcode', function (code) {
if( isReady ) {
//console.log("Code:", code);
isReady = false;
// Check if it is a configurable code
if( ! commands.isDefaultCode(code) ) {
// This is a configurable code and is 1 char repeated N times
// It separate the code char by char, groups by equals char, sorts them and take the char with most occurences
code = _.chain(code)
.words(/./g)
.groupBy()
.sortBy((el) => el.length * -1) // Descending
.first().first()
.value();
}
// Check if the code is different from the previous one. Executing the same code twice is not allowed
if( lastExecutedCode === code ) {
isReady = true;
return;
}
if( ! commands.isValidCode(code) ) {
isReady = true;
return;
}
//console.log("--- Code:", code);
// Is start code
if( isFirstQRDetected && commands.isStartCode(code) ) {
executeCodeAfter('takeoff', 0, function() {
executeCodeAfter('stop', 3000, function() {
executeCodeAfter('calibrate', 5000, function() {
executeCodeAfter('enable-bottom-camera', 2000);
executeCodeAfter('initiator', 2000, function() {
isReady = true;
isFirstQRDetected = false;
lastExecutedCode = code;
});
});
});
});
return;
}
if( ! isFirstQRDetected ) {
executeCodeAfter('stop', 0, function() {
executeCodeAfter(code, 2000, function() {
// Check if it is final code
if( commands.isFinalCode(code) ) {
isFirstQRDetected = true;
executeCodeAfter('enable-front-camera', 0);
}
isReady = true;
lastExecutedCode = code;
});
});
return;
}
// Nothing
isReady = true;
return;
}
});
// Empezar a detectar QR
codes.start();