Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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_t 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_t 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 bool_t 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  survey.poly_first = first_wp;
146  survey.poly_count = size;
147 
148  survey.psa_sweep_width = sweep_width;
149  survey.psa_min_rad = min_rad;
150  survey.psa_shot_dist = shot_dist;
151  survey.psa_altitude = altitude;
152 
153  survey.segment_angle = angle;
154  survey.return_angle = angle + 180;
155  if (survey.return_angle > 359) { survey.return_angle -= 360; }
156 
157  if (angle <= 45.0 || angle >= 315.0) {
158  //north
159  survey.dir_vec.y = 1.0;
160  survey.dir_vec.x = 1.0 * tanf(angle_rad);
161  sweep.x = 1.0;
162  sweep.y = - survey.dir_vec.x / survey.dir_vec.y;
163  } else if (angle <= 135.0) {
164  //east
165  survey.dir_vec.x = 1.0;
166  survey.dir_vec.y = 1.0 / tanf(angle_rad);
167  sweep.y = - 1.0;
168  sweep.x = survey.dir_vec.y / survey.dir_vec.x;
169  } else if (angle <= 225.0) {
170  //south
171  survey.dir_vec.y = -1.0;
172  survey.dir_vec.x = -1.0 * tanf(angle_rad);
173  sweep.x = -1.0;
174  sweep.y = survey.dir_vec.x / survey.dir_vec.y;
175  } else {
176  //west
177  survey.dir_vec.x = -1.0;
178  survey.dir_vec.y = -1.0 / tanf(angle_rad);
179  sweep.y = 1.0;
180  sweep.x = - survey.dir_vec.y / survey.dir_vec.x;
181  }
182 
183  //normalize
184  FLOAT_VECT2_NORMALIZE(sweep);
185 
188 
189  //begin at leftmost position (relative to 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 < survey.poly_count; i++)
197  if ((survey.dir_vec.x * (waypoints[survey.poly_first + i].y - small.y)) + (survey.dir_vec.y *
198  (small.x - waypoints[survey.poly_first + i].x)) > 0.0) {
199  VECT2_COPY(small, waypoints[survey.poly_first + i]);
200  }
201  } else
202  for (i = 1; i < survey.poly_count; i++)
203  if ((survey.dir_vec.x * (waypoints[survey.poly_first + i].y - small.y)) + (survey.dir_vec.y *
204  (small.x - waypoints[survey.poly_first + i].x)) > 0.0) {
205  VECT2_COPY(small, waypoints[survey.poly_first + i]);
206  }
207 
208  //calculate the line the defines the first flyover
209  survey.seg_start.x = small.x + 0.5 * survey.sweep_vec.x;
210  survey.seg_start.y = small.y + 0.5 * survey.sweep_vec.y;
212 
214  survey.stage = ERR;
215  return FALSE;
216  }
217 
218  //center of the entry circle
220 
221  //fast climbing to desired altitude
224 
225  survey.stage = ENTRY;
226 
227  return FALSE;
228 }
229 
236 {
239 
240  //entry circle around entry-center until the desired altitude is reached
241  if (survey.stage == ENTRY) {
245  && fabs(stateGetPositionUtm_f()->alt - survey.psa_altitude) <= 20) {
246  survey.stage = SEG;
247  nav_init_stage();
248 #ifdef DIGITAL_CAM
251 #endif
252  }
253  }
254  //fly the segment until seg_end is reached
255  if (survey.stage == SEG) {
257  //calculate all needed points for the next flyover
259 #ifdef DIGITAL_CAM
260  dc_stop();
261 #endif
265 
266  //if we get no intersection the survey is finished
267  static struct FloatVect2 sum_start_sweep;
268  static struct FloatVect2 sum_end_sweep;
269  VECT2_SUM(sum_start_sweep, survey.seg_start, survey.sweep_vec);
270  VECT2_SUM(sum_end_sweep, survey.seg_end, survey.sweep_vec);
271  if (!get_two_intersects(&survey.seg_start, &survey.seg_end, sum_start_sweep, sum_end_sweep)) {
272  return FALSE;
273  }
274 
277 
280 
281  survey.stage = TURN1;
282  nav_init_stage();
283  }
284  }
285  //turn from stage to return
286  else if (survey.stage == TURN1) {
289  survey.stage = RET;
290  nav_init_stage();
291  }
292  //return
293  } else if (survey.stage == RET) {
296  survey.stage = TURN2;
297  nav_init_stage();
298  }
299  //turn from return to stage
300  } else if (survey.stage == TURN2) {
303  survey.stage = SEG;
304  nav_init_stage();
305 #ifdef DIGITAL_CAM
308 #endif
309  }
310  }
311 
312  return TRUE;
313 }
float x
Definition: common_nav.h:40
#define VECT2_SUM(_c, _a, _b)
Definition: pprz_algebra.h:85
#define FLOAT_VECT2_NORMALIZE(_v)
#define VECT2_COPY(_a, _b)
Definition: pprz_algebra.h:67
#define VECT2_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:91
#define FALSE
Definition: std.h:5
uint8_t dc_survey(float interval, float x, float y)
Sets the dc control in distance mode.
Definition: dc.c:201
#define TRUE
Definition: std.h:4
float y
Definition: common_nav.h:41
Standard Digital Camera Control Interface.
#define VECT2_SMUL(_vo, _vi, _s)
Definition: pprz_algebra.h:97
static struct UtmCoor_f * stateGetPositionUtm_f(void)
Get position in UTM coordinates (float).
Definition: state.h:675
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:223
struct point waypoints[NB_WAYPOINT]
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:38
static float p[2][2]