-
Notifications
You must be signed in to change notification settings - Fork 17
/
gestures.js
109 lines (84 loc) · 4.15 KB
/
gestures.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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...");
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();
}
// 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);
}
// Found at https://github.com/orbotix/sphero.js/issues/45#issuecomment-221169893
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();
};