|
| 1 | +var keypress = require('keypress'); |
| 2 | + |
| 3 | +module.exports = function (bb8) { |
| 4 | + |
| 5 | + var stdin = process.stdin; |
| 6 | + stdin.setEncoding('utf8'); |
| 7 | + keypress(stdin); |
| 8 | + |
| 9 | + console.log("Welcome to BB-8 Gestures!"); |
| 10 | + console.log("Press 'e' to calibrate. Press 'q' to finish calibration."); |
| 11 | + console.log("Press 'y' for yes. Press 'n' for no. Press 't' to do the twist!"); |
| 12 | + console.log("Put BB-8 into the dock and press 'h' for head roll. "); |
| 13 | + console.log("Press ctrl+c to exit."); |
| 14 | + console.log("Listening for key presses..."); |
| 15 | + bb8.color("white"); |
| 16 | + |
| 17 | + stdin.on("keypress", function(ch,key) { |
| 18 | + |
| 19 | + if (key && key.ctrl && key.name === 'c') { |
| 20 | + bb8.disconnect(function() { |
| 21 | + console.log("BB-8 says goodbye!"); |
| 22 | + }); |
| 23 | + |
| 24 | + process.stdin.pause(); |
| 25 | + process.exit(); |
| 26 | + } |
| 27 | + |
| 28 | + // how to use setRawMotors - from https://github.com/orbotix/DeveloperDocumentation/blob/0a4b007600f55c05889734aa3aa6727f86dc1d51/src/content/imports/sphero-js/devices/sphero.md#setrawmotorsopts-callback |
| 29 | + // Possible modes: |
| 30 | + // |
| 31 | + // 0x00: Off (motor is open circuit) |
| 32 | + // 0x01: Forward |
| 33 | + // 0x02: Reverse |
| 34 | + // 0x03: Brake (motor is shorted) |
| 35 | + // 0x04: Ignore (motor mode and power is left unchanged |
| 36 | + |
| 37 | + if(key && key.name === 'h') { |
| 38 | + |
| 39 | + // head roll |
| 40 | + setTimeout(function() { opts = {lmode: 1, lpower: 100, rmode: 0, rpower: 0}; bb8.setRawMotors(opts, function() { }); }, 100); |
| 41 | + setTimeout(function() { opts = {lmode: 0, lpower: 0, rmode: 0, rpower: 0}; bb8.setRawMotors(opts, function() { }); bb8.setStabilization(1, function() { }); }, 800); |
| 42 | + } |
| 43 | + |
| 44 | + // let's do the twist |
| 45 | + if(key && key.name === 't') { |
| 46 | + |
| 47 | + bb8.setStabilization(0, function() { }); |
| 48 | + |
| 49 | + // twist right |
| 50 | + setTimeout(function() { opts = {lmode: 1, lpower: 150, rmode: 2, rpower: 150}; bb8.setRawMotors(opts, function() { }); }, 100); |
| 51 | + setTimeout(function() { opts = {lmode: 0, lpower: 0, rmode: 0, rpower: 0}; bb8.setRawMotors(opts, function() { } ); }, 300); |
| 52 | + |
| 53 | + // twist left |
| 54 | + setTimeout(function() { opts = {lmode: 2, lpower: 150, rmode: 1, rpower: 150}; bb8.setRawMotors(opts, function() { }); }, 400); |
| 55 | + setTimeout(function() { opts = {lmode: 0, lpower: 0, rmode: 0, rpower: 0}; bb8.setRawMotors(opts, function() { }); bb8.setStabilization(1, function() { }); }, 600); |
| 56 | + |
| 57 | + // twist right |
| 58 | + setTimeout(function() { opts = {lmode: 1, lpower: 150, rmode: 2, rpower: 150}; bb8.setRawMotors(opts, function() { }); }, 700); |
| 59 | + setTimeout(function() { opts = {lmode: 0, lpower: 0, rmode: 0, rpower: 0}; bb8.setRawMotors(opts, function() { }); bb8.setStabilization(1, function() { }); }, 900); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + // Found at https://github.com/orbotix/sphero.js/issues/45#issuecomment-221169893 |
| 64 | + if(key && key.name === 'y') { |
| 65 | + |
| 66 | + // YES |
| 67 | + opts = {lmode: 2, lpower: 80, rmode: 2, rpower: 80}; |
| 68 | + bb8.setRawMotors(opts, function() { }); |
| 69 | + setTimeout(function() { opts = {lmode: 1, lpower: 80, rmode: 1, rpower: 80}; bb8.setRawMotors(opts, function() { }); }, 100); |
| 70 | + setTimeout(function() { opts = {lmode: 2, lpower: 80, rmode: 2, rpower: 80}; bb8.setRawMotors(opts, function() { }); }, 200); |
| 71 | + setTimeout(function() { opts = {lmode: 1, lpower: 80, rmode: 1, rpower: 80}; bb8.setRawMotors(opts, function() { }); }, 300); |
| 72 | + setTimeout(function() { opts = {lmode: 0, lpower: 0, rmode: 0, rpower: 0}; bb8.setRawMotors(opts, function() { }); bb8.setStabilization(1, function() { }); }, 400); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + if(key && key.name === 'n') { |
| 77 | + |
| 78 | + // NO |
| 79 | + bb8.roll(0,60); |
| 80 | + setTimeout(function() { bb8.roll(0,300); }, 200); |
| 81 | + setTimeout(function() { bb8.roll(0,60); }, 400); |
| 82 | + setTimeout(function() { bb8.roll(0,300); }, 600); |
| 83 | + setTimeout(function() { bb8.roll(0,60); }, 400); |
| 84 | + setTimeout(function() { bb8.roll(0,300); }, 600); |
| 85 | + setTimeout(function() { bb8.roll(0,0); }, 800); |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + // Calbiration from drive.js |
| 90 | + if(key && key.name === 'e') { |
| 91 | + bb8.startCalibration (); |
| 92 | + bb8.roll(1, 90, 2, function() { |
| 93 | + setTimeout(function() { |
| 94 | + bb8.setHeading(0, function() { |
| 95 | + bb8.roll(0,0,1); |
| 96 | + }); |
| 97 | + }, 300); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + if(key && key.name === 'q') { |
| 102 | + bb8.finishCalibration(); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + stdin.setRawMode(true); |
| 107 | + stdin.resume(); |
| 108 | + |
| 109 | +}; |
0 commit comments