/
servo.js
55 lines (48 loc) · 1.77 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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2014, Decoded Ltd dev@decoded.com
*/
// Map a servo to a single hand, single axis
// grab the leap data
var webSocket = require('ws'),
ws = new webSocket('ws://127.0.0.1:6437'),
five = require('johnny-five'),
board = new five.Board(),
// mm range of leap motion to use, see leap-range.js to find
leap_range = [-100,100], // x of right hand
frame, palm;
// parse the data and respond
board.on('ready', function() {
// setup a servo on pin 9
servo = new five.Servo({
pin: 9,
range: [0, 179] // dependent on servo
});
// set to midpoint
servo.to(90);
ws.on('message', function(data, flags) {
frame = JSON.parse(data);
// if only one hand is present
if (frame.hands && frame.hands.length == 1) {
// extract centre palm position in mm [x,y,z]
palm = frame.hands[0].palmPosition;
// map x position of leap to servo
//console.log("input",palm[0]);
console.log("output", palm[0].map())
servo.to(palm[0].map());
}
});
});
// map two number ranges, adapted from SO: 10756313
Number.prototype.map = function () {
var output = Math.round((this - leap_range[0]) * (servo.range[1] - servo.range[0]) / (leap_range[1] - leap_range[0]) + servo.range[0]);
// check output is within range, or cap
output = (output > servo.range[1]) ? servo.range[1] : output;
output = (output < servo.range[0]) ? servo.range[0] : output;
// is the servo range reversed? uncomment below
output = servo.range[1] - output;
return output;
}