Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
mission_fw_nav.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Gautier Hattenberger
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 
30 #include <stdio.h>
32 #include "autopilot.h"
35 #include "generated/flight_plan.h"
36 
38 bool mission_point_of_lla(struct EnuCoor_f *point, struct LlaCoor_i *lla)
39 {
41  struct LlaCoor_f lla_f;
42  LLA_FLOAT_OF_BFP(lla_f, *lla);
43 
44  /* Computes from (lat, long) in the referenced UTM zone */
45  struct UtmCoor_f utm;
46  utm.zone = nav_utm_zone0;
47  utm_of_lla_f(&utm, &lla_f);
48 
49  /* Computes relative position to HOME waypoint
50  * and bound the distance to max_dist_from_home
51  */
52  float dx, dy;
53  dx = utm.east - nav_utm_east0 - waypoints[WP_HOME].x;
54  dy = utm.north - nav_utm_north0 - waypoints[WP_HOME].y;
55  BoundAbs(dx, max_dist_from_home);
56  BoundAbs(dy, max_dist_from_home);
57 
58  /* Update point */
59  point->x = waypoints[WP_HOME].x + dx;
60  point->y = waypoints[WP_HOME].y + dy;
61  point->z = lla_f.alt;
62 
63  return true;
64 }
65 
66 // navigation time step
67 static const float dt_navigation = 1.0 / ((float)NAVIGATION_FREQUENCY);
68 
69 // dirty hack to comply with nav_approaching_xy function
70 struct EnuCoor_f last_wp_f = { 0., 0., 0. };
71 
74 static inline bool mission_nav_wp(struct _mission_wp *wp)
75 {
76  if (nav_approaching_xy(wp->wp.wp_f.x, wp->wp.wp_f.y, last_wp_f.x, last_wp_f.y, CARROT)) {
77  last_wp_f = wp->wp.wp_f; // store last wp
78  return false; // end of mission element
79  }
80  // set navigation command
81  fly_to_xy(wp->wp.wp_f.x, wp->wp.wp_f.y);
83  NavVerticalAltitudeMode(wp->wp.wp_f.z, 0.);
84  return true;
85 }
86 
89 static inline bool mission_nav_circle(struct _mission_circle *circle)
90 {
91  nav_circle_XY(circle->center.center_f.x, circle->center.center_f.y, circle->radius);
93  NavVerticalAltitudeMode(circle->center.center_f.z, 0.);
94  return true;
95 }
96 
99 static inline bool mission_nav_segment(struct _mission_segment *segment)
100 {
101  if (nav_approaching_xy(segment->to.to_f.x, segment->to.to_f.y, segment->from.from_f.x, segment->from.from_f.y,
102  CARROT)) {
103  last_wp_f = segment->to.to_f;
104  return false; // end of mission element
105  }
106  nav_route_xy(segment->from.from_f.x, segment->from.from_f.y, segment->to.to_f.x, segment->to.to_f.y);
108  NavVerticalAltitudeMode(segment->to.to_f.z, 0.); // both altitude should be the same anyway
109  return true;
110 }
111 
114 static inline bool mission_nav_path(struct _mission_path *path)
115 {
116  if (path->nb == 0) {
117  return false; // nothing to do
118  }
119  if (path->nb == 1) {
120  // handle as a single waypoint
121  struct _mission_wp wp;
122  wp.wp.wp_f = path->path.path_f[0];
123  return mission_nav_wp(&wp);
124  }
125  if (path->path_idx == path->nb - 1) {
126  last_wp_f = path->path.path_f[path->path_idx]; // store last wp
127  return false; // end of path
128  }
129  // normal case
130  struct EnuCoor_f from_f = path->path.path_f[path->path_idx];
131  struct EnuCoor_f to_f = path->path.path_f[path->path_idx + 1];
132  nav_route_xy(from_f.x, from_f.y, to_f.x, to_f.y);
134  NavVerticalAltitudeMode(to_f.z, 0.); // both altitude should be the same anyway
135  if (nav_approaching_xy(to_f.x, to_f.y, from_f.x, from_f.y, CARROT)) {
136  path->path_idx++; // go to next segment
137  }
138  return true;
139 }
140 
143 static inline bool mission_nav_custom(struct _mission_custom *custom, bool init)
144 {
145  return custom->reg->cb(custom->nb, custom->params, init);
146 }
147 
149 {
150  // current element
151  struct _mission_element *el = NULL;
152  if ((el = mission_get()) == NULL) {
153  // TODO do something special like a waiting circle before ending the mission ?
154  return false; // end of mission
155  }
156 
157  bool el_running = false;
158  switch (el->type) {
159  case MissionWP:
160  el_running = mission_nav_wp(&(el->element.mission_wp));
161  break;
162  case MissionCircle:
163  el_running = mission_nav_circle(&(el->element.mission_circle));
164  break;
165  case MissionSegment:
166  el_running = mission_nav_segment(&(el->element.mission_segment));
167  break;
168  case MissionPath:
169  el_running = mission_nav_path(&(el->element.mission_path));
170  break;
171  case MissionCustom:
172  el_running = mission_nav_custom(&(el->element.mission_custom), mission.element_time < dt_navigation);
173  break;
174  default:
175  // invalid type or pattern not yet handled
176  break;
177  }
178 
179  // increment element_time
181 
182  // test if element is finished or element time is elapsed
183  if (!el_running || (el->duration > 0. && mission.element_time >= el->duration)) {
184  // reset timer
185  mission.element_time = 0.;
186  // go to next element
188  }
189 
190  return true;
191 }
float x
Definition: common_nav.h:40
float east
in meters
union _mission_wp::@294 wp
float north
in meters
struct _mission_element * mission_get(void)
Get current mission element.
vector in East North Up coordinates Units: meters
static bool mission_nav_custom(struct _mission_custom *custom, bool init)
Call custom navigation function.
uint8_t nav_utm_zone0
Definition: common_nav.c:44
position in UTM coordinates Units: meters
float duration
time to spend in the element (<= 0 to disable)
vector in Latitude, Longitude and Altitude
const float max_dist_from_home
Definition: navigation.c:79
bool mission_point_of_lla(struct EnuCoor_f *point, struct LlaCoor_i *lla)
Utility function: converts lla (int) to local point (float)
struct _mission_registered * reg
pointer to a registered custom mission element
float element_time
time in second spend in the current element
vector in Latitude, Longitude and Altitude
float y
Definition: common_nav.h:41
#define MISSION_ELEMENT_NB
Max number of elements in the tasks' list can be redefined.
int32_t nav_utm_north0
Definition: common_nav.c:43
float x
in meters
mission planner library
static const float dt_navigation
uint8_t path_idx
static bool mission_nav_segment(struct _mission_segment *segment)
Navigation function along a segment.
#define NAVIGATION_FREQUENCY
mission_custom_cb cb
navigation/action function callback
uint8_t zone
UTM zone number.
float alt
in meters (normally above WGS84 reference ellipsoid)
Core autopilot interface common to all firmwares.
int mission_run()
Run mission.
uint8_t nb
number of parameters
union _mission_segment::@297 to
int32_t nav_utm_east0
Definition: common_nav.c:42
struct point waypoints[NB_WAYPOINT]
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:38
float z
in meters
uint8_t current_idx
current mission element index
static bool mission_nav_circle(struct _mission_circle *circle)
Navigation function on a circle.
struct _mission mission
static bool mission_nav_wp(struct _mission_wp *wp)
Navigation function to a single waypoint.
float params[MISSION_CUSTOM_MAX]
list of parameters
struct EnuCoor_f last_wp_f
static bool mission_nav_path(struct _mission_path *path)
Navigation function along a path.
float y
in meters
union _mission_segment::@296 from
#define LLA_FLOAT_OF_BFP(_o, _i)
void utm_of_lla_f(struct UtmCoor_f *utm, struct LlaCoor_f *lla)
union _mission_path::@298 path
#define CARROT
default approaching_time for a wp
Definition: navigation.h:40
union _mission_circle::@295 center
enum MissionType type
union _mission_element::@299 element