Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures 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, float b1, float b2)
56 {
57  float divider, fac;
58 
59  divider = (((b2 - a2)*(y.x - x.x)) + ((x.y - y.y)*(b1 - a1)));
60  if (divider == 0) return FALSE;
61  fac = ((y.x*(x.y - a2)) + (x.x*(a2 - y.y)) + (a1*(y.y - x.y))) / divider;
62  if (fac > 1.0) return FALSE;
63  if (fac < 0.0) return FALSE;
64 
65  p->x = a1 + fac*(b1 - a1);
66  p->y = a2 + fac*(b2 - a2);
67 
68  return TRUE;
69 }
70 
77 static bool_t get_two_intersects(struct FloatVect2 *x, struct FloatVect2 *y, struct FloatVect2 a, struct FloatVect2 b)
78 {
79  int i, count = 0;
80  struct FloatVect2 tmp;
81 
82  for (i=0;i<survey.poly_count-1;i++)
84  if (count == 0) {
85  *x = tmp;
86  count++;
87  }
88  else {
89  *y = tmp;
90  count++;
91  break;
92  }
93  }
94 
95  //wrapover first,last polygon waypoint
97  *y = tmp;
98  count++;
99  }
100 
101  if (count != 2)
102  return FALSE;
103 
104  //change points
105  if (fabs(survey.dir_vec.x) > fabs(survey.dir_vec.y)) {
106  if ((y->x - x->x) / survey.dir_vec.x < 0.0){
107  tmp = *x;
108  *x = *y;
109  *y = tmp;
110  }
111  }
112  else
113  if ((y->y - x->y) / survey.dir_vec.y < 0.0) {
114  tmp = *x;
115  *x = *y;
116  *y = tmp;
117  }
118 
119  return TRUE;
120 }
121 
132 bool_t nav_survey_polygon_setup(uint8_t first_wp, uint8_t size, float angle, float sweep_width, float shot_dist, float min_rad, float altitude)
133 {
134  int i;
135  struct FloatVect2 small, sweep;
136  float divider, angle_rad = angle/180.0*M_PI;
137 
138  if (angle < 0.0) angle += 360.0;
139  if (angle >= 360.0) angle -= 360.0;
140 
141  survey.poly_first = first_wp;
142  survey.poly_count = size;
143 
144  survey.psa_sweep_width = sweep_width;
145  survey.psa_min_rad = min_rad;
146  survey.psa_shot_dist = shot_dist;
147  survey.psa_altitude = altitude;
148 
149  survey.segment_angle = angle;
150  survey.return_angle = angle+180;
151  if (survey.return_angle > 359) survey.return_angle -= 360;
152 
153  if (angle <= 45.0 || angle >= 315.0) {
154  //north
155  survey.dir_vec.y = 1.0;
156  survey.dir_vec.x = 1.0*tanf(angle_rad);
157  sweep.x = 1.0;
158  sweep.y = - survey.dir_vec.x / survey.dir_vec.y;
159  }
160  else if (angle <= 135.0) {
161  //east
162  survey.dir_vec.x = 1.0;
163  survey.dir_vec.y = 1.0/tanf(angle_rad);
164  sweep.y = - 1.0;
165  sweep.x = survey.dir_vec.y / survey.dir_vec.x;
166  }
167  else if (angle <= 225.0) {
168  //south
169  survey.dir_vec.y = -1.0;
170  survey.dir_vec.x = -1.0*tanf(angle_rad);
171  sweep.x = -1.0;
172  sweep.y = survey.dir_vec.x / survey.dir_vec.y;
173  }
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
183  FLOAT_VECT2_NORMALIZE(sweep);
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*(small.x - waypoints[survey.poly_first+i].x)) > 0.0) {
198  }
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*(small.x - waypoints[survey.poly_first+i].x)) > 0.0) {
204  }
205 
206  //calculate the line the defines the first flyover
207  survey.seg_start.x = small.x + 0.5*survey.sweep_vec.x;
208  survey.seg_start.y = small.y + 0.5*survey.sweep_vec.y;
210 
212  survey.stage = ERR;
213  return FALSE;
214  }
215 
216  //center of the entry circle
218 
219  //fast climbing to desired altitude
222 
223  survey.stage = ENTRY;
224 
225  return FALSE;
226 }
227 
234 {
237 
238  //entry circle around entry-center until the desired altitude is reached
239  if (survey.stage == ENTRY) {
243  && fabs(stateGetPositionUtm_f()->alt - survey.psa_altitude) <= 20) {
244  survey.stage = SEG;
245  nav_init_stage();
246 #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 
273 
276 
277  survey.stage = TURN1;
278  nav_init_stage();
279  }
280  }
281  //turn from stage to return
282  else if (survey.stage == TURN1) {
285  survey.stage = RET;
286  nav_init_stage();
287  }
288  //return
289  } else if (survey.stage == RET) {
292  survey.stage = TURN2;
293  nav_init_stage();
294  }
295  //turn from return to stage
296  } else if (survey.stage == TURN2) {
299  survey.stage = SEG;
300  nav_init_stage();
301 #ifdef DIGITAL_CAM
303 #endif
304  }
305  }
306 
307  return TRUE;
308 }
#define FLOAT_VECT2_DIFF(_c, _a, _b)
struct FloatVect2 entry_center
struct FloatVect2 seg_end
#define FLOAT_VECT2_SMUL(_vo, _vi, _s)
#define FALSE
Definition: imu_chimu.h:141
#define FLOAT_VECT2_SUM(_c, _a, _b)
struct FloatVect2 rad_vec
struct FloatVect2 seg_center1
uint8_t dc_survey(float interval, float x, float y)
Sets the dc control in distance mode.
Definition: dc.c:130
struct FloatVect2 seg_start
struct FloatVect2 sweep_vec
Standard Digital Camera Control Interface.
struct FloatVect2 ret_end
#define FLOAT_VECT2_COPY(_a, _b)
#define TRUE
Definition: imu_chimu.h:144
#define VECT2_SUM(_c, _a, _b)
Definition: pprz_algebra.h:68
static struct UtmCoor_f * stateGetPositionUtm_f(void)
Get position in UTM coordinates (float).
Definition: state.h:651
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
uint8_t dc_stop(void)
Sets the dc control in inactive mode, stopping all current actions.
Definition: dc.c:150
struct FloatVect2 dir_vec
static float p[2][2]
struct FloatVect2 ret_start
struct FloatVect2 seg_center2
int32_t y
North.
enum SurveyStage stage
#define FLOAT_VECT2_NORMALIZE(_v)
int32_t x
East.