Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures 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 
37 // Utility function: converts lla to local point
38 bool_t mission_point_of_lla(struct EnuCoor_f *point, struct LlaCoor_f *lla) {
39  /* Computes from (lat, long) in the referenced UTM zone */
40  struct UtmCoor_f utm;
41  utm.zone = nav_utm_zone0;
42  utm_of_lla_f(&utm, lla);
43 
44  /* Computes relative position to HOME waypoint
45  * and bound the distance to max_dist_from_home
46  */
47  float dx, dy;
48  dx = utm.east - nav_utm_east0 - waypoints[WP_HOME].x;
49  dy = utm.north - nav_utm_north0 - waypoints[WP_HOME].y;
50  BoundAbs(dx, max_dist_from_home);
51  BoundAbs(dy, max_dist_from_home);
52 
53  /* Update point */
54  point->x = waypoints[WP_HOME].x + dx;
55  point->y = waypoints[WP_HOME].y + dy;
56  point->z = lla->alt;
57 
58  return TRUE;
59 }
60 
61 // navigation time step
62 const float dt_navigation = 1.0 / ((float)NAVIGATION_FREQUENCY);
63 
64 // dirty hack to comply with nav_approaching_xy function
65 struct EnuCoor_f last_wp_f = { 0., 0., 0. };
66 
69 static inline bool_t mission_nav_wp(struct _mission_wp * wp) {
70  if (nav_approaching_xy(wp->wp.wp_f.x, wp->wp.wp_f.y, last_wp_f.x, last_wp_f.y, CARROT)) {
71  last_wp_f = wp->wp.wp_f; // store last wp
72  return FALSE; // end of mission element
73  }
74  // set navigation command
75  fly_to_xy(wp->wp.wp_f.x, wp->wp.wp_f.y);
78  return TRUE;
79 }
80 
83 static inline bool_t mission_nav_circle(struct _mission_circle * circle) {
84  nav_circle_XY(circle->center.center_f.x, circle->center.center_f.y, circle->radius);
87  return TRUE;
88 }
89 
92 static inline bool_t mission_nav_segment(struct _mission_segment * segment) {
93  if (nav_approaching_xy(segment->to.to_f.x, segment->to.to_f.y, segment->from.from_f.x, segment->from.from_f.y, CARROT)) {
94  last_wp_f = segment->to.to_f;
95  return FALSE; // end of mission element
96  }
97  nav_route_xy(segment->from.from_f.x, segment->from.from_f.y, segment->to.to_f.x, segment->to.to_f.y);
99  NavVerticalAltitudeMode(segment->to.to_f.z, 0.); // both altitude should be the same anyway
100  return TRUE;
101 }
102 
105 static inline bool_t mission_nav_path(struct _mission_path * path) {
106  if (path->nb == 0) {
107  return FALSE; // nothing to do
108  }
109  if (path->nb == 1) {
110  // handle as a single waypoint
111  struct _mission_wp wp;
112  wp.wp.wp_f = path->path.path_f[0];
113  return mission_nav_wp(&wp);
114  }
115  if (path->path_idx == path->nb-1) {
116  last_wp_f = path->path.path_f[path->path_idx]; // store last wp
117  return FALSE; // end of path
118  }
119  // normal case
120  struct EnuCoor_f from_f = path->path.path_f[path->path_idx];
121  struct EnuCoor_f to_f = path->path.path_f[path->path_idx+1];
122  nav_route_xy(from_f.x, from_f.y, to_f.x, to_f.y);
124  NavVerticalAltitudeMode(to_f.z, 0.); // both altitude should be the same anyway
125  if (nav_approaching_xy(to_f.x, to_f.y, from_f.x, from_f.y, CARROT)) {
126  path->path_idx++; // go to next segment
127  }
128  return TRUE;
129 }
130 
131 
132 int mission_run() {
133  // current element
134  struct _mission_element * el = NULL;
135  if ((el = mission_get()) == NULL) {
136  // TODO do something special like a waiting circle before ending the mission ?
137  return FALSE; // end of mission
138  }
139 
140  bool_t el_running = FALSE;
141  switch (el->type){
142  case MissionWP:
143  el_running = mission_nav_wp(&(el->element.mission_wp));
144  break;
145  case MissionCircle:
146  el_running = mission_nav_circle(&(el->element.mission_circle));
147  break;
148  case MissionSegment:
149  el_running = mission_nav_segment(&(el->element.mission_segment));
150  break;
151  case MissionPath:
152  el_running = mission_nav_path(&(el->element.mission_path));
153  break;
154  default:
155  // invalid type or pattern not yet handled
156  break;
157  }
158 
159  // increment element_time
161 
162  // test if element is finished or element time is elapsed
163  if (!el_running || (el->duration > 0. && mission.element_time >= el->duration)) {
164  // reset timer
165  mission.element_time = 0.;
166  // go to next element
168  }
169 
170  return TRUE;
171 }
struct EnuCoor_f path_f[MISSION_PATH_NB]
enum MissionType type
static bool_t mission_nav_segment(struct _mission_segment *segment)
Navigation function along a segment.
union _mission_path::@17 path
struct EnuCoor_f from_f
struct _mission_element * mission_get(void)
Get current mission element.
union _mission_wp::@13 wp
float z
in meters
uint8_t nav_utm_zone0
Definition: common_nav.c:44
uint8_t current_idx
current mission element index
static bool_t mission_nav_circle(struct _mission_circle *circle)
Navigation function on a circle.
struct _mission_wp mission_wp
union _mission_circle::@14 center
float y
in meters
vector in Latitude, Longitude and Altitude
struct _mission_segment mission_segment
#define FALSE
Definition: imu_chimu.h:141
uint8_t zone
UTM zone number.
#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
mission planner library
union _mission_segment::@15 from
const float dt_navigation
float north
in meters
position in UTM coordinates Units: meters
float x
in meters
struct _mission_path mission_path
struct _mission_circle mission_circle
static bool_t mission_nav_wp(struct _mission_wp *wp)
Navigation function to a single waypoint.
float element_time
time in second spend in the current element
float alt
in meters above WGS84 reference ellipsoid
static bool_t mission_nav_path(struct _mission_path *path)
Navigation function along a path.
#define TRUE
Definition: imu_chimu.h:144
uint8_t path_idx
int mission_run()
Run mission.
int32_t nav_utm_east0
Definition: common_nav.c:42
struct _mission mission
#define NAVIGATION_FREQUENCY
Definition: autopilot.h:145
union _mission_segment::@16 to
struct EnuCoor_f to_f
vector in East North Up coordinates Units: meters
struct EnuCoor_f wp_f
union _mission_element::@18 element
struct EnuCoor_f last_wp_f
struct EnuCoor_f center_f
int32_t y
North.
bool_t mission_point_of_lla(struct EnuCoor_f *point, struct LlaCoor_f *lla)
Get the ENU component of LLA mission point This function is firmware specific.
float east
in meters
int32_t x
East.
void utm_of_lla_f(struct UtmCoor_f *utm, struct LlaCoor_f *lla)
float duration
time to spend in the element (<= 0 to disable)
Fixedwing autopilot modes.