-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
102 lines (93 loc) · 2.79 KB
/
server.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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
const pdrone = require('pdrone');
const drone = pdrone({ id: 'Team13', debug: false });
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
http.listen(3000, function () {
console.log('open http://localhost:3000/');
});
// Clap-based drone contol
var clapDetector = require('clap-detector');
clapDetector.updateConfig({
CLAP_ENERGY_THRESHOLD: 0.3,
CLAP_AMPLITUDE_THRESHOLD: 0.9
});
// Voice-based drone control
const defaultWaitTime = 100;
drone.on('connected', async function () {
console.log('drone connected');
io.on('connection', function (socket) {
console.log('connected to socket.io');
socket.on('disconnect', function () {
console.log('disconnected');
});
socket.on('message', async function (msg) {
console.log('message: ' + msg);
switch(msg) {
case 'take off':
console.log('received take off command');;
drone.takeOff();
break;
case 'enough':
console.log('land');
drone.land();
break;
case 'go up':
drone.fly({ gaz: 100 });
await drone.wait(defaultWaitTime);
drone.fly({ gaz: 0 });
await drone.wait(defaultWaitTime);
break;
case 'go up ten':
drone.fly({ gaz: 100 });
break;
case 'flip left':
drone.flip({direction: 'left'});
break;
case 'flip right':
drone.flip({direction: 'right'});
break;
case 'flip front':
drone.flip({direction: 'front'});
break;
case 'flip back':
drone.flip({direction: 'back'});
break;
case 'fire':
drone.fire();
break;
case 'turn left':
drone.fly({yaw: -50});
break;
case 'turn right':
drone.fly({ yaw: 50 });
break;
default:
if (msg.match(/russian/ig)) {
clapDetector.start();
clapDetector.onClap(function (history) {
console.log('CLAP => flip left');
//drone.flip({direction: 'left'});
drone.fire();
});
console.log('russian roulette MODE ON :gun:');
drone.fly({yaw: -50});
// if (Math.random() >= 0.5) {
// drone.fire();
// }
}
}
// });
// drone.on('connected', async function () {
// // Get up a little bit
// drone.takeOff();
// await drone.wait(3000);
// drone.flatTrim();
// await drone.wait(1000);
// });
});
});
});