-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmotors.js
More file actions
99 lines (80 loc) · 2.2 KB
/
motors.js
File metadata and controls
99 lines (80 loc) · 2.2 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
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
const five = require("johnny-five");
const keypress = require("keypress");
var board, motor_l, motor_r;
var led;
var speed_setting = 255;
// set up the input
keypress(process.stdin);
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.setRawMode(true);
console.info("Setting up robot. Attempting J5 connect to Arduino")
board = new five.Board({port: process.argv[2]});
board.on("ready", function(err) {
if (err){
console.log(err);
return;
}
console.info("Board connected. Robot set up");
motor_r = new five.Motor({
pins: {
pwm: 9,
dir: 8,
},
invertPWM: true,
});
motor_l = new five.Motor({
pins: {
pwm: 6,
dir: 7,
},
invertPWM: true,
});
led = new five.Led(11);
console.info("Robot running issue commands to it.");
console.info("LRUD arrows. Space stop. Q to quit");
});
process.stdin.on('keypress', function(chunk, key) {
// is board ready?
if (!board.repl.ready) {
return;
}
// process the keypresses
if (key) {
switch (key.name) {
case "left":
motor_r.reverse(speed_setting);
motor_l.forward(speed_setting);
break;
case "right":
motor_r.forward(speed_setting);
motor_l.reverse(speed_setting);
break;
case "up":
motor_l.reverse(speed_setting);
motor_r.reverse(speed_setting);
break;
case "down":
motor_r.forward(speed_setting);
motor_l.forward(speed_setting);
break;
case "space":
motor_r.stop();
motor_l.stop();
break;
case "l":
console.log("LED ON");
led.on();
break;
case "o":
console.log("LED OFF");
led.off();
break;
case "q":
console.log("Exiting");
motor_r.stop();
motor_l.stop();
process.exit();
}
}
});