-
Notifications
You must be signed in to change notification settings - Fork 15
/
motor-sumo.js
126 lines (90 loc) · 2.42 KB
/
motor-sumo.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict';
var five = require('johnny-five');
var board = new five.Board();
var keypress = require('keypress');
board.on('ready', function() {
// Use your shield configuration from the list
// http://johnny-five.io/api/motor/#pre-packaged-shield-configs
var configs = five.Motor.SHIELD_CONFIGS.ADAFRUIT_V1;
var motors = new five.Motors([
configs.M1,
configs.M2
]);
// If you want to add a servo to your motor shield:
// You can also use continuous servos: new five.Servo.Continuous(10)
var servo1 = new five.Servo(10);
// Requires soldering lead to A0 pin on motor controller
// http://johnny-five.io/examples/proximity-hcsr04-analog/
// var proximity = new five.Proximity({
// controller: 'HCSR04',
// pin: 'A0'
// });
// Requires soldering lead to D3 pin on motor controller
// new five.Piezo({
// pin: 3
// });
this.repl.inject({
motors: motors,
servo1: servo1
});
console.log('Welcome to the Motorized SumoBot!');
console.log('Control the bot with the arrow keys, and SPACE to stop.');
function forward() {
console.log('Going forward');
motors.fwd(230);
}
function backward() {
console.log('Going backward');
motors.rev(230);
}
function left() {
console.log('Going left');
motors[0].rev(200);
motors[1].fwd(200);
}
function right() {
console.log('Going right');
motors[1].rev(200);
motors[0].fwd(200);
}
function stop() {
motors.stop();
// Optionally, stop servos from sweeping
servo1.stop();
}
function sweep() {
console.log('Sweep the leg!!');
// Sweep from 0-180 (repeat)
servo1.sweep();
}
function turbo() {
console.log('Turbo button engaged!');
motors.fwd(255);
}
keypress(process.stdin);
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.setRawMode(true);
process.stdin.on('keypress', function (ch, key) {
if ( !key ) { return; }
if ( key.name === 'q' ) {
console.log('Quitting');
stop();
process.exit();
} else if ( key.name === 'up' ) {
forward();
} else if ( key.name === 'down' ) {
backward();
} else if ( key.name === 'left' ) {
left();
} else if ( key.name === 'right' ) {
right();
} else if ( key.name === 'space' ) {
stop();
} else if ( key.name === 's' ) {
sweep();
} else if ( key.name === 't' ) {
turbo();
}
});
});