-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathautonomy.js
118 lines (111 loc) · 3.71 KB
/
autonomy.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
const express = require('express');
const bodyparser = require('body-parser');
const arDrone = require('ar-drone');
const control = require('ardrone-autonomy');
const router = express.Router();
const app = express();
const commands = ['takeoff', 'land','up','down','goleft','goright','turn','goforward','gobackward','stop'];
var client = arDrone.createClient();
var controller = control.control(client, {debug: false});
// disable emergency
client.disableEmergency();
// express
app.use(bodyparser.json());
app.use(express.static(__dirname + '/public'));
router.get('/',(req,res) => {
res.sendFile('index.html');
});
router.post('/command',(req,res) => {
console.log('command recieved ', req.body);
console.log('existing commands', commands);
let command = req.body.command.replace(/ /g,'');
if(commands.indexOf(command) !== -1) {
switch(command.toUpperCase()) {
case "TAKEOFF":
console.log('taking off the drone');
client.takeoff();
controller.zero();
break;
case "LAND":
console.log('landing the drone');
client.land();
break;
case "UP":
console.log('taking the drone up 1 meter up');
controller.up(0.5,(err,data) => {
if(err) {
client.stop();
client.land();
}
})
break;
case "DOWN":
console.log('taking the drone down 1 meter');
controller.down(0.5,(err,data) => {
if(err) {
client.stop();
client.land();
}
})
break;
case "GOLEFT":
console.log('taking the drone left half meter');
controller.left(0.5,(err,data) => {
if(err) {
client.stop();
client.land();
}
})
break;
case "GORIGHT":
console.log('taking the drone right half meter');
controller.right(0.5,(err,data) => {
if(err) {
client.stop();
client.land();
}
})
break;
case "TURN":
console.log('turning the drone');
controller.cw(90).run((err, data) => {
if(err) {
client.client().stop();
client.client().land();
}
});
break;
case "GOFORWARD":
console.log('moving the drone formward by 1 meter');
controller.forward(1,(err,data) => {
if(err) {
client.stop();
client.land();
}
})
break;
case "GOBACKWARD":
console.log('moving the drone backward by 1 meter');
controller.backward(1,(err,data) => {
if(err) {
client.stop();
client.land();
}
})
break;
case "STOP":
controller.hover(5000,(err,data) => {
if(err) {
client.stop();
client.land();
}
})
break;
default:
break;
}
}
res.send('OK');
});
app.use('/',router);
app.listen(process.env.port || 3000);