Paparazzi UAS  v5.15_devel-113-g1b57ff1
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
mission_common.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Paparazzi Team
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
28 
29 #include <string.h>
30 #include "generated/flight_plan.h"
31 #include "generated/airframe.h"
34 
35 struct _mission mission = { 0 };
36 
37 void mission_init(void)
38 {
39  mission.insert_idx = 0;
40  mission.current_idx = 0;
41  mission.element_time = 0.;
42 
43  // FIXME
44  // we have no guarantee that nav modules init are called after mission_init
45  // this would erase the already registered elements
46  // for now, rely on the static initialization
47  //for (int i = 0; i < MISSION_REGISTER_NB; i++) {
48  // mission.registered[i].cb = NULL;
49  // memset(mission.registered[i].type, '\0', MISSION_TYPE_SIZE);
50  //}
51 }
52 
53 
54 // Insert element
55 bool mission_insert(enum MissionInsertMode insert, struct _mission_element *element)
56 {
57  uint8_t tmp;
58  // convert element if needed, return FALSE if failed
59  if (!mission_element_convert(element)) { return false; }
60 
61  switch (insert) {
62  case Append:
63  tmp = (mission.insert_idx + 1) % MISSION_ELEMENT_NB;
64  if (tmp == mission.current_idx) { return false; } // no room to insert element
65  mission.elements[mission.insert_idx] = *element; // add element
66  mission.insert_idx = tmp; // move insert index
67  break;
68  case Prepend:
69  if (mission.current_idx == 0) { tmp = MISSION_ELEMENT_NB - 1; }
70  else { tmp = mission.current_idx - 1; }
71  if (tmp == mission.insert_idx) { return false; } // no room to inser element
72  mission.elements[tmp] = *element; // add element
73  mission.current_idx = tmp; // move current index
74  break;
75  case ReplaceCurrent:
76  // current element can always be modified, index are not changed
77  mission.elements[mission.current_idx] = *element;
78  break;
79  case ReplaceAll:
80  // reset queue and index
81  mission.elements[0] = *element;
82  mission.current_idx = 0;
83  mission.insert_idx = 1;
84  break;
85  case ReplaceNexts:
86  tmp = (mission.current_idx + 1) % MISSION_ELEMENT_NB;
87  mission.elements[tmp] = *element;
88  mission.insert_idx = (mission.current_idx + 2) % MISSION_ELEMENT_NB;
89  break;
90  default:
91  // unknown insertion mode
92  return false;
93  }
94  return true;
95 
96 }
97 
98 // Register new callback
99 bool mission_register(mission_custom_cb cb, char *type)
100 {
101  for (int i = 0; i < MISSION_REGISTER_NB; i++) {
102  if (str_equal(mission.registered[i].type, type)) {
103  return false; // identifier already registered
104  }
105  if (mission.registered[i].cb == NULL) {
106  strncpy(mission.registered[i].type, type, MISSION_TYPE_SIZE-1);
107  mission.registered[i].cb = cb;
108  return true;
109  }
110  }
111  return false; // no more room to register callbacks
112 }
113 
114 // Returns a pointer to a register struct with matching types, NULL if not found
116 {
117  for (int i = 0; i < MISSION_REGISTER_NB; i++) {
118  if (str_equal(mission.registered[i].type, type)) {
119  return &(mission.registered[i]);
120  }
121  }
122  return NULL; // not found
123 }
124 
125 // Weak implementation of mission_element_convert (leave element unchanged)
126 bool __attribute__((weak)) mission_element_convert(struct _mission_element *el __attribute__((unused))) { return true; }
127 
128 
129 // Get element
131 {
132  if (mission.current_idx == mission.insert_idx) {
133  return NULL;
134  }
135  return &(mission.elements[mission.current_idx]);
136 }
137 
138 
139 // Report function
141 {
142  // build index list
143  uint8_t index_list[MISSION_ELEMENT_NB];
144  uint8_t i = mission.current_idx, j = 0;
145  while (i != mission.insert_idx) {
146  index_list[j++] = mission.elements[i].index;
147  i = (i + 1) % MISSION_ELEMENT_NB;
148  }
149  if (j == 0) { index_list[j++] = 0; } // Dummy value if index list is empty
150  //compute remaining time (or -1. if no time limit)
151  float remaining_time = -1.;
152  if (mission.elements[mission.current_idx].duration > 0.) {
153  remaining_time = mission.elements[mission.current_idx].duration - mission.element_time;
154  }
155 
156  // send status
157  DOWNLINK_SEND_MISSION_STATUS(DefaultChannel, DefaultDevice, &remaining_time, j, index_list);
158 }
159 
160 
162 // Parsing functions //
164 
166 {
167  if (DL_MISSION_GOTO_WP_ac_id(dl_buffer) != AC_ID) { return false; } // not for this aircraft
168 
169  struct _mission_element me;
170  me.type = MissionWP;
171  me.element.mission_wp.wp.wp_f.x = DL_MISSION_GOTO_WP_wp_east(dl_buffer);
172  me.element.mission_wp.wp.wp_f.y = DL_MISSION_GOTO_WP_wp_north(dl_buffer);
173  me.element.mission_wp.wp.wp_f.z = DL_MISSION_GOTO_WP_wp_alt(dl_buffer);
174  me.duration = DL_MISSION_GOTO_WP_duration(dl_buffer);
175  me.index = DL_MISSION_GOTO_WP_index(dl_buffer);
176 
177  enum MissionInsertMode insert = (enum MissionInsertMode)(DL_MISSION_GOTO_WP_insert(dl_buffer));
178 
179  return mission_insert(insert, &me);
180 }
181 
183 {
184  if (DL_MISSION_GOTO_WP_LLA_ac_id(dl_buffer) != AC_ID) { return false; } // not for this aircraft
185 
186  struct LlaCoor_i lla;
187  lla.lat = DL_MISSION_GOTO_WP_LLA_wp_lat(dl_buffer);
188  lla.lon = DL_MISSION_GOTO_WP_LLA_wp_lon(dl_buffer);
189  lla.alt = DL_MISSION_GOTO_WP_LLA_wp_alt(dl_buffer);
190 
191  struct _mission_element me;
192  me.type = MissionWP;
193  // if there is no valid local coordinate, do not insert mission element
194  if (!mission_point_of_lla(&me.element.mission_wp.wp.wp_f, &lla)) { return false; }
195  me.duration = DL_MISSION_GOTO_WP_LLA_duration(dl_buffer);
196  me.index = DL_MISSION_GOTO_WP_LLA_index(dl_buffer);
197 
198  enum MissionInsertMode insert = (enum MissionInsertMode)(DL_MISSION_GOTO_WP_LLA_insert(dl_buffer));
199 
200  return mission_insert(insert, &me);
201 }
202 
204 {
205  if (DL_MISSION_CIRCLE_ac_id(dl_buffer) != AC_ID) { return false; } // not for this aircraft
206 
207  struct _mission_element me;
208  me.type = MissionCircle;
209  me.element.mission_circle.center.center_f.x = DL_MISSION_CIRCLE_center_east(dl_buffer);
210  me.element.mission_circle.center.center_f.y = DL_MISSION_CIRCLE_center_north(dl_buffer);
211  me.element.mission_circle.center.center_f.z = DL_MISSION_CIRCLE_center_alt(dl_buffer);
212  me.element.mission_circle.radius = DL_MISSION_CIRCLE_radius(dl_buffer);
213  me.duration = DL_MISSION_CIRCLE_duration(dl_buffer);
214  me.index = DL_MISSION_CIRCLE_index(dl_buffer);
215 
216  enum MissionInsertMode insert = (enum MissionInsertMode)(DL_MISSION_CIRCLE_insert(dl_buffer));
217 
218  return mission_insert(insert, &me);
219 }
220 
222 {
223  if (DL_MISSION_CIRCLE_LLA_ac_id(dl_buffer) != AC_ID) { return false; } // not for this aircraft
224 
225  struct LlaCoor_i lla;
226  lla.lat = DL_MISSION_CIRCLE_LLA_center_lat(dl_buffer);
227  lla.lon = DL_MISSION_CIRCLE_LLA_center_lon(dl_buffer);
228  lla.alt = DL_MISSION_CIRCLE_LLA_center_alt(dl_buffer);
229 
230  struct _mission_element me;
231  me.type = MissionCircle;
232  // if there is no valid local coordinate, do not insert mission element
233  if (!mission_point_of_lla(&me.element.mission_circle.center.center_f, &lla)) { return false; }
234  me.element.mission_circle.radius = DL_MISSION_CIRCLE_LLA_radius(dl_buffer);
235  me.duration = DL_MISSION_CIRCLE_LLA_duration(dl_buffer);
236  me.index = DL_MISSION_CIRCLE_LLA_index(dl_buffer);
237 
238  enum MissionInsertMode insert = (enum MissionInsertMode)(DL_MISSION_CIRCLE_LLA_insert(dl_buffer));
239 
240  return mission_insert(insert, &me);
241 }
242 
244 {
245  if (DL_MISSION_SEGMENT_ac_id(dl_buffer) != AC_ID) { return false; } // not for this aircraft
246 
247  struct _mission_element me;
248  me.type = MissionSegment;
249  me.element.mission_segment.from.from_f.x = DL_MISSION_SEGMENT_segment_east_1(dl_buffer);
250  me.element.mission_segment.from.from_f.y = DL_MISSION_SEGMENT_segment_north_1(dl_buffer);
251  me.element.mission_segment.from.from_f.z = DL_MISSION_SEGMENT_segment_alt(dl_buffer);
252  me.element.mission_segment.to.to_f.x = DL_MISSION_SEGMENT_segment_east_2(dl_buffer);
253  me.element.mission_segment.to.to_f.y = DL_MISSION_SEGMENT_segment_north_2(dl_buffer);
254  me.element.mission_segment.to.to_f.z = DL_MISSION_SEGMENT_segment_alt(dl_buffer);
255  me.duration = DL_MISSION_SEGMENT_duration(dl_buffer);
256  me.index = DL_MISSION_SEGMENT_index(dl_buffer);
257 
258  enum MissionInsertMode insert = (enum MissionInsertMode)(DL_MISSION_SEGMENT_insert(dl_buffer));
259 
260  return mission_insert(insert, &me);
261 }
262 
264 {
265  if (DL_MISSION_SEGMENT_LLA_ac_id(dl_buffer) != AC_ID) { return false; } // not for this aircraft
266 
267  struct LlaCoor_i from_lla, to_lla;
268  from_lla.lat = DL_MISSION_SEGMENT_LLA_segment_lat_1(dl_buffer);
269  from_lla.lon = DL_MISSION_SEGMENT_LLA_segment_lon_1(dl_buffer);
270  from_lla.alt = DL_MISSION_SEGMENT_LLA_segment_alt(dl_buffer);
271  to_lla.lat = DL_MISSION_SEGMENT_LLA_segment_lat_2(dl_buffer);
272  to_lla.lon = DL_MISSION_SEGMENT_LLA_segment_lon_2(dl_buffer);
273  to_lla.alt = DL_MISSION_SEGMENT_LLA_segment_alt(dl_buffer);
274 
275  struct _mission_element me;
276  me.type = MissionSegment;
277  // if there is no valid local coordinate, do not insert mission element
278  if (!mission_point_of_lla(&me.element.mission_segment.from.from_f, &from_lla)) { return false; }
279  if (!mission_point_of_lla(&me.element.mission_segment.to.to_f, &to_lla)) { return false; }
280  me.duration = DL_MISSION_SEGMENT_LLA_duration(dl_buffer);
281  me.index = DL_MISSION_SEGMENT_LLA_index(dl_buffer);
282 
283  enum MissionInsertMode insert = (enum MissionInsertMode)(DL_MISSION_SEGMENT_LLA_insert(dl_buffer));
284 
285  return mission_insert(insert, &me);
286 }
287 
289 {
290  if (DL_MISSION_PATH_ac_id(dl_buffer) != AC_ID) { return false; } // not for this aircraft
291 
292  struct _mission_element me;
293  me.type = MissionPath;
294  me.element.mission_path.path.path_f[0].x = DL_MISSION_PATH_point_east_1(dl_buffer);
295  me.element.mission_path.path.path_f[0].y = DL_MISSION_PATH_point_north_1(dl_buffer);
296  me.element.mission_path.path.path_f[0].z = DL_MISSION_PATH_path_alt(dl_buffer);
297  me.element.mission_path.path.path_f[1].x = DL_MISSION_PATH_point_east_2(dl_buffer);
298  me.element.mission_path.path.path_f[1].y = DL_MISSION_PATH_point_north_2(dl_buffer);
299  me.element.mission_path.path.path_f[1].z = DL_MISSION_PATH_path_alt(dl_buffer);
300  me.element.mission_path.path.path_f[2].x = DL_MISSION_PATH_point_east_3(dl_buffer);
301  me.element.mission_path.path.path_f[2].y = DL_MISSION_PATH_point_north_3(dl_buffer);
302  me.element.mission_path.path.path_f[2].z = DL_MISSION_PATH_path_alt(dl_buffer);
303  me.element.mission_path.path.path_f[3].x = DL_MISSION_PATH_point_east_4(dl_buffer);
304  me.element.mission_path.path.path_f[3].y = DL_MISSION_PATH_point_north_4(dl_buffer);
305  me.element.mission_path.path.path_f[3].z = DL_MISSION_PATH_path_alt(dl_buffer);
306  me.element.mission_path.path.path_f[4].x = DL_MISSION_PATH_point_east_5(dl_buffer);
307  me.element.mission_path.path.path_f[4].y = DL_MISSION_PATH_point_north_5(dl_buffer);
308  me.element.mission_path.path.path_f[4].z = DL_MISSION_PATH_path_alt(dl_buffer);
309  me.element.mission_path.nb = DL_MISSION_PATH_nb(dl_buffer);
310  if (me.element.mission_path.nb > MISSION_PATH_NB) { me.element.mission_path.nb = MISSION_PATH_NB; }
311  me.element.mission_path.path_idx = 0;
312  me.duration = DL_MISSION_PATH_duration(dl_buffer);
313  me.index = DL_MISSION_PATH_index(dl_buffer);
314 
315  enum MissionInsertMode insert = (enum MissionInsertMode)(DL_MISSION_PATH_insert(dl_buffer));
316 
317  return mission_insert(insert, &me);
318 }
319 
321 {
322  if (DL_MISSION_PATH_LLA_ac_id(dl_buffer) != AC_ID) { return false; } // not for this aircraft
323 
324  struct LlaCoor_i lla[MISSION_PATH_NB];
325  lla[0].lat = DL_MISSION_PATH_LLA_point_lat_1(dl_buffer);
326  lla[0].lon = DL_MISSION_PATH_LLA_point_lon_1(dl_buffer);
327  lla[0].alt = DL_MISSION_PATH_LLA_path_alt(dl_buffer);
328  lla[1].lat = DL_MISSION_PATH_LLA_point_lat_2(dl_buffer);
329  lla[1].lon = DL_MISSION_PATH_LLA_point_lon_2(dl_buffer);
330  lla[1].alt = DL_MISSION_PATH_LLA_path_alt(dl_buffer);
331  lla[2].lat = DL_MISSION_PATH_LLA_point_lat_3(dl_buffer);
332  lla[2].lon = DL_MISSION_PATH_LLA_point_lon_3(dl_buffer);
333  lla[2].alt = DL_MISSION_PATH_LLA_path_alt(dl_buffer);
334  lla[3].lat = DL_MISSION_PATH_LLA_point_lat_4(dl_buffer);
335  lla[3].lon = DL_MISSION_PATH_LLA_point_lon_4(dl_buffer);
336  lla[3].alt = DL_MISSION_PATH_LLA_path_alt(dl_buffer);
337  lla[4].lat = DL_MISSION_PATH_LLA_point_lat_5(dl_buffer);
338  lla[4].lon = DL_MISSION_PATH_LLA_point_lon_5(dl_buffer);
339  lla[4].alt = DL_MISSION_PATH_LLA_path_alt(dl_buffer);
340 
341  struct _mission_element me;
342  me.type = MissionPath;
343  uint8_t i;
344  me.element.mission_path.nb = DL_MISSION_PATH_LLA_nb(dl_buffer);
345  if (me.element.mission_path.nb > MISSION_PATH_NB) { me.element.mission_path.nb = MISSION_PATH_NB; }
346  for (i = 0; i < me.element.mission_path.nb; i++) {
347  // if there is no valid local coordinate, do not insert mission element
348  if (!mission_point_of_lla(&me.element.mission_path.path.path_f[i], &lla[i])) { return false; }
349  }
350  me.element.mission_path.path_idx = 0;
351  me.duration = DL_MISSION_PATH_LLA_duration(dl_buffer);
352  me.index = DL_MISSION_PATH_LLA_index(dl_buffer);
353 
354  enum MissionInsertMode insert = (enum MissionInsertMode)(DL_MISSION_PATH_LLA_insert(dl_buffer));
355 
356  return mission_insert(insert, &me);
357 }
358 
360 {
361  if (DL_MISSION_CUSTOM_ac_id(dl_buffer) != AC_ID) { return false; } // not for this aircraft
362 
363  struct _mission_element me;
364  me.type = MissionCustom;
365  me.element.mission_custom.reg = mission_get_registered(DL_MISSION_CUSTOM_type(dl_buffer));
366  if (me.element.mission_custom.reg == NULL) {
367  return false; // unknown type
368  }
369  me.element.mission_custom.nb = DL_MISSION_CUSTOM_params_length(dl_buffer);
370  for (int i = 0; i < me.element.mission_custom.nb; i++) {
371  me.element.mission_custom.params[i] = DL_MISSION_CUSTOM_params(dl_buffer)[i];
372  }
373  me.duration = DL_MISSION_CUSTOM_duration(dl_buffer);
374  me.index = DL_MISSION_CUSTOM_index(dl_buffer);
375 
376  enum MissionInsertMode insert = (enum MissionInsertMode)(DL_MISSION_CUSTOM_insert(dl_buffer));
377 
378  return mission_insert(insert, &me);
379 }
380 
382 {
383  if (DL_GOTO_MISSION_ac_id(dl_buffer) != AC_ID) { return false; } // not for this aircraft
384 
385  uint8_t mission_id = DL_GOTO_MISSION_mission_id(dl_buffer);
386  if (mission_id < MISSION_ELEMENT_NB) {
387  mission.current_idx = mission_id;
388  } else { return false; }
389 
390  return true;
391 }
392 
394 {
395  if (DL_NEXT_MISSION_ac_id(dl_buffer) != AC_ID) { return false; } // not for this aircraft
396 
397  if (mission.current_idx == mission.insert_idx) { return false; } // already at the last position
398 
399  // increment current index
400  mission.current_idx = (mission.current_idx + 1) % MISSION_ELEMENT_NB;
401  return true;
402 }
403 
405 {
406  if (DL_END_MISSION_ac_id(dl_buffer) != AC_ID) { return false; } // not for this aircraft
407 
408  // set current index to insert index (last position)
409  mission.current_idx = mission.insert_idx;
410  return true;
411 }
int mission_parse_NEXT_MISSION(void)
replace the next element and remove all the others
int mission_parse_CUSTOM(void)
int mission_parse_SEGMENT(void)
char type[MISSION_TYPE_SIZE]
mission element identifier (5 char max + 1 \0)
bool(* mission_custom_cb)(uint8_t nb, float *params, bool init)
custom mission element callback
struct _mission_element * mission_get(void)
Get current mission element.
struct _mission_registered registered[MISSION_REGISTER_NB]
void mission_init(void)
Init mission structure.
bool mission_insert(enum MissionInsertMode insert, struct _mission_element *element)
Insert a mission element according to the insertion mode.
bool mission_element_convert(struct _mission_element *el)
Convert mission element's points format if needed.
bool mission_register(mission_custom_cb cb, char *type)
Register a new navigation or action callback function.
void mission_status_report(void)
Report mission status.
MissionInsertMode
remove all elements and add the new one
union _mission_element::@295 element
float duration
time to spend in the element (<= 0 to disable)
add before the current element
vector in Latitude, Longitude and Altitude
#define MISSION_REGISTER_NB
Max number of registered nav/action callbacks can be redefined.
replace current element
int mission_parse_GOTO_WP(void)
Parsing functions called when a mission message is received.
int mission_parse_CIRCLE(void)
int32_t alt
in millimeters above WGS84 reference ellipsoid
float element_time
time in second spend in the current element
int mission_parse_GOTO_WP_LLA(void)
#define MISSION_ELEMENT_NB
Max number of elements in the tasks' list can be redefined.
mission planner library
add at the last position
mission_custom_cb cb
navigation/action function callback
int mission_parse_PATH(void)
int32_t lon
in degrees*1e7
uint8_t insert_idx
inserstion index
int mission_parse_SEGMENT_LLA(void)
#define MISSION_TYPE_SIZE
unsigned char uint8_t
Definition: types.h:14
int mission_parse_GOTO_MISSION(void)
uint8_t index
index of mission element
#define MISSION_PATH_NB
uint8_t current_idx
current mission element index
struct _mission mission
uint8_t dl_buffer[MSG_SIZE]
Definition: main_demo5.c:64
struct _mission_element elements[MISSION_ELEMENT_NB]
bool mission_point_of_lla(struct EnuCoor_f *point, struct LlaCoor_i *lla)
Get the ENU component of LLA mission point This function is firmware specific.
int32_t lat
in degrees*1e7
static struct _mission_registered * mission_get_registered(char *type)
int mission_parse_PATH_LLA(void)
int mission_parse_CIRCLE_LLA(void)
int mission_parse_END_MISSION(void)
enum MissionType type