Paparazzi UAS  v5.15_devel-81-gd13dafb
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 
43 
44 static void gvf_nav_points(struct FloatVect2 start, struct FloatVect2 end)
45 {
46  gvf_segment_XY1_XY2(start.x, start.y, end.x, end.y);
47 }
48 
57 static bool gvf_intercept_two_lines(struct FloatVect2 *p, struct FloatVect2 x, struct FloatVect2 y, float a1, float a2,
58  float b1, float b2)
59 {
60  float divider, fac;
61 
62  divider = (((b2 - a2) * (y.x - x.x)) + ((x.y - y.y) * (b1 - a1)));
63  if (divider == 0) { return false; }
64  fac = ((y.x * (x.y - a2)) + (x.x * (a2 - y.y)) + (a1 * (y.y - x.y))) / divider;
65  if (fac > 1.0) { return false; }
66  if (fac < 0.0) { return false; }
67 
68  p->x = a1 + fac * (b1 - a1);
69  p->y = a2 + fac * (b2 - a2);
70 
71  return true;
72 }
73 
80 static bool gvf_get_two_intersects(struct FloatVect2 *x, struct FloatVect2 *y, struct FloatVect2 a, struct FloatVect2 b)
81 {
82  int i, count = 0;
83  struct FloatVect2 tmp;
84 
85  for (i = 0; i < gvf_survey.poly_count - 1; i++)
88  if (count == 0) {
89  *x = tmp;
90  count++;
91  } else {
92  *y = tmp;
93  count++;
94  break;
95  }
96  }
97 
98  //wrapover first,last polygon waypoint
99  if (count == 1
103  *y = tmp;
104  count++;
105  }
106 
107  if (count != 2) {
108  return false;
109  }
110 
111  //change points
112  if (fabs(gvf_survey.dir_vec.x) > fabs(gvf_survey.dir_vec.y)) {
113  if ((y->x - x->x) / gvf_survey.dir_vec.x < 0.0) {
114  tmp = *x;
115  *x = *y;
116  *y = tmp;
117  }
118  } else if ((y->y - x->y) / gvf_survey.dir_vec.y < 0.0) {
119  tmp = *x;
120  *x = *y;
121  *y = tmp;
122  }
123 
124  return true;
125 }
126 
137 void gvf_nav_survey_polygon_setup(uint8_t first_wp, uint8_t size, float angle, float sweep_width, float shot_dist,
138  float min_rad, float altitude)
139 {
140  int i;
141  struct FloatVect2 small, sweep;
142  float divider, angle_rad = angle / 180.0 * M_PI;
143 
144  if (angle < 0.0) { angle += 360.0; }
145  if (angle >= 360.0) { angle -= 360.0; }
146 
147  gvf_survey.poly_first = first_wp;
148  gvf_survey.poly_count = size;
149 
150  gvf_survey.psa_sweep_width = sweep_width;
151  gvf_survey.psa_min_rad = min_rad;
152  gvf_survey.psa_shot_dist = shot_dist;
154 
155  gvf_survey.segment_angle = angle;
156  gvf_survey.return_angle = angle + 180;
157  if (gvf_survey.return_angle > 359) { gvf_survey.return_angle -= 360; }
158 
159  if (angle <= 45.0 || angle >= 315.0) {
160  //north
161  gvf_survey.dir_vec.y = 1.0;
162  gvf_survey.dir_vec.x = 1.0 * tanf(angle_rad);
163  sweep.x = 1.0;
164  sweep.y = - gvf_survey.dir_vec.x / gvf_survey.dir_vec.y;
165  } else if (angle <= 135.0) {
166  //east
167  gvf_survey.dir_vec.x = 1.0;
168  gvf_survey.dir_vec.y = 1.0 / tanf(angle_rad);
169  sweep.y = - 1.0;
171  } else if (angle <= 225.0) {
172  //south
173  gvf_survey.dir_vec.y = -1.0;
174  gvf_survey.dir_vec.x = -1.0 * tanf(angle_rad);
175  sweep.x = -1.0;
177  } else {
178  //west
179  gvf_survey.dir_vec.x = -1.0;
180  gvf_survey.dir_vec.y = -1.0 / tanf(angle_rad);
181  sweep.y = 1.0;
182  sweep.x = - gvf_survey.dir_vec.y / gvf_survey.dir_vec.x;
183  }
184 
185  //normalize
186  FLOAT_VECT2_NORMALIZE(sweep);
187 
190 
191  //begin at leftmost position (relative to gvf_survey.dir_vec)
193 
195 
196  //calculate the leftmost point if one sees the dir vec as going "up" and the sweep vec as going right
197  if (divider < 0.0) {
198  for (i = 1; i < gvf_survey.poly_count; i++) {
199  if ((gvf_survey.dir_vec.x * (waypoints[gvf_survey.poly_first + i].y - small.y)) + (gvf_survey.dir_vec.y *
200  (small.x - waypoints[gvf_survey.poly_first + i].x)) > 0.0) {
202  }
203  }
204  } else {
205  for (i = 1; i < gvf_survey.poly_count; i++) {
206  if ((gvf_survey.dir_vec.x * (waypoints[gvf_survey.poly_first + i].y - small.y)) + (gvf_survey.dir_vec.y *
207  (small.x - waypoints[gvf_survey.poly_first + i].x)) > 0.0) {
209  }
210  }
211  }
212 
213  //calculate the line the defines the first flyover
214  gvf_survey.seg_start.x = small.x + 0.5 * gvf_survey.sweep_vec.x;
215  gvf_survey.seg_start.y = small.y + 0.5 * gvf_survey.sweep_vec.y;
217 
220  return;
221  }
222 
223  //center of the entry circle
225 
226  //fast climbing to desired altitude
229 
231 }
232 
239 {
240  if (rad > 0) {
241  gvf_set_direction(-1);
242  } else {
244  }
245 }
246 
248 {
251 
252  //entry circle around entry-center until the desired altitude is reached
253  if (gvf_survey.stage == gENTRY) {
258  && fabs(stateGetPositionUtm_f()->alt - gvf_survey.psa_altitude) <= 20) {
260  nav_init_stage();
261 #ifdef DIGITAL_CAM
264 #endif
265  }
266  }
267  //fly the segment until seg_end is reached
268  if (gvf_survey.stage == gSEG) {
270  //calculate all needed points for the next flyover
272 #ifdef DIGITAL_CAM
273  dc_stop();
274 #endif
278 
279  //if we get no intersection the survey is finished
280  static struct FloatVect2 sum_start_sweep;
281  static struct FloatVect2 sum_end_sweep;
284  if (!gvf_get_two_intersects(&gvf_survey.seg_start, &gvf_survey.seg_end, sum_start_sweep, sum_end_sweep)) {
285  return false;
286  }
287 
290 
293 
295  nav_init_stage();
296  }
297  }
298  //turn from stage to return
299  else if (gvf_survey.stage == gTURN1) {
304  nav_init_stage();
305  }
306  //return
307  } else if (gvf_survey.stage == gRET) {
311  nav_init_stage();
312  }
313  //turn from return to stage
314  } else if (gvf_survey.stage == gTURN2) {
315  float rad_sur = (2 * gvf_survey.psa_min_rad + gvf_survey.psa_sweep_width) * 0.5;
316  gvf_nav_direction_circle(rad_sur);
320  nav_init_stage();
321 #ifdef DIGITAL_CAM
324 #endif
325  }
326  }
327 
328  return true;
329 }
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