Skip to content

Commit

Permalink
added stop after 300ms for keyboard commands
Browse files Browse the repository at this point in the history
+ small refactoring
  • Loading branch information
tonnoz committed Sep 3, 2019
1 parent 1ca2379 commit 53dc722
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 28 deletions.
24 changes: 14 additions & 10 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,39 @@ const eegDevice = require('./libs/eeglib');
const sse = require('connect-sse')();
const PubSub = require('pubsub-js');
var resp;
var EEG_ENABLED = false
const eegClient = eegDevice.eegClient();





//*********** START HERE ***********

const eegClient = eegDevice.eegClient();
// eegClient.connect();
if(EEG_ENABLED){
eegClient.connect();
}
startExpress();

//*********** END HERE ***********



var mySubscriber = function (msg, data) {
console.log("RAW DATA RECEIVED:");
console.log( msg, data );
console.log( "\n");
console.log(msg, data);
console.log("\n");

const dataToSend = prepareDataToSend(data);

console.log("SENDING client:");
console.log("SENDING data to client:");
console.log(JSON.stringify(dataToSend) + "\n");

if(resp) {
resp.json(dataToSend);
console.log("Message sent correctly to subscriber.\n");
console.log("Message sent correctly to subscriber. \n");
}else{
console.log("There are no subscribers at the moment, skipping.\n");
}
};


PubSub.subscribe('eeg', mySubscriber);


Expand Down Expand Up @@ -77,10 +76,15 @@ function startExpress() {
const EXPRESS_PORT = 8080;
const app = express();
// app.use(express.static(path.join(__dirname,'public')));

//expose public folder
app.use(express.static('public'))

//expose S.S.E. endpoint for live EEG data stream
app.get('/liveEEGData', sse, function (req, res) {
resp = res; // expose response to global scope
});

app.listen(EXPRESS_PORT, function () {
console.log("Application running at Port " + EXPRESS_PORT);
});
Expand Down
3 changes: 2 additions & 1 deletion example EEG Data.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
highGamma: 1740 },
poorSignalLevel: 0
},
{ blinkStrength: 68 }

{ eSense: { attention: 83, meditation: 53 },
eegPower:
Expand All @@ -30,4 +31,4 @@

{ blinkStrength: 68 } //only if blinking

]
]
62 changes: 51 additions & 11 deletions libs/dronelib.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ if(process.stdin.isTTY) {

//keyboard binding
process.stdin.on('keypress', (str, key) => {

if (key && key.ctrl && key.name == 'c') {
quit();
}
Expand Down Expand Up @@ -41,10 +42,10 @@ process.stdin.on('keypress', (str, key) => {
if (key && key.shift && key.name == 'down') {
down();
}
if(key && key.name ===','){
if(key && key.name ==='i'){
yaw_left();
}
if(key && key.name ==='.'){
if(key && key.name ==='o'){
yaw_right();
}
if(key && key.name ==='x'){
Expand Down Expand Up @@ -82,43 +83,82 @@ function emergency(){

function front(){
console.log("****************** front *********************\n");
droneClient.front(0.1);
droneClient
.after(50, function() {
this.front(0.2);
}).after(300, function() {
this.stop();
});
}

function back(){
console.log("****************** back *********************\n");
droneClient.back(0.1);
droneClient
.after(50, function() {
this.back(0.2);
}).after(300, function() {
this.stop();
});
}

function left(){
console.log("****************** left *********************\n");
droneClient.left(0.1);
droneClient
.after(50, function() {
this.left(0.2);
}).after(300, function() {
this.stop();
});
}

function right(){
console.log("****************** right *********************\n");
droneClient.right(0.1);
droneClient
.after(50, function() {
this.right(0.2);
}).after(300, function() {
this.stop();
});
}

function up(){
console.log("****************** up *********************\n");
droneClient.up(0.1);
droneClient
.after(50, function() {
this.up(0.2);
}).after(300, function() {
this.stop();
});
}

function down(){
console.log("****************** down *********************\n");
droneClient.down(0.1);
droneClient
.after(50, function() {
this.down(0.2);
}).after(300, function() {
this.stop();
});
}


function yaw_left(){
console.log("****************** yaw_left *********************\n");
droneClient.counterClockwise(0.1);
droneClient
.after(50, function() {
this.counterClockwise(0.2);
}).after(300, function() {
this.stop();
});
}

function yaw_right(){
console.log("****************** yaw_right *********************\n");
droneClient.clockwise(0.1);
droneClient
.after(50, function() {
this.clockwise(0.2);
}).after(300, function() {
this.stop();
});
}

function quit(){
Expand Down
14 changes: 8 additions & 6 deletions libs/eeglib.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ module.exports = {

eegClient.on('data', function(data) {
PubSub.publish('eeg', data); //send raw data to 'eeg' topic

if(data.blinkStrength && data.blinkStrength >= BLINK_STRENGTH_THRESHOLD && !recently_takeOffOrLand) {
setTimeout(function(){recently_takeOffOrLand = false}, MIN_INTERVAL_ACTION);
recently_takeOffOrLand = true;
drone.takeOffOrLand();
}else{
if(data.eSense.attention && data.eSense.attention >= ATTENTION_STRENGTH_THRESHOLD && !recently_waved){
setTimeout(function(){recently_takeOffOrLand = false}, MIN_INTERVAL_ACTION);
recently_takeOffOrLand = true;
drone.wave();
}
}

if(data.eSense.attention && data.eSense.attention >= ATTENTION_STRENGTH_THRESHOLD && !recently_waved){
setTimeout(function(){recently_takeOffOrLand = false}, MIN_INTERVAL_ACTION);
recently_takeOffOrLand = true;
drone.wave();
}

});

return eegClient;
Expand Down

0 comments on commit 53dc722

Please sign in to comment.