-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
311 lines (259 loc) · 7 KB
/
index.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* drone-pet – walk your parrot ar.drone 2.0 like a pet
* Copyright (C) 2016 Jan Koppe, Jan Kruse, Saskia Geuking
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Load configuration
var config = require('./config');
var debug = require('debug')('drone-pet');
// set up connection to drone
var drone = require('ar-drone').createClient(config.drone);
// setup opencv processing
var opencv = require('opencv');
var cvstream = new opencv.ImageStream();
// TUI library
var blessed = require('blessed');
var screen = blessed.screen({
smartCSR: true,
autoPadding: true
});
var objectsBox = blessed.box({
tags: true,
style: {
border: {
fg: 'white'
}
}
});
var flyingBox = blessed.box({
top: '30%',
tags: true,
style: {
border: {
fg: 'white'
}
}
});
screen.append(objectsBox);
screen.append(flyingBox);
screen.key('f', () => {
if (flying) {
drone.land();
flying = false;
} else {
drone.takeoff();
flying = true;
}
uiUpdateFlying();
});
screen.key('r', () => {
drone.disableEmergency();
});
screen.key('q', () => {
drone.left(config.steering.speed);
setTimeout(() => {
drone.left(0);
}, 500);
});
screen.key('e', () => {
drone.right(config.steering.speed);
setTimeout(() => {
drone.left(0);
}, 500);
});
screen.key('w', () => {
drone.front(config.steering.speed);
setTimeout(() => {
drone.front(0);
}, 500);
});
screen.key('s', () => {
drone.back(config.steering.speed);
setTimeout(() => {
drone.front(0);
}, 500);
});
screen.key('a', () => {
drone.counterClockwise(config.steering.speed);
setTimeout(() => {
drone.clockwise(0);
}, 500);
});
screen.key('d', () => {
drone.clockwise(config.steering.speed);
setTimeout(() => {
drone.clockwise(0);
}, 500);
});
screen.key('u', () => {
drone.up(config.steering.speed);
setTimeout(() => {
drone.up(0);
}, 500);
});
screen.key('h', () => {
drone.down(config.steering.speed);
setTimeout(() => {
drone.down(0);
}, 500);
});
screen.key('escape', () => {
drone.stop();
drone.land();
flying = false;
follow = false;
});
screen.key('c', () => {
(follow) ? follow = false : follow = true;
uiUpdateFlying();
});
// some state variables
var flying = false;
var imgproc = 0;
var battery = 0;
var altitude = 0;
var follow = false;
// Quit on Escape, q, or Control-C.
screen.key('C-c', (ch, key) => {
drone.land();
return process.exit(0);
});
// create & connect png stream to opencv pipeline
var pngstream = drone.getPngStream();
pngstream.pipe(cvstream);
cvstream.on('data', (img) => {
var crosshair = img.copy();
// filter for pixels in colour range
img.inRange(config.filter.low, config.filter.high);
// edge detection
img.canny(0, 100);
// dilate to smooth out edge detection
img.dilate(config.filter.dilate);
// find contours in image
let contours = img.findContours();
let objects = [];
for (let i = 0; i < contours.size(); i++) {
// skip too small objects
if (contours.area(i) < config.filter.minArea) continue;
/* calculate mass-center
* http://docs.opencv.org/3.1.0/d8/d23/classcv_1_1Moments.html
*
* This will be the aiming point for the drone.
*/
let m = contours.moments(i);
let x = Math.round(m.m10 / m.m00);
let y = Math.round(m.m01 / m.m00);
let coords = coordsTranslate({x, y}, config.steering.res.x, config.steering.res.y);
coords = coordsDeadzone(coords, config.steering.ignoreRadius);
if (coords.x !== 0 && coords.y !== 0) objects.push(coords);
crosshair.line([x-5,y],[x+5,y],[255,255,0]);
crosshair.line([x,y-5],[x,y+5],[255,255,0]);
}
let mean = objectsMean(objects);
crosshair.line([mean.x+5,mean.y],[mean.x-5,mean.y],[0,255,0]);
crosshair.save('asdf.png');
uiUpdateObjects(mean, objects.length);
if (follow) {
steerTo(mean, config.steering.speed);
}
});
var coordsTranslate = (coords, w, h) => {
/*
* Translate input x and y values, respective to the width and height of the
* viewport, into vertical and horizontal values in range [-1,1].
*/
// Move coordinate origin to center of viewframe. Coordinates need to be translated
coords.x -= w/2;
coords.y -= h/2;
// Scale to [-1,1] range
coords.x *= 2/w;
coords.y *= -2/h;
return coords;
};
var coordsDeadzone = (coords, r) => {
/*
* Blank out deadzone with radius r from coordinate origin. This is done to
* avoid jittering when the object is very near to the center.
*/
if (Math.sqrt(coords.x * coords.x + coords.y + coords.y) < r) {
coords.x = 0.0;
coords.y = 0.0;
}
return coords;
};
var steerTo = (coords, speed) => {
if (coords.x > 0.0) {
drone.clockwise(speed * 1.5);
} else if (coords.x < 0.0 ){
drone.counterClockwise(speed);
} else {
drone.clockwise(0.0);
}
if (coords.y > 0.0) {
//drone.up(speed);
} else if (coords.y < 0.0) {
// drone.down(speed);
} else {
drone.down(0.0);
}
};
var objectsMean = (objects) => {
/*
* If calculate the mean of multiple object coordinates
*/
var mean = { x: 0.0, y: 0.0 };
if (objects.length === 0) return mean;
objects.forEach((coords) => {
mean.x += coords.x;
mean.y += coords.y;
});
mean.x = mean.x / objects.length;
mean.y = mean.y / objects.length;
return mean;
};
var uiUpdateObjects = (mean, count) => {
let text = '{red-fg}' + count + '{/red-fg} objects at: \n' +
'x: {green-fg}' + mean.x.toFixed(2) + '{/green-fg}' +
', y: {blue-fg}' + mean.y.toFixed(2) + '{/blue-fg}\n' +
imgproc + ' frames';
objectsBox.setContent(text);
screen.render();
};
var uiUpdateFlying = () => {
let text = '';
if (flying) text = '{red-bg} !!! FLYING !!!{/red-bg}';
else text = '{green-bg} not flying {/green-bg}';
if (follow) text += '{blue-bg} following {/blue-bg}';
else text += ' not following';
text += '\n\n' + battery + '% battery, ' +altitude + 'm altitude';
text += '\n\n press f to toggle flying, c to enable follow, r to reset';
text += '\n q-e lateral, w-s longitudinal, a-d yaw, u-h vertical, esc panic!';
flyingBox.setContent(text);
screen.render();
};
//pngstream.on('data', (buff) => {
// detectfaces(buff);
//});
// enable demo navdata to receive battery and altitude
drone.config('general:navdata_demo', 'TRUE');
drone.on('batteryChange', (data) => {
battery = data;
});
drone.on('altitudeChange', (data) => {
altitude = data;
});
// Initial screen update
uiUpdateFlying(false);
uiUpdateObjects({x:0.0,y:0.0}, 0);