-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
135 lines (121 loc) · 3.35 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
129
130
131
132
133
134
135
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, arDrone = require('ar-drone')
, fs = require('fs')
, io = require('socket.io');
var client = arDrone.createClient();
var lastPng = fs.readFileSync('./test/image.png');
pngStream = client.createPngStream();
pngStream
.on('error', console.log)
.on('data', function(pngBuffer) {
lastPng = pngBuffer;
});
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.get('/image.png', function(req, res) {
res.writeHead(200, {'Content-Type': 'image/png'});
res.end(lastPng);
});
http = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
io = io.listen(http);
var lastDroneState = null;
client.on('navdata', function navigationData(data) {
if (lastDroneState) {
for (key in lastDroneState) {
if (lastDroneState[key] != data.droneState) {
console.log("NAVDATA => " + key + " has become " + (data.droneState[key] ? "TRUE" : "FALSE"));
}
}
}
lastDroneState = data.droneState;
});
var speed = 1;
var mouseSensibility = 2;
var isFlipping = false;
var lastDirections = [false,false,false,false]
io.sockets.on('connection', function (socket) {
socket.on('movement', function (data) {
if (isFlipping) return;
console.log(data);
if (lastDirections != directions) {
client.stop();
if (!data) return;
var directions = data.directions;
var mouse = data.mouse;
if (directions) {
directions[0] && client.front(speed);
directions[1] && client.right(speed);
directions[2] && client.back(speed);
directions[3] && client.left(speed);
}
}
if (mouse) {
if (!isNaN(mouse[0])) {
if (mouse[0] < -mouseSensibility) {
client.counterClockwise(speed);
} else if (mouseSensibility < mouse[0]) {
client.clockwise(speed);
}
}
if (!isNaN(mouse[1])) {
if (mouse[1] < -mouseSensibility) {
client.up(speed);
} else if (mouseSensibility < mouse[1]) {
client.down(speed);
}
}
}
});
socket.on('animation', function (data) {
console.log(data);
client.stop();
if (!data) return;
client.animate(data[0], data[1]);
isFlipping = true;
setTimeout(function(){
isFlipping = false
}, data[1] * 2);
});
socket.on('light', function (data) {
console.log(data);
client.stop();
if (!data) return;
client.animateLeds(data[0], data[1], data[2]);
});
socket.on('takeoff', function () {
console.log('takeoff');
isFlipping = true;
setTimeout(function(){
isFlipping = false
}, 3000);
client.takeoff();
});
socket.on('land', function () {
console.log('land');
client.stop();
client.land();
});
});