From 73b64cc714e44d470685b9e9b3c60b6a0dc843ef Mon Sep 17 00:00:00 2001 From: Sara Ford Date: Fri, 22 Jul 2016 17:54:07 -0700 Subject: [PATCH 1/5] added gestures in training.js --- commands/train.js | 104 ++++++++++++++++++++++++++++++++++++++++++++++ index.js | 7 ++++ 2 files changed, 111 insertions(+) create mode 100644 commands/train.js diff --git a/commands/train.js b/commands/train.js new file mode 100644 index 0000000..8387aae --- /dev/null +++ b/commands/train.js @@ -0,0 +1,104 @@ +var keypress = require('keypress'); + +module.exports = function (bb8) { + + var stdin = process.stdin; + stdin.setEncoding('utf8'); + keypress(stdin); + + console.log("Welcome to BB-8 Gestures!"); + console.log("Press 'e' to calibrate. Press 'q' to finish calibration."); + console.log("Press 'y' for yes. Press 'n' for no. Press 't' to do the twist!"); + console.log("Put BB-8 into the dock and press 'h' for head roll. "); + console.log('Press ctrl+c to exit.'); + console.log("Listening for key presses..."); + + stdin.on("keypress", function(ch,key) { + + if (key && key.ctrl && key.name === 'c') { + process.stdin.pause(); + process.exit(); + } + + // how to use setRawMotors - from https://github.com/orbotix/DeveloperDocumentation/blob/0a4b007600f55c05889734aa3aa6727f86dc1d51/src/content/imports/sphero-js/devices/sphero.md#setrawmotorsopts-callback + // Possible modes: + // + // 0x00: Off (motor is open circuit) + // 0x01: Forward + // 0x02: Reverse + // 0x03: Brake (motor is shorted) + // 0x04: Ignore (motor mode and power is left unchanged + + if(key && key.name === 'h') { + + // head roll + setTimeout(function() { opts = {lmode: 1, lpower: 100, rmode: 0, rpower: 0}; bb8.setRawMotors(opts, function() { }); }, 100); + setTimeout(function() { opts = {lmode: 0, lpower: 0, rmode: 0, rpower: 0}; bb8.setRawMotors(opts, function() { }); bb8.setStabilization(1, function() { }); }, 800); + } + + // let's do the twist + if(key && key.name === 't') { + + bb8.setStabilization(0, function() { }); + + // twist right + setTimeout(function() { opts = {lmode: 1, lpower: 150, rmode: 2, rpower: 150}; bb8.setRawMotors(opts, function() { }); }, 100); + setTimeout(function() { opts = {lmode: 0, lpower: 0, rmode: 0, rpower: 0}; bb8.setRawMotors(opts, function() { } ); }, 300); + + // twist left + setTimeout(function() { opts = {lmode: 2, lpower: 150, rmode: 1, rpower: 150}; bb8.setRawMotors(opts, function() { }); }, 400); + setTimeout(function() { opts = {lmode: 0, lpower: 0, rmode: 0, rpower: 0}; bb8.setRawMotors(opts, function() { }); bb8.setStabilization(1, function() { }); }, 600); + + // twist right + setTimeout(function() { opts = {lmode: 1, lpower: 150, rmode: 2, rpower: 150}; bb8.setRawMotors(opts, function() { }); }, 700); + setTimeout(function() { opts = {lmode: 0, lpower: 0, rmode: 0, rpower: 0}; bb8.setRawMotors(opts, function() { }); bb8.setStabilization(1, function() { }); }, 900); + + } + + if(key && key.name === 'y') { + + // YES + opts = {lmode: 2, lpower: 80, rmode: 2, rpower: 80}; + bb8.setRawMotors(opts, function() { }); + setTimeout(function() { opts = {lmode: 1, lpower: 80, rmode: 1, rpower: 80}; bb8.setRawMotors(opts, function() { }); }, 100); + setTimeout(function() { opts = {lmode: 2, lpower: 80, rmode: 2, rpower: 80}; bb8.setRawMotors(opts, function() { }); }, 200); + setTimeout(function() { opts = {lmode: 1, lpower: 80, rmode: 1, rpower: 80}; bb8.setRawMotors(opts, function() { }); }, 300); + setTimeout(function() { opts = {lmode: 0, lpower: 0, rmode: 0, rpower: 0}; bb8.setRawMotors(opts, function() { }); bb8.setStabilization(1, function() { }); }, 400); + + } + + if(key && key.name === 'n') { + + // NO + bb8.roll(0,60); + setTimeout(function() { bb8.roll(0,300); }, 200); + setTimeout(function() { bb8.roll(0,60); }, 400); + setTimeout(function() { bb8.roll(0,300); }, 600); + setTimeout(function() { bb8.roll(0,60); }, 400); + setTimeout(function() { bb8.roll(0,300); }, 600); + setTimeout(function() { bb8.roll(0,0); }, 800); + + } + + // Calbiration from drive.js + if(key && key.name === 'e') { + bb8.startCalibration (); + bb8.roll(1, 90, 2, function() { + setTimeout(function() { + bb8.setHeading(0, function() { + bb8.roll(0,0,1); + }); + }, 300); + }); + } + + if(key && key.name === 'q') { + bb8.finishCalibration(); + } + + + stdin.setRawMode(true); + stdin.resume(); + }); + +}; diff --git a/index.js b/index.js index 0fdfc19..ae3e919 100644 --- a/index.js +++ b/index.js @@ -86,6 +86,13 @@ program executeCommand('power'); }); +program + .command('train') + .description('How to train your BB-8') + .action(function (options) { + executeCommand('train'); + }); + program .command('drive') .description('Command to accept keyboard input--use arrow keys') From 03942645d19e749ef46d09712c1e5a544ec7b038 Mon Sep 17 00:00:00 2001 From: Sara Ford Date: Fri, 22 Jul 2016 17:55:28 -0700 Subject: [PATCH 2/5] renamed to gestures --- commands/{train.js => gestures.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename commands/{train.js => gestures.js} (100%) diff --git a/commands/train.js b/commands/gestures.js similarity index 100% rename from commands/train.js rename to commands/gestures.js From 24ef45d1884652c8962ca3acd65078e661aabfe8 Mon Sep 17 00:00:00 2001 From: Sara Ford Date: Fri, 22 Jul 2016 17:58:26 -0700 Subject: [PATCH 3/5] updated for new gesture command --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index ae3e919..5dcdd4f 100644 --- a/index.js +++ b/index.js @@ -87,10 +87,10 @@ program }); program - .command('train') - .description('How to train your BB-8') + .command('gestures') + .description('Some gestures for your BB-8') .action(function (options) { - executeCommand('train'); + executeCommand('gestures'); }); program From 274b1a4132a8ff3695a7a673533b35154dd1c69f Mon Sep 17 00:00:00 2001 From: Sara Ford Date: Fri, 22 Jul 2016 18:01:05 -0700 Subject: [PATCH 4/5] added comment for y/n gestures source --- commands/gestures.js | 1 + 1 file changed, 1 insertion(+) diff --git a/commands/gestures.js b/commands/gestures.js index 8387aae..d17c884 100644 --- a/commands/gestures.js +++ b/commands/gestures.js @@ -55,6 +55,7 @@ module.exports = function (bb8) { } + // Found at https://github.com/orbotix/sphero.js/issues/45#issuecomment-221169893 if(key && key.name === 'y') { // YES From 45049f59483e59d0fe5b7b6fe42baf994dcea5c1 Mon Sep 17 00:00:00 2001 From: Sara Ford Date: Tue, 26 Jul 2016 18:08:32 -0700 Subject: [PATCH 5/5] added disconnect; fixed first keystroke --- commands/gestures.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/commands/gestures.js b/commands/gestures.js index d17c884..deba830 100644 --- a/commands/gestures.js +++ b/commands/gestures.js @@ -10,12 +10,17 @@ module.exports = function (bb8) { console.log("Press 'e' to calibrate. Press 'q' to finish calibration."); console.log("Press 'y' for yes. Press 'n' for no. Press 't' to do the twist!"); console.log("Put BB-8 into the dock and press 'h' for head roll. "); - console.log('Press ctrl+c to exit.'); + console.log("Press ctrl+c to exit."); console.log("Listening for key presses..."); + bb8.color("white"); stdin.on("keypress", function(ch,key) { if (key && key.ctrl && key.name === 'c') { + bb8.disconnect(function() { + console.log("BB-8 says goodbye!"); + }); + process.stdin.pause(); process.exit(); } @@ -96,10 +101,9 @@ module.exports = function (bb8) { if(key && key.name === 'q') { bb8.finishCalibration(); } - - - stdin.setRawMode(true); - stdin.resume(); }); + stdin.setRawMode(true); + stdin.resume(); + };