Paparazzi UAS  v5.8.2_stable-0-g6260b7c
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>
35 #include "generated/flight_plan.h"
36 
38 bool_t 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->alt;
62 
63  return TRUE;
64 }
65 
66 // navigation time step
67 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_t 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_t 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_t 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_t 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 
141 
143 {
144  // current element
145  struct _mission_element *el = NULL;
146  if ((el = mission_get()) == NULL) {
147  // TODO do something special like a waiting circle before ending the mission ?
148  return FALSE; // end of mission
149  }
150 
151  bool_t el_running = FALSE;
152  switch (el->type) {
153  case MissionWP:
154  el_running = mission_nav_wp(&(el->element.mission_wp));
155  break;
156  case MissionCircle:
157  el_running = mission_nav_circle(&(el->element.mission_circle));
158  break;
159  case MissionSegment:
160  el_running = mission_nav_segment(&(el->element.mission_segment));
161  break;
162  case MissionPath:
163  el_running = mission_nav_path(&(el->element.mission_path));
164  break;
165  default:
166  // invalid type or pattern not yet handled
167  break;
168  }
169 
170  // increment element_time
172 
173  // test if element is finished or element time is elapsed
174  if (!el_running || (el->duration > 0. && mission.element_time >= el->duration)) {
175  // reset timer
176  mission.element_time = 0.;
177  // go to next element
179  }
180 
181  return TRUE;
182 }
float x
Definition: common_nav.h:40
float east
in meters
float north
in meters
static bool_t mission_nav_segment(struct _mission_segment *segment)
Navigation function along a segment.
union _mission_segment::@19 to
struct _mission_element * mission_get(void)
Get current mission element.
vector in East North Up coordinates Units: meters
union _mission_circle::@17 center
uint8_t nav_utm_zone0
Definition: common_nav.c:44
static bool_t mission_nav_circle(struct _mission_circle *circle)
Navigation function on a circle.
position in UTM coordinates Units: meters
float duration
time to spend in the element (<= 0 to disable)
vector in Latitude, Longitude and Altitude
#define FALSE
Definition: std.h:5
int32_t alt
in millimeters above WGS84 reference ellipsoid
bool_t mission_point_of_lla(struct EnuCoor_f *point, struct LlaCoor_i *lla)
Utility function: converts lla (int) to local point (float)
union _mission_element::@21 element
float element_time
time in second spend in the current element
vector in Latitude, Longitude and Altitude
#define TRUE
Definition: std.h:4
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
const float dt_navigation
uint8_t path_idx
static bool_t mission_nav_wp(struct _mission_wp *wp)
Navigation function to a single waypoint.
union _mission_path::@20 path
uint8_t zone
UTM zone number.
static bool_t mission_nav_path(struct _mission_path *path)
Navigation function along a path.
union _mission_segment::@18 from
int mission_run()
Run mission.
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
struct _mission mission
#define NAVIGATION_FREQUENCY
Definition: autopilot.h:145
union _mission_wp::@16 wp
struct EnuCoor_f last_wp_f
float y
in meters
#define LLA_FLOAT_OF_BFP(_o, _i)
void utm_of_lla_f(struct UtmCoor_f *utm, struct LlaCoor_f *lla)
enum MissionType type
Fixedwing autopilot modes.