-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaypoints.js
More file actions
executable file
·193 lines (147 loc) · 4.9 KB
/
Copy pathwaypoints.js
File metadata and controls
executable file
·193 lines (147 loc) · 4.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
/**
* Waypoint utils library
*/
var waypoints = {
_is_changed: false,
last_waypoint: null,
next_waypoint: null,
current_waypoint: null,
_waypoint_count: 0,
waypoints: {},
// return waypoint by key
get_waypoint: function(key) {
return waypoints.waypoints[key];
},
get_waypoints: function() {
return waypoints.waypoints;
},
get_last_waypoint: function() {
return waypoints.last_waypoint;
},
get_next_waypoint: function() {
return waypoints.next_waypoint;
},
get_current_waypoint: function() {
return waypoints.current_waypoint;
},
get_waypoint_count: function() {
return waypoints._waypoint_count;
},
/**
* Setting the current waypoint will also set the
* next, previous, and next waypoints. If the
* argument passed does not correspond to a waypoint
* that has previously been set, or if the argument
* modified (+1, -1) to retrieve next, previous, or
* next waypoints points to a waypoint that does
* not exist or has not yet been set, then that
* waypoint will be set to NULL.
*
* If the next waypoint differs from the current
* waypoint assigned as waypoints.next_waypoint,
* then waypoints._is_changed will be set to true.
*
* @param wpt_key int representing the hash key, or
* index of the current waypoint
*
* @return the wpt_key, or sequence index, of the
* waypoint being set as the current waypoint, or
* NULL if the current_waypoint does not exist.
*/
set_current_waypoint: function(mavl, wpt_key) {
if(waypoints.get_current_waypoint() && (wpt_key == waypoints.get_current_waypoint().seq)) {
return;
}
// assumes we are following waypoints in order
var previous_waypoint = waypoints.get_current_waypoint();
var current_waypoint = waypoints.get_waypoint(waypoints.get_next_nav_cmd(mavl, wpt_key));
var next_waypoint = current_waypoint ? waypoints.get_waypoint(waypoints.get_next_nav_cmd(mavl, current_waypoint.seq + 1)) : null;
if(previous_waypoint && current_waypoint && next_waypoint) {
console.log('INFO WAYPOINTS waypoint #' + wpt_key + ' reached (prev, current, next waypoints are now defined).');
}
if((previous_waypoint && waypoints.get_last_waypoint() && waypoints.get_last_waypoint().seq != previous_waypoint.seq)
|| (current_waypoint && waypoints.get_current_waypoint() && waypoints.get_current_waypoint().seq != current_waypoint.seq)
|| (next_waypoint && waypoints.get_next_waypoint() && waypoints.get_next_waypoint().seq != next_waypoint.seq)) {
waypoints._is_changed = true;
}
waypoints.last_waypoint = previous_waypoint;
waypoints.current_waypoint = current_waypoint;
waypoints.next_waypoint = next_waypoint;
return current_waypoint ? current_waypoint.seq : null;
},
/**
* Determines if wpt_key corresponds
* to a navigation instruction, such as jpm
*/
is_nav_cmd: function(mavl, wpt_key) {
return waypoints.get_waypoint(wpt_key).command <= mavl.get_mav_const('MAV_CMD', 'MAV_CMD_NAV_LAST');
},
/**
* From a "current" waypoint id passed, the next
* non special waypoint nav command wpt_key is returned.
* if no other normal nav waypoint cmds are not found,
* null is returned.
*/
get_next_nav_cmd: function(mavl, wpt_key) {
var current_wpt_key = wpt_key;
while(current_wpt_key < waypoints.get_waypoint_count()) {
current_wpt_key = waypoints.get_next_cmd(mavl, current_wpt_key)
if(!current_wpt_key) {
return null;
}
if(waypoints.is_nav_cmd(mavl, current_wpt_key)) {
return current_wpt_key;
}
current_wpt_key++;
}
return current_wpt_key;
},
/**
* Assumes intent is to always follow jumps in nav cmd list
*/
get_next_cmd: function(mavl, wpt_key) {
var current_wpt_key = wpt_key;
var current_wpt = null;
var max_loops = 64; // mage nums ftw
var wpt_jump_key = -1;
while(current_wpt_key < waypoints.get_waypoint_count()) {
current_wpt = waypoints.get_waypoint(current_wpt_key);
if(current_wpt.command == mavl.get_mav_const('MAV_CMD', 'MAV_CMD_DO_JUMP')) {
if(max_loops-- == 0) {
return null;
}
if(current_wpt.param1 >= waypoints.get_waypoint_count() || current_wpt.param1 == 0) {
return null;
}
if(wpt_jump_key == current_wpt_key) {
return null;
}
if(wpt_jump_key == -1) {
wpt_jump_key = current_wpt_key;
}
current_wpt_key = current_wpt.param1;
console.log('WAYPOINTS NEXT_CMD taking jump to waypoint #' + waypoints.get_waypoint(current_wpt_key).seq);
} else {
return current_wpt_key;
}
}
return null;
},
/**
* Sets a received collection of MISSION_ITEM
* as the current waypoints.
*/
set_waypoints: function(waypts) {
waypoints.waypoints = waypts;
waypoints._waypoint_count = Object.keys(waypts).length;
},
is_changed: function(arg) {
if(arg !== null && arg !== undefined) {
var previous_state = waypoints._is_changed;
waypoints._is_changed = arg;
return previous_state;
}
return waypoints._is_changed;
}
};
module.exports = waypoints;