This repository has been archived by the owner on Jun 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
105 lines (96 loc) · 2.1 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
var arDrone = require("ar-drone");
var drone = arDrone.createClient();
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app,{log:false})
, fs = require('fs');
require("dronestream").listen(app);
app.listen(1025);
var toClientData;
var setToClientData = function(data){
var alertstate;
var english;
if(data.droneState.flying){
alertstate = "success";
english = "flying";
}
else{
alertstate = "info";
english = "not flying";
}
toClientData = {
'droneState':{
'alertstate':alertstate,
'english':english,
'batteryPercentage':data.demo.batteryPercentage
},
'flightDynamics':{
'pitch':data.demo.rotation.pitch,
'roll':data.demo.rotation.roll,
'yaw':data.demo.rotation.yaw,
'altitude':data.demo.altitude,
}
};
}
// drone.config('general:navdata_demo', 'FALSE');
drone.on('navdata', setToClientData);
function handler (req, res) {
var requestedFile = req.url.substring(1);
if (requestedFile == ""||requestedFile[requestedFile.length-1]=="/"){
requestedFile += "index.html";
}
fs.readFile("webroot/"+requestedFile,function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.write(data);
res.end();
});
}
var timeout = 200;
var speed = 0.5;
io.sockets.on('connection', function (socket) {
socket.on('controlTrigger', function (data) {
switch(data.function){
case "up":
drone.up(speed);
break;
case "down":
drone.down(speed);
break;
case "left":
drone.left(speed);
break;
case "right":
drone.right(speed);
break;
case "forth":
drone.front(speed);
break;
case "back":
drone.back(speed);
break;
case "rotateR":
drone.clockwise(speed);
break;
case "rotateL":
drone.counterClockwise(speed);
break;
case "takeoff":
drone.takeoff();
break;
case "land":
drone.land();
break;
case "stop":
drone.stop();
break;
}
});
socket.on('getData',function(){
if(toClientData != undefined && toClientData != null){
socket.emit('nodecopterData',toClientData);
}
})
});