-
Notifications
You must be signed in to change notification settings - Fork 0
/
drone01.js
153 lines (126 loc) · 2.8 KB
/
drone01.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
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
'use strict';
const arDrone = require('ar-drone');
const client = arDrone.createClient();
const pngStream = client.getPngStream();
let lastPng;
pngStream.on('error', console.log)
.on('data', function(pngBuffer) {
lastPng = pngBuffer;
});
const delay = async (ms) => new Promise(resolve => { setTimeout(resolve, ms); });
const http = require('http');
const Koa = require('koa');
const app = new Koa();
const Router = require('koa-router');
const router = new Router();
app.use(router.routes()).use(router.allowedMethods());
const staticServer = require('koa-static');
app.use(staticServer(__dirname + '/public'));
router.get('/png', (ctx, next) => {
if (lastPng) {
//ctx.is('image/png')
ctx.body= lastPng;
} else {
ctx.body='not ready';
}
})
router.get('/api/:key', (ctx, next) => {
// ctx.router available
let key = ctx.params.key;
// console.log(key);
if (key === 'takeoff') {
client.takeoff();
console.log('TAKEOFF');
} else if (key === 'land') {
client.land();
console.log('LAND');
} else if (key === 'counterClockwise') {
client.counterClockwise(0.5)
client.after(100, function() {
this.stop();
})
} else if (key === 'clockwise') {
client.clockwise(0.5);
client.after(100, function() {
this.stop();
})
}
else if (client[key]) {
client[key](0.5);
// .after(0, function() {
// this[key](0.5);
// })
client.after(100, function() {
this.stop();
})
}
/*
switch (key) {
case 'left':
client
.after(10, function() {
this.left(0.5);
})
.after(500, function() {
this.stop();
})
console.log(key);
break;
case 'right':
client
.after(10, function() {
this(0.5);
})
.after(500, function() {
this.stop();
})
console.log(key);
break;
case 'up':
console.log(key);
break;
case 'down':
console.log(key);
break;
case 'forward':
console.log(key);
break;
case 'back':
console.log(key);
break;
case 'takeoff':
// client.takeoff();
console.log(key);
break;
case 'land':
console.log(key);
break;
default:
}*/
ctx.body = `<h2>Method ${key}</h2> `;
});
http.createServer(app.callback()).listen(3000);
/*
// client.takeoff();
// client.createRepl();
(async () => {
// client.createRepl();
//client.takeoff();
await delay(2000);
client.front(0.01);
await delay(2000);
client.back(0.01);
await delay(2000);
client.clockwise(0.01);
await delay(2000);
client.counterClockwise(0.01);
await delay(2000);
client.left(0.01);
await delay(2000);
client.right(0.01);
await delay(2000);
client.stop();
await delay(2000);
client.land();
})().catch( e => {console.error(e);});
*/