Paparazzi UAS  v6.2_unstable
Paparazzi is a free software Unmanned Aircraft System.
nav_survey_polygon.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011-2013 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 
29 #include "nav_survey_polygon.h"
30 
32 #include "state.h"
33 #include "autopilot.h"
34 #include "generated/flight_plan.h"
35 
36 #ifdef DIGITAL_CAM
37 #include "modules/digital_cam/dc.h"
38 #endif
39 
41 
42 static void nav_points(struct FloatVect2 start, struct FloatVect2 end)
43 {
44  nav_route_xy(start.x, start.y, end.x, end.y);
45 }
46 
55 static bool 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 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 < survey.poly_count - 1; i++)
85  waypoints[survey.poly_first + i + 1].x, waypoints[survey.poly_first + i + 1].y)) {
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(survey.dir_vec.x) > fabs(survey.dir_vec.y)) {
111  if ((y->x - x->x) / survey.dir_vec.x < 0.0) {
112  tmp = *x;
113  *x = *y;
114  *y = tmp;
115  }
116  } else if ((y->y - x->y) / survey.dir_vec.y < 0.0) {
117  tmp = *x;
118  *x = *y;
119  *y = tmp;
120  }
121 
122  return true;
123 }
124 
135 void nav_survey_polygon_setup(uint8_t first_wp, uint8_t size, float angle, float sweep_width, float shot_dist, float min_rad, float altitude)
136 {
137  int i;
138  struct FloatVect2 small, sweep;
139  float divider, angle_rad = angle / 180.0 * M_PI;
140 
141  if (angle < 0.0) { angle += 360.0; }
142  if (angle >= 360.0) { angle -= 360.0; }
143 
144  survey.poly_first = first_wp;
145  survey.poly_count = size;
146 
147  survey.psa_sweep_width = sweep_width;
148  survey.psa_min_rad = min_rad;
149  survey.psa_shot_dist = shot_dist;
151 
152  survey.segment_angle = angle;
153  survey.return_angle = angle + 180;
154  if (survey.return_angle > 359) { survey.return_angle -= 360; }
155 
156  if (angle <= 45.0 || angle >= 315.0) {
157  //north
158  survey.dir_vec.y = 1.0;
159  survey.dir_vec.x = 1.0 * tanf(angle_rad);
160  sweep.x = 1.0;
161  sweep.y = - survey.dir_vec.x / survey.dir_vec.y;
162  } else if (angle <= 135.0) {
163  //east
164  survey.dir_vec.x = 1.0;
165  survey.dir_vec.y = 1.0 / tanf(angle_rad);
166  sweep.y = - 1.0;
168  } else if (angle <= 225.0) {
169  //south
170  survey.dir_vec.y = -1.0;
171  survey.dir_vec.x = -1.0 * tanf(angle_rad);
172  sweep.x = -1.0;
174  } else {
175  //west
176  survey.dir_vec.x = -1.0;
177  survey.dir_vec.y = -1.0 / tanf(angle_rad);
178  sweep.y = 1.0;
179  sweep.x = - survey.dir_vec.y / survey.dir_vec.x;
180  }
181 
182  //normalize
184 
187 
188  //begin at leftmost position (relative to survey.dir_vec)
190 
192 
193  //calculate the leftmost point if one sees the dir vec as going "up" and the sweep vec as going right
194  if (divider < 0.0) {
195  for (i = 1; i < survey.poly_count; i++)
196  if ((survey.dir_vec.x * (waypoints[survey.poly_first + i].y - small.y)) + (survey.dir_vec.y *
197  (small.x - waypoints[survey.poly_first + i].x)) > 0.0) {
198  VECT2_COPY(small, waypoints[survey.poly_first + i]);
199  }
200  } else
201  for (i = 1; i < survey.poly_count; i++)
202  if ((survey.dir_vec.x * (waypoints[survey.poly_first + i].y - small.y)) + (survey.dir_vec.y *
203  (small.x - waypoints[survey.poly_first + i].x)) > 0.0) {
204  VECT2_COPY(small, waypoints[survey.poly_first + i]);
205  }
206 
207  //calculate the line the defines the first flyover
208  survey.seg_start.x = small.x + 0.5 * survey.sweep_vec.x;
209  survey.seg_start.y = small.y + 0.5 * survey.sweep_vec.y;
211 
213  survey.stage = ERR;
214  return;
215  }
216 
217  //center of the entry circle
219 
220  //fast climbing to desired altitude
223 
224  survey.stage = ENTRY;
225 }
226 
233 {
236 
237  //entry circle around entry-center until the desired altitude is reached
238  if (survey.stage == ENTRY) {
242  && fabs(stateGetPositionUtm_f()->alt - survey.psa_altitude) <= 20) {
243  survey.stage = SEG;
244  nav_init_stage();
245 #ifdef DIGITAL_CAM
248 #endif
249  }
250  }
251  //fly the segment until seg_end is reached
252  if (survey.stage == SEG) {
254  //calculate all needed points for the next flyover
256 #ifdef DIGITAL_CAM
257  dc_stop();
258 #endif
262 
263  //if we get no intersection the survey is finished
264  static struct FloatVect2 sum_start_sweep;
265  static struct FloatVect2 sum_end_sweep;
266  VECT2_SUM(sum_start_sweep, survey.seg_start, survey.sweep_vec);
267  VECT2_SUM(sum_end_sweep, survey.seg_end, survey.sweep_vec);
268  if (!get_two_intersects(&survey.seg_start, &survey.seg_end, sum_start_sweep, sum_end_sweep)) {
269  return false;
270  }
271 
274 
277 
278  survey.stage = TURN1;
279  nav_init_stage();
280  }
281  }
282  //turn from stage to return
283  else if (survey.stage == TURN1) {
286  survey.stage = RET;
287  nav_init_stage();
288  }
289  //return
290  } else if (survey.stage == RET) {
293  survey.stage = TURN2;
294  nav_init_stage();
295  }
296  //turn from return to stage
297  } else if (survey.stage == TURN2) {
300  survey.stage = SEG;
301  nav_init_stage();
302 #ifdef DIGITAL_CAM
305 #endif
306  }
307  }
308 
309  return true;
310 }
SurveyPolyAdv::psa_sweep_width
float psa_sweep_width
Definition: nav_survey_polygon.h:63
last_x
float last_x
Definition: nav.c:47
uint8_t
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
VECT2_SMUL
#define VECT2_SMUL(_vo, _vi, _s)
Definition: pprz_algebra.h:98
RET
@ RET
Definition: nav_survey_polygon.h:45
SurveyPolyAdv::stage
enum SurveyStage stage
Definition: nav_survey_polygon.h:74
SurveyPolyAdv::seg_center2
struct FloatVect2 seg_center2
Definition: nav_survey_polygon.h:79
b
float b
Definition: wedgebug.c:202
SEG
@ SEG
Definition: nav_survey_polygon.h:45
SurveyPolyAdv::sweep_vec
struct FloatVect2 sweep_vec
Definition: nav_survey_polygon.h:54
FLOAT_VECT2_NORMALIZE
#define FLOAT_VECT2_NORMALIZE(_v)
Definition: pprz_algebra_float.h:154
SurveyPolyAdv::seg_start
struct FloatVect2 seg_start
Definition: nav_survey_polygon.h:76
SurveyPolyAdv::psa_shot_dist
float psa_shot_dist
Definition: nav_survey_polygon.h:64
get_two_intersects
static bool 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.c:78
dc_stop
uint8_t dc_stop(void)
Stop dc control.
Definition: dc.c:256
nav_circle_XY
void nav_circle_XY(float x, float y, float radius)
Navigates around (x, y).
Definition: nav.c:111
SurveyPolyAdv::ret_end
struct FloatVect2 ret_end
Definition: nav_survey_polygon.h:82
nav_survey_polygon.h
stateGetPositionUtm_f
static struct UtmCoor_f * stateGetPositionUtm_f(void)
Get position in UTM coordinates (float).
Definition: state.h:692
waypoints
struct point waypoints[NB_WAYPOINT]
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:39
SurveyPolyAdv::rad_vec
struct FloatVect2 rad_vec
Definition: nav_survey_polygon.h:55
dc.h
ERR
@ ERR
Definition: nav_survey_polygon.h:45
NavVerticalAutoThrottleMode
#define NavVerticalAutoThrottleMode(_pitch)
Set the climb control to auto-throttle with the specified pitch pre-command.
Definition: nav.h:180
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
survey
struct SurveyPolyAdv survey
Definition: nav_survey_polygon.c:40
NavCourseCloseTo
#define NavCourseCloseTo(x)
Definition: nav.h:166
nav_survey_polygon_setup
void 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.c:135
nav_route_xy
void nav_route_xy(float last_wp_x, float last_wp_y, float wp_x, float wp_y)
Computes the carrot position along the desired segment.
Definition: nav.c:383
SurveyPolyAdv::psa_altitude
float psa_altitude
Definition: nav_survey_polygon.h:65
last_y
float last_y
Definition: nav.c:47
sweep
float sweep
Definition: nav_survey_rectangle_rotorcraft.c:62
logger_uart_parse.a1
a1
Definition: logger_uart_parse.py:9
SurveyPolyAdv::dir_vec
struct FloatVect2 dir_vec
Definition: nav_survey_polygon.h:53
SurveyPolyAdv::psa_min_rad
float psa_min_rad
Definition: nav_survey_polygon.h:62
SurveyPolyAdv::segment_angle
int segment_angle
Definition: nav_survey_polygon.h:68
nav_init_stage
void nav_init_stage(void)
needs to be implemented by fixedwing and rotorcraft seperately
Definition: nav.c:95
SurveyPolyAdv::seg_center1
struct FloatVect2 seg_center1
Definition: nav_survey_polygon.h:78
TURN2
@ TURN2
Definition: nav_survey_polygon.h:45
SurveyPolyAdv::ret_start
struct FloatVect2 ret_start
Definition: nav_survey_polygon.h:81
nav_points
static void nav_points(struct FloatVect2 start, struct FloatVect2 end)
Definition: nav_survey_polygon.c:42
point::x
float x
Definition: common_nav.h:40
SurveyPolyAdv::seg_end
struct FloatVect2 seg_end
Definition: nav_survey_polygon.h:77
SurveyPolyAdv::poly_count
uint8_t poly_count
Definition: nav_survey_polygon.h:59
autopilot.h
FloatVect2::y
float y
Definition: pprz_algebra_float.h:51
SurveyPolyAdv::poly_first
uint8_t poly_first
Definition: nav_survey_polygon.h:58
nav.h
SurveyPolyAdv::return_angle
int return_angle
Definition: nav_survey_polygon.h:69
SurveyPolyAdv
Definition: nav_survey_polygon.h:47
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:326
ENTRY
@ ENTRY
Definition: nav_survey_polygon.h:45
VECT2_SUM
#define VECT2_SUM(_c, _a, _b)
Definition: pprz_algebra.h:86
intercept_two_lines
static bool 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.c:55
point::y
float y
Definition: common_nav.h:41
altitude
static int32_t altitude
Definition: airspeed_uADC.c:59
FloatVect2::x
float x
Definition: pprz_algebra_float.h:50
SurveyPolyAdv::entry_center
struct FloatVect2 entry_center
Definition: nav_survey_polygon.h:80
logger_uart_parse.a2
a2
Definition: logger_uart_parse.py:9
state.h
dc_survey
uint8_t dc_survey(float interval, float x, float y)
Sets the dc control in distance mode.
Definition: dc.c:234
nav_survey_polygon_run
bool nav_survey_polygon_run(void)
main navigation routine.
Definition: nav_survey_polygon.c:232
p
static float p[2][2]
Definition: ins_alt_float.c:268
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:194
TURN1
@ TURN1
Definition: nav_survey_polygon.h:45