Paparazzi UAS  v5.12_stable-4-g9b43e9b
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 get_dist2_to_waypoint(uint8_t wp_id);
117 extern float get_dist2_to_point(struct EnuCoor_i *p);
118 extern void compute_dist2_to_home(void);
119 extern void nav_home(void);
120 extern void nav_set_manual(int32_t roll, int32_t pitch, int32_t yaw);
121 
122 extern void nav_reset_reference(void) __attribute__((unused));
123 extern void nav_reset_alt(void) __attribute__((unused));
124 extern void nav_periodic_task(void);
125 
126 extern bool nav_is_in_flight(void);
127 
128 extern void nav_set_heading_rad(float rad);
129 extern void nav_set_heading_deg(float deg);
130 extern void nav_set_heading_towards(float x, float y);
132 extern void nav_set_heading_towards_target(void);
133 extern void nav_set_heading_current(void);
134 extern void nav_set_failsafe(void);
135 
136 /* ground detection */
137 extern bool nav_detect_ground(void);
138 #define NavStartDetectGround() ({ autopilot.detect_ground_once = true; false; })
139 #define NavDetectGround() nav_detect_ground()
140 
141 /* switching motors on/off */
142 static inline void NavKillThrottle(void)
143 {
145 }
146 static inline void NavResurrect(void)
147 {
149 }
150 
151 
152 #define NavSetManual nav_set_manual
153 #define NavSetFailsafe nav_set_failsafe
154 
155 
156 #define NavSetGroundReferenceHere nav_reset_reference
157 #define NavSetAltitudeReferenceHere nav_reset_alt
158 
159 #define NavSetWaypointHere waypoint_set_here_2d
160 #define NavCopyWaypoint waypoint_copy
161 #define NavCopyWaypointPositionOnly waypoint_position_copy
162 
163 
165 bool nav_approaching_from(struct EnuCoor_i *wp, struct EnuCoor_i *from, int16_t approaching_time);
166 #define NavApproaching(wp, time) nav_approaching_from(&waypoints[wp].enu_i, NULL, time)
167 #define NavApproachingFrom(wp, from, time) nav_approaching_from(&waypoints[wp].enu_i, &waypoints[from].enu_i, time)
168 
170 bool nav_check_wp_time(struct EnuCoor_i *wp, uint16_t stay_time);
171 #define NavCheckWaypointTime(wp, time) nav_check_wp_time(&waypoints[wp].enu_i, time)
172 
173 
174 extern void navigation_update_wp_from_speed(uint8_t wp, struct Int16Vect3 speed_sp, int16_t heading_rate_sp);
175 
176 /* should we really keep this one ??
177  * maybe better to use the `goto` flight plan primitive and
178  * add a `pre_call` or `call_once` to set the heading?
179  */
180 static inline void NavGotoWaypointHeading(uint8_t wp)
181 {
182  vertical_mode = VERTICAL_MODE_ALT;
183  horizontal_mode = HORIZONTAL_MODE_WAYPOINT;
185  dist2_to_wp = get_dist2_to_waypoint(wp);
187 }
188 
189 
190 
191 /***********************************************************
192  * macros used by flight plan to set different modes
193  **********************************************************/
194 
197 #define NavVerticalAutoThrottleMode(_pitch) { \
198  nav_pitch = ANGLE_BFP_OF_REAL(_pitch); \
199  }
200 
203 #define NavVerticalAutoPitchMode(_throttle) {}
204 
207 #define NavVerticalAltitudeMode(_alt, _pre_climb) { \
208  vertical_mode = VERTICAL_MODE_ALT; \
209  nav_altitude = POS_BFP_OF_REAL(_alt); \
210  }
211 
213 #define NavVerticalClimbMode(_climb) { \
214  vertical_mode = VERTICAL_MODE_CLIMB; \
215  nav_climb = SPEED_BFP_OF_REAL(_climb); \
216  }
217 
219 #define NavVerticalThrottleMode(_throttle) { \
220  vertical_mode = VERTICAL_MODE_MANUAL; \
221  nav_throttle = _throttle; \
222  }
223 
225 #define NavHeading nav_set_heading_rad
226 
227 #define NavAttitude(_roll) { \
228  horizontal_mode = HORIZONTAL_MODE_ATTITUDE; \
229  nav_roll = ANGLE_BFP_OF_REAL(_roll); \
230  }
231 
232 
233 
234 /***********************************************************
235  * built in navigation routines
236  **********************************************************/
237 
238 /*********** Navigation to waypoint *************************************/
239 static inline void NavGotoWaypoint(uint8_t wp)
240 {
241  horizontal_mode = HORIZONTAL_MODE_WAYPOINT;
243  dist2_to_wp = get_dist2_to_waypoint(wp);
244 }
245 
246 /*********** Navigation on a circle **************************************/
247 extern void nav_circle(struct EnuCoor_i *wp_center, int32_t radius);
248 static inline void NavCircleWaypoint(uint8_t wp_center, float radius)
249 {
250  horizontal_mode = HORIZONTAL_MODE_CIRCLE;
251  nav_circle(&waypoints[wp_center].enu_i, POS_BFP_OF_REAL(radius));
252 }
253 
254 #define NavCircleCount() ((float)abs(nav_circle_radians) / INT32_ANGLE_2_PI)
255 #define NavCircleQdr() ({ int32_t qdr = INT32_DEG_OF_RAD(INT32_ANGLE_PI_2 - nav_circle_qdr) >> INT32_ANGLE_FRAC; NormCourse(qdr); qdr; })
256 
257 #define CloseDegAngles(_c1, _c2) ({ int32_t _diff = _c1 - _c2; NormCourse(_diff); 350 < _diff || _diff < 10; })
258 
259 #define NavQdrCloseTo(x) CloseDegAngles(((x) >> INT32_ANGLE_FRAC), NavCircleQdr())
260 #define NavCourseCloseTo(x) {}
261 
262 /*********** Navigation along an oval *************************************/
263 extern void nav_oval_init(void);
264 extern void nav_oval(uint8_t, uint8_t, float);
265 extern uint8_t nav_oval_count;
266 #define Oval(a, b, c) nav_oval((b), (a), (c))
267 
268 /*********** Navigation along a line *************************************/
269 extern void nav_route(struct EnuCoor_i *wp_start, struct EnuCoor_i *wp_end);
270 static inline void NavSegment(uint8_t wp_start, uint8_t wp_end)
271 {
272  horizontal_mode = HORIZONTAL_MODE_ROUTE;
273  nav_route(&waypoints[wp_start].enu_i, &waypoints[wp_end].enu_i);
274 }
275 
277 static inline void NavGlide(uint8_t start_wp, uint8_t wp)
278 {
279  int32_t start_alt = waypoints[start_wp].enu_i.z;
280  int32_t diff_alt = waypoints[wp].enu_i.z - start_alt;
281  int32_t alt = start_alt + ((diff_alt * nav_leg_progress) / nav_leg_length);
283 }
284 
285 /* follow another aircraft */
286 #define NavFollow nav_follow
287 extern void nav_follow(uint8_t _ac_id, uint32_t distance, uint32_t height);
288 
289 
290 
291 /***********************************************************
292  * settings handlers
293  **********************************************************/
294 #define nav_IncreaseShift(x) {}
295 #define nav_SetNavRadius(x) {}
296 #define navigation_SetFlightAltitude(x) { \
297  flight_altitude = x; \
298  nav_flight_altitude = POS_BFP_OF_REAL(flight_altitude - state.ned_origin_f.hmsl); \
299  }
300 
301 #endif /* NAVIGATION_H */
unsigned short uint16_t
Definition: types.h:16
int32_t y
North.
int32_t x
East.
#define POS_BFP_OF_REAL(_af)
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:139
static float radius
Definition: chemotaxis.c:15
#define FALSE
Definition: std.h:5
#define TRUE
Definition: std.h:4
unsigned long uint32_t
Definition: types.h:18
signed short int16_t
Definition: types.h:17
Paparazzi fixed point math for geodetic calculations.
signed long int32_t
Definition: types.h:19
Core autopilot interface common to all firmwares.
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 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:195
Common flight_plan functions shared between fixedwing and rotorcraft.
static float p[2][2]
#define POS_FLOAT_OF_BFP(_ai)
uint8_t autopilot_get_mode(void)
get autopilot mode
Definition: autopilot.c:179