forked from rwaldron/johnny-five
/
gripper.js
95 lines (88 loc) · 2 KB
/
gripper.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
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
var Board = require("../lib/board.js"),
Servo = require("../lib/servo.js"),
__ = require("../lib/fn.js");
/**
* Gripper
*
* Supports:
* [Parallax Boe-Bot gripper](http://www.parallax.com/Portals/0/Downloads/docs/prod/acc/GripperManual-v3.0.pdf)
*
* [DFRobot LG-NS](http://www.dfrobot.com/index.php?route=product/product&filter_name=gripper&product_id=628#.UCvGymNST_k)
*
*
* @param {[type]} servo [description]
*/
function Gripper( opts ) {
if ( !(this instanceof Gripper) ) {
return new Gripper( opts );
}
// Default options mode, assume only when opts is a pin number
if ( typeof opts === "number" ) {
opts = {
servo: {
pin: opts,
range: [ 0, 180 ]
},
scale: [ 0, 10 ]
};
}
// Default set() args to 0-10
this.scale = opts.scale || [ 0, 10 ];
// Setup servo
// Allows pre-constructed servo or creating new servo.
// Defaults for new Servo creation fall back to Servo defaults
this.servo = opts.servo instanceof Servo ?
opts.servo : new Servo( opts.servo );
}
[
/**
* open Open the gripper
*
* @return {Object} this
*/
{
name: "open",
args: function() {
return this.servo.range[0];
}
},
/**
* close Close the gripper
*
* @return {Object} this
*/
{
name: "close",
args: function() {
return this.servo.range[1];
}
},
/**
* set Set the gripper's open width
*
* @param {Number} 0-10, 0 is closed, 10 is open
*
* @return {Object} this
*/
{
name: "set",
args: function( position ) {
// Map/Scale position value to a value within
// the servo's lo/hi range
return Math.floor(
__.map(
position,
this.scale[0], this.scale[1],
this.servo.range[1], this.servo.range[0]
)
);
}
}
].forEach(function( api ) {
Gripper.prototype[ api.name ] = function() {
return this.servo.move(
api.args.apply( this, [].slice.call(arguments) )
);
};
});
module.exports = Gripper;