Paparazzi UAS  v5.17_devel-24-g2ae834f
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
navigation.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2008-2011 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 
28 #ifndef NAVIGATION_H
29 #define NAVIGATION_H
30 
31 #include "std.h"
32 #include "math/pprz_geodetic_int.h"
33 
36 #include "autopilot.h"
37 
39 #ifndef CARROT
40 #define CARROT 0
41 #endif
42 
43 #define NAV_FREQ 16
44 
45 extern struct EnuCoor_i navigation_target;
46 extern struct EnuCoor_i navigation_carrot;
47 
48 extern uint8_t last_wp __attribute__((unused));
49 
51 
53 #define HORIZONTAL_MODE_WAYPOINT 0
54 #define HORIZONTAL_MODE_ROUTE 1
55 #define HORIZONTAL_MODE_CIRCLE 2
56 #define HORIZONTAL_MODE_ATTITUDE 3
57 #define HORIZONTAL_MODE_MANUAL 4
58 extern int32_t nav_roll, nav_pitch;
59 extern int32_t nav_heading;
61 extern float nav_radius;
63 
66 
67 extern bool nav_survey_active;
68 
69 extern uint8_t vertical_mode;
70 extern uint32_t nav_throttle;
72 extern float flight_altitude;
73 #define VERTICAL_MODE_MANUAL 0
74 #define VERTICAL_MODE_CLIMB 1
75 #define VERTICAL_MODE_ALT 2
76 
77 extern float dist2_to_home;
78 extern bool too_far_from_home;
79 extern float failsafe_mode_dist2;
80 
81 extern float dist2_to_wp;
82 
83 extern bool exception_flag[10];
84 
85 
86 /*****************************************************************
87  * macros to ensure compatibility between fixedwing and rotorcraft
88  *****************************************************************/
89 
91 #define GetPosX() (stateGetPositionEnu_f()->x)
92 #define GetPosY() (stateGetPositionEnu_f()->y)
94 #define GetPosAlt() (stateGetPositionEnu_f()->z+state.ned_origin_f.hmsl)
96 
102 #define GetAltRef() (state.ned_origin_f.hmsl)
103 
104 
106 #define NormCourse(x) { \
107  while (x < 0) x += 360; \
108  while (x >= 360) x -= 360; \
109  }
110 
111 extern void nav_init(void);
112 extern void nav_run(void);
113 
114 extern void set_exception_flag(uint8_t flag_num);
115 
116 extern float nav_max_speed;
117 extern bool force_forward;
118 extern struct FloatVect3 nav_get_speed_sp_from_go(struct EnuCoor_i target, float pos_gain);
119 extern struct FloatVect3 nav_get_speed_setpoint(float pos_gain);
120 extern struct FloatVect3 nav_get_speed_sp_from_line(struct FloatVect2 line_v, struct FloatVect2 to_end_v, struct EnuCoor_i target, float pos_gain);
121 
122 extern float get_dist2_to_waypoint(uint8_t wp_id);
123 extern float get_dist2_to_point(struct EnuCoor_i *p);
124 extern void compute_dist2_to_home(void);
125 extern void nav_home(void);
126 extern void nav_set_manual(int32_t roll, int32_t pitch, int32_t yaw);
127 
128 extern void nav_reset_reference(void) __attribute__((unused));
129 extern void nav_reset_alt(void) __attribute__((unused));
130 extern void nav_periodic_task(void);
131 
132 extern bool nav_is_in_flight(void);
133 
134 extern void nav_set_heading_rad(float rad);
135 extern void nav_set_heading_deg(float deg);
136 extern void nav_set_heading_towards(float x, float y);
138 extern void nav_set_heading_towards_target(void);
139 extern void nav_set_heading_current(void);
140 extern void nav_set_failsafe(void);
141 
142 /* ground detection */
143 extern bool nav_detect_ground(void);
144 #define NavStartDetectGround() ({ autopilot.detect_ground_once = true; false; })
145 #define NavDetectGround() nav_detect_ground()
146 
147 /* switching motors on/off */
148 static inline void NavKillThrottle(void)
149 {
151 }
152 static inline void NavResurrect(void)
153 {
155 }
156 
157 
158 #define NavSetManual nav_set_manual
159 #define NavSetFailsafe nav_set_failsafe
160 
161 
162 #define NavSetGroundReferenceHere nav_reset_reference
163 #define NavSetAltitudeReferenceHere nav_reset_alt
164 
165 #define NavSetWaypointHere waypoint_set_here_2d
166 #define NavCopyWaypoint waypoint_copy
167 #define NavCopyWaypointPositionOnly waypoint_position_copy
168 
169 
171 bool nav_approaching_from(struct EnuCoor_i *wp, struct EnuCoor_i *from, int16_t approaching_time);
172 #define NavApproaching(wp, time) nav_approaching_from(&waypoints[wp].enu_i, NULL, time)
173 #define NavApproachingFrom(wp, from, time) nav_approaching_from(&waypoints[wp].enu_i, &waypoints[from].enu_i, time)
174 
176 bool nav_check_wp_time(struct EnuCoor_i *wp, uint16_t stay_time);
177 #define NavCheckWaypointTime(wp, time) nav_check_wp_time(&waypoints[wp].enu_i, time)
178 
179 
180 extern void navigation_update_wp_from_speed(uint8_t wp, struct Int16Vect3 speed_sp, int16_t heading_rate_sp);
181 
182 /* should we really keep this one ??
183  * maybe better to use the `goto` flight plan primitive and
184  * add a `pre_call` or `call_once` to set the heading?
185  */
186 static inline void NavGotoWaypointHeading(uint8_t wp)
187 {
188  vertical_mode = VERTICAL_MODE_ALT;
189  horizontal_mode = HORIZONTAL_MODE_WAYPOINT;
191  dist2_to_wp = get_dist2_to_waypoint(wp);
193 }
194 
195 
196 
197 /***********************************************************
198  * macros used by flight plan to set different modes
199  **********************************************************/
200 
203 #define NavVerticalAutoThrottleMode(_pitch) { \
204  nav_pitch = ANGLE_BFP_OF_REAL(_pitch); \
205  }
206 
209 #define NavVerticalAutoPitchMode(_throttle) {}
210 
213 #define NavVerticalAltitudeMode(_alt, _pre_climb) { \
214  vertical_mode = VERTICAL_MODE_ALT; \
215  nav_altitude = POS_BFP_OF_REAL(_alt); \
216  }
217 
219 #define NavVerticalClimbMode(_climb) { \
220  vertical_mode = VERTICAL_MODE_CLIMB; \
221  nav_climb = SPEED_BFP_OF_REAL(_climb); \
222  }
223 
225 #define NavVerticalThrottleMode(_throttle) { \
226  vertical_mode = VERTICAL_MODE_MANUAL; \
227  nav_throttle = _throttle; \
228  }
229 
231 #define NavHeading nav_set_heading_rad
232 
233 #define NavAttitude(_roll) { \
234  horizontal_mode = HORIZONTAL_MODE_ATTITUDE; \
235  nav_roll = ANGLE_BFP_OF_REAL(_roll); \
236  }
237 
238 
239 
240 /***********************************************************
241  * built in navigation routines
242  **********************************************************/
243 
244 /*********** Navigation to waypoint *************************************/
245 static inline void NavGotoWaypoint(uint8_t wp)
246 {
247  horizontal_mode = HORIZONTAL_MODE_WAYPOINT;
249  dist2_to_wp = get_dist2_to_waypoint(wp);
250 }
251 
252 /*********** Navigation on a circle **************************************/
253 extern void nav_circle(struct EnuCoor_i *wp_center, int32_t radius);
254 static inline void NavCircleWaypoint(uint8_t wp_center, float radius)
255 {
256  horizontal_mode = HORIZONTAL_MODE_CIRCLE;
257  nav_circle(&waypoints[wp_center].enu_i, POS_BFP_OF_REAL(radius));
258 }
259 
260 #define NavCircleCount() ((float)abs(nav_circle_radians) / INT32_ANGLE_2_PI)
261 #define NavCircleQdr() ({ int32_t qdr = INT32_DEG_OF_RAD(INT32_ANGLE_PI_2 - nav_circle_qdr) >> INT32_ANGLE_FRAC; NormCourse(qdr); qdr; })
262 
263 #define CloseDegAngles(_c1, _c2) ({ int32_t _diff = _c1 - _c2; NormCourse(_diff); 350 < _diff || _diff < 10; })
264 #define CloseRadAngles(_c1, _c2) ({ float _diff = _c1 - _c2; NormRadAngle(_diff); fabsf(_diff) < 0.0177; })
265 
266 #define NavQdrCloseTo(x) CloseDegAngles(((x) >> INT32_ANGLE_FRAC), NavCircleQdr())
267 #define NavCourseCloseTo(x) {}
268 
269 /*********** Navigation along an oval *************************************/
270 extern void nav_oval_init(void);
271 extern void nav_oval(uint8_t, uint8_t, float);
272 extern uint8_t nav_oval_count;
273 #define Oval(a, b, c) nav_oval((b), (a), (c))
274 
275 /*********** Navigation along a line *************************************/
276 extern void nav_route(struct EnuCoor_i *wp_start, struct EnuCoor_i *wp_end);
277 extern struct FloatVect2 line_vect, to_end_vect;
278 #ifdef GUIDANCE_INDI_HYBRID
279 static inline void NavSegment(uint8_t wp_start, uint8_t wp_end)
280 {
281  VECT2_DIFF(line_vect, waypoints[wp_end].enu_f, waypoints[wp_start].enu_f);
283  VECT3_COPY(navigation_target, waypoints[wp_end].enu_i);
284  horizontal_mode = HORIZONTAL_MODE_ROUTE;
285 }
286 #else
287 static inline void NavSegment(uint8_t wp_start, uint8_t wp_end)
288 {
289  horizontal_mode = HORIZONTAL_MODE_ROUTE;
290  nav_route(&waypoints[wp_start].enu_i, &waypoints[wp_end].enu_i);
291 }
292 #endif
293 
295 static inline void NavGlide(uint8_t start_wp, uint8_t wp)
296 {
297  int32_t start_alt = waypoints[start_wp].enu_i.z;
298  int32_t diff_alt = waypoints[wp].enu_i.z - start_alt;
299  int32_t alt = start_alt + ((diff_alt * nav_leg_progress) / nav_leg_length);
301 }
302 
303 /* follow another aircraft */
304 #define NavFollow nav_follow
305 extern void nav_follow(uint8_t _ac_id, uint32_t distance, uint32_t height);
306 
307 
308 
309 /***********************************************************
310  * settings handlers
311  **********************************************************/
312 #define nav_IncreaseShift(x) {}
313 #define nav_SetNavRadius(x) {}
314 #define navigation_SetFlightAltitude(x) { \
315  flight_altitude = x; \
316  nav_flight_altitude = POS_BFP_OF_REAL(flight_altitude - state.ned_origin_f.hmsl); \
317  }
318 
319 #endif /* NAVIGATION_H */
unsigned short uint16_t
Definition: types.h:16
void nav_set_heading_rad(float rad)
Set nav_heading in radians.
Definition: navigation.c:489
uint32_t nav_leg_length
Definition: navigation.c:103
bool nav_detect_ground(void)
Definition: navigation.c:413
float nav_climb_vspeed
Definition: navigation.c:111
int32_t nav_leg_progress
Definition: navigation.c:102
uint8_t last_wp
Index of last waypoint.
Definition: nav.c:48
uint32_t nav_throttle
direct throttle from 0:MAX_PPRZ, used in VERTICAL_MODE_MANUAL
Definition: navigation.c:114
#define POS_BFP_OF_REAL(_af)
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
float nav_radius
Definition: nav.c:57
static void NavSegment(uint8_t wp_start, uint8_t wp_end)
Definition: navigation.h:287
int32_t nav_circle_radius
Definition: nav.c:69
void nav_reset_reference(void)
Reset the geographic reference to the current GPS fix.
Definition: navigation.c:353
#define VECT2_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:92
#define HORIZONTAL_MODE_CIRCLE
Definition: navigation.h:55
void nav_reset_alt(void)
Reset the altitude reference to the current GPS alt.
Definition: navigation.c:360
float dist2_to_wp
squared distance to next waypoint
Definition: navigation.c:89
struct FloatVect3 nav_get_speed_sp_from_go(struct EnuCoor_i target, float pos_gain)
Go to a waypoint in the shortest way.
void nav_circle(struct EnuCoor_i *wp_center, int32_t radius)
Definition: navigation.c:543
int32_t nav_flight_altitude
Definition: navigation.c:115
bool nav_approaching_from(struct EnuCoor_i *wp, struct EnuCoor_i *from, int16_t approaching_time)
Proximity tests on approaching a wp.
Definition: navigation.c:262
float flight_altitude
Dynamically adjustable, reset to nav_altitude when it is changing.
Definition: nav.c:75
static void NavGlide(uint8_t start_wp, uint8_t wp)
Nav glide routine.
Definition: navigation.h:295
uint8_t horizontal_mode
Definition: nav.c:71
int32_t nav_cmd_roll
Definition: navigation.c:109
bool nav_survey_active
Definition: nav.c:89
uint8_t vertical_mode
Definition: sim_ap.c:35
int32_t nav_climb
Definition: nav.c:57
void compute_dist2_to_home(void)
Computes squared distance to the HOME waypoint potentially sets too_far_from_home.
Definition: navigation.c:478
int32_t nav_altitude
Definition: nav.c:305
static struct EnuCoor_f * stateGetPositionEnu_f(void)
Get position in local ENU coordinates (float).
Definition: state.h:719
struct FloatVect3 nav_get_speed_setpoint(float pos_gain)
function that returns a speed setpoint based on flight plan.
#define FALSE
Definition: std.h:5
void nav_route(struct EnuCoor_i *wp_start, struct EnuCoor_i *wp_end)
Definition: navigation.c:589
void nav_periodic_task(void)
Navigation main: call to the code generated from the XML flight plan.
Definition: nav.c:443
bool nav_is_in_flight(void)
Definition: navigation.c:420
void nav_set_heading_current(void)
Set heading to the current yaw angle.
Definition: navigation.c:528
bool nav_check_wp_time(struct EnuCoor_i *wp, uint16_t stay_time)
Check the time spent in a radius of 'ARRIVED_AT_WAYPOINT' around a wp.
Definition: navigation.c:306
#define TRUE
Definition: std.h:4
#define HORIZONTAL_MODE_ROUTE
Definition: navigation.h:54
static void NavGotoWaypointHeading(uint8_t wp)
Definition: navigation.h:186
void nav_follow(uint8_t _ac_id, uint32_t distance, uint32_t height)
Definition: navigation.c:743
struct FloatVect3 speed_sp
float get_dist2_to_point(struct EnuCoor_i *p)
Returns squared horizontal distance to given point.
Definition: navigation.c:460
bool too_far_from_home
Definition: navigation.c:87
static void NavKillThrottle(void)
Definition: navigation.h:148
unsigned long uint32_t
Definition: types.h:18
signed short int16_t
Definition: types.h:17
static void NavResurrect(void)
Definition: navigation.h:152
int32_t nav_cmd_pitch
Definition: navigation.c:109
struct FloatVect2 line_vect to_end_vect
Definition: navigation.c:81
void nav_oval(uint8_t, uint8_t, float)
Navigation along a figure O.
Definition: nav.c:733
Paparazzi fixed point math for geodetic calculations.
uint8_t nav_oval_count
Navigation along a figure O.
Definition: nav.c:725
void nav_run(void)
Definition: navigation.c:248
bool exception_flag[10]
Definition: navigation.c:98
void nav_set_heading_towards(float x, float y)
Set heading to point towards x,y position in local coordinates.
Definition: navigation.c:502
#define NavVerticalAltitudeMode(_alt, _pre_climb)
Set the vertical mode to altitude control with the specified altitude setpoint and climb pre-command...
Definition: navigation.h:213
signed long int32_t
Definition: types.h:19
void set_exception_flag(uint8_t flag_num)
Definition: navigation.c:130
Core autopilot interface common to all firmwares.
int32_t nav_circle_qdr
Definition: navigation.c:120
struct EnuCoor_i navigation_carrot
Definition: navigation.c:92
void nav_set_manual(int32_t roll, int32_t pitch, int32_t yaw)
Set manual roll, pitch and yaw without stabilization.
Definition: navigation.c:451
int32_t nav_circle_radians
Status on the current circle.
Definition: nav.c:54
vector in East North Up coordinates
#define AP_MODE_NAV
unsigned char uint8_t
Definition: types.h:14
struct point waypoints[NB_WAYPOINT]
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:38
void nav_home(void)
Home mode navigation (circle around HOME)
Definition: nav.c:422
int32_t nav_roll
Definition: navigation.c:107
static void NavGotoWaypoint(uint8_t wp)
Definition: navigation.h:245
void autopilot_set_motors_on(bool motors_on)
turn motors on/off, eventually depending of the current mode set kill_throttle accordingly FIXME is i...
Definition: autopilot.c:200
#define VERTICAL_MODE_ALT
Definition: navigation.h:75
bool force_forward
Definition: navigation.c:79
Common flight_plan functions shared between fixedwing and rotorcraft.
void nav_oval_init(void)
Definition: nav.c:727
struct FloatVect3 nav_get_speed_sp_from_line(struct FloatVect2 line_v, struct FloatVect2 to_end_v, struct EnuCoor_i target, float pos_gain)
follow a line.
float nav_descend_vspeed
Definition: navigation.c:111
void navigation_update_wp_from_speed(uint8_t wp, struct Int16Vect3 speed_sp, int16_t heading_rate_sp)
Definition: navigation.c:389
static void NavCircleWaypoint(uint8_t wp_center, float radius)
Definition: navigation.h:254
void nav_set_heading_deg(float deg)
Set nav_heading in degrees.
Definition: navigation.c:496
static float p[2][2]
float dist2_to_home
squared distance to home waypoint
Definition: navigation.c:86
float failsafe_mode_dist2
maximum squared distance to home wp before going to failsafe mode
Definition: navigation.c:85
#define POS_FLOAT_OF_BFP(_ai)
#define HORIZONTAL_MODE_WAYPOINT
Definition: navigation.h:53
void nav_init(void)
Navigation Initialisation.
Definition: nav.c:530
void nav_set_failsafe(void)
Definition: navigation.c:533
struct EnuCoor_i navigation_target
Definition: navigation.c:91
struct FloatVect2 target
float get_dist2_to_waypoint(uint8_t wp_id)
Returns squared horizontal distance to given waypoint.
Definition: navigation.c:470
void nav_set_heading_towards_target(void)
Set heading in the direction of the target.
Definition: navigation.c:521
int32_t nav_cmd_yaw
Definition: navigation.c:109
void nav_set_heading_towards_waypoint(uint8_t wp)
Set heading in the direction of a waypoint.
Definition: navigation.c:515
float nav_max_speed
int32_t nav_heading
with INT32_ANGLE_FRAC
Definition: navigation.c:108
uint8_t autopilot_get_mode(void)
get autopilot mode
Definition: autopilot.c:184
int32_t nav_pitch
with INT32_ANGLE_FRAC
Definition: nav.c:308