This repository has been archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeapController.js
108 lines (90 loc) · 2.35 KB
/
LeapController.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
96
97
98
99
100
101
102
103
104
105
106
107
"use strict";
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
const Cylon = require('cylon');
const https = require('https')
let range = {
x: { min: -200, max: 200 },
y: { min: 150, max: 550 },
z: { min: -200, max: 200 },
a: { min: -2.5, max: 2.5 }
}
const
hostname = "192.168.102.73", //"10.37.120.78",//'192.168.1.13',
port = 3000,
route = '/api/robots/Drone/commands',
command = {
set: route + '/set',
kill: route + '/kill'
},
endpoint = {
set: {
hostname: hostname,
port: port,
path: command.set,
method: 'POST',
headers: { 'Content-Type': 'application/json' }
},
kill: {
hostname: hostname,
port: port,
path: command.kill,
method: 'POST',
headers: { 'Content-Type': 'application/json' }
}
};
Cylon.robot({
connections: {
leapmotion: { adaptor: "leapmotion" }
},
devices: {
leapmotion: { driver: "leapmotion" }
},
work: (my) => {
let delay = { count: 0, frequency: 30, bang: false }
my.leapmotion.on('frame', (frame) => {
( delay.count >= delay.frequency ) ? delay.bang = true : delay.bang = !delay.bang; delay.count++
if ( frame.hands.length === 0 && delay.bang) {
var req = https.request(endpoint.kill, (res) => {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
//console.log('no hands')
});
})
req.write(JSON.stringify({}))
req.end()
delay.count = 0
}
})
my.leapmotion.on("hand", (hand) => {
if (!delay.bang) { return }
var scaled = {
throttle: ((hand.palmY).fromScale(range.y.min,range.y.max)).toFixed(2),
rudder: ((hand.rotation.matrix).fromScale( range.a.min, range.a.max )).toFixed(2),
aileron: ((hand.palmX).fromScale( range.x.min ,range.x.max )).toFixed(2),
elevator: (1.0 - (hand.palmZ).fromScale( range.z.min ,range.z.max )).toFixed(2)
}
var req = https.request(endpoint.set, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
//console.log(body.toString())
});
});
req.write(JSON.stringify({
t: scaled.throttle,
r: scaled.rudder,
a: scaled.aileron,
e: scaled.elevator
}));
req.end();
delay.count = 0
});
}
}).start();