-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmavl.js
More file actions
executable file
·254 lines (192 loc) · 5.35 KB
/
mavl.js
File metadata and controls
executable file
·254 lines (192 loc) · 5.35 KB
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
/**
* Mavlink utils library
*/
var Mavlink = require('mavlink');
var config = require('./config.js');
var mavl = {
// true when at least one message
// has been received
_has_received_message: false,
_is_incoming_ready: false,
_is_outgoing_ready: false,
EVENT_KEY_ON_MAVL_READY: 'ready',
time_boot: null,
previous_time_boot: null,
callbacks: {},
consts: {
'MAV_CMD_NAV_LAST': 95
},
incoming: null,
outgoing: null,
/**
* Sets the timestamp of the latest message received
*
* @param timeboot Integer message timestamp
*/
set_time_boot: function(timeboot) {
mavl.previous_time_boot = mavl.time_boot;
mavl.time_boot = timeboot;
},
/**
* Sets the timestamp of the last message received
*
* @param timeboot Integer message timestamp
*/
set_previous_time_boot: function(timeboot) {
mavl.previous_time_boot = timeboot;
},
/**
* Retrieves the timestamp of the latest message received
*
* @return timestamp of latest message
*/
get_time_boot: function() {
return mavl.time_boot;
},
/**
* Retrieves the timestamp of the last message received
*
* @return timestamp of last message received
*/
get_previous_time_boot: function() {
return mavl.previous_time_boot;
},
/**
* Determines if the mavlink module has received at least one message
*
* @return boolean true if at least one message has been received
* from at least one client
*/
is_received_message: function(arg) {
if(arg !== null && arg !== undefined) {
var previous_state = mavl._has_received_message;
mavl._has_received_message = arg;
return previous_state;
}
return mavl._has_received_message;
},
/**
* Determines if Mavlink library used to parse incoming messages has
* finished loading message definitions.
*
* @return boolean true if 'ready' event has fired by the Mavlink library
*/
is_incoming_ready: function(arg) {
if(arg !== null && arg !== undefined) {
var previous_state = mavl._is_incoming_ready;
mavl._is_incoming_ready = arg;
return previous_state;
}
return mavl._is_incoming_ready;
},
/**
* Determines if Mavlink library used to send messages back to clients
* has finished loading message definitions.
*
* @return boolean true if 'ready' event has fired by the Mavlink library
*/
is_outgoing_ready: function(arg) {
if(arg !== null && arg !== undefined) {
var previous_state = mavl._is_outgoing_ready;
mavl._is_outgoing_ready = arg;
return previous_state;
}
return mavl._is_outgoing_ready;
},
/**
* Handles socket message dispatches.
*
* @param libsock Object socket library
* @param message_type String type of mavlink message
* @param message_body Object in json format containing message contents
* @param target_ip String containing ip address of target host
* @param target_port Integer containing port of target host
* @param callback Function to be called on message sent
*/
send_message: function(libsock, message_type, message_body, target_ip, target_port, callback) {
if(typeof callback != 'function') {
callback = function() {};
}
mavl.outgoing.createMessage(message_type, message_body, function(message) {
libsock.send(message.buffer, 0, message.buffer.length, target_port, target_ip, function(err, bytes) {
if(err) {
callback.call(this, err);
} else {
callback.call(this, null);
}
});
});
},
/**
* Initialize incoming and outgoing socket connections
*
* @emits mavl.EVENT_KEY_ON_MAVL_READY
*/
init: function(callback) {
mavl.incoming = new Mavlink(0, 0);
mavl.outgoing = new Mavlink(250, 1);
if(typeof callback != 'function') {
callback = function() {};
}
mavl.incoming.on('ready', function() {
check_mavlink_ready();
});
mavl.outgoing.on('ready', function() {
check_mavlink_ready();
});
function check_mavlink_ready() {
if(!check_mavlink_ready.mavlink_ready_count) {
check_mavlink_ready.mavlink_ready_count = 0;
}
check_mavlink_ready.mavlink_ready_count++;
if(check_mavlink_ready.mavlink_ready_count >= 2) {
callback.call(mavl);
mavl.emit(mavl.EVENT_KEY_ON_MAVL_READY, []);
}
}
},
/**
* Returns constant integer associated with mavlink command name.
* 95 for MAV_CMD_NAV_LAST, and 177 for MAV_CMD_DO_JUMP
*/
get_mav_const: function(enum_name, const_name) {
try {
console.log('INFO MAV_CONST_NAME', const_name);
if(!mavl.consts[const_name]) {
mavl.consts[const_name] = mavl.incoming.enums.filter(function(item) {
return item.$.name == enum_name;
})[0].entry.filter(function(item) {
return item.$.name == const_name;
})[0].$.value;
return mavl.consts[const_name];
}
return mavl.consts[const_name];
} catch(e) {
console.log('EXCEPTION MAV_CONST_NAME', '"' + const_name + '"',
'is not defined in the mavlink incoming library. \
Returning hardcoded value.');
if(const_name == 'MAV_CMD_DO_JUMP') {
return 177;
}
return 95;
}
},
on: function(evtKey, callback) {
if(!mavl.callbacks[evtKey]) {
mavl.callbacks[evtKey] = [];
}
mavl.callbacks[evtKey].push(callback);
},
emit: function(evtKey, params) {
if(!mavl.callbacks[evtKey]) {
return;
}
if(!(params instanceof Array)) {
params = [params];
}
for(var i = 0; i < mavl.callbacks[evtKey].length; i++) {
mavl.callbacks[evtKey][i].apply(mavl, params);
}
}
};
module.exports = mavl;