Paparazzi UAS  v5.18.0_stable
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 }
gvf_SurveyPolyAdv::seg_center2
struct FloatVect2 seg_center2
Definition: nav_survey_polygon_gvf.h:80
last_x
float last_x
Definition: nav.c:45
gvf_SurveyPolyAdv::psa_min_rad
float psa_min_rad
Definition: nav_survey_polygon_gvf.h:63
VECT2_SMUL
#define VECT2_SMUL(_vo, _vi, _s)
Definition: pprz_algebra.h:98
gvf_SurveyPolyAdv::ret_start
struct FloatVect2 ret_start
Definition: nav_survey_polygon_gvf.h:82
gvf_SurveyPolyAdv::poly_first
uint8_t poly_first
Definition: nav_survey_polygon_gvf.h:59
gvf_segment_XY1_XY2
bool gvf_segment_XY1_XY2(float x1, float y1, float x2, float y2)
Definition: gvf.c:317
gvf_nav_survey_polygon_run
bool gvf_nav_survey_polygon_run(void)
Definition: nav_survey_polygon_gvf.c:245
b
float b
Definition: wedgebug.c:202
FLOAT_VECT2_NORMALIZE
#define FLOAT_VECT2_NORMALIZE(_v)
Definition: pprz_algebra_float.h:154
gRET
@ gRET
Definition: nav_survey_polygon_gvf.h:46
gvf_SurveyPolyAdv::poly_count
uint8_t poly_count
Definition: nav_survey_polygon_gvf.h:60
dc_stop
uint8_t dc_stop(void)
Stop dc control.
Definition: dc.c:256
gvf_SurveyPolyAdv::segment_angle
int segment_angle
Definition: nav_survey_polygon_gvf.h:69
gvf_SurveyPolyAdv::psa_altitude
float psa_altitude
Definition: nav_survey_polygon_gvf.h:66
stateGetPositionUtm_f
static struct UtmCoor_f * stateGetPositionUtm_f(void)
Get position in UTM coordinates (float).
Definition: state.h:692
gvf_nav_points
static void gvf_nav_points(struct FloatVect2 start, struct FloatVect2 end)
Definition: nav_survey_polygon_gvf.c:42
waypoints
struct point waypoints[NB_WAYPOINT]
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:38
gvf_survey
struct gvf_SurveyPolyAdv gvf_survey
Definition: nav_survey_polygon_gvf.c:40
dc.h
gvf_ellipse_XY
bool gvf_ellipse_XY(float x, float y, float a, float b, float alpha)
Definition: gvf.c:361
NavVerticalAutoThrottleMode
#define NavVerticalAutoThrottleMode(_pitch)
Set the climb control to auto-throttle with the specified pitch pre-command.
Definition: nav.h:171
VECT2_DIFF
#define VECT2_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:92
VECT2_COPY
#define VECT2_COPY(_a, _b)
Definition: pprz_algebra.h:68
CARROT
#define CARROT
default approaching_time for a wp
Definition: navigation.h:40
FloatVect2
Definition: pprz_algebra_float.h:49
NavCourseCloseTo
#define NavCourseCloseTo(x)
Definition: nav.h:157
last_y
float last_y
Definition: nav.c:45
sweep
float sweep
Definition: nav_survey_rectangle_rotorcraft.c:62
gTURN1
@ gTURN1
Definition: nav_survey_polygon_gvf.h:46
gvf_SurveyPolyAdv::sweep_vec
struct FloatVect2 sweep_vec
Definition: nav_survey_polygon_gvf.h:55
logger_uart_parse.a1
a1
Definition: logger_uart_parse.py:9
gSEG
@ gSEG
Definition: nav_survey_polygon_gvf.h:46
gvf_SurveyPolyAdv::return_angle
int return_angle
Definition: nav_survey_polygon_gvf.h:70
gvf.h
gvf_get_two_intersects
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
Definition: nav_survey_polygon_gvf.c:78
gENTRY
@ gENTRY
Definition: nav_survey_polygon_gvf.h:46
gvf_SurveyPolyAdv::seg_center1
struct FloatVect2 seg_center1
Definition: nav_survey_polygon_gvf.h:79
nav_init_stage
void nav_init_stage(void)
needs to be implemented by fixedwing and rotorcraft seperately
Definition: nav.c:93
uint8_t
unsigned char uint8_t
Definition: types.h:14
gvf_nav_survey_polygon_setup
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
Definition: nav_survey_polygon_gvf.c:135
point::x
float x
Definition: common_nav.h:40
autopilot.h
FloatVect2::y
float y
Definition: pprz_algebra_float.h:51
nav.h
gvf_SurveyPolyAdv::psa_shot_dist
float psa_shot_dist
Definition: nav_survey_polygon_gvf.h:65
nav_approaching_xy
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:323
gTURN2
@ gTURN2
Definition: nav_survey_polygon_gvf.h:46
VECT2_SUM
#define VECT2_SUM(_c, _a, _b)
Definition: pprz_algebra.h:86
point::y
float y
Definition: common_nav.h:41
gvf_SurveyPolyAdv::rad_vec
struct FloatVect2 rad_vec
Definition: nav_survey_polygon_gvf.h:56
altitude
static int32_t altitude
Definition: airspeed_uADC.c:59
gvf_SurveyPolyAdv::dir_vec
struct FloatVect2 dir_vec
Definition: nav_survey_polygon_gvf.h:54
gvf_SurveyPolyAdv::seg_start
struct FloatVect2 seg_start
Definition: nav_survey_polygon_gvf.h:77
gvf_nav_direction_circle
void gvf_nav_direction_circle(float rad)
main navigation routine.
Definition: nav_survey_polygon_gvf.c:236
FloatVect2::x
float x
Definition: pprz_algebra_float.h:50
gvf_SurveyPolyAdv::psa_sweep_width
float psa_sweep_width
Definition: nav_survey_polygon_gvf.h:64
gERR
@ gERR
Definition: nav_survey_polygon_gvf.h:46
logger_uart_parse.a2
a2
Definition: logger_uart_parse.py:9
state.h
gvf_set_direction
void gvf_set_direction(int8_t s)
Definition: gvf.c:222
gvf_SurveyPolyAdv
Definition: nav_survey_polygon_gvf.h:48
dc_survey
uint8_t dc_survey(float interval, float x, float y)
Sets the dc control in distance mode.
Definition: dc.c:234
gvf_SurveyPolyAdv::entry_center
struct FloatVect2 entry_center
Definition: nav_survey_polygon_gvf.h:81
gvf_SurveyPolyAdv::stage
enum gvf_SurveyStage stage
Definition: nav_survey_polygon_gvf.h:75
gvf_SurveyPolyAdv::ret_end
struct FloatVect2 ret_end
Definition: nav_survey_polygon_gvf.h:83
p
static float p[2][2]
Definition: ins_alt_float.c:268
gvf_SurveyPolyAdv::seg_end
struct FloatVect2 seg_end
Definition: nav_survey_polygon_gvf.h:78
NavVerticalAltitudeMode
#define NavVerticalAltitudeMode(_alt, _pre_climb)
Set the vertical mode to altitude control with the specified altitude setpoint and climb pre-command.
Definition: nav.h:185
gvf_intercept_two_lines
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
Definition: nav_survey_polygon_gvf.c:55