forked from paulhayes/copterface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
128 lines (99 loc) · 2.91 KB
/
app.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
// Run this to receive a png image stream from your drone.
var arDrone = require('ar-drone');
var cv = require('opencv');
var http = require('http');
var fs = require('fs');
console.log('Connecting png stream ...');
var client = arDrone.createClient();
var pngStream = client.getPngStream();
var processingImage = false;
var lastPng;
var navData;
var flying = false;
var startTime = new Date().getTime();
var log = function(s) {
var time = ((new Date().getTime() - startTime) / 1000).toFixed(2);
console.log(time + " \t" + s);
};
client.config('control:altitude_max', 3000);
pngStream
.on('error', console.log)
.on('data', function(pngBuffer) {
lastPng = pngBuffer;
});
var foundFace = function() {
client.animateLeds('blinkGreenRed', 5, 2);
console.log('running away!');
client.clockwise(1);
setTimeout(function() {
client.stop();
client.front(0.1);
}, 1000);
setTimeout(function() {
client.stop();
}, 2000);
};
function flip() {
client.animate('flipBehind', 1000);
setTimeout(function() {
console.log('stopping and going down');
client.stop();
client.land();
}, 2000);
}
var detectFaces = function() {
if (!flying) return;
if ((!processingImage) && lastPng) {
processingImage = true;
cv.readImage(lastPng, function(err, im) {
var opts = {};
im.detectObject(cv.FACE_CASCADE, opts, function(err, faces) {
var biggestFace;
for (var k = 0; k < faces.length; k++) {
face = faces[k];
if (!biggestFace || biggestFace.width < face.width)
biggestFace = face;
//im.rectangle([face.x, face.y], [face.x + face.width, face.y + face.height], [0, 255, 0], 2);
}
if (biggestFace) {
foundFace();
}
processingImage = false;
//im.save('/tmp/salida.png');
}, opts.scale, opts.neighbors, opts.min && opts.min[0],
opts.min && opts.min[1]);
});
}
};
var faceInterval = setInterval(detectFaces, 150);
client.takeoff();
client.after(5000, function() {
log("going up");
this.up(1);
}).after(1000, function() {
log("stopping");
this.stop();
flying = true;
});
client.after(30000, function() {
flying = false;
this.stop();
this.land();
});
client.on('navdata', function(navdata) {
navData = navdata;
});
var server = http.createServer(function(req, res) {
if (!lastPng) {
res.writeHead(503);
res.end('Did not receive any png data yet.');
return;
}
res.writeHead(200, {
'Content-Type': 'image/png'
});
res.end(lastPng);
});
server.listen(8080, function() {
console.log('Serving latest png on port 8080 ...');
});