Paparazzi UAS  v6.2_unstable
Paparazzi is a free software Unmanned Aircraft System.
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
36 #include "autopilot.h"
37 #include "modules/gps/gps.h"
38 
39 #include "generated/flight_plan.h"
40 
41 #if !USE_GENERATED_AUTOPILOT
42 PRINT_CONFIG_VAR(NAVIGATION_FREQUENCY)
43 #endif
44 
46 
47 float last_x, last_y;
48 
50 uint8_t last_wp __attribute__((unused));
51 
52 float rc_pitch;
54 
56 float nav_circle_radians; /* Cumulated */
57 float nav_circle_radians_no_rewind; /* Cumulated */
58 float nav_circle_trigo_qdr; /* Angle from center to mobile */
60 
61 
63 static float nav_leg_progress;
65 
67 static float nav_leg_length;
68 
69 bool nav_in_circle = false;
70 bool nav_in_segment = false;
74 float circle_bank = 0;
75 
78 
80 #ifndef NAV_GLIDE_PITCH_TRIM
81 #define NAV_GLIDE_PITCH_TRIM 0.
82 #endif
83 
84 
85 
87 
88 /* Used in nav_survey_rectangle. Defined here for downlink and uplink */
92 
94 
95 void nav_init_stage(void)
96 {
99  stage_time = 0;
100  nav_circle_radians = 0;
102  nav_in_circle = false;
103  nav_in_segment = false;
104  nav_shift = 0;
105 }
106 
107 #define MIN_DX ((int16_t)(MAX_PPRZ * 0.05))
108 
109 
111 void nav_circle_XY(float x, float y, float radius)
112 {
113  struct EnuCoor_f *pos = stateGetPositionEnu_f();
114  float last_trigo_qdr = nav_circle_trigo_qdr;
115  nav_circle_trigo_qdr = atan2f(pos->y - y, pos->x - x);
116  float sign_radius = radius > 0 ? 1 : -1;
117 
118  if (nav_in_circle) {
119  float trigo_diff = nav_circle_trigo_qdr - last_trigo_qdr;
120  NormRadAngle(trigo_diff);
121  nav_circle_radians += trigo_diff;
122  trigo_diff *= - sign_radius;
123  if (trigo_diff > 0) { // do not rewind if the change in angle is in the opposite sense than nav_radius
124  nav_circle_radians_no_rewind += trigo_diff;
125  }
126  }
127 
128  float dist2_center = DistanceSquare(pos->x, pos->y, x, y);
129  float dist_carrot = CARROT * NOMINAL_AIRSPEED;
130 
131  radius += -nav_shift;
132 
133  float abs_radius = fabs(radius);
134 
136  circle_bank =
137  (dist2_center > Square(abs_radius + dist_carrot)
138  || dist2_center < Square(abs_radius - dist_carrot)) ?
139  0 :
141 
142  float carrot_angle = dist_carrot / abs_radius;
143  carrot_angle = Min(carrot_angle, M_PI / 4);
144  carrot_angle = Max(carrot_angle, M_PI / 16);
145  float alpha_carrot = nav_circle_trigo_qdr - sign_radius * carrot_angle;
147  float radius_carrot = abs_radius;
148  if (nav_mode == NAV_MODE_COURSE) {
149  radius_carrot += (abs_radius / cosf(carrot_angle) - abs_radius);
150  }
151  fly_to_xy(x + cosf(alpha_carrot)*radius_carrot,
152  y + sinf(alpha_carrot)*radius_carrot);
153  nav_in_circle = true;
154  nav_in_segment = false;
155  nav_circle_x = x;
156  nav_circle_y = y;
157  nav_circle_radius = radius;
158 }
159 
160 
161 void nav_glide(uint8_t start_wp, uint8_t wp)
162 {
163  float start_alt = waypoints[start_wp].a;
164  float diff_alt = waypoints[wp].a - start_alt;
165  float alt = start_alt + nav_leg_progress * diff_alt;
166  float pre_climb = stateGetHorizontalSpeedNorm_f() * diff_alt / nav_leg_length;
167  NavVerticalAltitudeMode(alt, pre_climb);
168 }
169 
170 
171 #define MAX_DIST_CARROT 250.
172 #define MIN_HEIGHT_CARROT 50.
173 #define MAX_HEIGHT_CARROT 150.
174 
175 #define Goto3D(radius) { \
176  if (autopilot_get_mode() == AP_MODE_AUTO2) { \
177  int16_t yaw = radio_control_get(RADIO_YAW); \
178  if (yaw > MIN_DX || yaw < -MIN_DX) { \
179  carrot_x += FLOAT_OF_PPRZ(yaw, 0, -20.); \
180  carrot_x = Min(carrot_x, MAX_DIST_CARROT); \
181  carrot_x = Max(carrot_x, -MAX_DIST_CARROT); \
182  } \
183  int16_t pitch = radio_control_get(RADIO_PITCH); \
184  if (pitch > MIN_DX || pitch < -MIN_DX) { \
185  carrot_y += FLOAT_OF_PPRZ(pitch, 0, -20.); \
186  carrot_y = Min(carrot_y, MAX_DIST_CARROT); \
187  carrot_y = Max(carrot_y, -MAX_DIST_CARROT); \
188  } \
189  v_ctl_mode = V_CTL_MODE_AUTO_ALT; \
190  int16_t roll = radio_control_get(RADIO_ROLL); \
191  if (roll > MIN_DX || roll < -MIN_DX) { \
192  nav_altitude += FLOAT_OF_PPRZ(roll, 0, -1.0); \
193  nav_altitude = Max(nav_altitude, MIN_HEIGHT_CARROT+ground_alt); \
194  nav_altitude = Min(nav_altitude, MAX_HEIGHT_CARROT+ground_alt); \
195  } \
196  } \
197  nav_circle_XY(carrot_x, carrot_y, radius); \
198  }
199 
200 
201 
202 #ifdef NAV_GROUND_SPEED_PGAIN
203 
205 static void nav_ground_speed_loop(void)
206 {
207  if (MINIMUM_AIRSPEED < nav_ground_speed_setpoint
208  && nav_ground_speed_setpoint < MAXIMUM_AIRSPEED) {
213  } else {
214  /* Reset cruise throttle to nominal value */
215  v_ctl_auto_throttle_cruise_throttle = V_CTL_AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE;
216  }
217 }
218 #endif
219 
221 void nav_compute_baseleg(uint8_t wp_af, uint8_t wp_td, uint8_t wp_baseleg, float radius)
222 {
223  nav_radius = radius;
224 
225  float x_0 = waypoints[wp_td].x - waypoints[wp_af].x;
226  float y_0 = waypoints[wp_td].y - waypoints[wp_af].y;
227 
228  /* Unit vector from AF to TD */
229  float d = sqrtf(x_0 * x_0 + y_0 * y_0);
230  float x_1 = x_0 / d;
231  float y_1 = y_0 / d;
232 
233  waypoints[wp_baseleg].x = waypoints[wp_af].x + y_1 * nav_radius;
234  waypoints[wp_baseleg].y = waypoints[wp_af].y - x_1 * nav_radius;
235  waypoints[wp_baseleg].a = waypoints[wp_af].a;
236  baseleg_out_qdr = M_PI - atan2f(-y_1, -x_1);
237  if (nav_radius < 0) {
238  baseleg_out_qdr += M_PI;
239  }
240 }
241 
242 void nav_compute_final_from_glide(uint8_t wp_af, uint8_t wp_td, float glide)
243 {
244 
245  float x_0 = waypoints[wp_td].x - waypoints[wp_af].x;
246  float y_0 = waypoints[wp_td].y - waypoints[wp_af].y;
247  float h_0 = waypoints[wp_td].a - waypoints[wp_af].a;
248 
249  /* Unit vector from AF to TD */
250  float d = sqrtf(x_0 * x_0 + y_0 * y_0);
251  float x_1 = x_0 / d;
252  float y_1 = y_0 / d;
253 
254  waypoints[wp_af].x = waypoints[wp_td].x + x_1 * h_0 * glide;
255  waypoints[wp_af].y = waypoints[wp_td].y + y_1 * h_0 * glide;
256 }
257 
258 
259 /* For a landing UPWIND.
260  Computes Top Of Descent waypoint from Touch Down and Approach Fix
261  waypoints, using glide airspeed, glide vertical speed and wind */
262 static inline void compute_TOD(uint8_t _af, uint8_t _td, uint8_t _tod, float glide_airspeed, float glide_vspeed)
263 {
264  struct FloatVect2 *wind = stateGetHorizontalWindspeed_f();
265  float td_af_x = WaypointX(_af) - WaypointX(_td);
266  float td_af_y = WaypointY(_af) - WaypointY(_td);
267  float td_af = sqrtf(td_af_x * td_af_x + td_af_y * td_af_y);
268  float td_tod = (WaypointAlt(_af) - WaypointAlt(_td)) / glide_vspeed * (glide_airspeed - sqrtf(
269  wind->x * wind->x + wind->y * wind->y));
270  WaypointX(_tod) = WaypointX(_td) + td_af_x / td_af * td_tod;
271  WaypointY(_tod) = WaypointY(_td) + td_af_y / td_af * td_tod;
272  WaypointAlt(_tod) = WaypointAlt(_af);
273 }
274 
275 
276 #ifndef LINE_START_FUNCTION
277 #define LINE_START_FUNCTION {}
278 #endif
279 #ifndef LINE_STOP_FUNCTION
280 #define LINE_STOP_FUNCTION {}
281 #endif
282 
283 #ifdef TRAFFIC_INFO
285 
286 void nav_follow(uint8_t ac_id, float distance, float height)
287 {
288  struct EnuCoor_f *ac = acInfoGetPositionEnu_f(ac_id);
290  NavVerticalAltitudeMode(Max(ac->z + height, ground_alt + SECURITY_HEIGHT), 0.);
291  float alpha = M_PI / 2 - acInfoGetCourse(ac_id);
292  float ca = cosf(alpha), sa = sinf(alpha);
293  float x = ac->x - distance * ca;
294  float y = ac->y - distance * sa;
295  fly_to_xy(x, y);
296 #ifdef NAV_FOLLOW_PGAIN
297  float s = (stateGetPositionEnu_f()->x - x) * ca + (stateGetPositionEnu_f()->y - y) * sa;
298  nav_ground_speed_setpoint = acInfoGetGspeed(ac_id) + NAV_FOLLOW_PGAIN * s;
299  nav_ground_speed_loop();
300 #endif
301 }
302 #else
303 void nav_follow(uint8_t __attribute__((unused)) _ac_id, float __attribute__((unused)) distance,
304  float __attribute__((unused)) height) {}
305 #endif // TRAFFIC_INFO
306 
307 
308 float nav_altitude = GROUND_ALT + MIN_HEIGHT_CARROT;
311 float nav_pitch; /* Rad */
312 float fp_pitch; /* deg */
313 float fp_throttle; /* [0-1] */
314 float fp_climb; /* m/s */
315 
316 
326 bool nav_approaching_xy(float x, float y, float from_x, float from_y, float approaching_time)
327 {
329  float pw_x = x - stateGetPositionEnu_f()->x;
331  float pw_y = y - stateGetPositionEnu_f()->y;
332 
333  if (approaching_time < 0.) {
334  // fly after the destination waypoint
335  float leg_x = x - from_x;
336  float leg_y = y - from_y;
337  float leg = sqrtf(Max(leg_x * leg_x + leg_y * leg_y, 1.));
338  float exceed_dist = approaching_time * stateGetHorizontalSpeedNorm_f(); // negative value
339  float scal_prod = (leg_x * pw_x + leg_y * pw_y) / leg;
340  return (scal_prod < exceed_dist);
341  } else {
342  // fly close enough of the waypoint or cross it
343  dist2_to_wp = pw_x * pw_x + pw_y * pw_y;
344  float min_dist = approaching_time * stateGetHorizontalSpeedNorm_f();
345  if (dist2_to_wp < min_dist * min_dist) {
346  return true;
347  }
348  float scal_prod = (x - from_x) * pw_x + (y - from_y) * pw_y;
349  return (scal_prod < 0.);
350  }
351 }
352 
356 //static inline void fly_to_xy(float x, float y) {
357 void fly_to_xy(float x, float y)
358 {
359  struct EnuCoor_f *pos = stateGetPositionEnu_f();
360  desired_x = x;
361  desired_y = y;
362  if (nav_mode == NAV_MODE_COURSE) {
363  h_ctl_course_setpoint = atan2f(x - pos->x, y - pos->y);
364  if (h_ctl_course_setpoint < 0.) {
365  h_ctl_course_setpoint += 2 * M_PI;
366  }
368  } else {
369  float diff = atan2f(x - pos->x, y - pos->y) - stateGetHorizontalSpeedDir_f();
370  NormRadAngle(diff);
371  BoundAbs(diff, M_PI / 2.);
372  float s = sinf(diff);
373  float speed = stateGetHorizontalSpeedNorm_f();
374  h_ctl_roll_setpoint = atanf(2 * speed * speed * s * h_ctl_course_pgain / (CARROT * NOMINAL_AIRSPEED * 9.81));
377  }
378 }
379 
383 void nav_route_xy(float last_wp_x, float last_wp_y, float wp_x, float wp_y)
384 {
385  float leg_x = wp_x - last_wp_x;
386  float leg_y = wp_y - last_wp_y;
387  float leg2 = Max(leg_x * leg_x + leg_y * leg_y, 1.);
388  nav_leg_progress = ((stateGetPositionEnu_f()->x - last_wp_x) * leg_x + (stateGetPositionEnu_f()->y - last_wp_y) *
389  leg_y) / leg2;
390  nav_leg_length = sqrtf(leg2);
391 
393  float carrot = CARROT * NOMINAL_AIRSPEED;
394 
396  nav_in_circle = false;
397  nav_in_segment = true;
398  nav_segment_x_1 = last_wp_x;
399  nav_segment_y_1 = last_wp_y;
400  nav_segment_x_2 = wp_x;
401  nav_segment_y_2 = wp_y;
403 
404  fly_to_xy(last_wp_x + nav_carrot_leg_progress * leg_x + nav_shift * leg_y / nav_leg_length,
405  last_wp_y + nav_carrot_leg_progress * leg_y - nav_shift * leg_x / nav_leg_length);
406 }
407 
408 #include "modules/nav/common_nav.c"
409 
410 #ifndef FAILSAFE_HOME_RADIUS
411 #define FAILSAFE_HOME_RADIUS DEFAULT_CIRCLE_RADIUS
412 #endif
413 
414 static void nav_set_altitude(void)
415 {
416  static float last_nav_altitude;
417  if (fabs(nav_altitude - last_nav_altitude) > 1.) {
419  last_nav_altitude = nav_altitude;
420  }
422 }
423 
425 void nav_home(void)
426 {
429  nav_pitch = 0.;
430  if (autopilot.launch) {
432  } else {
435  }
436  nav_altitude = ground_alt + HOME_MODE_HEIGHT;
440 }
441 
447 {
448  nav_survey_active = false;
449 
451  dist2_to_wp = 0.;
452 
453  auto_nav(); /* From flight_plan.h */
454 
456 
457 #ifdef AGR_CLIMB
460  }
461 #endif
462 
464 }
465 
469 #if PERIODIC_TELEMETRY
471 
472 static void send_nav_ref(struct transport_tx *trans, struct link_device *dev)
473 {
474  pprz_msg_send_NAVIGATION_REF(trans, dev, AC_ID,
476 }
477 
478 static void send_nav(struct transport_tx *trans, struct link_device *dev)
479 {
480  SEND_NAVIGATION(trans, dev);
481 }
482 
483 static void DownlinkSendWp(struct transport_tx *trans, struct link_device *dev, uint8_t _wp)
484 {
485  float x = nav_utm_east0 + waypoints[_wp].x;
486  float y = nav_utm_north0 + waypoints[_wp].y;
487  pprz_msg_send_WP_MOVED(trans, dev, AC_ID, &_wp, &x, &y, &(waypoints[_wp].a), &nav_utm_zone0);
488 }
489 
490 static void send_wp_moved(struct transport_tx *trans, struct link_device *dev)
491 {
492  static uint8_t i;
493  i++;
494  if (i >= nb_waypoint) { i = 0; }
495  DownlinkSendWp(trans, dev, i);
496 }
497 
499 {
500  if (_wp >= nb_waypoint) return;
501  DownlinkSendWp(&(DefaultChannel).trans_tx, &(DefaultDevice).device, _wp);
502 }
503 
504 
505 static void send_circle(struct transport_tx *trans, struct link_device *dev)
506 {
507  if (nav_in_circle) {
508  pprz_msg_send_CIRCLE(trans, dev, AC_ID,
510  }
511 }
512 
513 static void send_segment(struct transport_tx *trans, struct link_device *dev)
514 {
515  if (nav_in_segment) {
516  pprz_msg_send_SEGMENT(trans, dev, AC_ID,
518  }
519 }
520 
521 static void send_survey(struct transport_tx *trans, struct link_device *dev)
522 {
523  if (nav_survey_active) {
524  pprz_msg_send_SURVEY(trans, dev, AC_ID,
526  }
527 }
528 #endif
529 
533 void nav_init(void)
534 {
535  nav_block = 0;
536  nav_stage = 0;
537  ground_alt = GROUND_ALT;
542 
543  fp_pitch = 0.f;
544  fp_throttle = 0.f;
545  fp_climb = 0.f;
546 
547 #ifdef NAV_GROUND_SPEED_PGAIN
548  nav_ground_speed_pgain = ABS(NAV_GROUND_SPEED_PGAIN);
549  nav_ground_speed_setpoint = NOMINAL_AIRSPEED;
550 #endif
551 
552 #if PERIODIC_TELEMETRY
553  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_NAVIGATION_REF, send_nav_ref);
554  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_NAVIGATION, send_nav);
559 #endif
560 
561  // generated init function
562  auto_nav_init();
563 }
564 
571 void nav_without_gps(void)
572 {
575 
576 #ifdef SECTION_FAILSAFE
577  h_ctl_roll_setpoint = FAILSAFE_DEFAULT_ROLL;
578  nav_pitch = FAILSAFE_DEFAULT_PITCH;
579  nav_throttle_setpoint = TRIM_UPPRZ((FAILSAFE_DEFAULT_THROTTLE) * MAX_PPRZ);
580 #else
582  nav_pitch = 0;
583  nav_throttle_setpoint = TRIM_UPPRZ((V_CTL_AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE) * MAX_PPRZ);
584 #endif
585 }
586 
587 void nav_parse_BLOCK(struct link_device *dev, struct transport_tx *trans, uint8_t *buf)
588 {
589  if (DL_BLOCK_ac_id(buf) != AC_ID) { return; }
590  nav_goto_block(DL_BLOCK_block_id(buf));
591  SEND_NAVIGATION(trans, dev);
592 }
593 
594 void nav_parse_MOVE_WP(struct link_device *dev, struct transport_tx *trans, uint8_t *buf)
595 {
596  if (DL_MOVE_WP_ac_id(buf) != AC_ID) { return; }
597  uint8_t wp_id = DL_MOVE_WP_wp_id(buf);
598 
599  /* Computes from (lat, long) in the referenced UTM zone */
600  struct LlaCoor_f lla;
601  lla.lat = RadOfDeg((float)(DL_MOVE_WP_lat(buf) / 1e7f));
602  lla.lon = RadOfDeg((float)(DL_MOVE_WP_lon(buf) / 1e7f));
603  lla.alt = (float)(DL_MOVE_WP_alt(buf) / 1000.f); // mm to m
604  struct UtmCoor_f utm;
605  utm.zone = nav_utm_zone0;
606  utm_of_lla_f(&utm, &lla);
607  nav_move_waypoint(wp_id, utm.east, utm.north, utm.alt);
608 
609  /* Waypoint range is limited. Computes the UTM pos back from the relative
610  coordinates */
611  utm.east = waypoints[wp_id].x + nav_utm_east0;
612  utm.north = waypoints[wp_id].y + nav_utm_north0;
613  pprz_msg_send_WP_MOVED(trans, dev, AC_ID, &wp_id, &utm.east, &utm.north, &utm.alt, &nav_utm_zone0);
614 }
615 
616 /**************** 8 Navigation **********************************************/
617 
618 
619 enum eight_status { R1T, RT2, C2, R2T, RT1, C1 };
620 
622 void nav_eight_init(void)
623 {
624  eight_status = C1;
625 }
626 
635 void nav_eight(uint8_t target, uint8_t c1, float radius)
636 {
637  float aradius = fabs(radius);
638  float alt = waypoints[target].a;
639  waypoints[c1].a = alt;
640 
641  float target_c1_x = waypoints[c1].x - waypoints[target].x;
642  float target_c1_y = waypoints[c1].y - waypoints[target].y;
643  float d = sqrtf(target_c1_x * target_c1_x + target_c1_y * target_c1_y);
644  d = Max(d, 1.); /* To prevent a division by zero */
645 
646  /* Unit vector from target to c1 */
647  float u_x = target_c1_x / d;
648  float u_y = target_c1_y / d;
649 
650  /* Move [c1] closer if needed */
651  if (d > 2 * aradius) {
652  d = 2 * aradius;
653  waypoints[c1].x = waypoints[target].x + d * u_x;
654  waypoints[c1].y = waypoints[target].y + d * u_y;
655  }
656 
657  /* The other center */
658  struct point c2 = {
659  waypoints[target].x - d * u_x,
660  waypoints[target].y - d * u_y,
661  alt
662  };
663 
664  struct point c1_in = {
665  waypoints[c1].x + radius * -u_y,
666  waypoints[c1].y + radius * u_x,
667  alt
668  };
669  struct point c1_out = {
670  waypoints[c1].x - radius * -u_y,
671  waypoints[c1].y - radius * u_x,
672  alt
673  };
674 
675  struct point c2_in = {
676  c2.x + radius * -u_y,
677  c2.y + radius * u_x,
678  alt
679  };
680  struct point c2_out = {
681  c2.x - radius * -u_y,
682  c2.y - radius * u_x,
683  alt
684  };
685 
686  float qdr_out = M_PI - atan2f(u_y, u_x);
687  if (radius < 0) {
688  qdr_out += M_PI;
689  }
690 
691  switch (eight_status) {
692  case C1 :
693  NavCircleWaypoint(c1, radius);
694  if (NavQdrCloseTo(DegOfRad(qdr_out) - 10)) {
695  eight_status = R1T;
696  InitStage();
697  }
698  return;
699 
700  case R1T:
701  nav_route_xy(c1_out.x, c1_out.y, c2_in.x, c2_in.y);
702  if (nav_approaching_xy(waypoints[target].x, waypoints[target].y, c1_out.x, c1_out.y, 0)) {
703  eight_status = RT2;
704  InitStage();
705  }
706  return;
707 
708  case RT2:
709  nav_route_xy(c1_out.x, c1_out.y, c2_in.x, c2_in.y);
710  if (nav_approaching_xy(c2_in.x, c2_in.y, c1_out.x, c1_out.y, CARROT)) {
711  eight_status = C2;
712  InitStage();
713  }
714  return;
715 
716  case C2 :
717  nav_circle_XY(c2.x, c2.y, -radius);
718  if (NavQdrCloseTo(DegOfRad(qdr_out) + 10)) {
719  eight_status = R2T;
720  InitStage();
721  }
722  return;
723 
724  case R2T:
725  nav_route_xy(c2_out.x, c2_out.y, c1_in.x, c1_in.y);
726  if (nav_approaching_xy(waypoints[target].x, waypoints[target].y, c2_out.x, c2_out.y, 0)) {
727  eight_status = RT1;
728  InitStage();
729  }
730  return;
731 
732  case RT1:
733  nav_route_xy(c2_out.x, c2_out.y, c1_in.x, c1_in.y);
734  if (nav_approaching_xy(c1_in.x, c1_in.y, c2_out.x, c2_out.y, CARROT)) {
735  eight_status = C1;
736  InitStage();
737  }
738  return;
739 
740  default:/* Should not occur !!! Doing nothing */
741  return;
742  } /* switch */
743 }
744 
745 /************** Oval Navigation **********************************************/
746 
757 
758 void nav_oval_init(void)
759 {
760  oval_status = OC2;
761  nav_oval_count = 0;
762 }
763 
764 void nav_oval(uint8_t p1, uint8_t p2, float radius)
765 {
766  radius = - radius; /* Historical error ? */
767 
768  float alt = waypoints[p1].a;
769  waypoints[p2].a = alt;
770 
771  float p2_p1_x = waypoints[p1].x - waypoints[p2].x;
772  float p2_p1_y = waypoints[p1].y - waypoints[p2].y;
773  float d = sqrtf(p2_p1_x * p2_p1_x + p2_p1_y * p2_p1_y);
774 
775  /* Unit vector from p1 to p2 */
776  float u_x = p2_p1_x / d;
777  float u_y = p2_p1_y / d;
778 
779  /* The half circle centers and the other leg */
780  struct point p1_center = { waypoints[p1].x + radius * -u_y,
781  waypoints[p1].y + radius * u_x,
782  alt
783  };
784  struct point p1_out = { waypoints[p1].x + 2 * radius * -u_y,
785  waypoints[p1].y + 2 * radius * u_x,
786  alt
787  };
788 
789  struct point p2_in = { waypoints[p2].x + 2 * radius * -u_y,
790  waypoints[p2].y + 2 * radius * u_x,
791  alt
792  };
793  struct point p2_center = { waypoints[p2].x + radius * -u_y,
794  waypoints[p2].y + radius * u_x,
795  alt
796  };
797 
798  float qdr_out_2 = M_PI - atan2f(u_y, u_x);
799  float qdr_out_1 = qdr_out_2 + M_PI;
800  if (radius < 0) {
801  qdr_out_2 += M_PI;
802  qdr_out_1 += M_PI;
803  }
804  float qdr_anticipation = (radius > 0 ? -15 : 15);
805 
806  switch (oval_status) {
807  case OC1 :
808  nav_circle_XY(p1_center.x, p1_center.y, -radius);
809  if (NavQdrCloseTo(DegOfRad(qdr_out_1) - qdr_anticipation)) {
810  oval_status = OR12;
811  InitStage();
813  }
814  return;
815 
816  case OR12:
817  nav_route_xy(p1_out.x, p1_out.y, p2_in.x, p2_in.y);
818  if (nav_approaching_xy(p2_in.x, p2_in.y, p1_out.x, p1_out.y, CARROT)) {
819  oval_status = OC2;
820  nav_oval_count++;
821  InitStage();
823  }
824  return;
825 
826  case OC2 :
827  nav_circle_XY(p2_center.x, p2_center.y, -radius);
828  if (NavQdrCloseTo(DegOfRad(qdr_out_2) - qdr_anticipation)) {
829  oval_status = OR21;
830  InitStage();
832  }
833  return;
834 
835  case OR21:
836  nav_route_xy(waypoints[p2].x, waypoints[p2].y, waypoints[p1].x, waypoints[p1].y);
837  if (nav_approaching_xy(waypoints[p1].x, waypoints[p1].y, waypoints[p2].x, waypoints[p2].y, CARROT)) {
838  oval_status = OC1;
839  InitStage();
841  }
842  return;
843 
844  default: /* Should not occur !!! Doing nothing */
845  return;
846  }
847 }
nav_climb
float nav_climb
Definition: nav.c:59
point::a
float a
Definition: common_nav.h:42
carrot_y
float carrot_y
Definition: nav.c:53
send_circle
static void send_circle(struct transport_tx *trans, struct link_device *dev)
Definition: nav.c:505
traffic_info.h
MAX_PPRZ
#define MAX_PPRZ
Definition: paparazzi.h:8
nav_follow
void nav_follow(uint8_t _ac_id, float distance, float height)
Definition: nav.c:303
LINE_START_FUNCTION
#define LINE_START_FUNCTION
Definition: nav.c:277
acInfoGetGspeed
static float acInfoGetGspeed(uint8_t ac_id)
Get vehicle ground speed (float).
Definition: traffic_info.h:424
nav_circle_trigo_qdr
float nav_circle_trigo_qdr
Definition: nav.c:58
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
stateGetHorizontalSpeedDir_f
static float stateGetHorizontalSpeedDir_f(void)
Get dir of horizontal ground speed (float).
Definition: state.h:944
nav_in_circle
bool nav_in_circle
Definition: nav.c:69
v_ctl_mode
uint8_t v_ctl_mode
Definition: energy_ctrl.c:74
V_CTL_MODE_AUTO_ALT
#define V_CTL_MODE_AUTO_ALT
Definition: guidance_common.h:39
oval_status
enum oval_status oval_status
Definition: nav.c:45
InitStage
#define InitStage()
Definition: common_flight_plan.h:44
nav_circle_x
float nav_circle_x
Definition: nav.c:71
nav_leg_progress
static float nav_leg_progress
Status on the current leg (percentage, 0.
Definition: nav.c:63
nav_mode
int nav_mode
Definition: nav.c:93
baseleg_out_qdr
float baseleg_out_qdr
Definition: nav.c:220
UtmCoor_f::north
float north
in meters
Definition: pprz_geodetic_float.h:82
nav_circle_y
float nav_circle_y
Definition: nav.c:71
nav_set_altitude
static void nav_set_altitude(void)
Definition: nav.c:414
nav_utm_east0
int32_t nav_utm_east0
Definition: common_nav.c:43
nav_ground_speed_pgain
float nav_ground_speed_pgain
Definition: nav.c:86
send_nav_ref
static void send_nav_ref(struct transport_tx *trans, struct link_device *dev)
Periodic telemetry.
Definition: nav.c:472
fp_climb
float fp_climb
Definition: nav.c:314
dist2_to_home
float dist2_to_home
squared distance to home waypoint
Definition: navigation.c:91
compute_dist2_to_home
void compute_dist2_to_home(void)
Computes squared distance to the HOME waypoint potentially sets too_far_from_home.
Definition: navigation.c:483
WaypointY
#define WaypointY(_wp)
Definition: common_nav.h:46
v_ctl_auto_throttle_max_cruise_throttle
float v_ctl_auto_throttle_max_cruise_throttle
Definition: guidance_v.c:59
LATERAL_MODE_ROLL
#define LATERAL_MODE_ROLL
Definition: autopilot_firmware.h:38
stateGetPositionEnu_f
static struct EnuCoor_f * stateGetPositionEnu_f(void)
Get position in local ENU coordinates (float).
Definition: state.h:719
s
static uint32_t s
Definition: light_scheduler.c:33
WaypointX
#define WaypointX(_wp)
Definition: common_nav.h:45
LlaCoor_f::lon
float lon
in radians
Definition: pprz_geodetic_float.h:56
UtmCoor_f::east
float east
in meters
Definition: pprz_geodetic_float.h:83
nav_circle_XY
void nav_circle_XY(float x, float y, float radius)
Navigates around (x, y).
Definition: nav.c:111
acInfoGetCourse
static float acInfoGetCourse(uint8_t ac_id)
Get vehicle course (float).
Definition: traffic_info.h:416
h_ctl_roll_max_setpoint
float h_ctl_roll_max_setpoint
Definition: stabilization_adaptive.c:85
Square
#define Square(_x)
Definition: nav.h:53
nav_survey_west
float nav_survey_west
Definition: nav.c:90
NAV_GLIDE_PITCH_TRIM
#define NAV_GLIDE_PITCH_TRIM
Definition: nav.c:81
nav_oval_init
void nav_oval_init(void)
Definition: nav.c:758
carrot_x
float carrot_x
Definition: nav.c:53
NavQdrCloseTo
#define NavQdrCloseTo(x)
True if x (in degrees) is close to the current QDR (less than 10 degrees)
Definition: nav.h:164
alpha
float alpha
Definition: textons.c:107
nav_stage
uint8_t nav_stage
Definition: common_flight_plan.c:35
stabilization_attitude.h
OR12
@ OR12
Definition: nav.h:60
nav_shift
float nav_shift
Definition: nav.c:59
nav_circle_radians
float nav_circle_radians
Status on the current circle.
Definition: nav.c:56
nav_init
void nav_init(void)
Navigation Initialisation.
Definition: nav.c:533
nav_home
void nav_home(void)
Home mode navigation (circle around HOME)
Definition: nav.c:425
point
Definition: common_nav.h:39
waypoints
struct point waypoints[NB_WAYPOINT]
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:39
v_ctl_auto_throttle_min_cruise_throttle
float v_ctl_auto_throttle_min_cruise_throttle
Definition: guidance_v.c:58
nav_utm_north0
int32_t nav_utm_north0
Definition: common_nav.c:44
nav_glide
void nav_glide(uint8_t start_wp, uint8_t wp)
Definition: nav.c:161
h_ctl_course_pgain
float h_ctl_course_pgain
Definition: stabilization_adaptive.c:83
nav_ground_speed_setpoint
float nav_ground_speed_setpoint
Definition: nav.c:86
h_ctl_roll_setpoint
float h_ctl_roll_setpoint
Definition: stabilization_adaptive.c:157
EnuCoor_f::y
float y
in meters
Definition: pprz_geodetic_float.h:74
v_ctl_auto_throttle_cruise_throttle
float v_ctl_auto_throttle_cruise_throttle
Definition: energy_ctrl.c:103
EnuCoor_f::z
float z
in meters
Definition: pprz_geodetic_float.h:75
NavVerticalAutoThrottleMode
#define NavVerticalAutoThrottleMode(_pitch)
Set the climb control to auto-throttle with the specified pitch pre-command.
Definition: nav.h:180
nav_circle_radius
float nav_circle_radius
Definition: nav.c:71
rc_pitch
float rc_pitch
Definition: nav.c:52
NavCircleWaypoint
#define NavCircleWaypoint(wp, radius)
Definition: nav.h:147
utm_of_lla_f
void utm_of_lla_f(struct UtmCoor_f *utm, struct LlaCoor_f *lla)
Definition: pprz_geodetic_float.c:308
acInfoGetPositionEnu_f
static struct EnuCoor_f * acInfoGetPositionEnu_f(uint8_t ac_id)
Get position in local ENU coordinates (float).
Definition: traffic_info.h:383
CARROT
#define CARROT
default approaching_time for a wp
Definition: navigation.h:40
nav_survey_east
float nav_survey_east
Definition: nav.c:90
flight_altitude
float flight_altitude
Dynamically adjustable, reset to nav_altitude when it is changing.
Definition: nav.c:77
FloatVect2
Definition: pprz_algebra_float.h:49
telemetry.h
nav_pitch
float nav_pitch
Definition: nav.c:311
std.h
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
OC1
@ OC1
Definition: nav.h:60
last_y
float last_y
Definition: nav.c:47
pprz_t
int16_t pprz_t
Definition: paparazzi.h:6
gps.h
Device independent GPS code (interface)
autopilot
struct pprz_autopilot autopilot
Global autopilot structure.
Definition: autopilot.c:48
desired_x
float desired_x
Definition: nav.c:309
nav_leg_length
static float nav_leg_length
length of the current leg (m)
Definition: nav.c:67
lateral_mode
uint8_t lateral_mode
Definition: autopilot_firmware.c:38
UtmCoor_f::alt
float alt
in meters (above WGS84 reference ellipsoid or above MSL)
Definition: pprz_geodetic_float.h:84
DEFAULT_CIRCLE_RADIUS
#define DEFAULT_CIRCLE_RADIUS
default nav_circle_radius in meters
Definition: navigation.c:56
nb_waypoint
const uint8_t nb_waypoint
Definition: common_nav.c:38
UtmCoor_f::zone
uint8_t zone
UTM zone number.
Definition: pprz_geodetic_float.h:85
desired_y
float desired_y
Definition: nav.c:309
nav_eight
void nav_eight(uint8_t target, uint8_t c1, float radius)
Navigation along a figure 8.
Definition: nav.c:635
last_wp
uint8_t last_wp
Index of last waypoint.
Definition: nav.c:50
OR21
@ OR21
Definition: nav.h:60
nav_compute_final_from_glide
void nav_compute_final_from_glide(uint8_t wp_af, uint8_t wp_td, float glide)
Definition: nav.c:242
nav_init_stage
void nav_init_stage(void)
needs to be implemented by fixedwing and rotorcraft seperately
Definition: nav.c:95
dev
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
DownlinkSendWpNr
void DownlinkSendWpNr(uint8_t _wp)
Definition: nav.c:498
C2
@ C2
Definition: nav.c:619
TRIM_UPPRZ
#define TRIM_UPPRZ(pprz)
Definition: paparazzi.h:14
nav_block
uint8_t nav_block
Definition: common_flight_plan.c:35
nav_in_segment
bool nav_in_segment
Definition: nav.c:70
NAV_MODE_COURSE
#define NAV_MODE_COURSE
Definition: nav.h:88
RT1
@ RT1
Definition: nav.c:619
HORIZONTAL_MODE_CIRCLE
#define HORIZONTAL_MODE_CIRCLE
Definition: nav.h:94
nav_compute_baseleg
void nav_compute_baseleg(uint8_t wp_af, uint8_t wp_td, uint8_t wp_baseleg, float radius)
Definition: nav.c:221
nav_segment_x_2
float nav_segment_x_2
Definition: nav.c:72
NAVIGATION_FREQUENCY
#define NAVIGATION_FREQUENCY
Default fixedwing navigation frequency.
Definition: nav.h:49
nav_course
float nav_course
Definition: nav.c:59
RT2
@ RT2
Definition: nav.c:619
NAV_GRAVITY
#define NAV_GRAVITY
Definition: nav.h:52
register_periodic_telemetry
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:51
point::x
float x
Definition: common_nav.h:40
EnuCoor_f
vector in East North Up coordinates Units: meters
Definition: pprz_geodetic_float.h:72
FAILSAFE_HOME_RADIUS
#define FAILSAFE_HOME_RADIUS
Definition: nav.c:411
nav_move_waypoint
void nav_move_waypoint(uint8_t wp_id, float ux, float uy, float alt)
Move a waypoint to given UTM coordinates.
Definition: common_nav.c:164
nav_oval
void nav_oval(uint8_t p1, uint8_t p2, float radius)
Definition: nav.c:764
V_CTL_AUTO_THROTTLE_STANDARD
#define V_CTL_AUTO_THROTTLE_STANDARD
Definition: guidance_common.h:51
nav_parse_MOVE_WP
void nav_parse_MOVE_WP(struct link_device *dev, struct transport_tx *trans, uint8_t *buf)
Definition: nav.c:594
autopilot.h
common_nav.c
stage_time
uint16_t stage_time
In s.
Definition: common_flight_plan.c:33
oval_status
oval_status
Definition: nav.h:60
c1
static uint16_t c1
Definition: baro_MS5534A.c:203
nav_glide_pitch_trim
float nav_glide_pitch_trim
Definition: nav.c:79
LlaCoor_f::alt
float alt
in meters (normally above WGS84 reference ellipsoid)
Definition: pprz_geodetic_float.h:57
DownlinkSendWp
static void DownlinkSendWp(struct transport_tx *trans, struct link_device *dev, uint8_t _wp)
Definition: nav.c:483
FloatVect2::y
float y
Definition: pprz_algebra_float.h:51
V_CTL_MODE_AUTO_THROTTLE
#define V_CTL_MODE_AUTO_THROTTLE
Definition: guidance_common.h:37
stateGetHorizontalWindspeed_f
static struct FloatVect2 * stateGetHorizontalWindspeed_f(void)
Get horizontal windspeed (float).
Definition: state.h:1377
h_ctl_course_pre_bank
float h_ctl_course_pre_bank
Definition: stabilization_adaptive.c:81
nav_parse_BLOCK
void nav_parse_BLOCK(struct link_device *dev, struct transport_tx *trans, uint8_t *buf)
Definition: nav.c:587
circle_bank
float circle_bank
Definition: nav.c:74
v_ctl_throttle_setpoint
pprz_t v_ctl_throttle_setpoint
Definition: energy_ctrl.c:131
nav_radius
float nav_radius
Definition: nav.c:59
nav.h
R1T
@ R1T
Definition: nav.c:619
WaypointAlt
#define WaypointAlt(_wp)
waypoint altitude in m above MSL
Definition: common_nav.h:48
nav_survey_north
float nav_survey_north
Definition: nav.c:90
stateGetHorizontalSpeedNorm_f
static float stateGetHorizontalSpeedNorm_f(void)
Get norm of horizontal ground speed (float).
Definition: state.h:935
c2
static uint16_t c2
Definition: baro_MS5534A.c:203
DistanceSquare
#define DistanceSquare(p1_x, p1_y, p2_x, p2_y)
Definition: nav.h:54
ground_alt
float ground_alt
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:41
nav_circle_radians_no_rewind
float nav_circle_radians_no_rewind
Definition: nav.c:57
nav_survey_shift
float nav_survey_shift
Definition: nav.c:89
SEND_NAVIGATION
#define SEND_NAVIGATION(_trans, _dev)
Definition: nav.h:249
v_ctl_altitude_setpoint
float v_ctl_altitude_setpoint
in meters above MSL
Definition: energy_ctrl.c:88
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
send_nav
static void send_nav(struct transport_tx *trans, struct link_device *dev)
Definition: nav.c:478
MIN_HEIGHT_CARROT
#define MIN_HEIGHT_CARROT
Definition: nav.c:172
nav_survey_active
bool nav_survey_active
Definition: nav.c:91
UtmCoor_f
position in UTM coordinates Units: meters
Definition: pprz_geodetic_float.h:81
LATERAL_MODE_COURSE
#define LATERAL_MODE_COURSE
Definition: autopilot_firmware.h:39
nav_segment_y_1
float nav_segment_y_1
Definition: nav.c:72
eight_status
eight_status
Definition: nav.c:619
nav_utm_zone0
uint8_t nav_utm_zone0
Definition: common_nav.c:45
nav_segment_x_1
float nav_segment_x_1
Definition: nav.c:72
unit
static unit_t unit
Definition: nav.c:31
point::y
float y
Definition: common_nav.h:41
send_survey
static void send_survey(struct transport_tx *trans, struct link_device *dev)
Definition: nav.c:521
nav_periodic_task
void nav_periodic_task(void)
Navigation main: call to the code generated from the XML flight plan.
Definition: nav.c:446
LlaCoor_f::lat
float lat
in radians
Definition: pprz_geodetic_float.h:55
LINE_STOP_FUNCTION
#define LINE_STOP_FUNCTION
Definition: nav.c:280
send_wp_moved
static void send_wp_moved(struct transport_tx *trans, struct link_device *dev)
Definition: nav.c:490
FloatVect2::x
float x
Definition: pprz_algebra_float.h:50
EnuCoor_f::x
float x
in meters
Definition: pprz_geodetic_float.h:73
nav_altitude
float nav_altitude
Definition: nav.c:308
ac_id
uint8_t ac_id
Definition: sim_ap.c:47
nav_carrot_leg_progress
static float nav_carrot_leg_progress
Definition: nav.c:64
nav_throttle_setpoint
pprz_t nav_throttle_setpoint
Definition: nav.c:310
HORIZONTAL_MODE_ROUTE
#define HORIZONTAL_MODE_ROUTE
Definition: nav.h:93
compute_TOD
static void compute_TOD(uint8_t _af, uint8_t _td, uint8_t _tod, float glide_airspeed, float glide_vspeed)
Definition: nav.c:262
V_CTL_MODE_AUTO_CLIMB
#define V_CTL_MODE_AUTO_CLIMB
Definition: guidance_common.h:38
v_ctl_auto_throttle_submode
uint8_t v_ctl_auto_throttle_submode
Definition: energy_ctrl.c:76
nav_oval_count
uint8_t nav_oval_count
Navigation along a figure O.
Definition: nav.c:756
R2T
@ R2T
Definition: nav.c:619
dist2_to_wp
float dist2_to_wp
squared distance to next waypoint
Definition: navigation.c:94
target
struct FloatVect2 target
Definition: obstacle_avoidance.c:78
horizontal_mode
uint8_t horizontal_mode
Definition: nav.c:73
DefaultPeriodic
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
pprz_autopilot::launch
bool launch
request launch
Definition: autopilot.h:71
nav_goto_block
void nav_goto_block(uint8_t b)
Definition: common_flight_plan.c:51
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
nav_survey_south
float nav_survey_south
Definition: nav.c:90
nav_eight_init
void nav_eight_init(void)
Definition: nav.c:622
nav_segment_y_2
float nav_segment_y_2
Definition: nav.c:72
nav_without_gps
void nav_without_gps(void)
Failsafe navigation without position estimation.
Definition: nav.c:571
fp_throttle
float fp_throttle
Definition: nav.c:313
fp_pitch
float fp_pitch
Definition: nav.c:312
send_segment
static void send_segment(struct transport_tx *trans, struct link_device *dev)
Definition: nav.c:513
C1
@ C1
Definition: nav.c:619
LlaCoor_f
vector in Latitude, Longitude and Altitude
Definition: pprz_geodetic_float.h:54
Min
#define Min(x, y)
Definition: esc_dshot.c:85
fly_to_xy
void fly_to_xy(float x, float y)
Computes desired_x, desired_y and desired_course.
Definition: nav.c:357
OC2
@ OC2
Definition: nav.h:60
h_ctl_course_setpoint
float h_ctl_course_setpoint
Definition: stabilization_adaptive.c:80