-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservo.js
46 lines (40 loc) · 1003 Bytes
/
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
'use strict';
var IO = require('thingjs-io');
/**
* Servo module.
* @constructor
* @param {Array} pins The pin numbers.
*/
function Servo (pins) {
var options = {
// Default 20ms period(500Hz) is for Linkit 7688.
period: 20000000,
maxDutyCycle: 2200000,
minDutyCycle: 500000,
};
this._servo = new IO(pins, 'pwm', options);
// Rotate to 90 degree at beginning.
this.rotate(90);
}
Servo.MAX_DEGREE = 180;
Servo.prototype = {
id: null,
module: 'Servo',
type: 'output',
_degree: 0,
/**
* Rotate the servo.
* @param {Number} degree The degree is between 0 and 180.
* @return {Number} Current rotation degree.
*/
rotate: function(degree) {
if (degree >= 0 && degree <= Servo.MAX_DEGREE) {
this._servo.val(degree / Servo.MAX_DEGREE);
this._degree = degree;
} else if (degree !== undefined) {
console.warn('The degree: ' + degree + ' is invalid.');
}
return this._degree;
}
};
module.exports = Servo;