File: src/io/Servo.js
/**
* Based on Servo.as originally written in as3.
* Copyright (c) the Funnel development team
* http://www.funnel.cc
*
* Ported to JavaScript by Jeff Hoefs
* Copyright (c) 2011-2012 Jeff Hoefs <soundanalogous@gmail.com>
*
* Released under the MIT license. See LICENSE file for details.
*/
JSUTILS.namespace('BO.io.Servo');
BO.io.Servo = (function() {
var Servo;
// dependencies
var Pin = BO.Pin;
/**
* Creates an interface to a Servo motor. Use this object to set
* the angle of the servo head. You can simply specify and angle between
* 0 and 180 degrees and the servo head will rotate to that angle. See
* [Breakout/examples/actuators/servo.html](https://github.com/soundanalogous/Breakout/blob/master/examples/actuators/servo.html) for an example application.
* You can also use this with a continuous rotation servo. See the
* description for the angle property for use with a continuous rotation
* servo.
*
* @class Servo
* @constructor
* @param {IOBoard} board A reference to the IOBoard instance that the
* servo is attached to.
* @param {Pin} servoPin A reference to the Pin the servo is connected to.
* @param {Number} minAngle The minimum angle the server can rotate to
* (default = 0).
* @param {Number} maxAngle The maximum angle the server can rotate to
* (default = 180).
*/
Servo = function(board, servoPin, minAngle, maxAngle) {
"use strict";
this.name = "Servo";
this._pin = servoPin;
this._angle = undefined;
this._minAngle = minAngle || 0;
this._maxAngle = maxAngle || 180;
var pinNumber = servoPin.number;
// sendServoAttach will set the pin mode to Pin.SERVO
board.sendServoAttach(pinNumber);
};
Servo.prototype = {
constructor: Servo,
/**
* Set the angle (in degrees) to rotate the server head to.
*
* <p>If you are using a continuous rotation servo, a value of 90
* will stop the servo. A value of 0 (or < 90 depending on the servo)
* will cause continous clockwise rotation and a value of 180 (or > 90)
* will cause continuous counter-clockwise rotation. If your motor
* does not come to a full stop when setting 90 degrees, you will need
* to adjust the servo (there is typically a screw on the motor) to
* adjust</p>
*
* @property angle
* @type Number
*/
set angle(value) {
if (this._pin.getType() === Pin.SERVO) {
this._angle = value;
//this._pin.value = this._angle;
this._pin.value = Math.max(0, Math.min(1, (this._angle - this._minAngle) /
(this._maxAngle - this._minAngle) * Servo.COEF_TO_0_180));
}
},
get angle() {
if (this._pin.getType() === Pin.SERVO) {
return this._angle;
}
}
};
/**
* The scale to convert 0-1 (0-255 in 8bit) to 0-0.706 (0-180 in 8bit).
* @property Servo.COEF_TO_0_180
* @static
*/
Servo.COEF_TO_0_180 = 180 / 255;
return Servo;
}());