Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
nav_takeoff_and_landing_rotorcraft.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Gautier Hattenberger <gautier.hattenberger@enac.fr>
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, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
29 #include "generated/flight_plan.h"
31 #include "modules/nav/waypoints.h"
32 #include "autopilot.h"
33 
34 #ifndef NAV_TAKEOFF_CLIMB_SPEED
35 #define NAV_TAKEOFF_CLIMB_SPEED NAV_CLIMB_VSPEED
36 #endif
37 
38 #ifndef NAV_TAKEOFF_HEIGHT
39 #define NAV_TAKEOFF_HEIGHT 2.f
40 #endif
41 
42 #ifndef NAV_LANDING_DESCEND_SPEED
43 #define NAV_LANDING_DESCEND_SPEED NAV_DESCEND_VSPEED
44 #endif
45 
46 #ifndef NAV_LANDING_AF_HEIGHT
47 #define NAV_LANDING_AF_HEIGHT 5.f
48 #endif
49 
50 #ifndef NAV_LANDING_FLARE_HEIGHT
51 #define NAV_LANDING_FLARE_HEIGHT 2.f
52 #endif
53 
54 static struct nav_takeoff takeoff;
55 static struct nav_landing landing;
56 
57 static bool nav_takeoff_run(void);
58 static bool nav_land_run(void);
59 
61 
62 #if USE_MISSION
64 
65 static bool nav_takeoff_mission(uint8_t nb UNUSED, float *params UNUSED, enum MissionRunFlag flag)
66 {
67  if (flag == MissionInit) {
69  return nav_takeoff_from_here();
70  }
71  else if (flag == MissionRun) {
72  return nav_takeoff_run();
73  }
74  return false; // not a valid case
75 }
76 
77 static bool nav_land_mission(uint8_t nb, float *params, enum MissionRunFlag flag)
78 {
79  if (flag == MissionInit && nb == 1) {
80  float td_alt = params[0];
82  return nav_land_here(td_alt, 0.f);
83  } else if (flag == MissionInit && nb == 2) {
84  uint8_t td_id = (uint8_t)(params[0]);
85  uint8_t af_id = (uint8_t)(params[1]);
86  return nav_land_at_wp(td_id, af_id, 0.f);
87  } else if (flag == MissionInit && nb == 5) {
88  float td_alt = params[0];
89  float lat = params[1];
90  float lon = params[2];
91  float dir = params[3];
92  float dist = params[4];
93  return nav_land_at_loc(td_alt, lat, lon, dir, dist, 0.f);
94  } else if (flag == MissionRun) {
95  return nav_land_run();
96  }
97  return false; // not a valid case
98 }
99 
100 #endif
101 
103 {
105  takeoff.climb_id = 0;
106  takeoff.timeout = false;
108  landing.radius = 0.f; // no effect
109  landing.td_id = 0;
110  landing.af_id = 0;
111  landing.timeout = false;
112 
113  nav_takeoff_direction = QFU;
114 
115 #if USE_MISSION
116  mission_register(nav_takeoff_mission, "TKOFF");
117  mission_register(nav_land_mission, "LAND");
118 #endif
119 }
120 
122 {
123  if (takeoff.timeout) {
125  }
126  if (landing.timeout) {
128  }
129 
130  // reset flag to true, set to false if run function is called during period
131  takeoff.timeout = true;
132  landing.timeout = true;
133 }
134 
135 static bool nav_takeoff_run(void) {
136  static int start_motor_counter = 0;
137 
138  switch (takeoff.status) {
139  case NAV_TAKEOFF_INIT:
141  start_motor_counter = 0;
142  break;
144  NavResurrect();
145  start_motor_counter++;
146  if (start_motor_counter == NAVIGATION_FREQUENCY) {
147  start_motor_counter = 0;
148  if (autopilot_get_motors_on()) {
149  takeoff.status = NAV_TAKEOFF_CLIMB; // next step after 1s
150  } else {
151  takeoff.status = NAV_TAKEOFF_DONE; // failed to start motors, end now
152  }
153  }
154  break;
155  case NAV_TAKEOFF_CLIMB:
156  // call vertical climb from nav/guidance
161  // end when takeoff height is reached
163  }
164  break;
165  case NAV_TAKEOFF_DONE:
166  default:
168  return false;
169  }
170  takeoff.timeout = false;
171  return true;
172 }
173 
175 {
177  takeoff.climb_id = wp_id;
178  waypoint_set_here_2d(wp_id);
181  }
182 
183  return nav_takeoff_run();
184 }
185 
186 bool nav_takeoff_from_loc(float lat UNUSED, float lon UNUSED)
187 {
188  return nav_takeoff_from_here();
189 }
190 
192 {
194  takeoff.climb_id = 0; // use dummy hidden WP
198  }
199 
200  return nav_takeoff_run();
201 }
202 
203 
204 static bool nav_land_run(void)
205 {
206  float dx, dy, dist2;
207  switch (landing.status) {
208  case NAV_LANDING_INIT:
210  break;
214  if (nav.nav_approaching(&landing.af_pos, NULL, 0.f)) {
216  }
217  break;
218  case NAV_LANDING_DESCENT:
219  // test if glide is needed or not
220  dx = landing.af_pos.x - landing.td_pos.x;
221  dy = landing.af_pos.y - landing.td_pos.y;
222  dist2 = dx*dx + dy*dy;
223  if (dist2 > 1.f) {
226  } else {
229  }
231  || (stateGetPositionEnu_f()->z < landing.td_pos.z)) {
233  }
234  break;
235  case NAV_LANDING_FLARE:
238  if (!nav_is_in_flight()) {
240  }
241  break;
242  case NAV_LANDING_DONE:
243  default:
244  NavKillThrottle();
246  return false;
247  }
248  landing.timeout = false;
249  return true;
250 }
251 
253 {
255  landing.td_id = td_id;
256  landing.af_id = af_id;
261  }
262 
263  return nav_land_run();
264 }
265 
266 bool nav_land_at_loc(float td_alt, float lat, float lon, float dir, float dist, float radius UNUSED)
267 {
269  landing.td_id = 0;
270  landing.af_id = 0;
271  struct LlaCoor_f lla = { RadOfDeg(lat), RadOfDeg(lon), state.ned_origin_f.lla.alt + td_alt };
274  landing.af_pos.x = landing.td_pos.x + dist * sinf(RadOfDeg(dir));
275  landing.af_pos.y = landing.td_pos.y + dist * cosf(RadOfDeg(dir));
277  }
278 
279  return nav_land_run();
280 }
281 
282 bool nav_land_here(float td_alt, float radius UNUSED)
283 {
285  landing.td_id = 0;
286  landing.af_id = 0;
291  }
292 
293  return nav_land_run();
294 }
295 
void autopilot_set_in_flight(bool in_flight)
set in_flight flag
Definition: autopilot.c:330
bool autopilot_get_motors_on(void)
get motors status
Definition: autopilot.c:295
Core autopilot interface common to all firmwares.
uint8_t last_wp UNUSED
struct State state
Definition: state.c:36
static struct EnuCoor_f * stateGetPositionEnu_f(void)
Get position in local ENU coordinates (float).
Definition: state.h:719
struct LtpDef_f ned_origin_f
Definition of the local (flat earth) coordinate system.
Definition: state.h:220
bool mission_register(mission_custom_cb cb, char *type)
Register a new navigation or action callback function.
mission planner library
MissionRunFlag
@ MissionInit
first exec
@ MissionRun
normal run
void waypoint_set_here_2d(uint8_t wp_id)
set waypoint to current horizontal location without modifying altitude
Definition: waypoints.c:298
struct EnuCoor_f * waypoint_get_enu_f(uint8_t wp_id)
Get ENU coordinates (float)
Definition: waypoints.c:387
void waypoint_set_enu(uint8_t wp_id, struct EnuCoor_f *enu)
Set local ENU waypoint coordinates.
Definition: waypoints.c:177
#define NavVerticalClimbMode(_climb)
Set the vertical mode to climb control with the specified climb setpoint.
Definition: nav.h:198
#define NavGotoWaypoint(_wp)
Definition: nav.h:96
#define NavKillThrottle()
Definition: nav.h:226
#define NAVIGATION_FREQUENCY
Default fixedwing navigation frequency.
Definition: nav.h:49
#define NavVerticalAltitudeMode(_alt, _pre_climb)
Set the vertical mode to altitude control with the specified altitude setpoint and climb pre-command.
Definition: nav.h:191
enum nav_takeoff_status status
current step
uint8_t td_id
touch down wp id
enum nav_landing_status status
current step
struct EnuCoor_f af_pos
start of descent point
@ NAV_LANDING_REACH_AF
@ NAV_LANDING_DESCENT
@ NAV_LANDING_FLARE
@ NAV_LANDING_INIT
@ NAV_LANDING_DONE
float radius
circle radius for fixedwing landing
bool timeout
true if status should be set to init
struct EnuCoor_f td_pos
touch down point
uint8_t af_id
start of descent wp id
bool timeout
true if status should be set to init
struct EnuCoor_f start_pos
start position
uint8_t climb_id
climb waypoint id
struct EnuCoor_f climb_pos
climb point
@ NAV_TAKEOFF_DONE
@ NAV_TAKEOFF_INIT
@ NAV_TAKEOFF_CLIMB
@ NAV_TAKEOFF_START_MOTOR
Structure for landing.
Structure for takeoff.
bool nav_takeoff_from_wp(uint8_t wp_id)
Takeoff from a waypoint.
bool nav_takeoff_from_loc(float lat UNUSED, float lon UNUSED)
#define NAV_LANDING_DESCEND_SPEED
#define NAV_LANDING_FLARE_HEIGHT
bool nav_land_at_loc(float td_alt, float lat, float lon, float dir, float dist, float radius UNUSED)
static struct nav_landing landing
static bool nav_land_run(void)
static bool nav_takeoff_run(void)
float nav_takeoff_direction
Takeoff direction in range [0-360] (deg) set to flight plan QFU by default.
bool nav_land_here(float td_alt, float radius UNUSED)
void nav_takeoff_and_landing_init(void)
Init function.
#define NAV_TAKEOFF_CLIMB_SPEED
bool nav_takeoff_from_here(void)
Takeoff from current location.
static struct nav_takeoff takeoff
void nav_takeoff_and_landing_periodic(void)
Periodic timeout check function.
#define NAV_TAKEOFF_HEIGHT
bool nav_land_at_wp(uint8_t td_id, uint8_t af_id, float radius UNUSED)
#define NAV_LANDING_AF_HEIGHT
void enu_of_lla_point_f(struct EnuCoor_f *enu, struct LtpDef_f *def, struct LlaCoor_f *lla)
Paparazzi floating point math for geodetic calculations.
float alt
in meters (normally above WGS84 reference ellipsoid)
float y
in meters
float x
in meters
float lon
in radians
float lat
in radians
float z
in meters
struct LlaCoor_f lla
origin of local frame in LLA
vector in Latitude, Longitude and Altitude
bool nav_is_in_flight(void)
Definition: navigation.c:277
struct RotorcraftNavigation nav
Definition: navigation.c:51
void nav_glide_points(struct EnuCoor_f *start_point, struct EnuCoor_f *end_point)
Definition: navigation.c:282
Rotorcraft navigation functions.
navigation_approaching nav_approaching
Definition: navigation.h:153
navigation_goto nav_goto
Definition: navigation.h:151
static void NavResurrect(void)
Definition: navigation.h:233
navigation_route nav_route
Definition: navigation.h:152
static const float dir[]
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98