Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions HTML5Application/public_html/admin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,43 @@
<html>
<head>
<title>Droid admin</title>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<!--<meta http-equiv="Content-Type" content="text/html;charset=windows-1251"/>-->
<style type="text/css">
#tele {
width: 60px;
border: 1px solid red;
* {
font-family: Verdana;
}
</style>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<script type="text/javascript">
window.onload = function() {
window.onload = function () {
var source = new EventSource('/dron/state');
source.addEventListener('message', function(event) {
var s = '',
d = new Date;
source.addEventListener('message', function (event) {
var s = '', d = new Date;
var data = JSON.parse(event.data);
if (event.data) {
s += '<table>';
s += '<tr>';
s += '<td>State:</td>';
s += '<td>' + data.readystate + '</td>';
s += '</tr>';
s += '<tr>';
s += '<td>Timestamp:</td>';
s += '<td>' + (d + '.' + d.getMilliseconds()) + '</td>';
s += '</tr>';
for (var i in event.data) {
s += '<tr>';
s += '<td>' + i + ':</td>';
s += '<td>' + event.data[i] + '</td>';
s += '</tr>';
for (var i in data.demo) {
s += '<tr>';
s += '<td>Drone.' + i + ':</td>';
s += '<td>' + data.demo[i] + '</td>';
s += '</tr>';
}

for (var i = 0, n = data.qrcodes.length; i < n; ++i) {
s += '<tr>';
s += '<td>QR-code ' + (i + 1) + '</td>';
s += '<td>' + data.qrcodes[i] + '</td>';
s += '</tr>';
}

s += '</table>';
}
document.getElementById('tele').innerHTML = s;
Expand Down
4 changes: 2 additions & 2 deletions HTML5Application/public_html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<!DOCTYPE html>
<html>
<head>
<title>Droid</title>
<meta charset="utf-8" />
<title>Droid</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
Expand Down
1 change: 1 addition & 0 deletions HTML5Application/public_html/js/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@
_onImgChanged: function (_sImage) {
if (this._canvas) {
this._canvas.setBackgroundImage(_sImage);
this._canvas.renderAll();
}
},

Expand Down
209 changes: 96 additions & 113 deletions NodejsServer/ardrone.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
var http = require('http'),
var http = require('http'),
express = require('express'),
qrcode = require('./qrcode'),
drone = require('./drone').drone,
qrcode = require('./qrcode'),
drone = require(!process.env["drone_fake"] ? './drone' : './fakedrone').drone,
app = express();
var _DRONE_IP_ADDRESS = '192.168.1.1';


drone.options = {
'general:navdata_demo': 'FALSE',
'video:video_channel': parseInt(process.env["drone_video_channel"]) || null
};

drone.features = {
'motion-filter': process.env["drone_motion_filter"] ? true : false,
'motion-smooth': process.env["drone_motion_smooth"] ? true : false,
'motion-smooth-factor': parseInt(process.env["drone_motion_smooth_factor"]) || 10
};


// Configuration
app.configure(function(){
app.configure(function () {
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.static('./project/HTML5Application/public_html'));
});

var oCurrentState = {
'qrcodes' : [],
'recognitionStatus' : 0
};

function getDrone(_stream) {
var droneClient = drone.get();
Expand All @@ -32,18 +40,27 @@ function getDrone(_stream) {
return droneClient;
}

function sendEvent(_stream, _drone) {
_stream.write('data:' + JSON.stringify({
'readystate': (_drone.navdata && _drone.navdata.droneState ) ? _drone.navdata.droneState.flying == 1 ? 'flying' : 'landed' : 'uninited',
'img': _drone.lastImage && 'data:image/png;base64,' + _drone.lastImage.toString('base64')
}) + '\n\n');
}


function sendState(_stream, _drone) {
_stream.write('data:' + JSON.stringify({
'readystate' : _drone.navdata ? _drone.navdata.droneState.flying == 1 ? 'flying' : 'landed' : 'uninited',
'img' : _drone.lastImage && 'data:image/png;base64,' + _drone.lastImage.toString('base64')
}) + '\n\n');
'readystate': (_drone.navdata && _drone.navdata.droneState) ? _drone.navdata.droneState.flying == 1 ? 'flying' : 'landed' : 'uninited',
'demo': _drone.navdata && _drone.navdata.demo,
'qrcodes': _drone.qrcodes || []
}) + '\n\n');
}


// Sends drone state & image
app.get('/dron/events', function(request, response){
var droneClient = getDrone(response, true);
function onEvents(request, response) {
var droneClient = getDrone(response);
if (!droneClient) {
sendState(response, {'lastImage':''});
return;
}
// Push data through socket
Expand All @@ -54,8 +71,9 @@ app.get('/dron/events', function(request, response){
'Connection': 'keep-alive'
});
response.write('\n');

// Handle connection interrupt
request.on("close", function() {
request.on("close", function () {
var droneClient = getDrone(response);
if (droneClient) {
droneClient.stop();
Expand All @@ -64,67 +82,61 @@ app.get('/dron/events', function(request, response){
});

var prevState = null;
droneClient.listener.on('state', function(_DroneObject) {
if (prevState != _DroneObject.navdata.droneState.flying) {
sendState(response, _DroneObject);
droneClient.listener.on('state', function (_DroneObject) {
if (_DroneObject.navdata.droneState) {
if (prevState != _DroneObject.navdata.droneState.flying) {
sendEvent(response, _DroneObject);
}
prevState = _DroneObject.navdata.droneState.flying;
}
prevState = _DroneObject.navdata.droneState.flying
});

droneClient.listener.on('image', function(_DroneObject) {
sendState(response, _DroneObject);
droneClient.listener.on('image', function (_DroneObject) {
sendEvent(response, _DroneObject);
});
}


// Subscribe to recieve telemetrics
// droneClient.on('navdata', function(navdata) {
// if (navdata.droneState && navdata.demo) {
// var bStateChanged = oCurrentState.droneState != navdata.droneState;
// oCurrentState.droneState = navdata.droneState;
// oCurrentState.droneDemo = navdata.demo;
// // Sends update to client only if drone state has been changed
// if (bStateChanged) {
// response.write('data:' + JSON.stringify({
// 'readystate' : oCurrentState.droneState ? oCurrentState.droneState.flying == 1 ? 'flying' : 'landed' : 'uninited',
// 'img' : oCurrentState.img
// }) + '\n\n');
// }
// }
// });

// Create PNG stream and subscribe for it's data
/*
var pngStream = droneClient.createPngStream();
pngStream.on('data', function(pngImage) {
console.log('ondata');
var img = 'data:image/png;base64,' + (new Buffer(pngImage, 'binary').toString('base64'));
if (img != oCurrentState.img) {
oCurrentState.img = img;
response.write('data:' + JSON.stringify({
'readystate' : oCurrentState.droneState ? oCurrentState.droneState.flying == 1 ? 'flying' : 'landed' : 'uninited',
'img' : oCurrentState.img
}) + '\n\n');
if (!oCurrentState.recognitionStatus) {
oCurrentState.recognitionStatus = 1;
qrcode.recognize(img, function(sText){
oCurrentState.recognitionStatus = 0;
if (sText) {
oCurrentState.qrcodes.push(sText);
}
});
}
function onState(request, response) {
var droneClient = getDrone(response);
if (!droneClient) {
return;
}
// Push data through socket
request.socket.setTimeout(Infinity);
response.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
response.write('\n');

// Handle connection interrupt
request.on("close", function () {
var droneClient = getDrone(response);
if (droneClient) {
droneClient.stop();
}
response.end();
});

droneClient.listener.on('state', function (_DroneObject) {
sendState(response, _DroneObject);
});
*/
});

droneClient.listener.on('qrcodes', function (_DroneObject) {
sendState(response, _DroneObject);
});
}


// Take the dron off
function onTakeoff (request, response) {
function onTakeoff(request, response) {
var droneClient = getDrone(response);
if (!droneClient) {
return;
}
droneClient.takeoff(function(){
droneClient.takeoff(function () {
response.writeHead(200, {
'Content-Type': 'text/plain',
'Cache-Control': 'no-cache',
Expand All @@ -134,11 +146,10 @@ function onTakeoff (request, response) {
response.end();
});
}
app.get('/dron/takeoff', onTakeoff);
app.post('/dron/takeoff', onTakeoff);


// Makes the drom land
function onLand (request, response) {
function onLand(request, response) {
var droneClient = getDrone(response);
if (!droneClient) {
return;
Expand All @@ -152,11 +163,9 @@ function onLand (request, response) {
response.write('\n1');
response.end();
}
app.get('/dron/land', onLand);
app.post('/dron/land', onLand);

// Makes the dron move
app.post('/dron/move', function(request, response) {

function onMove(request, response) {
if (request.body) {
var droneClient = getDrone(response);
if (!droneClient) {
Expand All @@ -174,55 +183,29 @@ app.post('/dron/move', function(request, response) {
response.write('\n0');
}
response.end();
});
}

// Sends drone's full state
app.get('/dron/state', function(request, response){
var droneClient = getDrone(response);
if (!droneClient) {
return;
}

// Push data through socket
request.socket.setTimeout(Infinity);
// Sends drone state & image
app.get('/dron/events', onEvents);

response.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
response.write('\n');
// Takeoff drone
app.get('/dron/takeoff', onTakeoff);
app.post('/dron/takeoff', onTakeoff);

// Land drone
app.get('/dron/land', onLand);
app.post('/dron/land', onLand);

// Push telemetrics data via interval
var intervalId = setInterval(function() {
response.write('data:' + JSON.stringify({
'readystate' : oCurrentState.droneState ? oCurrentState.droneState.flying == 1 ? 'flying' : 'landed' : 'unknown',
'data' : oCurrentState.droneDemo,
'qrcodes' : oCurrentState.qrcodes
}) + '\n\n'); }, 100);
// Makes the drone move
app.post('/dron/move', onMove);

// Handle connection interrupt
request.on("close", function() {
clearInterval(intervalId);
response.end();
});
});
// Sends drone's full state
app.get('/dron/state', onState);

app.listen(1337, '0.0.0.0');

console.log('Server running at http://127.0.0.1:1337/');

/*
response.writeHead(200, {'Content-Type': 'text/plain'});
// var qrdecoder = require('node-zxing')({'ZXingLocation':'./node_modules/node-zxing/lib/'});
// var path = "./sample.jpg";
// qrdecoder.decode(path,
// function(err, out) {
// res.end(out + '\n');
// console.log(err,out);
// }
// );

var client = require('ar-drone').createClient({'ip':'127.0.0.1'});
client.up(0.5);
// client.front(1);
*/


Loading