-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmission.js
More file actions
executable file
·238 lines (174 loc) · 5.9 KB
/
mission.js
File metadata and controls
executable file
·238 lines (174 loc) · 5.9 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
/**
* Mission library and utils
*/
var config = require('./config.js');
var utils = require('./utils.js');
var mission = {
waypoints_request_timeout: null,
waypoints_queue_count_timeout: null,
waypoints_received_count: false,
waypoints_queued_count: false,
waypoints_request_isFinished: false,
waypoints_count: 0,
waypoints_count_limit: 0,
last_wp_seq: -1,
EVENT_KEY_ON_MISSION_WAYPOINTS: 'waypoints',
EVENT_KEY_ON_MISSION_WAYPOINT: 'waypoint',
EVENT_KEY_ON_MISSION_ERROR: 'error',
callbacks: {},
// temp list of mission items
mission_items: {},
/**
* Determines if a waypoint count for the current mission
* has been received from the ground station
*
* @param arg Boolean setting the new waypoints_received_count state
*/
is_received_waypoint_count: function(arg) {
if(arg !== null && arg !== undefined) {
var prev_state = mission.waypoints_received_count;
mission.waypoints_received_count = arg;
return prev_state;
}
return mission.waypoints_received_count;
},
is_queued_waypoint_count: function(arg) {
if(arg !== null && arg !== undefined) {
var prev_state = mission.waypoints_queued_count;
mission.waypoints_queued_count = arg;
return prev_state;
}
return mission.waypoints_queued_count;
},
/**
* Called any time a waypoint is received
*
* @param waypoint
* @emits mission.EVENT_KEY_ON_MISSION_WAYPOINT
*/
receive_waypoint: function(libsock, mavlink, waypoint, limit) {
mission.waypoints_count = waypoint.seq;
mission.mission_items[waypoint.seq] = waypoint;
mission.emit(mission.EVENT_KEY_ON_MISSION_WAYPOINT);
if(!mission.waypoints_request_isFinished) {
mission.request_waypoint(libsock, mavlink, mission.waypoints_count + 1, limit);
} else {
mission.end_request_waypoints();
}
},
/**
* Receives total number of waypoints to fetch.
* Receiving a waypoint count begins starts a new process
* of fetching waypoints. When this process is finished,
* a mission.EVENT_KEY_ON_MISSION_WAYPOINTS event is emitted.
*
* @param limit int total number of waypoints to request
*/
receive_waypoint_count: function(libsock, mavlink, limit) {
if(!mission.is_received_waypoint_count(true)) {
clearTimeout(mission.waypoints_request_timeout);
}
if(!mission.is_queued_waypoint_count(true)) {
clearTimeout(mission.waypoints_queue_count_timeout);
}
mission.waypoints_count = 0;
mission.request_waypoint(libsock, mavlink, 0, limit);
},
/**
* Request a single mission waypoint
*
* @param count int index of item being requested
* @param libsock Object socket manager, used to request a new waypoint by
* using the mavlink outgoing socket to send a message to the ground station.
*/
request_waypoint: function(libsock, mavlink, wp_seq, limit) {
if(mission.last_wp_seq == wp_seq) {
return;
}
mission.last_wp_seq = wp_seq;
if(mission.last_wp_seq == limit) {
mission.waypoints_request_isFinished = true;
return;
}
utils.log('request_mission_item> requesting item #' + (mission.last_wp_seq + 1) + '/' + limit);
mavlink.send_message(libsock, 'MISSION_REQUEST', {
'target_system': 1,
'target_component': 1,
'seq': mission.last_wp_seq
}, config.get_config('mavlink').outgoing_host, config.get_config('mavlink').outgoing_port, function(err, response) {
utils.log('mavlink>MISSION_REQUEST> sent MISSION_ITEM request');
});
},
/**
* Sends a mavlink message to the ground station
* requesting a new mission count. This begins the
* waypoint-fetching process.
*
* @param mavlink Object mavlink manager
* @emits mission.EVENT_KEY_ON_MISSION_ERROR if a response to
* waypoints request does not happen in less than 30 seconds.
*/
request_waypoints: function(libsock, mavlink) {
mission.is_queued_waypoint_count(true);
clearTimeout(mission.waypoints_queue_count_timeout);
clearTimeout(mission.waypoints_request_timeout);
mission.waypoints_queue_count_timeout = setTimeout(function() {
mission.is_queued_waypoint_count(false);
}, 10000);
var host = config.get_config('mavlink').outgoing_host;
var port = config.get_config('mavlink').outgoing_port;
mavlink.send_message(libsock, 'MISSION_REQUEST_LIST', {
'target_system': 1,
'target_component': 1
}, host, port, function(err, response) {
if(err) {
return utils.log(err);
}
clearTimeout(mission.waypoints_request_timeout);
mission.waypoints_request_timeout = setTimeout(function() {
// throw error if received_mission_count == false in 30 secs
if(!mission.is_received_waypoint_count()) {
mission.emit('error', ['ERROR>mavlink_outgoing>mission_request_list> MISSION_COUNT not received in 30 seconds...']);
}
}, 30000);
});
},
/**
* Ends a request for n amount of waypoints begun
* by calling mission.request_waypoints.
*
* @emits mission.EVENT_KEY_ON_MISSION_WAYPOINTS
*/
end_request_waypoints: function() {
console.log('INFO MISSION WAYPOINTS request ended. Collected', (mission.waypoints_count + 1) + '/' + mission.waypoints_count_limit, 'items.');
// reset mission retrieval flags and temp array
clearTimeout(mission.waypoints_request_timeout);
mission.is_received_waypoint_count(false);
mission.is_queued_waypoint_count(false);
mission.waypoints_count = 0;
mission.waypoints_count_limit = 0;
mission.waypoints_request_isFinished = false;
// emit waypoints
mission.emit(mission.EVENT_KEY_ON_MISSION_WAYPOINTS, [mission.mission_items]);
// reset waypoints
mission.mission_items = {};
},
on: function(evtKey, callback) {
if(!mission.callbacks[evtKey]) {
mission.callbacks[evtKey] = [];
}
mission.callbacks[evtKey].push(callback);
},
emit: function(evtKey, params) {
if(!mission.callbacks[evtKey]) {
return;
}
if(!(params instanceof Array)) {
params = [params];
}
for(var i = 0; i < mission.callbacks[evtKey].length; i++) {
mission.callbacks[evtKey][i].apply(mission, params);
}
}
};
module.exports = mission;