-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrover.js
More file actions
121 lines (104 loc) · 2.71 KB
/
rover.js
File metadata and controls
121 lines (104 loc) · 2.71 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var five = require("johnny-five"),
keypress = require('keypress'),
mouse = require('macmouse'),
board = new five.Board({
repl: false,
debug: false
}),
stdin = process.stdin,
moving = false,
turning = false,
leftSpeed = 0,
rightSpeed = 0,
prevY, prevY;
mouse.init();
keypress(process.stdin);
stdin.setRawMode(true);
stdin.resume();
board.on("ready", function() {
// Johnny-Five provides pre-packages shield configurations!
// http://johnny-five.io/api/motor/#pre-packaged-shield-configs
var motors = new five.Motors([
five.Motor.SHIELD_CONFIGS.POLOLU_DRV8835_SHIELD.M1,
five.Motor.SHIELD_CONFIGS.POLOLU_DRV8835_SHIELD.M2,
]),
leftMotor = motors[0],
rightMotor = motors[1];
console.log('Place cursor in top right corner to calibrate...');
setTimeout(function() {
var pos = mouse.getRealPos();
prevX = pos.x / 2;
prevY = pos.y / 2;
mouse.Place(prevX, prevY);
robot();
}, 1000)
function robot() {
console.log('Ready!');
setInterval(function() {
var pos = mouse.getRealPos(),
newY = pos.y,
newX = pos.x;
// forward/backward
leftSpeed = leftSpeed + (newY - prevY) / 2;
rightSpeed = rightSpeed + (newY - prevY) / 2;
// left/right
leftSpeed = leftSpeed + (prevX - newX) / 2;
rightSpeed = rightSpeed - (prevX - newX) / 2;
if (leftSpeed > 0) {
leftMotor.fwd(leftSpeed);
} else {
leftMotor.rev(Math.abs(leftSpeed));
}
if (rightSpeed > 0) {
rightMotor.fwd(rightSpeed);
} else {
rightMotor.rev(Math.abs(rightSpeed));
}
prevX = newX;
prevY = newY;
}, 250);
stdin.on("keypress", function (chunk, key) {
if (!key) return;
if (key.ctrl && key.name == 'c' || key.name == 'q') {
mouse.quit();
process.exit();
}
switch(key.name) {
case "up":
motors.fwd(255);
moving = true;
break;
case "down":
motors.rev(255);
moving = true;
break;
case "space":
motors.stop();
moving = false;
leftSpeed = 0;
rightSpeed = 0;
break;
case "right":
if (moving) {
motors[0].speed(150);
motors[1].speed(255);
} else {
motors[1].fwd(75);
motors[0].rev(75);
}
break;
case "left":
if (moving) {
motors[1].speed(150);
motors[0].speed(255);
} else {
motors[0].fwd(75);
motors[1].rev(75);
}
break;
default:
break;
}
});
}
});