Paparazzi UAS  v5.15_devel-229-g3fb4ee4
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
nav_survey_polygon_gvf.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 The 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 
30 #include "nav_survey_polygon_gvf.h"
31 
33 #include "state.h"
34 #include "autopilot.h"
35 #include "generated/flight_plan.h"
37 
38 #ifdef DIGITAL_CAM
39 #include "modules/digital_cam/dc.h"
40 #endif
41 
42 float gvf_nav_survey_sweep = 100.f; // dummy non-zero value, will be set at setup
43 
45 
46 static void gvf_nav_points(struct FloatVect2 start, struct FloatVect2 end)
47 {
48  gvf_segment_XY1_XY2(start.x, start.y, end.x, end.y);
49 }
50 
59 static bool gvf_intercept_two_lines(struct FloatVect2 *p, struct FloatVect2 x, struct FloatVect2 y, float a1, float a2,
60  float b1, float b2)
61 {
62  float divider, fac;
63 
64  divider = (((b2 - a2) * (y.x - x.x)) + ((x.y - y.y) * (b1 - a1)));
65  if (divider == 0) { return false; }
66  fac = ((y.x * (x.y - a2)) + (x.x * (a2 - y.y)) + (a1 * (y.y - x.y))) / divider;
67  if (fac > 1.0) { return false; }
68  if (fac < 0.0) { return false; }
69 
70  p->x = a1 + fac * (b1 - a1);
71  p->y = a2 + fac * (b2 - a2);
72 
73  return true;
74 }
75 
82 static bool gvf_get_two_intersects(struct FloatVect2 *x, struct FloatVect2 *y, struct FloatVect2 a, struct FloatVect2 b)
83 {
84  int i, count = 0;
85  struct FloatVect2 tmp;
86 
87  for (i = 0; i < gvf_survey.poly_count - 1; i++)
90  if (count == 0) {
91  *x = tmp;
92  count++;
93  } else {
94  *y = tmp;
95  count++;
96  break;
97  }
98  }
99 
100  //wrapover first,last polygon waypoint
101  if (count == 1
105  *y = tmp;
106  count++;
107  }
108 
109  if (count != 2) {
110  return false;
111  }
112 
113  //change points
114  if (fabs(gvf_survey.dir_vec.x) > fabs(gvf_survey.dir_vec.y)) {
115  if ((y->x - x->x) / gvf_survey.dir_vec.x < 0.0) {
116  tmp = *x;
117  *x = *y;
118  *y = tmp;
119  }
120  } else if ((y->y - x->y) / gvf_survey.dir_vec.y < 0.0) {
121  tmp = *x;
122  *x = *y;
123  *y = tmp;
124  }
125 
126  return true;
127 }
128 
139 void gvf_nav_survey_polygon_setup(uint8_t first_wp, uint8_t size, float angle, float sweep_width, float shot_dist,
140  float min_rad, float altitude)
141 {
142  int i;
143  struct FloatVect2 small, sweep;
144  float divider, angle_rad = angle / 180.0 * M_PI;
145 
146  if (angle < 0.0) { angle += 360.0; }
147  if (angle >= 360.0) { angle -= 360.0; }
148 
149  gvf_survey.poly_first = first_wp;
150  gvf_survey.poly_count = size;
151 
152  gvf_nav_survey_sweep = sweep_width;
153  gvf_survey.psa_sweep_width = sweep_width;
154  gvf_survey.psa_min_rad = min_rad;
155  gvf_survey.psa_shot_dist = shot_dist;
157 
158  gvf_survey.segment_angle = angle;
159  gvf_survey.return_angle = angle + 180;
160  if (gvf_survey.return_angle > 359) { gvf_survey.return_angle -= 360; }
161 
162  if (angle <= 45.0 || angle >= 315.0) {
163  //north
164  gvf_survey.dir_vec.y = 1.0;
165  gvf_survey.dir_vec.x = 1.0 * tanf(angle_rad);
166  sweep.x = 1.0;
167  sweep.y = - gvf_survey.dir_vec.x / gvf_survey.dir_vec.y;
168  } else if (angle <= 135.0) {
169  //east
170  gvf_survey.dir_vec.x = 1.0;
171  gvf_survey.dir_vec.y = 1.0 / tanf(angle_rad);
172  sweep.y = - 1.0;
174  } else if (angle <= 225.0) {
175  //south
176  gvf_survey.dir_vec.y = -1.0;
177  gvf_survey.dir_vec.x = -1.0 * tanf(angle_rad);
178  sweep.x = -1.0;
180  } else {
181  //west
182  gvf_survey.dir_vec.x = -1.0;
183  gvf_survey.dir_vec.y = -1.0 / tanf(angle_rad);
184  sweep.y = 1.0;
185  sweep.x = - gvf_survey.dir_vec.y / gvf_survey.dir_vec.x;
186  }
187 
188  //normalize
189  FLOAT_VECT2_NORMALIZE(sweep);
190 
193 
194  //begin at leftmost position (relative to gvf_survey.dir_vec)
196 
198 
199  //calculate the leftmost point if one sees the dir vec as going "up" and the sweep vec as going right
200  if (divider < 0.0) {
201  for (i = 1; i < gvf_survey.poly_count; i++) {
202  if ((gvf_survey.dir_vec.x * (waypoints[gvf_survey.poly_first + i].y - small.y)) + (gvf_survey.dir_vec.y *
203  (small.x - waypoints[gvf_survey.poly_first + i].x)) > 0.0) {
205  }
206  }
207  } else {
208  for (i = 1; i < gvf_survey.poly_count; i++) {
209  if ((gvf_survey.dir_vec.x * (waypoints[gvf_survey.poly_first + i].y - small.y)) + (gvf_survey.dir_vec.y *
210  (small.x - waypoints[gvf_survey.poly_first + i].x)) > 0.0) {
212  }
213  }
214  }
215 
216  //calculate the line the defines the first flyover
217  gvf_survey.seg_start.x = small.x + 0.5 * gvf_survey.sweep_vec.x;
218  gvf_survey.seg_start.y = small.y + 0.5 * gvf_survey.sweep_vec.y;
220 
223  return;
224  }
225 
226  //center of the entry circle
228 
229  //fast climbing to desired altitude
232 
234 }
235 
242 {
243  if (rad > 0) {
244  gvf_set_direction(-1);
245  } else {
247  }
248 }
249 
251 {
252  #ifdef NAV_SURVEY_POLY_GVF_DYNAMIC
254  #endif
255 
258 
259  //entry circle around entry-center until the desired altitude is reached
260  if (gvf_survey.stage == gENTRY) {
265  && fabs(stateGetPositionUtm_f()->alt - gvf_survey.psa_altitude) <= 20) {
267  nav_init_stage();
268 #ifdef DIGITAL_CAM
271 #endif
272  }
273  }
274  //fly the segment until seg_end is reached
275  if (gvf_survey.stage == gSEG) {
277  //calculate all needed points for the next flyover
279 #ifdef DIGITAL_CAM
280  dc_stop();
281 #endif
285 
286  //if we get no intersection the survey is finished
287  static struct FloatVect2 sum_start_sweep;
288  static struct FloatVect2 sum_end_sweep;
291  if (!gvf_get_two_intersects(&gvf_survey.seg_start, &gvf_survey.seg_end, sum_start_sweep, sum_end_sweep)) {
292  return false;
293  }
294 
297 
300 
302  nav_init_stage();
303  }
304  }
305  //turn from stage to return
306  else if (gvf_survey.stage == gTURN1) {
311  nav_init_stage();
312  }
313  //return
314  } else if (gvf_survey.stage == gRET) {
318  nav_init_stage();
319  }
320  //turn from return to stage
321  } else if (gvf_survey.stage == gTURN2) {
322  float rad_sur = (2 * gvf_survey.psa_min_rad + gvf_survey.psa_sweep_width) * 0.5;
323  gvf_nav_direction_circle(rad_sur);
327  nav_init_stage();
328 #ifdef DIGITAL_CAM
331 #endif
332  }
333  }
334 
335  return true;
336 }
float x
Definition: common_nav.h:40
#define VECT2_SUM(_c, _a, _b)
Definition: pprz_algebra.h:86
#define FLOAT_VECT2_NORMALIZE(_v)
#define VECT2_COPY(_a, _b)
Definition: pprz_algebra.h:68
#define VECT2_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:92
bool gvf_ellipse_XY(float x, float y, float a, float b, float alpha)
Definition: gvf.c:356
bool gvf_segment_XY1_XY2(float x1, float y1, float x2, float y2)
Definition: gvf.c:312
static int32_t altitude
Definition: airspeed_uADC.c:58
uint8_t dc_survey(float interval, float x, float y)
Sets the dc control in distance mode.
Definition: dc.c:234
float y
Definition: common_nav.h:41
Standard Digital Camera Control Interface.
Guidance algorithm based on vector fields.
void gvf_set_direction(int8_t s)
Definition: gvf.c:205
#define VECT2_SMUL(_vo, _vi, _s)
Definition: pprz_algebra.h:98
Core autopilot interface common to all firmwares.
static struct UtmCoor_f * stateGetPositionUtm_f(void)
Get position in UTM coordinates (float).
Definition: state.h:692
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
uint8_t dc_stop(void)
Stop dc control.
Definition: dc.c:256
struct point waypoints[NB_WAYPOINT]
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:38
static float p[2][2]
#define CARROT
default approaching_time for a wp
Definition: navigation.h:40