-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathservo.js
More file actions
147 lines (117 loc) · 3.3 KB
/
servo.js
File metadata and controls
147 lines (117 loc) · 3.3 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var events = require('events'),
util = require('util');
/*
* Main Servo constructor
* Process options
* Tell the board to set it up
*/
var Servo = function (options) {
if (!options || !options.board) throw new Error('Must supply required options to Servo');
this.board = options.board;
this.pin = this.board.normalizePin(options.pin || 9);
var types = {
attached: true,
detached: true,
read: true,
moved: true
};
if (this.board.serial && this.board.serial.readStream) {
console.log('board already ready, attaching servo', this);
this.attach();
} else {
this.board.on('ready', function () {
console.log('board ready, attaching servo', this);
this.attach();
}.bind(this));
}
this.board.on('data', function (message) {
var m = message.slice(0, -1).split('::'),
err = null,
pin, type, data;
if (!m.length) {
return;
}
pin = m[0]
type = m[1];
data = m.length === 3 ? m[2] : null;
if (pin === this.pin && types[type]) {
this.emit(type, err, data);
}
}.bind(this));
};
util.inherits(Servo, events.EventEmitter);
Servo.prototype.command = function () {
var msg = '98' + this.pin + ([].slice.call(arguments).join(''));
// this.board.log( 'info', 'command', msg );
this.board.write(msg);
};
Servo.prototype.detach = function () {
this.command('00');
};
Servo.prototype.attach = function () {
this.command('01');
};
Servo.prototype.write = function (pos) {
pos = this.board.lpad(3, '0', pos);
this.board.log('info', 'moving to: ' + pos);
this.command('02' + pos);
};
Servo.prototype.read = function () {
this.command('03');
};
// Servo.prototype.writeMilliseconds = function () {};
// Servo.prototype.attached = function (callback) {
// this.callbacks.attached.push(callback);
// };
Servo.prototype.sweep = function (options) {
// Ensure no missing options object
options = options || {};
var timeout = {
inner: null,
outer: null
},
moves = 0,
// sweep settings
lapse = options.lapse || 2000,
to = options.to || 180,
from = options.from || 1,
// sweep handlers
doSweep = function doSweep(pos) {
// this.board.log('info', 'current position: ', pos);
var moveTo,
posint = +pos;
// this.board.log('info', 'current pos int: ', posint);
if (posint === 93) {
moveTo = 1;
} else {
// this.board.log('info', 'posint not 93.....');
if (posint < to) {
moveTo = to;
} else if (posint == to) {
moveTo = 90;
} else {
moveTo = from;
}
}
this.write(moveTo);
moves++;
};
this.on('read', doSweep);
// Initialize sweep; wait for for stack unwind.
timeout.outer = setTimeout(function loop() {
// Read the current position, will trigger
// 'read' event with position data;
if (moves < 2) {
this.read();
timeout.inner = setTimeout(loop.bind(this), lapse);
} else {
// this.board.log('info', 'info', 'clearing');
clearTimeout(timeout.inner);
clearTimeout(timeout.outer);
loop = null;
this.removeListener('read', doSweep);
this.emit.call(this, 'aftersweep');
}
}.bind(this), 0);
};
module.exports = Servo;