Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
nav_catapult.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012, Christophe De Wagter
3  * Copyright (C) 2016, Gautier Hattenberger <gautier.hattenberger@enac.fr>
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, see
19  * <http://www.gnu.org/licenses/>.
20  *
21  */
22 
37 
38 #include "state.h"
39 #include "generated/airframe.h"
41 #include "autopilot.h"
43 
44 #include "mcu_periph/uart.h"
45 #include "pprzlink/messages.h"
47 
48 
49 // Default values for take-off procedure
50 
51 #ifndef NAV_CATAPULT_ACCELERATION_THRESHOLD
52 #define NAV_CATAPULT_ACCELERATION_THRESHOLD 1.5 // in g
53 #endif
54 
55 #ifndef NAV_CATAPULT_ACCELERATION_DETECTION
56 #define NAV_CATAPULT_ACCELERATION_DETECTION 5 // number of valid measurements for launch detection
57 #endif
58 
59 #ifndef NAV_CATAPULT_MOTOR_DELAY
60 #define NAV_CATAPULT_MOTOR_DELAY 0.75 // in seconds
61 #endif
62 
63 #ifndef NAV_CATAPULT_HEADING_DELAY
64 #define NAV_CATAPULT_HEADING_DELAY 3.0 // in seconds
65 #endif
66 
67 #ifndef NAV_CATAPULT_INITIAL_PITCH
68 #define NAV_CATAPULT_INITIAL_PITCH RadOfDeg(10) // in radians
69 #endif
70 
71 #ifndef NAV_CATAPULT_INITIAL_THROTTLE
72 #define NAV_CATAPULT_INITIAL_THROTTLE 1.0 // [0, 1]
73 #endif
74 
75 #ifndef NAV_CATAPULT_CLIMB_DISTANCE
76 #define NAV_CATAPULT_CLIMB_DISTANCE 300. // distance of the climb point ahead of catapult
77 #endif
78 
79 #ifndef NAV_CATAPULT_TIMEOUT
80 #define NAV_CATAPULT_TIMEOUT 30. // disarm timeout (in seconds)
81 #endif
82 
84 
85 
87 {
88 
90  nav_catapult.timer = 0;
96 
97 }
98 
99 //#############################################################
100 // Code that Runs in a Fast Module
101 
103 {
105  nav_catapult.timer = 0;
106  // nothing more to do
107  return;
108  }
109 
110  // increase timer
112 
113  // wait for acceleration
115 
116  // launch detection filter
118  // several consecutive measurements above threshold
119 #ifndef SITL
120  struct FloatVect3 *accel_ned = (struct FloatVect3 *)stateGetAccelNed_f();
121  struct FloatRMat *ned_to_body = stateGetNedToBodyRMat_f();
122  struct FloatVect3 accel_body;
123  float_rmat_transp_vmult(&accel_body, ned_to_body, accel_ned);
124  if (accel_body.x < nav_catapult.accel_threshold * 9.81) {
125  // accel is low, reset timer
126  nav_catapult.timer = 0;
127  return;
128  }
129 #else
130  if (autopilot.launch != true) {
131  // wait for simulated launch
132  nav_catapult.timer = 0;
133  return;
134  }
135 #endif
136  }
137  // launch was detected: Motor Delay Counter
138  else if (nav_catapult.timer >= nav_catapult.motor_delay * NAV_CATAPULT_HIGHRATE_MODULE_FREQ) {
139  // turn on motor
141  autopilot.launch = true;
142  // go to next stage
144  }
145  }
146 
147  // reaching timeout and function still running
148  // shuting it down
149  if (nav_catapult.timer > NAV_CATAPULT_TIMEOUT * NAV_CATAPULT_HIGHRATE_MODULE_FREQ) {
151  nav_catapult_nav_catapult_highrate_module_status = MODULES_STOP;
152  }
153 }
154 
155 
156 //############################################################
157 // Code that runs in 4Hz Nav
158 
160 {
161  switch (nav_catapult.status) {
162  case NAV_CATAPULT_UNINIT:
163  // start high freq function if not done
164  if (nav_catapult_nav_catapult_highrate_module_status != MODULES_RUN) {
165  nav_catapult_nav_catapult_highrate_module_status = MODULES_START;
166  }
167  // arm catapult
169  break;
170  case NAV_CATAPULT_ARMED:
171  // store initial position
174  nav_catapult.pos.z = stateGetPositionUtm_f()->alt; // useful ?
176  break;
178  // no throttle, zero attitude
179  NavAttitude(RadOfDeg(0.f));
182  // wait for acceleration from high speed function
183  break;
185  // fixed attitude and motor
186  NavAttitude(RadOfDeg(0.f));
189  if (nav_catapult.timer >= nav_catapult.heading_delay * NAV_CATAPULT_HIGHRATE_MODULE_FREQ) {
190  // store heading, move climb waypoint
191  float dir_x = stateGetPositionEnu_f()->x - nav_catapult.pos.x;
192  float dir_y = stateGetPositionEnu_f()->y - nav_catapult.pos.y;
193  float dir_L = sqrtf(dir_x * dir_x + dir_y * dir_y);
194  WaypointX(_climb) = nav_catapult.pos.x + (dir_x / dir_L) * NAV_CATAPULT_CLIMB_DISTANCE;
195  WaypointY(_climb) = nav_catapult.pos.y + (dir_y / dir_L) * NAV_CATAPULT_CLIMB_DISTANCE;
196  DownlinkSendWpNr(_climb);
197  // next step
199  }
200  break;
202  // normal climb: heading locked by waypoint target
203  NavVerticalAltitudeMode(WaypointAlt(_climb), 0.f); // vertical mode (folow glideslope)
204  NavVerticalAutoThrottleMode(0.f); // throttle mode
205  NavGotoWaypoint(_climb); // horizontal mode (stay on localiser)
206  if (nav_approaching_xy(WaypointX(_climb), WaypointY(_climb), nav_catapult.pos.x, nav_catapult.pos.y, 0.f)) {
207  // reaching climb waypoint, end procedure
209  }
210  break;
211  case NAV_CATAPULT_DISARM:
212  // end procedure
214  nav_catapult_nav_catapult_highrate_module_status = MODULES_STOP;
215  return false;
216  default:
217  return false;
218  }
219 
220  // procedure still running
221  return true;
222 
223 }
224 
struct pprz_autopilot autopilot
Global autopilot structure.
Definition: autopilot.c:49
Core autopilot interface common to all firmwares.
bool launch
request launch
Definition: autopilot.h:71
#define WaypointX(_wp)
Definition: common_nav.h:45
#define WaypointAlt(_wp)
waypoint altitude in m above MSL
Definition: common_nav.h:48
#define WaypointY(_wp)
Definition: common_nav.h:46
Fixed wing horizontal control.
void float_rmat_transp_vmult(struct FloatVect3 *vb, struct FloatRMat *m_b2a, struct FloatVect3 *va)
rotate 3D vector by transposed rotation matrix.
rotation matrix
static struct NedCoor_f * stateGetAccelNed_f(void)
Get acceleration in NED coordinates (float).
Definition: state.h:1038
static struct FloatRMat * stateGetNedToBodyRMat_f(void)
Get vehicle body attitude rotation matrix (float).
Definition: state.h:1137
static struct EnuCoor_f * stateGetPositionEnu_f(void)
Get position in local ENU coordinates (float).
Definition: state.h:719
static struct UtmCoor_f * stateGetPositionUtm_f(void)
Get position in UTM coordinates (float).
Definition: state.h:692
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:325
void DownlinkSendWpNr(uint8_t _wp)
Definition: nav.c:497
Fixedwing Navigation library.
#define NavGotoWaypoint(_wp)
Definition: nav.h:96
#define NavVerticalThrottleMode(_throttle)
Set the vertical mode to fixed throttle with the specified setpoint.
Definition: nav.h:204
#define NavAttitude(_roll)
Definition: nav.h:214
#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
#define NavVerticalAutoThrottleMode(_pitch)
Set the climb control to auto-throttle with the specified pitch pre-command.
Definition: nav.h:177
#define NAV_CATAPULT_HEADING_DELAY
Definition: nav_catapult.c:64
#define NAV_CATAPULT_ACCELERATION_DETECTION
Definition: nav_catapult.c:56
#define NAV_CATAPULT_MOTOR_DELAY
Definition: nav_catapult.c:60
#define NAV_CATAPULT_INITIAL_THROTTLE
Definition: nav_catapult.c:72
#define NAV_CATAPULT_INITIAL_PITCH
Definition: nav_catapult.c:68
bool nav_catapult_run(uint8_t _climb)
Definition: nav_catapult.c:159
#define NAV_CATAPULT_ACCELERATION_THRESHOLD
Definition: nav_catapult.c:52
#define NAV_CATAPULT_CLIMB_DISTANCE
Definition: nav_catapult.c:76
struct nav_catapult_struct nav_catapult
Definition: nav_catapult.c:83
void nav_catapult_init(void)
Definition: nav_catapult.c:86
#define NAV_CATAPULT_TIMEOUT
Definition: nav_catapult.c:80
void nav_catapult_highrate_module(void)
Definition: nav_catapult.c:102
catapult launch timing system
uint32_t timer
internal timer
Definition: nav_catapult.h:55
@ NAV_CATAPULT_WAIT_ACCEL
Definition: nav_catapult.h:46
@ NAV_CATAPULT_ARMED
Definition: nav_catapult.h:45
@ NAV_CATAPULT_DISARM
Definition: nav_catapult.h:49
@ NAV_CATAPULT_UNINIT
Definition: nav_catapult.h:44
@ NAV_CATAPULT_MOTOR_CLIMB
Definition: nav_catapult.h:48
@ NAV_CATAPULT_MOTOR_ON
Definition: nav_catapult.h:47
enum nav_catapult_state status
current procedure state
Definition: nav_catapult.h:54
float initial_throttle
throttle during first take-off phase (in radian)
Definition: nav_catapult.h:61
float motor_delay
delay to start motor after launch detection (in seconds)
Definition: nav_catapult.h:58
float heading_delay
delay to estimate initial heading after launch (in seconds)
Definition: nav_catapult.h:59
float initial_pitch
pitch angle during first take-off phase (in radian)
Definition: nav_catapult.h:60
float accel_threshold
acceleration threshold for launch detection (in g)
Definition: nav_catapult.h:57
struct FloatVect3 pos
catapult position
Definition: nav_catapult.h:56
#define MAX_PPRZ
Definition: paparazzi.h:8
float y
in meters
float x
in meters
float alt
in meters (above WGS84 reference ellipsoid or above MSL)
API to get/set the generic vehicle states.
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98