-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
361 lines (291 loc) · 9.64 KB
/
server.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
'use strict'
var express = require('express')
var app = express()
var http = require('http').Server(app)
var io = require('socket.io')(http)
var arDrone = require('ar-drone')
var cheasser = arDrone.createClient()
var arDroneConstants = require('./node_modules/ar-drone/lib/constants')
//Static router
app.use(express.static(__dirname + '/public'))
var walk = require('walk');
//leer de un directorio
//Dependences to autonomy and manual fly
function navdata_option_mask(c) {
return 1 << c;
}
// From the SDK.
var navdata_options = (
navdata_option_mask(arDroneConstants.options.DEMO)
| navdata_option_mask(arDroneConstants.options.VISION_DETECT)
| navdata_option_mask(arDroneConstants.options.MAGNETO)
| navdata_option_mask(arDroneConstants.options.WIFI)
);
//Initial configuration for ArDrone
cheasser.config('general:navdata_demo', false)
cheasser.config('general:navdata_options', navdata_options)
cheasser.config('video:video_channel', 1)
cheasser.config('detect:detect_type', 12)
cheasser.config('control:altitude_max', 1500)
//cheasser.config('network:ssid_single_player', 'UTTE-MMSS')
//cheasser.config('network:wifi_mode', 2)
//Require messages and functions for fly
var control = require('./Fly/Fly.js')
var video = require('./Video/Video.js')
var messages = require('./messages')
//Export functions to the server
var counter = 0;
var action = []
var number = []
//Streaming class
var fsS = require('fs')
var path = require('path')
var os = require('os')
var tmpDir = os.tmpDir()
var output
var pngStream
var lastPng
var counter
var tmpDir = os.tmpDir()
var recOn = false
var flag
class Streaming {
constructor(){
counter = 0
console.log("Generating streaming")
}
getStream(cheasser){
pngStream = cheasser.getPngStream()
pngStream
.on('error', console.log)
.on('data', function(pngBuffer) {
lastPng = pngBuffer
if(recOn == true){
getVideo.generateImages(lastPng)
}
flag = true
})
}
generateImages(bufferImage){
counter ++
output = fsS.createWriteStream(path.join(tmpDir,'img' + counter + '.png'))
output.write(bufferImage)
}
}
var getVideo = new Streaming()
getVideo.getStream(cheasser)
//require("dronestream").listen(http);
var altitude, velocity, battery, wifi, gpsState, gpsSatellite,
windSpeed, windAngle, motorProblem, usbReady, emergencyLanding, flyState, speedDrone
io.on('connection', function(socket){
var files = [];
// Walker options
var walker = walk.walk('./public/videos', { followLinks: false });
walker.on('file', function(root, stat, next) {
// Add this file to the list of files
files.push(stat.name);
next();
});
walker.on('end', function() {
//console.log(files);
socket.emit('files', files)
});
setInterval(function(){
if(flag == true){
socket.emit('frame', { image: true, buffer: lastPng.toString('base64') , binary: true}) //send video
}
}, 100)
//Socket to send drone data (altitude, velocity, etc)
cheasser.on('navdata', function(data) {
socket.emit('general-data', {altitude: altitude, velocity: velocity, battery: battery, wifi: wifi, gpsState: gpsState, gpsSat: gpsSatellite, windSpeed: windSpeed, windAngle: windAngle, motor: motorProblem, usb: usbReady, el: emergencyLanding, fly: flyState})
socket.emit('drone-ubication', {lat: currentLat, lon: currentLon, yaw: currentYaw, distance: currentDistance, speed: speedDrone})
socket.emit('drone', {lat: currentLat, lon: currentLon, yaw: currentYaw, distance: currentDistance, speed: speedDrone})
})
socket.on('control', function(ev) {
console.log('[control]', JSON.stringify(ev));
if(ev.action == 'animate'){
cheasser.animate(ev.animation, ev.duration)
} else {
cheasser[ev.action].call(cheasser, ev.speed);
}
})
socket.on('event', function(event){
if(event == 82){
io.emit('autonomy', 'next');
} else if(event == 86) {
video(socket)
} else if(event == 78) {
var execute = require('./mission/index.js')(parrot)
} else if(event == 'rec'){
recOn = event
} else if(event == 'droneStream'){
console.log('dronestream')
pngStream
} else {
control(cheasser, event)
console.log(event)
}
io.emit('message', '#Hibou:~ ' + messages[event]);
});
socket.on('rec', function(state){
recOn = state
});
//Socket to stop drone
socket.on('stop', function(event){
cheasser.stop();
io.emit('message', '#Hibou:~ Stop');
});
socket.on('configuration', function(event){
console.log(event[0])
console.log(event[1])
if(event[0] == 'usbRecord')
cheasser.config('video:video_on_usb ', event[1])
if(event[0] == 'altitude-max')
cheasser.config('control:altitude_max', event[1] * 1000)
if(event[0] == 'detec-type')
cheasser.config('detect:detect_type', event[1])
});
//GPS section
socket.on('reset', function(data){
console.log('reset', data)
cheasser.disableEmergency()
})
socket.on('go', function(data){
console.log(data)
targetLat = data.lat
targetLon = data.lon
})
socket.on('stopMap', function(data){
stop()
})
socket.on('delete-video', function(data){
var fileToDelete = './public/videos/' + data
fsS.unlinkSync(fileToDelete)
files = []
var walker = walk.walk('./public/videos', { followLinks: false });
walker.on('file', function(root, stat, next) {
// Add this file to the list of files
files.push(stat.name);
next();
});
walker.on('end', function() {
console.log(files);
socket.emit('files', files)
});
})
socket.on('backup-video', function(data){
console.log("vidiio")
console.log(data)
var fileToBackup = './public/videos/' + data
var SaveFileOn = './public/videobackup/' + data
fsS.createReadStream(fileToBackup).pipe(fsS.createWriteStream(SaveFileOn))
})
socket.on('go-start', function(data){
var intervalStop
var timeOutUp
console.log('gostart')
cheasser.takeoff()
var timeOutUp = setTimeout(function(){
cheasser.up(data.altitude)
clearTimeout(timeOutUp)
//console.log('subiendo')
}, 5000)
intervalStop = setInterval(function(){
console.log()
if(altitude > 1.8){
cheasser.stop()
//console.log('stop')
targetLat = data.lat
targetLon = data.lon
clearInterval(intervalStop)
}
},100)
})
})
/*GPS Section elements and functions*/
var PID = require('./PID');
var vincenty = require('node-vincenty');
var yawPID = new PID(1.0, 0, 0.30);
var targetLat, targetLon, targetYaw,
cyaw, currentLat, currentLon,
currentDistance, currentYaw, phoneAccuracy
var stop = function(){
//console.log('stop', data)
targetYaw = null
targetLat = null
targetLon = null
cheasser.stop()
}
var handleNavData = function(data){
//console.log(data)
if(data.demo){
altitude = data.demo.altitudeMeters
velocity = data.demo.velocity
battery = data.demo.batteryPercentage
wifi = data.wifi
flyState = data.demo.flyState
}
if(data.gps){
gpsState = data.gps.gpsState
gpsSatellite = data.gps.nbSatellites
speedDrone = data.gps.speed
}
if(data.windSpeed){
windSpeed = data.windSpeed.speed
windAngle = data.windSpeed.angle
}
if(data.droneState){
motorProblem = data.droneState.motorProblem
usbReady = data.droneState.usbReady
emergencyLanding = data.droneState.userEmergencyLanding
}
if ( data.demo == null || data.gps == null) {
//console.log('demo or gps null')
return
}
currentLat = data.gps.latitude
currentLon = data.gps.longitude
currentYaw = data.demo.rotation.yaw;
//console.log(data.gps)
if (targetLat == null || targetLon == null || currentYaw == null || currentLat == null || currentLon == null) {
//console.log("broken")
return
}
var bearing = vincenty.distVincenty(currentLat, currentLon, targetLat, targetLon)
console.log(bearing.distance)
if(bearing.distance > 1){
//console.log('goRoute')
currentDistance = bearing.distance
console.log('distance', bearing.distance)
console.log('bearing:', bearing.initialBearing)
targetYaw = bearing.initialBearing
console.log('currentYaw:', currentYaw);
var eyaw = targetYaw - currentYaw;
console.log('eyaw:', eyaw);
var uyaw = yawPID.getCommand(eyaw);
console.log('uyaw:', uyaw);
var cyaw = within(uyaw, -1, 1);
console.log('cyaw:', cyaw);
cheasser.clockwise(cyaw)
cheasser.front(0.01)
} else {
targetYaw = null
io.sockets.emit('waypointReached', {lat: targetLat, lon: targetLon})
console.log('Reached ', targetLat, targetLon)
stop()
}
}
cheasser.on('navdata', handleNavData);
function within(x, min, max) {
if (x < min) {
return min;
} else if (x > max) {
return max;
} else {
return x;
}
}
//If server it's ready print port
http.listen(3000, function(res){
console.log("listen port 3000");
});