-
Notifications
You must be signed in to change notification settings - Fork 0
/
servo.js
55 lines (47 loc) · 1.21 KB
/
servo.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
var fs = require("fs")
var queue = require('queue');
var Servo = function(channel) {
this.channel = channel;
this.pipe = '/var/servoDaemon/servo' + channel + '/position';
this.curPos = 0;
this.position = function(pos) {
fs.open(this.pipe, 'w', (err, fd) => {
if (err == null) {
fs.write(fd,pos);
this.curPos = pos;
}
else {
console.log(err);
}
});
};
this.q = queue();
this.q.concurrency = 1;
//Move from A to B in duration T
this.moveTo = function(startPos,endPos,time) {
var steps = time / 10;
var stepSize = (endPos - startPos) / steps;
var stepDur = time / steps;
var servo = this;
var i;
for (i = 0; i < steps ; i++) {
(function(pos) {
servo.q.push(function(done) {
setTimeout(function() {
servo.position(pos);
done();
}, stepDur);
});
})(startPos + (i * stepSize));
};
this.run();
}
this.run = function() {
this.q.start(function(err) {
if (err != null) {
console.log(err);
}
});
}
};
module.exports = Servo;