Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
nav.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-2005 Pascal Brisset, Antoine Drouin
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 
28 #include <math.h>
29 #include "std.h"
30 
31 static unit_t unit __attribute__((unused));
32 
33 #define NAV_C
37 #include "inter_mcu.h"
38 #include "subsystems/gps.h"
39 
40 #include "generated/flight_plan.h"
41 
42 
44 
45 float last_x, last_y;
46 
48 uint8_t last_wp __attribute__((unused));
49 
50 float rc_pitch;
52 
54 float nav_circle_radians; /* Cumulated */
55 float nav_circle_radians_no_rewind; /* Cumulated */
56 float nav_circle_trigo_qdr; /* Angle from center to mobile */
58 
59 
61 static float nav_leg_progress;
63 
65 static float nav_leg_length;
66 
67 bool nav_in_circle = false;
68 bool nav_in_segment = false;
72 float circle_bank = 0;
73 
76 
78 #ifndef NAV_GLIDE_PITCH_TRIM
79 #define NAV_GLIDE_PITCH_TRIM 0.
80 #endif
81 
82 
83 
85 
86 /* Used in nav_survey_rectangle. Defined here for downlink and uplink */
90 
92 
93 void nav_init_stage(void)
94 {
95  last_x = stateGetPositionEnu_f()->x;
96  last_y = stateGetPositionEnu_f()->y;
97  stage_time = 0;
98  nav_circle_radians = 0;
99  nav_circle_radians_no_rewind = 0;
100  nav_in_circle = false;
101  nav_in_segment = false;
102  nav_shift = 0;
103 }
104 
105 #define MIN_DX ((int16_t)(MAX_PPRZ * 0.05))
106 
107 
109 void nav_circle_XY(float x, float y, float radius)
110 {
111  struct EnuCoor_f *pos = stateGetPositionEnu_f();
112  float last_trigo_qdr = nav_circle_trigo_qdr;
113  nav_circle_trigo_qdr = atan2f(pos->y - y, pos->x - x);
114  float sign_radius = radius > 0 ? 1 : -1;
115 
116  if (nav_in_circle) {
117  float trigo_diff = nav_circle_trigo_qdr - last_trigo_qdr;
118  NormRadAngle(trigo_diff);
119  nav_circle_radians += trigo_diff;
120  trigo_diff *= - sign_radius;
121  if (trigo_diff > 0) { // do not rewind if the change in angle is in the opposite sense than nav_radius
122  nav_circle_radians_no_rewind += trigo_diff;
123  }
124  }
125 
126  float dist2_center = DistanceSquare(pos->x, pos->y, x, y);
127  float dist_carrot = CARROT * NOMINAL_AIRSPEED;
128 
129  radius += -nav_shift;
130 
131  float abs_radius = fabs(radius);
132 
134  circle_bank =
135  (dist2_center > Square(abs_radius + dist_carrot)
136  || dist2_center < Square(abs_radius - dist_carrot)) ?
137  0 :
139 
140  float carrot_angle = dist_carrot / abs_radius;
141  carrot_angle = Min(carrot_angle, M_PI / 4);
142  carrot_angle = Max(carrot_angle, M_PI / 16);
143  float alpha_carrot = nav_circle_trigo_qdr - sign_radius * carrot_angle;
144  horizontal_mode = HORIZONTAL_MODE_CIRCLE;
145  float radius_carrot = abs_radius;
146  if (nav_mode == NAV_MODE_COURSE) {
147  radius_carrot += (abs_radius / cosf(carrot_angle) - abs_radius);
148  }
149  fly_to_xy(x + cosf(alpha_carrot)*radius_carrot,
150  y + sinf(alpha_carrot)*radius_carrot);
151  nav_in_circle = true;
152  nav_circle_x = x;
153  nav_circle_y = y;
154  nav_circle_radius = radius;
155 }
156 
157 
158 void nav_glide(uint8_t start_wp, uint8_t wp)
159 {
160  float start_alt = waypoints[start_wp].a;
161  float diff_alt = waypoints[wp].a - start_alt;
162  float alt = start_alt + nav_leg_progress * diff_alt;
163  float pre_climb = stateGetHorizontalSpeedNorm_f() * diff_alt / nav_leg_length;
164  NavVerticalAltitudeMode(alt, pre_climb);
165 }
166 
167 
168 #define MAX_DIST_CARROT 250.
169 #define MIN_HEIGHT_CARROT 50.
170 #define MAX_HEIGHT_CARROT 150.
171 
172 #define Goto3D(radius) { \
173  if (pprz_mode == PPRZ_MODE_AUTO2) { \
174  int16_t yaw = imcu_get_radio(RADIO_YAW); \
175  if (yaw > MIN_DX || yaw < -MIN_DX) { \
176  carrot_x += FLOAT_OF_PPRZ(yaw, 0, -20.); \
177  carrot_x = Min(carrot_x, MAX_DIST_CARROT); \
178  carrot_x = Max(carrot_x, -MAX_DIST_CARROT); \
179  } \
180  int16_t pitch = imcu_get_radio(RADIO_PITCH); \
181  if (pitch > MIN_DX || pitch < -MIN_DX) { \
182  carrot_y += FLOAT_OF_PPRZ(pitch, 0, -20.); \
183  carrot_y = Min(carrot_y, MAX_DIST_CARROT); \
184  carrot_y = Max(carrot_y, -MAX_DIST_CARROT); \
185  } \
186  v_ctl_mode = V_CTL_MODE_AUTO_ALT; \
187  int16_t roll = imcu_get_radio(RADIO_ROLL); \
188  if (roll > MIN_DX || roll < -MIN_DX) { \
189  nav_altitude += FLOAT_OF_PPRZ(roll, 0, -1.0); \
190  nav_altitude = Max(nav_altitude, MIN_HEIGHT_CARROT+ground_alt); \
191  nav_altitude = Min(nav_altitude, MAX_HEIGHT_CARROT+ground_alt); \
192  } \
193  } \
194  nav_circle_XY(carrot_x, carrot_y, radius); \
195  }
196 
197 
198 
199 #ifdef NAV_GROUND_SPEED_PGAIN
200 
202 static void nav_ground_speed_loop(void)
203 {
204  if (MINIMUM_AIRSPEED < nav_ground_speed_setpoint
205  && nav_ground_speed_setpoint < MAXIMUM_AIRSPEED) {
206  float err = nav_ground_speed_setpoint - stateGetHorizontalSpeedNorm_f();
207  v_ctl_auto_throttle_cruise_throttle += nav_ground_speed_pgain * err;
210  } else {
211  /* Reset cruise throttle to nominal value */
212  v_ctl_auto_throttle_cruise_throttle = V_CTL_AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE;
213  }
214 }
215 #endif
216 
218 bool nav_compute_baseleg(uint8_t wp_af, uint8_t wp_td, uint8_t wp_baseleg, float radius)
219 {
220  nav_radius = radius;
221 
222  float x_0 = waypoints[wp_td].x - waypoints[wp_af].x;
223  float y_0 = waypoints[wp_td].y - waypoints[wp_af].y;
224 
225  /* Unit vector from AF to TD */
226  float d = sqrtf(x_0 * x_0 + y_0 * y_0);
227  float x_1 = x_0 / d;
228  float y_1 = y_0 / d;
229 
230  waypoints[wp_baseleg].x = waypoints[wp_af].x + y_1 * nav_radius;
231  waypoints[wp_baseleg].y = waypoints[wp_af].y - x_1 * nav_radius;
232  waypoints[wp_baseleg].a = waypoints[wp_af].a;
233  baseleg_out_qdr = M_PI - atan2f(-y_1, -x_1);
234  if (nav_radius < 0) {
235  baseleg_out_qdr += M_PI;
236  }
237 
238  return false;
239 }
240 
241 bool nav_compute_final_from_glide(uint8_t wp_af, uint8_t wp_td, float glide)
242 {
243 
244  float x_0 = waypoints[wp_td].x - waypoints[wp_af].x;
245  float y_0 = waypoints[wp_td].y - waypoints[wp_af].y;
246  float h_0 = waypoints[wp_td].a - waypoints[wp_af].a;
247 
248  /* Unit vector from AF to TD */
249  float d = sqrtf(x_0 * x_0 + y_0 * y_0);
250  float x_1 = x_0 / d;
251  float y_1 = y_0 / d;
252 
253  waypoints[wp_af].x = waypoints[wp_td].x + x_1 * h_0 * glide;
254  waypoints[wp_af].y = waypoints[wp_td].y + y_1 * h_0 * glide;
255 
256  return false;
257 }
258 
259 
260 /* For a landing UPWIND.
261  Computes Top Of Descent waypoint from Touch Down and Approach Fix
262  waypoints, using glide airspeed, glide vertical speed and wind */
263 static inline bool compute_TOD(uint8_t _af, uint8_t _td, uint8_t _tod, float glide_airspeed, float glide_vspeed)
264 {
265  struct FloatVect2 *wind = stateGetHorizontalWindspeed_f();
266  float td_af_x = WaypointX(_af) - WaypointX(_td);
267  float td_af_y = WaypointY(_af) - WaypointY(_td);
268  float td_af = sqrtf(td_af_x * td_af_x + td_af_y * td_af_y);
269  float td_tod = (WaypointAlt(_af) - WaypointAlt(_td)) / glide_vspeed * (glide_airspeed - sqrtf(
270  wind->x * wind->x + wind->y * wind->y));
271  WaypointX(_tod) = WaypointX(_td) + td_af_x / td_af * td_tod;
272  WaypointY(_tod) = WaypointY(_td) + td_af_y / td_af * td_tod;
273  WaypointAlt(_tod) = WaypointAlt(_af);
274  return false;
275 }
276 
277 
278 #ifndef LINE_START_FUNCTION
279 #define LINE_START_FUNCTION {}
280 #endif
281 #ifndef LINE_STOP_FUNCTION
282 #define LINE_STOP_FUNCTION {}
283 #endif
284 
285 #ifdef TRAFFIC_INFO
287 
288 void nav_follow(uint8_t ac_id, float distance, float height)
289 {
290  struct EnuCoor_f *ac = acInfoGetPositionEnu_f(ac_id);
292  NavVerticalAltitudeMode(Max(ac->z + height, ground_alt + SECURITY_HEIGHT), 0.);
293  float alpha = M_PI / 2 - acInfoGetCourse(ac_id);
294  float ca = cosf(alpha), sa = sinf(alpha);
295  float x = ac->x - distance * ca;
296  float y = ac->y - distance * sa;
297  fly_to_xy(x, y);
298 #ifdef NAV_FOLLOW_PGAIN
299  float s = (stateGetPositionEnu_f()->x - x) * ca + (stateGetPositionEnu_f()->y - y) * sa;
300  nav_ground_speed_setpoint = acInfoGetGspeed(ac_id) + NAV_FOLLOW_PGAIN * s;
301  nav_ground_speed_loop();
302 #endif
303 }
304 #else
305 void nav_follow(uint8_t __attribute__((unused)) _ac_id, float __attribute__((unused)) distance,
306  float __attribute__((unused)) height) {}
307 #endif // TRAFFIC_INFO
308 
309 
310 float nav_altitude = GROUND_ALT + MIN_HEIGHT_CARROT;
313 float nav_pitch; /* Rad */
314 float fp_pitch; /* deg */
315 float fp_throttle; /* [0-1] */
316 float fp_climb; /* m/s */
317 
318 
328 bool nav_approaching_xy(float x, float y, float from_x, float from_y, float approaching_time)
329 {
331  float pw_x = x - stateGetPositionEnu_f()->x;
333  float pw_y = y - stateGetPositionEnu_f()->y;
334 
335  if (approaching_time < 0.) {
336  // fly after the destination waypoint
337  float leg_x = x - from_x;
338  float leg_y = y - from_y;
339  float leg = sqrtf(Max(leg_x * leg_x + leg_y * leg_y, 1.));
340  float exceed_dist = approaching_time * stateGetHorizontalSpeedNorm_f(); // negative value
341  float scal_prod = (leg_x * pw_x + leg_y * pw_y) / leg;
342  return (scal_prod < exceed_dist);
343  } else {
344  // fly close enough of the waypoint or cross it
345  dist2_to_wp = pw_x * pw_x + pw_y * pw_y;
346  float min_dist = approaching_time * stateGetHorizontalSpeedNorm_f();
347  if (dist2_to_wp < min_dist * min_dist) {
348  return true;
349  }
350  float scal_prod = (x - from_x) * pw_x + (y - from_y) * pw_y;
351  return (scal_prod < 0.);
352  }
353 }
354 
355 
359 //static inline void fly_to_xy(float x, float y) {
360 void fly_to_xy(float x, float y)
361 {
362  struct EnuCoor_f *pos = stateGetPositionEnu_f();
363  desired_x = x;
364  desired_y = y;
365  if (nav_mode == NAV_MODE_COURSE) {
366  h_ctl_course_setpoint = atan2f(x - pos->x, y - pos->y);
367  if (h_ctl_course_setpoint < 0.) {
368  h_ctl_course_setpoint += 2 * M_PI;
369  }
371  } else {
372  float diff = atan2f(x - pos->x, y - pos->y) - stateGetHorizontalSpeedDir_f();
373  NormRadAngle(diff);
374  BoundAbs(diff, M_PI / 2.);
375  float s = sinf(diff);
376  float speed = stateGetHorizontalSpeedNorm_f();
377  h_ctl_roll_setpoint = atanf(2 * speed * speed * s * h_ctl_course_pgain / (CARROT * NOMINAL_AIRSPEED * 9.81));
380  }
381 }
382 
386 void nav_route_xy(float last_wp_x, float last_wp_y, float wp_x, float wp_y)
387 {
388  float leg_x = wp_x - last_wp_x;
389  float leg_y = wp_y - last_wp_y;
390  float leg2 = Max(leg_x * leg_x + leg_y * leg_y, 1.);
391  nav_leg_progress = ((stateGetPositionEnu_f()->x - last_wp_x) * leg_x + (stateGetPositionEnu_f()->y - last_wp_y) *
392  leg_y) / leg2;
393  nav_leg_length = sqrtf(leg2);
394 
396  float carrot = CARROT * NOMINAL_AIRSPEED;
397 
398  nav_carrot_leg_progress = nav_leg_progress + Max(carrot / nav_leg_length, 0.);
399  nav_in_segment = true;
400  nav_segment_x_1 = last_wp_x;
401  nav_segment_y_1 = last_wp_y;
402  nav_segment_x_2 = wp_x;
403  nav_segment_y_2 = wp_y;
404  horizontal_mode = HORIZONTAL_MODE_ROUTE;
405 
406  fly_to_xy(last_wp_x + nav_carrot_leg_progress * leg_x + nav_shift * leg_y / nav_leg_length,
407  last_wp_y + nav_carrot_leg_progress * leg_y - nav_shift * leg_x / nav_leg_length);
408 }
409 
411 
412 #ifndef FAILSAFE_HOME_RADIUS
413 #define FAILSAFE_HOME_RADIUS DEFAULT_CIRCLE_RADIUS
414 #endif
415 
416 static void nav_set_altitude(void)
417 {
418  static float last_nav_altitude;
419  if (fabs(nav_altitude - last_nav_altitude) > 1.) {
420  flight_altitude = nav_altitude;
421  last_nav_altitude = nav_altitude;
422  }
424 }
425 
427 void nav_home(void)
428 {
431  nav_pitch = 0.;
433  nav_altitude = ground_alt + HOME_MODE_HEIGHT;
437 }
438 
444 {
445  nav_survey_active = false;
446 
448  dist2_to_wp = 0.;
449 
450  auto_nav(); /* From flight_plan.h */
451 
452  h_ctl_course_pre_bank = nav_in_circle ? circle_bank : 0;
453 
454 #ifdef AGR_CLIMB
457  }
458 #endif
459 
461 }
462 
466 #if PERIODIC_TELEMETRY
468 
469 static void send_nav_ref(struct transport_tx *trans, struct link_device *dev)
470 {
471  pprz_msg_send_NAVIGATION_REF(trans, dev, AC_ID,
473 }
474 
475 static void send_nav(struct transport_tx *trans, struct link_device *dev)
476 {
477  SEND_NAVIGATION(trans, dev);
478 }
479 
480 static void send_wp_moved(struct transport_tx *trans, struct link_device *dev)
481 {
482  static uint8_t i;
483  i++;
484  if (i >= nb_waypoint) { i = 0; }
485  DownlinkSendWp(trans, dev, i);
486 }
487 
489 {
490  DownlinkSendWp(&(DefaultChannel).trans_tx, &(DefaultDevice).device, _wp);
491  return false;
492 }
493 
494 
495 static void send_circle(struct transport_tx *trans, struct link_device *dev)
496 {
497  if (nav_in_circle) {
498  pprz_msg_send_CIRCLE(trans, dev, AC_ID,
499  &nav_circle_x, &nav_circle_y, &nav_circle_radius);
500  }
501 }
502 
503 static void send_segment(struct transport_tx *trans, struct link_device *dev)
504 {
505  if (nav_in_segment) {
506  pprz_msg_send_SEGMENT(trans, dev, AC_ID,
507  &nav_segment_x_1, &nav_segment_y_1, &nav_segment_x_2, &nav_segment_y_2);
508  }
509 }
510 
511 static void send_survey(struct transport_tx *trans, struct link_device *dev)
512 {
513  if (nav_survey_active) {
514  pprz_msg_send_SURVEY(trans, dev, AC_ID,
515  &nav_survey_east, &nav_survey_north, &nav_survey_west, &nav_survey_south);
516  }
517 }
518 #endif
519 
523 void nav_init(void)
524 {
525  nav_block = 0;
526  nav_stage = 0;
527  ground_alt = GROUND_ALT;
528  nav_glide_pitch_trim = NAV_GLIDE_PITCH_TRIM;
529  nav_radius = DEFAULT_CIRCLE_RADIUS;
530  nav_survey_shift = 2 * DEFAULT_CIRCLE_RADIUS;
531  nav_mode = NAV_MODE_COURSE;
532 
533  fp_pitch = 0.f;
534  fp_throttle = 0.f;
535  fp_climb = 0.f;
536 
537 #ifdef NAV_GROUND_SPEED_PGAIN
538  nav_ground_speed_pgain = ABS(NAV_GROUND_SPEED_PGAIN);
539  nav_ground_speed_setpoint = NOMINAL_AIRSPEED;
540 #endif
541 
542 #if PERIODIC_TELEMETRY
543  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_NAVIGATION_REF, send_nav_ref);
544  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_NAVIGATION, send_nav);
549 #endif
550 }
551 
558 void nav_without_gps(void)
559 {
562 
563 #ifdef SECTION_FAILSAFE
564  h_ctl_roll_setpoint = FAILSAFE_DEFAULT_ROLL;
565  nav_pitch = FAILSAFE_DEFAULT_PITCH;
566  nav_throttle_setpoint = TRIM_UPPRZ((FAILSAFE_DEFAULT_THROTTLE) * MAX_PPRZ);
567 #else
569  nav_pitch = 0;
570  nav_throttle_setpoint = TRIM_UPPRZ((V_CTL_AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE) * MAX_PPRZ);
571 #endif
572 }
573 
574 
575 /**************** 8 Navigation **********************************************/
576 
577 
578 enum eight_status { R1T, RT2, C2, R2T, RT1, C1 };
579 
581 void nav_eight_init(void)
582 {
583  eight_status = C1;
584 }
585 
595 {
596  float aradius = fabs(radius);
597  float alt = waypoints[target].a;
598  waypoints[c1].a = alt;
599 
600  float target_c1_x = waypoints[c1].x - waypoints[target].x;
601  float target_c1_y = waypoints[c1].y - waypoints[target].y;
602  float d = sqrtf(target_c1_x * target_c1_x + target_c1_y * target_c1_y);
603  d = Max(d, 1.); /* To prevent a division by zero */
604 
605  /* Unit vector from target to c1 */
606  float u_x = target_c1_x / d;
607  float u_y = target_c1_y / d;
608 
609  /* Move [c1] closer if needed */
610  if (d > 2 * aradius) {
611  d = 2 * aradius;
612  waypoints[c1].x = waypoints[target].x + d * u_x;
613  waypoints[c1].y = waypoints[target].y + d * u_y;
614  }
615 
616  /* The other center */
617  struct point c2 = {
618  waypoints[target].x - d * u_x,
619  waypoints[target].y - d * u_y,
620  alt
621  };
622 
623  struct point c1_in = {
624  waypoints[c1].x + radius * -u_y,
625  waypoints[c1].y + radius * u_x,
626  alt
627  };
628  struct point c1_out = {
629  waypoints[c1].x - radius * -u_y,
630  waypoints[c1].y - radius * u_x,
631  alt
632  };
633 
634  struct point c2_in = {
635  c2.x + radius * -u_y,
636  c2.y + radius * u_x,
637  alt
638  };
639  struct point c2_out = {
640  c2.x - radius * -u_y,
641  c2.y - radius * u_x,
642  alt
643  };
644 
645  float qdr_out = M_PI - atan2f(u_y, u_x);
646  if (radius < 0) {
647  qdr_out += M_PI;
648  }
649 
650  switch (eight_status) {
651  case C1 :
652  NavCircleWaypoint(c1, radius);
653  if (NavQdrCloseTo(DegOfRad(qdr_out) - 10)) {
654  eight_status = R1T;
655  InitStage();
656  }
657  return;
658 
659  case R1T:
660  nav_route_xy(c1_out.x, c1_out.y, c2_in.x, c2_in.y);
661  if (nav_approaching_xy(waypoints[target].x, waypoints[target].y, c1_out.x, c1_out.y, 0)) {
662  eight_status = RT2;
663  InitStage();
664  }
665  return;
666 
667  case RT2:
668  nav_route_xy(c1_out.x, c1_out.y, c2_in.x, c2_in.y);
669  if (nav_approaching_xy(c2_in.x, c2_in.y, c1_out.x, c1_out.y, CARROT)) {
670  eight_status = C2;
671  InitStage();
672  }
673  return;
674 
675  case C2 :
676  nav_circle_XY(c2.x, c2.y, -radius);
677  if (NavQdrCloseTo(DegOfRad(qdr_out) + 10)) {
678  eight_status = R2T;
679  InitStage();
680  }
681  return;
682 
683  case R2T:
684  nav_route_xy(c2_out.x, c2_out.y, c1_in.x, c1_in.y);
685  if (nav_approaching_xy(waypoints[target].x, waypoints[target].y, c2_out.x, c2_out.y, 0)) {
686  eight_status = RT1;
687  InitStage();
688  }
689  return;
690 
691  case RT1:
692  nav_route_xy(c2_out.x, c2_out.y, c1_in.x, c1_in.y);
693  if (nav_approaching_xy(c1_in.x, c1_in.y, c2_out.x, c2_out.y, CARROT)) {
694  eight_status = C1;
695  InitStage();
696  }
697  return;
698 
699  default:/* Should not occur !!! Doing nothing */
700  return;
701  } /* switch */
702 }
703 
704 /************** Oval Navigation **********************************************/
705 
716 
717 void nav_oval_init(void)
718 {
719  oval_status = OC2;
720  nav_oval_count = 0;
721 }
722 
723 void nav_oval(uint8_t p1, uint8_t p2, float radius)
724 {
725  radius = - radius; /* Historical error ? */
726 
727  float alt = waypoints[p1].a;
728  waypoints[p2].a = alt;
729 
730  float p2_p1_x = waypoints[p1].x - waypoints[p2].x;
731  float p2_p1_y = waypoints[p1].y - waypoints[p2].y;
732  float d = sqrtf(p2_p1_x * p2_p1_x + p2_p1_y * p2_p1_y);
733 
734  /* Unit vector from p1 to p2 */
735  float u_x = p2_p1_x / d;
736  float u_y = p2_p1_y / d;
737 
738  /* The half circle centers and the other leg */
739  struct point p1_center = { waypoints[p1].x + radius * -u_y,
740  waypoints[p1].y + radius * u_x,
741  alt
742  };
743  struct point p1_out = { waypoints[p1].x + 2 * radius * -u_y,
744  waypoints[p1].y + 2 * radius * u_x,
745  alt
746  };
747 
748  struct point p2_in = { waypoints[p2].x + 2 * radius * -u_y,
749  waypoints[p2].y + 2 * radius * u_x,
750  alt
751  };
752  struct point p2_center = { waypoints[p2].x + radius * -u_y,
753  waypoints[p2].y + radius * u_x,
754  alt
755  };
756 
757  float qdr_out_2 = M_PI - atan2f(u_y, u_x);
758  float qdr_out_1 = qdr_out_2 + M_PI;
759  if (radius < 0) {
760  qdr_out_2 += M_PI;
761  qdr_out_1 += M_PI;
762  }
763  float qdr_anticipation = (radius > 0 ? -15 : 15);
764 
765  switch (oval_status) {
766  case OC1 :
767  nav_circle_XY(p1_center.x, p1_center.y, -radius);
768  if (NavQdrCloseTo(DegOfRad(qdr_out_1) - qdr_anticipation)) {
769  oval_status = OR12;
770  InitStage();
772  }
773  return;
774 
775  case OR12:
776  nav_route_xy(p1_out.x, p1_out.y, p2_in.x, p2_in.y);
777  if (nav_approaching_xy(p2_in.x, p2_in.y, p1_out.x, p1_out.y, CARROT)) {
778  oval_status = OC2;
779  nav_oval_count++;
780  InitStage();
782  }
783  return;
784 
785  case OC2 :
786  nav_circle_XY(p2_center.x, p2_center.y, -radius);
787  if (NavQdrCloseTo(DegOfRad(qdr_out_2) - qdr_anticipation)) {
788  oval_status = OR21;
789  InitStage();
791  }
792  return;
793 
794  case OR21:
795  nav_route_xy(waypoints[p2].x, waypoints[p2].y, waypoints[p1].x, waypoints[p1].y);
796  if (nav_approaching_xy(waypoints[p1].x, waypoints[p1].y, waypoints[p2].x, waypoints[p2].y, CARROT)) {
797  oval_status = OC1;
798  InitStage();
800  }
801  return;
802 
803  default: /* Should not occur !!! Doing nothing */
804  return;
805  }
806 }
uint8_t ac_id
Definition: sim_ap.c:46
Communication between fbw and ap processes.
static float stateGetHorizontalSpeedNorm_f(void)
Get norm of horizontal ground speed (float).
Definition: state.h:923
#define WaypointAlt(_wp)
waypoint altitude in m above MSL
Definition: common_nav.h:48
float x
Definition: common_nav.h:40
float v_ctl_altitude_setpoint
in meters above MSL
Definition: energy_ctrl.c:88
static float acInfoGetGspeed(uint8_t ac_id)
Get vehicle ground speed (float).
Definition: traffic_info.h:424
float ground_alt
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:40
Periodic telemetry system header (includes downlink utility and generated code).
float h_ctl_course_setpoint
vector in East North Up coordinates Units: meters
uint8_t lateral_mode
Definition: autopilot.c:50
static float acInfoGetCourse(uint8_t ac_id)
Get vehicle course (float).
Definition: traffic_info.h:416
int16_t pprz_t
Definition: paparazzi.h:6
float alpha
Definition: textons.c:107
static float radius
Definition: chemotaxis.c:15
uint8_t nav_utm_zone0
Definition: common_nav.c:44
#define InitStage()
uint8_t nav_block
#define V_CTL_MODE_AUTO_THROTTLE
static struct EnuCoor_f * stateGetPositionEnu_f(void)
Get position in local ENU coordinates (float).
Definition: state.h:713
float v_ctl_auto_throttle_max_cruise_throttle
Definition: guidance_v.c:59
Fixed wing horizontal control.
#define V_CTL_AUTO_THROTTLE_STANDARD
float y
Definition: common_nav.h:41
static struct EnuCoor_f * acInfoGetPositionEnu_f(uint8_t ac_id)
Get position in local ENU coordinates (float).
Definition: traffic_info.h:383
#define TRIM_UPPRZ(pprz)
Definition: paparazzi.h:14
#define V_CTL_MODE_AUTO_ALT
int32_t nav_utm_north0
Definition: common_nav.c:43
#define WaypointX(_wp)
Definition: common_nav.h:45
float x
in meters
Device independent GPS code (interface)
static uint16_t c1
Definition: baro_MS5534A.c:203
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
uint8_t v_ctl_auto_throttle_submode
Definition: energy_ctrl.c:76
float v_ctl_auto_throttle_min_cruise_throttle
Definition: guidance_v.c:58
uint8_t v_ctl_mode
Definition: energy_ctrl.c:74
#define Min(x, y)
Definition: main_fbw.c:52
#define V_CTL_MODE_AUTO_CLIMB
#define Max(x, y)
Definition: main_fbw.c:53
uint16_t stage_time
In s.
#define WaypointY(_wp)
Definition: common_nav.h:46
const uint8_t nb_waypoint
Definition: common_nav.c:37
float h_ctl_roll_max_setpoint
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
uint8_t nav_stage
static float stateGetHorizontalSpeedDir_f(void)
Get dir of horizontal ground speed (float).
Definition: state.h:932
float h_ctl_course_pre_bank
float v_ctl_auto_throttle_cruise_throttle
Definition: energy_ctrl.c:103
unsigned char uint8_t
Definition: types.h:14
float h_ctl_roll_setpoint
float h_ctl_course_pgain
int32_t nav_utm_east0
Definition: common_nav.c:42
struct point waypoints[NB_WAYPOINT]
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:38
float z
in meters
static struct FloatVect2 * stateGetHorizontalWindspeed_f(void)
Get horizontal windspeed (float).
Definition: state.h:1359
#define LATERAL_MODE_ROLL
Definition: autopilot.h:76
float a
Definition: common_nav.h:42
#define MAX_PPRZ
Definition: paparazzi.h:8
struct FloatVect2 target
#define LATERAL_MODE_COURSE
Definition: autopilot.h:77
float y
in meters
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
Fixedwing autopilot modes.