From 7e20174c38e0045fab24fcd8ec40d4febff9a2ee Mon Sep 17 00:00:00 2001 From: John Wiseman Date: Tue, 28 May 2013 18:41:56 -0700 Subject: [PATCH] Added magnetometer calibration to pilot plugin. Also pass some events through the websocket. --- app.js | 36 ++++++ plugins/hud/public/js/compass.js | 10 +- plugins/pilot/index.js | 4 + plugins/pilot/public/js/pilot.js | 212 +++++++++++++++++-------------- public/css/style.css | 16 ++- views/index.ejs | 9 +- 6 files changed, 178 insertions(+), 109 deletions(-) diff --git a/app.js b/app.js index 74cecf4..7104773 100644 --- a/app.js +++ b/app.js @@ -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) { diff --git a/plugins/hud/public/js/compass.js b/plugins/hud/public/js/compass.js index 6917a91..0ced1a9 100644 --- a/plugins/hud/public/js/compass.js +++ b/plugins/hud/public/js/compass.js @@ -13,13 +13,13 @@ // Add required UI elements $('#cockpit').append('
'); 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); }); } }); diff --git a/plugins/pilot/index.js b/plugins/pilot/index.js index 247683d..956899e 100644 --- a/plugins/pilot/index.js +++ b/plugins/pilot/index.js @@ -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); + }); }); }; diff --git a/plugins/pilot/public/js/pilot.js b/plugins/pilot/public/js/pilot.js index d6eb9e1..4b0859a 100644 --- a/plugins/pilot/public/js/pilot.js +++ b/plugins/pilot/public/js/pilot.js @@ -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)); diff --git a/public/css/style.css b/public/css/style.css index 9a6a6b6..0407e54 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -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 diff --git a/views/index.ejs b/views/index.ejs index 8379446..76641db 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -16,9 +16,12 @@ <% for(var i=0; i <% } %> - +
+ + +
WebFlight
@@ -28,9 +31,9 @@
-
+
- +