-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathkb_controller.js
More file actions
66 lines (52 loc) · 1.79 KB
/
kb_controller.js
File metadata and controls
66 lines (52 loc) · 1.79 KB
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
// sets up a simple controller using a keyboard interface to drive
// the simplebot. Assumes two servos passed in as a left and right wheel
//
var keypress = require('keypress');
var Controller = function(opts) {
// assume opts is a left and right wheel object.
if (opts == undefined) {
throw "No opts provided";
}
// get the servos
this.left = opts.left || null;
this.right = opts.right || null;
// set the stop values if needed
this.LSTOPVAL = opts.lstop || 90;
this.RSTOPVAL = opts.rstop || 90;
if (this.left == null || this.right == null) {
throw "Both servos must be supplied"
}
keypress(process.stdin);
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.setRawMode(true);
console.log("Control the bot with the arrow keys, SPACE to stop, Q to quit.");
process.stdin.on('keypress', function (ch, key) {
if ( !key ) return;
if ( key.name == 'q' ) {
console.log('Quitting');
process.exit();
} else if ( key.name == 'up' ) {
console.log('Forward');
this.left.cw();
this.right.ccw();
} else if ( key.name == 'down' ) {
console.log('Backward');
this.left.ccw();
this.right.cw();
} else if ( key.name == 'left' ) {
console.log('Left');
this.left.ccw();
this.right.ccw();
} else if ( key.name == 'right' ) {
console.log('Right');
this.left.cw();
this.right.cw();
} else if ( key.name == 'space' ) {
console.log('Stopping');
this.left.to(this.LSTOPVAL);
this.right.to(this.RSTOPVAL);
}
}.bind(this) );
};
module.exports = Controller;