/
drive.js
71 lines (54 loc) · 1.57 KB
/
drive.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
var keypress = require('keypress');
module.exports = function (bb8) {
//variation of the roll command to enable input from keyboard
//useful when it you get commands from the Internet or just to play with
//BB-8 using nodejs
console.log('Drive mode enabled\n Press e to calibrate and tap e again to point the BB8 in the direction you want to drive and q to finish calibration\n Use w,s,a,d style control the BB8 \n Use spacebar to stop the BB8');
console.log('Press ctrl+c to exit');
var stdin = process.stdin;
stdin.setEncoding('utf8');
keypress(stdin);
console.log("starting to listen for arrow key presses");
stdin.on("keypress", function(ch,key) {
bb8.color('#000000');
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();
}
if(key && key.name === 'w'){
bb8.stop();
bb8.roll(150, 0);
}
if(key && key.name === 'd'){
bb8.stop();
bb8.roll(150, 90);
}
if(key && key.name === 's'){
bb8.stop();
bb8.roll(150, 180);
}
if(key && key.name === 'a'){
bb8.stop();
bb8.roll(150, 270);
}
if(key && key.name === 'space'){
bb8.stop();
}
if (key && key.ctrl && key.name === 'c') {
process.stdin.pause();
process.exit();
}
});
stdin.setRawMode(true);
stdin.resume();
return false;
};