Added magnetometer calibration to pilot plugin.

Also pass some events through the websocket.
Esse commit está contido em:
John Wiseman
2013-05-28 18:41:56 -07:00
commit 7e20174c38
6 arquivos alterados com 178 adições e 109 exclusões
+36
Ver Arquivo
@@ -5,6 +5,7 @@ var express = require('express')
, server = require("http").createServer(app)
, io = require('socket.io').listen(server)
, arDrone = require('ar-drone')
, arDroneConstants = require('ar-drone/lib/constants')
, config = require('./config')
;
@@ -48,10 +49,23 @@ app.get('/', function (req, res) {
* call 'listen' on the server, not the express app
*/
function navdata_option_mask(c) {
return 1 << c;
}
// From the SDK.
var default_navdata_options = (
navdata_option_mask(arDroneConstants.options.DEMO) |
navdata_option_mask(arDroneConstants.options.VISION_DETECT));
// Connect and configure the drone
var client = new arDrone.createClient();
client.config('general:navdata_demo', 'TRUE');
client.config('video:video_channel', '0');
// Enable the magnetometer data.
client.config('general:navdata_options',
default_navdata_options |
navdata_option_mask(arDroneConstants.options.MAGNETO));
// Add a handler on navdata updates
var latestNavData;
@@ -59,6 +73,28 @@ client.on('navdata', function (d) {
latestNavData = d;
});
// Signal landed and flying events.
client.on('landing', function () {
console.log('LANDING');
io.sockets.emit('landing');
});
client.on('landed', function () {
console.log('LANDED');
io.sockets.emit('landed');
});
client.on('takeoff', function() {
console.log('TAKEOFF');
io.sockets.emit('takeoff');
});
client.on('hovering', function() {
console.log('HOVERING');
io.sockets.emit('hovering');
});
client.on('flying', function() {
console.log('FLYING');
io.sockets.emit('flying');
});
// Process new websocket connection
io.set('log level', 1);
io.sockets.on('connection', function (socket) {
+5 -5
Ver Arquivo
@@ -13,13 +13,13 @@
// Add required UI elements
$('#cockpit').append('<div id="compass"></div>');
var div = $('#compass').get(0);
// Listen to navdata updates
var compass = this;
this.cockpit.socket.on('navdata', function(data) {
if (!jQuery.isEmptyObject(data)) {
requestAnimationFrame(function() {
compass.moveTo(data.demo.rotation.clockwise);
this.cockpit.socket.on('navdata', function(data) {
if (!jQuery.isEmptyObject(data)) {
requestAnimationFrame(function() {
compass.moveTo(data.magneto.heading.fusionUnwrapped);
});
}
});
+4
Ver Arquivo
@@ -10,6 +10,10 @@ function pilot(name, deps) {
console.log("drone", cmd);
return typeof deps.client[_name = cmd.action] === "function" ? deps.client[_name]() : void 0;
});
socket.on('/pilot/calibrate', function (cmd) {
console.log("calibrate", cmd);
return deps.client.calibrate(cmd.device_num);
});
});
};
+117 -95
Ver Arquivo
@@ -1,111 +1,133 @@
PILOT_ACCELERATION = 0.08;
(function(window, document) {
'use strict';
'use strict';
// Static keymap used within this module
var Keymap = {
90 : {
ev : 'move',
action : 'front'
},
83 : {
ev : 'move',
action : 'back'
},
81 : {
ev : 'move',
action : 'left'
},
68 : {
ev : 'move',
action : 'right'
},
38 : {
ev : 'move',
action : 'up'
},
40 : {
ev : 'move',
action : 'down'
},
37 : {
ev : 'move',
action : 'counterClockwise'
},
39 : {
ev : 'move',
action : 'clockwise'
},
32 : {
ev : 'drone',
action : 'stop'
},
84 : {
ev : 'drone',
action : 'takeoff'
},
76 : {
ev : 'drone',
action : 'land'
},
69 : {
ev : 'drone',
action : 'disableEmergency'
}
};
// Static keymap used within this module
var Keymap = {
90 : {
ev : 'move',
action : 'front'
},
83 : {
ev : 'move',
action : 'back'
},
81 : {
ev : 'move',
action : 'left'
},
68 : {
ev : 'move',
action : 'right'
},
38 : {
ev : 'move',
action : 'up'
},
40 : {
ev : 'move',
action : 'down'
},
37 : {
ev : 'move',
action : 'counterClockwise'
},
39 : {
ev : 'move',
action : 'clockwise'
},
32 : {
ev : 'drone',
action : 'stop'
},
84 : {
ev : 'drone',
action : 'takeoff'
},
76 : {
ev : 'drone',
action : 'land'
},
69 : {
ev : 'drone',
action : 'disableEmergency'
}
};
/*
* Constructuor
*/
var Pilot = function Pilot(cockpit) {
/*
* Constructuor
*/
var Pilot = function Pilot(cockpit) {
console.log("Loading Pilot plugin.");
this.cockpit = cockpit;
this.speed = 0;
this.listen();
};
this.cockpit = cockpit;
this.speed = 0;
// Start with magneto calibration disabled.
$('#calibratemagneto').prop('disabled', true);
/*
* Register keyboard event listener
*/
Pilot.prototype.listen = function listen() {
var pilot = this;
$(document).keydown(function(ev) {
pilot.keyDown(ev);
});
this.listen();
};
$(document).keyup(function(ev) {
pilot.keyUp(ev);
});
};
/*
* Register keyboard event listener
*/
Pilot.prototype.listen = function listen() {
var pilot = this;
$(document).keydown(function(ev) {
pilot.keyDown(ev);
});
Pilot.prototype.keyDown = function keyDown(ev) {
console.log("Keydown: " + ev.keyCode);
if (Keymap[ev.keyCode] == null) {
return;
$(document).keyup(function(ev) {
pilot.keyUp(ev);
});
$('#calibratemagneto').click(function(ev) {
ev.preventDefault();
pilot.calibrate(0);
});
this.cockpit.socket.on('hovering', function() {
$('#calibratemagneto').prop('disabled', false);
});
this.cockpit.socket.on('landed', function() {
$('#calibratemagneto').prop('disabled', true);
});
};
Pilot.prototype.keyDown = function keyDown(ev) {
console.log("Keydown: " + ev.keyCode);
if (Keymap[ev.keyCode] == null) {
return;
}
var evData;
ev.preventDefault();
this.speed = this.speed >= 1 ? 1 : this.speed + PILOT_ACCELERATION / (1 - this.speed);
evData = Keymap[ev.keyCode];
this.cockpit.socket.emit("/pilot/" + evData.ev, {
action : evData.action,
speed : this.speed
});
};
var evData;
ev.preventDefault();
this.speed = this.speed >= 1 ? 1 : this.speed + PILOT_ACCELERATION / (1 - this.speed);
evData = Keymap[ev.keyCode];
this.cockpit.socket.emit("/pilot/" + evData.ev, {
action : evData.action,
speed : this.speed
});
};
Pilot.prototype.keyUp = function keyUp(ev) {
console.log("Keyup: " + ev.keyCode);
if (Keymap[ev.keyCode] == null) {
return;
Pilot.prototype.keyUp = function keyUp(ev) {
console.log("Keyup: " + ev.keyCode);
if (Keymap[ev.keyCode] == null) {
return;
}
ev.preventDefault();
this.speed = 0;
this.cockpit.socket.emit("/pilot/drone", {
action : "stop"
});
};
this.speed = 0;
this.cockpit.socket.emit("/pilot/drone", {
action : "stop"
});
};
window.Cockpit.plugins.push(Pilot);
Pilot.prototype.calibrate = function calibrate(deviceNum) {
this.cockpit.socket.emit("/pilot/calibrate", {
device_num : 0
});
};
window.Cockpit.plugins.push(Pilot);
}(window, document));
+10 -6
Ver Arquivo
@@ -21,13 +21,17 @@
bottom: 0px;
}
#controls {
float: right;
}
#glasspane {
position: absolute;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
z-index: 100;
position: absolute;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
z-index: 100;
-webkit-user-select: none; // webkit (safari, chrome) browsers
-moz-user-select: none; // mozilla browsers
+6 -3
Ver Arquivo
@@ -16,9 +16,12 @@
<script type="text/javascript" src="/js/cockpit.js"></script>
<% for(var i=0; i<scripts.length; i++) {%>
<script type="text/javascript" src="<%= scripts[i] %>"></script><% } %>
<body>
<div class="header-container">
<span id="controls">
<input type="button" id="calibratemagneto" value="Calibrate magneto">
</span>
<header class="wrapper clearfix">
<span class="brand">WebFlight</span>
</header>
@@ -28,9 +31,9 @@
<div id="glasspane">
<div id="cockpit">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="footer-container">
<footer class="wrapper">
</footer>