-
Notifications
You must be signed in to change notification settings - Fork 0
/
Control.js
79 lines (66 loc) · 2.39 KB
/
Control.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
// programmed in javascript but controls arduino
//used johnny five libraries to do so
// javascipt for ease of retrieval of leap motion data
//this sets up the websocket for the leapmotion
var webSocket = require('ws'),
//leap motion standard port
ws = new webSocket('ws://127.0.0.1:6437'),
//sets up connection to johhnny five library - enabling the use of javascript in controlling arduino
//johnny five library communicates with the standard firmata plus on arduino
five = require('johnny-five'),
//palm data
palm = Array(),
//arduino set up
board = new five.Board(),
led, frame, servo1;
board.on('ready', function() {
//test led
led = new five.Led(13);
//initializing servos for different controls
// started at zero position for each respective control
//yaw servo
servoyaw = new five.Servo({
pin: 9,
range: [0, 180],
startAt: 90
});
//pitch servo
servopitch = new five.Servo({
pin: 10,
range: [0, 180],
startAt: 90
});
//throttle servo
servothrottle = new five.Servo({
pin: 11,
range: [10, 180],
startAt: 10
});
ws.on('message', function(data, flags) {
//getting data from websocket of leap motion and parsing it as a json file
frame = JSON.parse(data);
//initiating control
if (frame.hands && frame.hands.length > 0) {
led.on();
//retrieving leap motion data - plotting palm position
palm[0] = frame.hands[0].palmPosition[0]; // x value
palm[1] = frame.hands[0].palmPosition[1]; // y value
palm[2] = frame.hands[0].palmPosition[2]; // z value
// mapping the palm position onto the servos
// the servos control the joy stick of the plane
//mapping involves calculating the respective positions of joystick and range of hand movements
//obsevre leap motion sample json data to understand this
servothrottle.to(140*palm[1]/475);
servoyaw.to(90+0.5*palm[0]);
servopitch.to(90+0.5*palm[2]);
// safety when the hand is not there, the drone is turned off immediately
}
else {
led.off();
// the default positions
servopitch.to(90);
servoyaw.to(90);
servothrottle.to(10)
}
});
});