Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
approach_moving_target.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 Ewoud Smeur <e.j.j.smeur@tudelft.nl>
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 */
26#include "generated/airframe.h"
27
29
30#include "generated/modules.h"
31#include "modules/core/abi.h"
33
34
35// Filter value in Hz
36#ifndef APPROACH_MOVING_TARGET_CUTOFF_FREQ_FILTERS_HZ
37#define APPROACH_MOVING_TARGET_CUTOFF_FREQ_FILTERS_HZ 0.5
38#endif
39
40// Approach slope angle in degrees
41#ifndef APPROACH_MOVING_TARGET_SLOPE
42#define APPROACH_MOVING_TARGET_SLOPE 35.0
43#endif
44
45// Approach initial distance in meters
46#ifndef APPROACH_MOVING_TARGET_DISTANCE
47#define APPROACH_MOVING_TARGET_DISTANCE 60.0
48#endif
49
50// Approach speed in meters per second along the slope
51#ifndef APPROACH_MOVING_TARGET_SPEED
52#define APPROACH_MOVING_TARGET_SPEED -1.0
53#endif
54
55// Gains
56#ifndef APPROACH_MOVING_TARGET_ERR_SLOWDOWN_GAIN
57#define APPROACH_MOVING_TARGET_ERR_SLOWDOWN_GAIN 0.25
58#endif
59
60#ifndef APPROACH_MOVING_TARGET_POS_GAIN
61#define APPROACH_MOVING_TARGET_POS_GAIN 0.3
62#endif
63
64#ifndef APPROACH_MOVING_TARGET_SPEED_GAIN
65#define APPROACH_MOVING_TARGET_SPEED_GAIN 1.0
66#endif
67
68#ifndef APPROACH_MOVING_TARGET_RELVEL_GAIN
69#define APPROACH_MOVING_TARGET_RELVEL_GAIN 1.0
70#endif
71
75
76#define DEBUG_AMT TRUE
77#include <stdio.h>
78
79struct Amt amt = {
82 .psi_ref = 180.0,
88 .cutoff_freq_filters_hz = APPROACH_MOVING_TARGET_CUTOFF_FREQ_FILTERS_HZ,
89 .enabled_time = 0,
90 .wp_id = 0,
91};
92
93struct AmtTelem {
96};
97
100
102void update_waypoint(uint8_t wp_id, struct FloatVect3 *target_ned);
103
104#if PERIODIC_TELEMETRY
117#endif
118
126
128{
129 amt.cutoff_freq_filters_hz = filter_freq;
130 // tau = 1/(2*pi*Fc)
131 float tau = 1.0 / (2.0 * M_PI * amt.cutoff_freq_filters_hz);
133 for (uint8_t i = 0; i < 3; i++) {
136 }
138
139}
140
141// Update a waypoint such that you can see on the GCS where the drone wants to go
143{
144
145 // Update the waypoint
146 struct EnuCoor_f target_enu;
149
150 // Send waypoint update every half second
151 RunOnceEvery(100 / 2, {
152 // Send to the GCS that the waypoint has been moved
154 &waypoints[wp_id].enu_i.x,
155 &waypoints[wp_id].enu_i.y,
156 &waypoints[wp_id].enu_i.z);
157 });
158}
159
160// Function to enable from flight plan (call repeatedly!)
166
172{
173
174 // Check if the flight plan recently called the enable function
176 return;
177 }
178
179 // Get the target position, velocity and heading
180 struct NedCoor_f target_pos, target_vel = {0};
181 float target_heading;
183 // TODO: What to do? Same can be checked for the velocity
184 return;
185 }
186 if (target_heading > 360.f) {
187 // TODO: We got an invalid heading and need to do something with it
188 return;
189 }
191
192 // filter target values
196
200
201 // just filtering the heading will go wrong if it wraps, instead do:
202 // take the diff with current heading_filtered, wrap that, add to current, insert in filter and wrap again?
203 // example: target_heading - target_heading_filt (170 - -170 = 340), -> -20 -> -190 -> into filter -> wrap
210
211 target_pos.x = target_pos_filt[0].o[0];
212 target_pos.y = target_pos_filt[1].o[0];
213 target_pos.z = target_pos_filt[2].o[0];
214
215 target_vel.x = target_vel_filt[0].o[0];
216 target_vel.y = target_vel_filt[1].o[0];
217 target_vel.z = target_vel_filt[2].o[0];
218
220
221 // Reference model
224
225 amt.rel_unit_vec.x = cosf(gamma_ref) * cosf(psi_ref);
226 amt.rel_unit_vec.y = cosf(gamma_ref) * sinf(psi_ref);
228
229 // desired position = rel_pos + target_pos
230 struct FloatVect3 ref_relpos;
232
233 // ATTENTION, target_pos is already relative now!
234 struct FloatVect3 des_pos;
236
237 struct FloatVect3 ref_relvel;
239
240 // error controller
241 struct FloatVect3 pos_err;
242 struct FloatVect3 ec_vel;
244 // ATTENTION, target_pos is already relative now!
245 // VECT3_DIFF(pos_err, des_pos, *drone_pos);
246 VECT3_COPY(pos_err, des_pos);
248
249 // desired velocity = rel_vel + target_vel + error_controller(using NED position)
250 struct FloatVect3 des_vel = {
252 ref_relvel.y + target_vel.y + ec_vel.y,
253 ref_relvel.z + target_vel.z + ec_vel.z,
254 };
255
256 float_vect3_bound_in_3d(&des_vel, 10.0);
257
258 // Bound vertical speed setpoint
259 if (stateGetAirspeed_f() > 13.0) {
260 Bound(des_vel.z, -4.0, 5.0);
261 } else {
263 }
264
266
267 /* limit the speed such that the vertical component is small enough
268 * and doesn't outrun the vehicle
269 */
270 float min_speed;
272 if (sin_gamma_ref > 0.05) {
274 } else {
275 min_speed = -5.0; // prevent dividing by zero
276 }
277
278 // The upper bound is not very important
279 Bound(amt.speed, min_speed, 4.0);
280
281 // Reduce approach speed if the error is large
284
285 // integrate speed to get the distance
287 amt.distance += int_speed * dt;
288
289 // For display purposes
293
294 // Debug target pos:
296
297 // Update values for telemetry
298 VECT3_COPY(amt_telem.des_pos, des_pos);
299 VECT3_COPY(amt_telem.des_vel, des_vel);
300}
Main include for ABI (AirBorneInterface).
#define VEL_SP_FCR_ID
#define APPROACH_MOVING_TARGET_CUTOFF_FREQ_FILTERS_HZ
#define APPROACH_MOVING_TARGET_POS_GAIN
void follow_diagonal_approach(void)
Generates a velocity reference from a diagonal approach path.
struct FloatVect3 nav_get_speed_sp_from_diagonal(struct EnuCoor_i target, float pos_gain, float rope_heading)
struct AmtTelem amt_telem
Butterworth2LowPass target_pos_filt[3]
Butterworth2LowPass target_heading_filt
#define APPROACH_MOVING_TARGET_RELVEL_GAIN
#define APPROACH_MOVING_TARGET_SPEED
struct Amt amt
#define APPROACH_MOVING_TARGET_SLOPE
void approach_moving_target_set_low_pass_freq(float filter_freq)
struct FloatVect3 des_vel
bool approach_moving_target_enabled
Butterworth2LowPass target_vel_filt[3]
struct FloatVect3 des_pos
static void send_approach_moving_target(struct transport_tx *trans, struct link_device *dev)
#define APPROACH_MOVING_TARGET_DISTANCE
void approach_moving_target_init(void)
void update_waypoint(uint8_t wp_id, struct FloatVect3 *target_ned)
#define APPROACH_MOVING_TARGET_SPEED_GAIN
void approach_moving_target_enable(uint8_t wp_id)
#define APPROACH_MOVING_TARGET_ERR_SLOWDOWN_GAIN
int32_t enabled_time
float err_slowdown_gain
float cutoff_freq_filters_hz
struct FloatVect3 rel_unit_vec
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
struct point waypoints[NB_WAYPOINT]
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition common_nav.c:44
float y
Definition common_nav.h:41
float x
Definition common_nav.h:40
#define FLOAT_ANGLE_NORMALIZE(_a)
void float_vect3_bound_in_3d(struct FloatVect3 *vect3, float bound)
#define VECT3_SUM(_c, _a, _b)
#define VECT3_NORM2(_v)
#define VECT3_SMUL(_vo, _vi, _s)
#define VECT3_COPY(_a, _b)
#define VECT3_DIFF(_c, _a, _b)
#define ENU_OF_TO_NED(_po, _pi)
vector in East North Up coordinates
static struct NedCoor_f * stateGetPositionNed_f(void)
Get position in local NED coordinates (float).
Definition state.h:839
static float stateGetAirspeed_f(void)
Get airspeed (float).
Definition state.h:1590
Simple first order low pass filter with bilinear transform.
float o[2]
output history
static void init_butterworth_2_low_pass(Butterworth2LowPass *filter, float tau, float sample_time, float value)
Init a second order Butterworth filter.
static float update_butterworth_2_low_pass(Butterworth2LowPass *filter, float value)
Update second order Butterworth low pass filter state with a new value.
Second order low pass filter structure.
uint16_t foo
Definition main_demo5.c:58
void waypoint_set_enu(uint8_t wp_id, struct EnuCoor_f *enu)
Set local ENU waypoint coordinates.
Definition waypoints.c:177
#define NAVIGATION_FREQUENCY
Default fixedwing navigation frequency.
Definition nav.h:49
vector in East North Up coordinates Units: meters
vector in North East Down coordinates Units: meters
struct RotorcraftNavigation nav
Definition navigation.c:51
float descend_vspeed
descend speed setting, mostly used in flight plans
Definition navigation.h:147
float climb_vspeed
climb speed setting, mostly used in flight plans
Definition navigation.h:146
static const struct usb_device_descriptor dev
Definition usb_ser_hw.c:74
bool target_get_pos(struct NedCoor_f *pos, float *heading)
Get the current target position (NED) and heading.
Definition target_pos.c:143
struct target_t target
Definition target_pos.c:65
bool target_get_vel(struct NedCoor_f *vel)
Get the current target velocity (NED)
Definition target_pos.c:192
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition telemetry.c:51
Periodic telemetry system header (includes downlink utility and generated code).
#define DefaultPeriodic
Set default periodic telemetry.
Definition telemetry.h:66
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.