Skip to content

Commit 263adff

Browse files
author
Amadeus Stevenson
committed
Add serial-port controlled 6 DoF robot MEGATRON arm!
1 parent 507b01e commit 263adff

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"license": "ISC",
1515
"dependencies": {
1616
"johnny-five": "^0.8.8",
17+
"serialport": "^1.4.6",
1718
"ws": "^0.4.32"
1819
}
1920
}

serial.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// TODO: set up 6 ranges for x1,x2,x3,
2+
// grab the leap data: leap reports x,z,y
3+
var webSocket = require('ws'),
4+
ws = new webSocket('ws://127.0.0.1:6437'),
5+
SerialPort = require("serialport").SerialPort,
6+
leap_range = { x: [-150,120], y: [-20,100], z: [40,300] }, // mm
7+
frame, palm,
8+
serialPort = new SerialPort("/dev/tty.NoZAP-PL2303-00001014", {
9+
baudrate: 115200
10+
}),
11+
servo_range = { x: [500,2500], y: [500,2500], z: [500,2500] };
12+
13+
serialPort.on("open", function () {
14+
ws.on('message', function(data, flags) {
15+
frame = JSON.parse(data);
16+
// if only one hand is present
17+
//if (frame.hands && frame.hands.length == 1) {
18+
if (frame.hands && frame.hands.length >= 2) {
19+
// extract centre palm position in mm [x,y,z]
20+
palm = { left: frame.hands[0].palmPosition,
21+
right: frame.hands[1].palmPosition };
22+
23+
//console.log(palm[0],palm[1],palm[2]);
24+
//console.log(map(palm[0],'x'),map(palm[2],'y'),map(palm[1],'z'));
25+
26+
serialPort.write("#0 P" + map(palm.left[0],'x') + " #1 P" + map(palm.left[2],'y') + " #2 P" + map(palm.left[1],'z') + " #3 P" + map(palm.right[1],'z') + " #4 P" + map(palm.right[1],'z') + " #5 P" + map(palm.right[1],'z') + " T1000\r\n");
27+
} else {
28+
console.log(frame.hands.length);
29+
}
30+
});
31+
});
32+
33+
// map leap input to servo range
34+
function map(input,axis) {
35+
switch (axis) {
36+
case 'x':
37+
servo=servo_range.x;
38+
leap=leap_range.x;
39+
break;
40+
case 'y':
41+
servo=servo_range.y;
42+
leap=leap_range.y;
43+
break;
44+
case 'z':
45+
servo=servo_range.z;
46+
leap=leap_range.z;
47+
}
48+
49+
var pos = Math.round( // round to integer
50+
servo[0] + ( (servo[1]-servo[0]) / (leap[1]-leap[0]) )
51+
* (input - leap[0])
52+
);
53+
54+
// bounds checking
55+
pos = pos > servo[1] ? servo[1] : pos;
56+
pos = pos < servo[0] ? servo[0] : pos;
57+
58+
return pos;
59+
}
60+
61+
function sleep(millis) {
62+
setTimeout(function() {}, millis);
63+
}

0 commit comments

Comments
 (0)