-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
227 lines (209 loc) · 5.64 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
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
var Ami = require('asterisk-ami');
var events = require('events').EventEmitter;
var util = require('util');
var config = require('./config');
var ar_drone = require('ar-drone');
var client = ar_drone.createClient();
util.inherits(Nodecopter, events);
var upcount = 0;
var downcount = 0;
var rightcount = 0;
var leftcount = 0;
var clockwisecount = 0
var counterclockwisecount = 0;
var frontcount = 0;
var backcount = 0;
var nodecopter = new Nodecopter();
function Nodecopter() {
var self = this;
this.ami = new Ami({ host: '192.168.15.101', username: 'nodecopter', password: 'secret' });
this.ami.on('ami_data', function(data){
if(data.event){
self.emit(data.event, data);
}
});
}
Nodecopter.prototype.ami_connect = function(cb) {
this.ami.connect(cb);
};
Nodecopter.prototype.ami_send = function(data) {
this.ami.send(data);
};
Nodecopter.prototype.decode_agi_env = function(data){
var str = decodeURIComponent(data);
str = str.substring(0,str.length - 2);
str = str.replace(/:\s/g, "\":\"");
str = str.replace(/\n/g,"\",\"");
str = str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
str = '{"' + str + '"}';
return JSON.parse(str);
}
Nodecopter.prototype.decode_agi = function(data){
var str = decodeURIComponent(data);
str = str.substring(0,str.length - 1);
var arr = /(\d+)\sresult=(.+)/.exec(str);
//BODGE!!!!
if(arr == null){
arr = Array();
arr[1] = '200';
arr[2] = '#';
}
var obj = {
code: arr[1],
result: arr[2]
}
return obj;
}
nodecopter.on('AsyncAGI', function(data){ // this.on(.... ???
var self = this;
if(data.result){
data.result = nodecopter.decode_agi(data.result);
}
if(data.env){
data.env = nodecopter.decode_agi_env(data.env);
}
console.log(data);
if(data.result) {
data.result.code == '200' ? self.commandControl(data.result.result) : console.error('Sorry, didn\'t get that');
self.amiSend(data);
}
if(data.subevent == 'Start'){
self.amiSend(data);
}
});
Nodecopter.prototype.amiSend = function(data) {
this.ami_send({
action:'AGI',
channel: data.channel,
command: 'GET DATA beep 30000000000000 1'
});
}
nodecopter.ami_connect(function(){
console.log('ami connected');
});
Nodecopter.prototype.reset = function(command) {
if(config[command] != 'left') {
leftcount = 0;
}if(config[command] != 'right') {
rightcount = 0;
}if(config[command] != 'up') {
upcount = 0;
}if(config[command] != 'down') {
downcount = 0;
}if(config[command] != 'counterclockwise') {
counterclockwisecount = 0;
}if(config[command] != 'front') {
frontcount = 0;
}if(config[command] != 'back') {
backscount = 0;
}
}
Nodecopter.prototype.commandControl = function(command) {
this.reset(command);
switch(config[command]) {
case 'land' :
console.log('landing drone...');
client.stop();
client.land();
break;
case 'takeoff':
console.log('taking off...');
client.takeoff();
break;
case 'stop':
console.log('Hovering hx-ardrone');
client.stop();
break;
case 'up':
upcount++;
if(upcount < 10) {
console.log('Current upward speed = 0.'+upcount);
client.up(upcount/10);
} else {
client.up(1);
console.log('Warning: reached upward max speed...');
}
break;
case 'down':
downcount++;
if(downcount < 10) {
console.log('Current downward speed = 0.'+downcount);
client.down(downcount/10);
} else {
console.log('Warning: reached downward max speed...');
client.down(1);
}
break;
case 'left':
console.log('banking left...');
leftcount++;
if(leftcount < 10) {
console.log('Current left speed = 0.'+leftcount);
client.left(leftcount/10);
} else {
console.log('Warning: reached left max speed...');
client.left(1);
}
break;
case 'right':
console.log('banking right...');
rightcount++;
if(rightcount < 10) {
console.log('Current right speed = 0.'+rightcount);
client.right(rightcount/10);
} else {
console.log('Warning: reached right max speed...');
client.right(1);
}
break;
case 'clockwise':
console.log('turning right...');
clockwisecount++;
if(clockwisecount < 10) {
console.log('Current right speed = 0.'+clockwisecount);
client.clockwise(clockwisecount/10);
} else {
console.log('Warning: reached right max speed...');
client.clockwise(1);
}
break;
case 'counterclockwise':
console.log('turning right...');
counterclockwisecount++;
if(counterclockwisecount < 10) {
console.log('Current left speed = 0.'+counterclockwisecount);
client.counterClockwise(counterclockwisecount/10);
} else {
console.log('Warning: reached left max speed...');
client.counterClockwise(1);
}
break;
case 'flip':
console.log('flipping!!!');
client.animate('flipAhead');
break;
case 'front':
frontcount++;
console.log('moving forward...');
if(frontcount < 10) {
client.front(frontcount/10);
} else {
console.log('Warning: reached forward max speed...');
client.front(1);
}
break;
case 'back':
backcount++;
console.log('moving backward...');
if(backcount < 10) {
client.back(backcount/10);
} else {
console.log('Warning: reached backward max speed...');
client.back(1);
}
}
};
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
client.land();
});