Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 
31 #include "state.h"
32 #include "autopilot.h"
33 #include "generated/flight_plan.h"
35 
36 #ifdef DIGITAL_CAM
37 #include "modules/digital_cam/dc.h"
38 #endif
39 
41 
42 static void gvf_nav_points(struct FloatVect2 start, struct FloatVect2 end)
43 {
44  gvf_segment_XY1_XY2(start.x, start.y, end.x, end.y);
45 }
46 
55 static bool gvf_intercept_two_lines(struct FloatVect2 *p, struct FloatVect2 x, struct FloatVect2 y, float a1, float a2,
56  float b1, float b2)
57 {
58  float divider, fac;
59 
60  divider = (((b2 - a2) * (y.x - x.x)) + ((x.y - y.y) * (b1 - a1)));
61  if (divider == 0) { return false; }
62  fac = ((y.x * (x.y - a2)) + (x.x * (a2 - y.y)) + (a1 * (y.y - x.y))) / divider;
63  if (fac > 1.0) { return false; }
64  if (fac < 0.0) { return false; }
65 
66  p->x = a1 + fac * (b1 - a1);
67  p->y = a2 + fac * (b2 - a2);
68 
69  return true;
70 }
71 
78 static bool gvf_get_two_intersects(struct FloatVect2 *x, struct FloatVect2 *y, struct FloatVect2 a, struct FloatVect2 b)
79 {
80  int i, count = 0;
81  struct FloatVect2 tmp;
82 
83  for (i = 0; i < gvf_survey.poly_count - 1; i++)
86  if (count == 0) {
87  *x = tmp;
88  count++;
89  } else {
90  *y = tmp;
91  count++;
92  break;
93  }
94  }
95 
96  //wrapover first,last polygon waypoint
97  if (count == 1
101  *y = tmp;
102  count++;
103  }
104 
105  if (count != 2) {
106  return false;
107  }
108 
109  //change points
110  if (fabs(gvf_survey.dir_vec.x) > fabs(gvf_survey.dir_vec.y)) {
111  if ((y->x - x->x) / gvf_survey.dir_vec.x < 0.0) {
112  tmp = *x;
113  *x = *y;
114  *y = tmp;
115  }
116  } else if ((y->y - x->y) / gvf_survey.dir_vec.y < 0.0) {
117  tmp = *x;
118  *x = *y;
119  *y = tmp;
120  }
121 
122  return true;
123 }
124 
135 void gvf_nav_survey_polygon_setup(uint8_t first_wp, uint8_t size, float angle, float sweep_width, float shot_dist,
136  float min_rad, float altitude)
137 {
138  int i;
139  struct FloatVect2 small, sweep;
140  float divider, angle_rad = angle / 180.0 * M_PI;
141 
142  if (angle < 0.0) { angle += 360.0; }
143  if (angle >= 360.0) { angle -= 360.0; }
144 
145  gvf_survey.poly_first = first_wp;
146  gvf_survey.poly_count = size;
147 
148  gvf_survey.psa_sweep_width = sweep_width;
149  gvf_survey.psa_min_rad = min_rad;
150  gvf_survey.psa_shot_dist = shot_dist;
152 
153  gvf_survey.segment_angle = angle;
154  gvf_survey.return_angle = angle + 180;
155  if (gvf_survey.return_angle > 359) { gvf_survey.return_angle -= 360; }
156 
157  if (angle <= 45.0 || angle >= 315.0) {
158  //north
159  gvf_survey.dir_vec.y = 1.0;
160  gvf_survey.dir_vec.x = 1.0 * tanf(angle_rad);
161  sweep.x = 1.0;
163  } else if (angle <= 135.0) {
164  //east
165  gvf_survey.dir_vec.x = 1.0;
166  gvf_survey.dir_vec.y = 1.0 / tanf(angle_rad);
167  sweep.y = -1.0;
169  } else if (angle <= 225.0) {
170  //south
171  gvf_survey.dir_vec.y = -1.0;
172  gvf_survey.dir_vec.x = -1.0 * tanf(angle_rad);
173  sweep.x = -1.0;
175  } else {
176  //west
177  gvf_survey.dir_vec.x = -1.0;
178  gvf_survey.dir_vec.y = -1.0 / tanf(angle_rad);
179  sweep.y = 1.0;
181  }
182 
183  //normalize
185 
188 
189  //begin at leftmost position (relative to gvf_survey.dir_vec)
191 
193 
194  //calculate the leftmost point if one sees the dir vec as going "up" and the sweep vec as going right
195  if (divider < 0.0) {
196  for (i = 1; i < gvf_survey.poly_count; i++) {
197  if ((gvf_survey.dir_vec.x * (waypoints[gvf_survey.poly_first + i].y - small.y)) + (gvf_survey.dir_vec.y *
198  (small.x - waypoints[gvf_survey.poly_first + i].x)) > 0.0) {
200  }
201  }
202  } else {
203  for (i = 1; i < gvf_survey.poly_count; i++) {
204  if ((gvf_survey.dir_vec.x * (waypoints[gvf_survey.poly_first + i].y - small.y)) + (gvf_survey.dir_vec.y *
205  (small.x - waypoints[gvf_survey.poly_first + i].x)) > 0.0) {
207  }
208  }
209  }
210 
211  //calculate the line the defines the first flyover
212  gvf_survey.seg_start.x = small.x + 0.5 * gvf_survey.sweep_vec.x;
213  gvf_survey.seg_start.y = small.y + 0.5 * gvf_survey.sweep_vec.y;
215 
218  return;
219  }
220 
221  //center of the entry circle
223 
224  //fast climbing to desired altitude
227 
229 }
230 
237 {
238  if (rad > 0) {
239  gvf_set_direction(-1);
240  } else {
242  }
243 }
244 
246 {
249 
250  //entry circle around entry-center until the desired altitude is reached
251  if (gvf_survey.stage == gENTRY) {
256  && fabs(stateGetPositionUtm_f()->alt - gvf_survey.psa_altitude) <= 20) {
258  nav_init_stage();
259 #ifdef DIGITAL_CAM
262 #endif
263  }
264  }
265  //fly the segment until seg_end is reached
266  if (gvf_survey.stage == gSEG) {
268  //calculate all needed points for the next flyover
270 #ifdef DIGITAL_CAM
271  dc_stop();
272 #endif
276 
277  //if we get no intersection the survey is finished
278  static struct FloatVect2 sum_start_sweep;
279  static struct FloatVect2 sum_end_sweep;
282  if (!gvf_get_two_intersects(&gvf_survey.seg_start, &gvf_survey.seg_end, sum_start_sweep, sum_end_sweep)) {
283  return false;
284  }
285 
288 
291 
293  nav_init_stage();
294  }
295  }
296  //turn from stage to return
297  else if (gvf_survey.stage == gTURN1) {
302  nav_init_stage();
303  }
304  //return
305  } else if (gvf_survey.stage == gRET) {
309  nav_init_stage();
310  }
311  //turn from return to stage
312  } else if (gvf_survey.stage == gTURN2) {
313  float rad_sur = (2 * gvf_survey.psa_min_rad + gvf_survey.psa_sweep_width) * 0.5;
314  gvf_nav_direction_circle(rad_sur);
318  nav_init_stage();
319 #ifdef DIGITAL_CAM
322 #endif
323  }
324  }
325 
326  return true;
327 }
static int32_t altitude
Definition: airspeed_uADC.c:59
Core autopilot interface common to all firmwares.
struct point waypoints[NB_WAYPOINT]
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:39
float y
Definition: common_nav.h:41
float x
Definition: common_nav.h:40
uint8_t dc_survey(float interval, float x, float y)
Sets the dc control in distance mode.
Definition: dc.c:240
uint8_t dc_stop(void)
Stop dc control.
Definition: dc.c:262
Standard Digital Camera Control Interface.
#define FLOAT_VECT2_NORMALIZE(_v)
#define VECT2_SMUL(_vo, _vi, _s)
Definition: pprz_algebra.h:98
#define VECT2_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:92
#define VECT2_COPY(_a, _b)
Definition: pprz_algebra.h:68
#define VECT2_SUM(_c, _a, _b)
Definition: pprz_algebra.h:86
static struct UtmCoor_f * stateGetPositionUtm_f(void)
Get position in UTM coordinates (float).
Definition: state.h:692
bool gvf_segment_XY1_XY2(float x1, float y1, float x2, float y2)
Definition: gvf.c:382
void gvf_set_direction(int8_t s)
Definition: gvf.c:266
bool gvf_ellipse_XY(float x, float y, float a, float b, float alpha)
Definition: gvf.c:433
Guidance algorithm based on vector fields.
static float p[2][2]
bool nav_approaching_xy(float x, float y, float from_x, float from_y, float approaching_time)
Decide if the UAV is approaching the current waypoint.
Definition: nav.c:325
void nav_init_stage(void)
needs to be implemented by fixedwing and rotorcraft seperately
Definition: nav.c:92
float last_y
Definition: nav.c:47
float last_x
Definition: nav.c:47
Fixedwing Navigation library.
#define NavCourseCloseTo(x)
Definition: nav.h:163
#define NavVerticalAltitudeMode(_alt, _pre_climb)
Set the vertical mode to altitude control with the specified altitude setpoint and climb pre-command.
Definition: nav.h:191
#define NavVerticalAutoThrottleMode(_pitch)
Set the climb control to auto-throttle with the specified pitch pre-command.
Definition: nav.h:177
static bool gvf_intercept_two_lines(struct FloatVect2 *p, struct FloatVect2 x, struct FloatVect2 y, float a1, float a2, float b1, float b2)
intercept two lines and give back the point of intersection
static bool gvf_get_two_intersects(struct FloatVect2 *x, struct FloatVect2 *y, struct FloatVect2 a, struct FloatVect2 b)
intersects a line with the polygon and gives back the two intersection points
void gvf_nav_direction_circle(float rad)
main navigation routine.
void gvf_nav_survey_polygon_setup(uint8_t first_wp, uint8_t size, float angle, float sweep_width, float shot_dist, float min_rad, float altitude)
initializes the variables needed for the survey to start
struct gvf_SurveyPolyAdv gvf_survey
bool gvf_nav_survey_polygon_run(void)
static void gvf_nav_points(struct FloatVect2 start, struct FloatVect2 end)
struct FloatVect2 seg_center2
struct FloatVect2 rad_vec
struct FloatVect2 ret_end
struct FloatVect2 entry_center
struct FloatVect2 seg_end
enum gvf_SurveyStage stage
struct FloatVect2 dir_vec
struct FloatVect2 seg_start
struct FloatVect2 sweep_vec
struct FloatVect2 seg_center1
struct FloatVect2 ret_start
#define CARROT
default approaching_time for a wp
Definition: navigation.h:70
API to get/set the generic vehicle states.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
float b
Definition: wedgebug.c:202