Commit 2f4fcb5c authored by 15김건우's avatar 15김건우

add comment

parent 965b9fc6
const faye = new Faye.Client('/faye', { timeout: 120 }); const faye = new Faye.Client('/faye', { timeout: 120 });
// get data from socket end set UI text
faye.subscribe('/drone/navdata', data => { faye.subscribe('/drone/navdata', data => {
[ [
'batteryPercentage', 'batteryPercentage',
...@@ -12,7 +14,6 @@ faye.subscribe('/drone/navdata', data => { ...@@ -12,7 +14,6 @@ faye.subscribe('/drone/navdata', data => {
].forEach(type => $('#' + type).html(Math.round(data.demo[type], 4))); ].forEach(type => $('#' + type).html(Math.round(data.demo[type], 4)));
return showBatteryStatus(data.demo.batteryPercentage); return showBatteryStatus(data.demo.batteryPercentage);
}); });
window.showBatteryStatus = batteryPercentage => { window.showBatteryStatus = batteryPercentage => {
$('#batterybar').width(`${batteryPercentage}%`); $('#batterybar').width(`${batteryPercentage}%`);
if (batteryPercentage < 30) if (batteryPercentage < 30)
...@@ -27,9 +28,12 @@ window.showBatteryStatus = batteryPercentage => { ...@@ -27,9 +28,12 @@ window.showBatteryStatus = batteryPercentage => {
'data-original-title': `Battery status: ${batteryPercentage}%` 'data-original-title': `Battery status: ${batteryPercentage}%`
}); });
}; };
// get image stream from server and set UI
faye.subscribe('/drone/image', src => $('#cam').attr({ src: src })); faye.subscribe('/drone/image', src => $('#cam').attr({ src: src }));
keymap = { // get keyinput
const keymap = {
87: { ev: 'move', action: 'front' }, // W 87: { ev: 'move', action: 'front' }, // W
83: { ev: 'move', action: 'back' }, // S 83: { ev: 'move', action: 'back' }, // S
65: { ev: 'move', action: 'left' }, // A 65: { ev: 'move', action: 'left' }, // A
...@@ -64,6 +68,8 @@ $(document).keyup(ev => { ...@@ -64,6 +68,8 @@ $(document).keyup(ev => {
speed = 0; speed = 0;
faye.publish('/drone/drone', { action: 'stop' }); faye.publish('/drone/drone', { action: 'stop' });
}); });
// get mouse event
$('*[data-action]').on('mousedown', ev => { $('*[data-action]').on('mousedown', ev => {
faye.publish('/drone/' + $(this).attr('data-action'), { faye.publish('/drone/' + $(this).attr('data-action'), {
action: $(this).attr('data-param'), action: $(this).attr('data-param'),
......
...@@ -2,9 +2,11 @@ const express = require('express'); ...@@ -2,9 +2,11 @@ const express = require('express');
const faye = require('faye'); const faye = require('faye');
const path = require('path'); const path = require('path');
// config drone
const drone = require('ar-drone').createClient(); const drone = require('ar-drone').createClient();
drone.config('general:navdata_demo', 'TRUE'); drone.config('general:navdata_demo', 'TRUE');
// config express app
const app = express(); const app = express();
app.configure(() => { app.configure(() => {
app.set('port', process.env.PORT || 3001); app.set('port', process.env.PORT || 3001);
...@@ -13,11 +15,12 @@ app.configure(() => { ...@@ -13,11 +15,12 @@ app.configure(() => {
app.use('/components', express.static(path.join(__dirname, 'components'))); app.use('/components', express.static(path.join(__dirname, 'components')));
}); });
// create server and socket
const server = require('http').createServer(app); const server = require('http').createServer(app);
new faye.NodeAdapter({ mount: '/faye', timeout: 45 }).attach(server); new faye.NodeAdapter({ mount: '/faye', timeout: 45 }).attach(server);
const socket = new faye.Client(`http://localhost:${app.get('port')}/faye`); const socket = new faye.Client(`http://localhost:${app.get('port')}/faye`);
// get request from socket -> set command to drone
socket.subscribe('/drone/move', cmd => { socket.subscribe('/drone/move', cmd => {
console.log('move', cmd); console.log('move', cmd);
if (drone[cmd.action]) drone[cmd.action](cmd.speed); if (drone[cmd.action]) drone[cmd.action](cmd.speed);
...@@ -30,13 +33,17 @@ socket.subscribe('/drone/drone', cmd => { ...@@ -30,13 +33,17 @@ socket.subscribe('/drone/drone', cmd => {
console.log('drone command: ', cmd); console.log('drone command: ', cmd);
if (drone[cmd.action]) drone[cmd.action](); if (drone[cmd.action]) drone[cmd.action]();
}); });
// start server
server.listen(app.get('port'), () => server.listen(app.get('port'), () =>
console.log('Express server listening on port ' + app.get('port')) console.log('Express server listening on port ' + app.get('port'))
); );
let currentImg = null; // send navdata to socket
drone.on('navdata', data => socket.publish('/drone/navdata', data)); drone.on('navdata', data => socket.publish('/drone/navdata', data));
// save latest img from drone PngStream
let currentImg = null;
let imageSendingPaused = false; let imageSendingPaused = false;
drone.createPngStream().on('data', frame => { drone.createPngStream().on('data', frame => {
currentImg = frame; currentImg = frame;
...@@ -48,6 +55,7 @@ drone.createPngStream().on('data', frame => { ...@@ -48,6 +55,7 @@ drone.createPngStream().on('data', frame => {
}, 100); }, 100);
}); });
// send latest img to server
app.get('/image/:id', (req, res) => { app.get('/image/:id', (req, res) => {
res.writeHead(200, { 'Content-Type': 'image/png' }); res.writeHead(200, { 'Content-Type': 'image/png' });
res.end(currentImg, 'binary'); res.end(currentImg, 'binary');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment