-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
315 lines (275 loc) · 9.48 KB
/
index.html
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!DOCTYPE html>
<html lang="en">
<head>
<title>ArDrone with Leap</title>
<meta charset="UTF-8" />
<script src="jquery-2.0.3.min.js"></script>
<script src="raphael.2.1.0.min.js"></script>
<script src="justgage.1.0.1.min.js"></script> <!-- from http://justgage.com/ -->
<link href="style.css" rel="stylesheet" type="text/css">
<script>
"use strict";
var controlEnabled=false;
var batteryGauge;
var speedGauge;
function initGauges() {
batteryGauge = new JustGage({
id: "batteryGaugeCanvas",
value: 67,
min: 0,
max: 100,
title: "battery",
levelColorsGradient: true,
levelColors: ["#e60000","#eaff00","#00cc00"]
});
speedGauge = new JustGage({
id: "speedGaugeCanvas",
value: 3,
min: 0,
max: 3,
title: "altitude",
levelColorsGradient: true,
levelColors: ["#00cc00","#eaff00","#e60000"]
});
}
function setBattery(battery){
batteryGauge.refresh(battery);
}
function setAltitude(altitude){
speedGauge.refresh(altitude);
}
function updateGauges(battery, horizontalSpeed){
setBattery(battery);
setAltitude(horizontalSpeed);
}
function drawHorizonCanvas(angle, noseAngle){
var drawingCanvas = document.getElementById('horizonCanvas');
// Check the element is in the DOM and the browser supports canvas
if(drawingCanvas.getContext) {
var ctxH = drawingCanvas.height;
var ctxW = drawingCanvas.width;
var centerX = drawingCanvas.width/2;
var centerY = drawingCanvas.height/2;
// Initaliase a 2-dimensional drawing context
var ctx = drawingCanvas.getContext('2d');
//Canvas commands go here
//empty it
drawingCanvas.width = drawingCanvas.width;
//draw text
ctx.fillText(Math.round(angle)+'°', 10, 10);
//draw triangle
ctx.beginPath();
ctx.strokeStye="#500";
var baseX = 100*Math.sin((angle-90)*Math.PI/180);
var baseY = 100*Math.cos((angle-90)*Math.PI/180);
var moveToX = 100*Math.sin((angle)*Math.PI/180);
var moveToY = 100*Math.cos((angle)*Math.PI/180);
ctx.moveTo(centerX+baseX, centerY-baseY);
ctx.lineTo(centerX, centerY-(200/90)*noseAngle);
ctx.lineTo(centerX-baseX, centerY+baseY);
var lingrad = ctx.createLinearGradient(0,0,0,400);
lingrad.addColorStop(0, '#66CC00');
lingrad.addColorStop(0.5, '#fff');
lingrad.addColorStop(1, '#408000');
ctx.fillStyle = lingrad;
ctx.fill();
ctx.strokeStyle = "green";
ctx.stroke();
//draw line
ctx.beginPath();
var moveToX = 100*Math.sin((angle-90)*Math.PI/180);
var moveToY = 100*Math.cos((angle-90)*Math.PI/180);
ctx.moveTo(centerX+moveToX, centerY-moveToY);
ctx.lineTo(centerX-moveToX, centerY+moveToY);
ctx.strokeStye="#500";
ctx.lineWidth=5;
ctx.stroke();
//draw axis
ctx.beginPath();
ctx.moveTo(0, centerY);
ctx.strokeStyle="#100";
ctx.lineWidth=1;
ctx.lineTo(ctxW, centerY);
ctx.stroke();
ctx.moveTo(centerX, 0);
ctx.lineTo(centerX, ctxH);
ctx.stroke();
}
}
function drawDirectionCanvas(angle){
var drawingCanvas = document.getElementById('directionCanvas');
// Check the element is in the DOM and the browser supports canvas
if(drawingCanvas.getContext) {
// Initaliase a 2-dimensional drawing context
var ctx = drawingCanvas.getContext('2d');
//Canvas commands go here
//empty it
drawingCanvas.width = drawingCanvas.width;
//draw text
ctx.fillText(Math.round(angle)+'°', 10, 10);
//draw triangle
ctx.beginPath();
ctx.moveTo(100, 200);
ctx.lineTo(200, 200-(200/90)*angle);
ctx.lineTo(300, 200);
ctx.closePath();
var lingrad = ctx.createLinearGradient(0,0,0,400);
lingrad.addColorStop(0, '#66CC00');
lingrad.addColorStop(0.5, '#fff');
lingrad.addColorStop(1, '#408000');
ctx.fillStyle = lingrad;
ctx.fill();
ctx.strokeStyle='green';
ctx.stroke();
}
}
function updateDirection(datas){
$('#leapDirectionContainer img').css('opacity','0.3');
var detectionLimit = 10;
var movement = {};
//see if going left or right
var rollAngle = datas['horizonAngle'];
if(rollAngle < -detectionLimit){
//roll left
$('#leftImage').css('opacity', '1');
}else if(rollAngle > detectionLimit){
//roll right
$('#rightImage').css('opacity', '1');
}
//see if going front or back
var noseAngle = datas['directionAngle'];
if(noseAngle < -detectionLimit){
//roll left
$('#frontImage').css('opacity', '1');
}else if(noseAngle > detectionLimit){
//roll right
$('#backImage').css('opacity', '1');
}
//see if turning clockwise or counterclockwise
var clockAngle = datas['clockWiseAngle'];
if(clockAngle < -detectionLimit){
//counterclockwise
//$('#turnaroundLeftImage').css('opacity', '1');
}else if(clockAngle > detectionLimit){
//clockwise
//$('#turnaroundRightImage').css('opacity', '1');
}
//see if going up or down
var heightFromLeap = datas['handHeight'];
if(heightFromLeap > 230){
$('#upImage').css('opacity', '1');
}else if(heightFromLeap < 170){
$('#downImage').css('opacity', '1');
}
}
function setControlState(enabled){
controlEnabled = enabled;
if(controlEnabled){
$('html').css('border', '4px solid green');
}else{
$('html').css('border', '4px solid red');
}
}
//example used for socket: http://martinsikora.com/nodejs-and-websocket-simple-chat-tutorial
// Initialize everything when the window finishes loading
window.addEventListener("load", function(event) {
$('html').css('border', '4px solid red');
$(window).keydown(function(event){
if(event['keyCode']==32){
setControlState(!controlEnabled);
connection.send(JSON.stringify({action: "updateControlEnabled",
controlEnabled: controlEnabled}));
return false;
}
});
$('#takeOffButton').click(function(){
connection.send(JSON.stringify({action: "takeOff"}));
//$('#takeOffButton').prop('disabled',true);
});
$('#landButton').click(function(){
connection.send(JSON.stringify({action: "land"}));
});
drawHorizonCanvas(0, 0);
//drawDirectionCanvas(0);
initGauges();
setInterval(function(){
var alt = Math.floor(Math.random()*101);
var battery = Math.floor(Math.random()*101);
//updateGauges(alt, battery);
}, 1000);
window.WebSocket = window.WebSocket || window.MozWebSocket;
var connection = new WebSocket('ws://127.0.0.1:8080', 'echo-protocol');
connection.onerror = function(error){
//an error occured when sending/receiving data
console.log("error during sending/receinving datas : "+error);
};
connection.onmessage = function(message){
//try to decode json (we assume that each message from server is json)
try{
var json = JSON.parse(message.data);
}catch(e){
console.log('this is not json : ',message.data);
return;
}
if(json['action']==='updateFrame'){
drawHorizonCanvas(json['horizonAngle'], json['directionAngle']);
updateDirection(json);
}else if(json['action']==='handLeftLeap'){
setControlState(false);
drawHorizonCanvas(0,0);
$('#leapDirectionContainer img').css('opacity','0.3');
}else if(json['action']==='updateLeapState'){
if(json['connected']){
$('#leapDotState').attr('src','imgs/green-dot.png');
}else{
$('#leapDotState').attr('src','imgs/red-dot.png');
}
}else if(json['action']==='updateDroneBattery'){
setBattery(json.battery);
}else if(json['action']==='updateDroneAltitude'){
setAltitude(json.altitude);
}
};
});
</script>
</head>
<body>
<div id="generalStates">
leap <img id="leapDotState" src="imgs/red-dot.png"/>
</div>
<div id="topContainer" style="width: 100%; height: 50%; float: left;">
<h1>Leap device</h1>
<div id="infos"></div>
<div id="leapDirectionContainer">
<table>
<tr>
<td></td>
<td><img id="frontImage" src="imgs/front.png" alt="" /></td>
<td></td>
<td><img id="upImage" src="imgs/up.png" alt="" /></td>
</tr>
<tr>
<td><img id="leftImage" src="imgs/left.png" alt="" /></td>
<td><img id="turnaroundRightImage" src="imgs/turnaround-right.png" alt="" /><img id="turnaroundLeftImage" src="imgs/turnaround-left.png" alt="" /></td>
<td><img id="rightImage" src="imgs/right.png" alt="" /></td>
</tr>
<tr>
<td></td>
<td><img id="backImage" src="imgs/back.png" alt="" /></td>
<td></td>
<td><img id="downImage" src="imgs/down.png" alt="" /></td>
</tr>
</table>
</div>
<canvas id="horizonCanvas" width="300" height="300" style="float: right;"></canvas>
<!--<canvas id="directionCanvas" width="400" height="400" style="float: right;"></canvas>-->
</div>
<div id="bottomContainer" style="width: 100%; height: 50%;">
<h1>AR Drone</h1>
<button id="takeOffButton">take off</button>
<button id="landButton">land</button>
<svg id="batteryGaugeCanvas" width="200" height="160" style="float: right;"></svg>
<svg id="speedGaugeCanvas" width="200" height="160" style="float: right;"></svg>
</div>
</body>
</html>