-
Notifications
You must be signed in to change notification settings - Fork 1
/
robot.js
48 lines (41 loc) · 1.05 KB
/
robot.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
var arduino = require('duino')
var PWM_R = 5;
var PWM_L = 10;
var DIR_R = 4;
var DIR_L = 12;
var ROBOT_LED = 13;
var Robot = function (descriptor) {
this.board = new arduino.Board({
debug: true,
device: descriptor.device
});
};
Robot.prototype.setLeft = function (value) {
if (value == 0) {
this.board.analogWrite(PWM_L, 0);
} else {
this.board.analogWrite(PWM_L, Math.abs(value));
this.board.digitalWrite(DIR_L, value < 0 ? this.board.HIGH : this.board.LOW);
}
};
Robot.prototype.setRight = function (value) {
if (value == 0) {
this.board.analogWrite(PWM_R, 0);
} else {
this.board.analogWrite(PWM_R, Math.abs(value));
this.board.digitalWrite(DIR_R, value < 0 ? this.board.HIGH : this.board.LOW);
}
};
Robot.prototype.bindSocket = function (socket) {
socket.on('robot', (function (data) {
if ('left' in data) {
this.setLeft(data.left);
}
if ('right' in data) {
this.setRight(data.right);
}
}).bind(this));
};
exports.create = function (descriptor) {
return new Robot(descriptor);
};