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
guidance_pid.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/airframe.h"
30#include "state.h"
31
32
33// Keep GUIDANCE_H_XXX format for backward compatibility
34
35#ifndef GUIDANCE_H_AGAIN
36#define GUIDANCE_H_AGAIN 0
37#endif
38
39#ifndef GUIDANCE_H_VGAIN
40#define GUIDANCE_H_VGAIN 0
41#endif
42
43/* error if some gains are negative */
44#if (GUIDANCE_H_PGAIN < 0) || \
45 (GUIDANCE_H_DGAIN < 0) || \
46 (GUIDANCE_H_IGAIN < 0) || \
47 (GUIDANCE_H_AGAIN < 0) || \
48 (GUIDANCE_H_VGAIN < 0)
49#error "ALL PID_H control gains have to be positive!!!"
50#endif
51
52#ifndef GUIDANCE_H_THRUST_CMD_FILTER
53#define GUIDANCE_H_THRUST_CMD_FILTER 10
54#endif
55
56#ifndef GUIDANCE_H_APPROX_FORCE_BY_THRUST
57#define GUIDANCE_H_APPROX_FORCE_BY_THRUST FALSE
58#endif
59
60// TODO configurable
61#define MAX_POS_ERR POS_BFP_OF_REAL(16.)
62#define MAX_SPEED_ERR SPEED_BFP_OF_REAL(16.)
63
64// Keep GUIDANCE_V_XXX format for backward compatibility
65
66/* error if some gains are negative */
67#if (GUIDANCE_V_HOVER_KP < 0) || \
68 (GUIDANCE_V_HOVER_KD < 0) || \
69 (GUIDANCE_V_HOVER_KI < 0)
70#error "ALL PID_V control gains must be positive!!!"
71#endif
72
73/* If only GUIDANCE_V_NOMINAL_HOVER_THROTTLE is defined,
74 * disable the adaptive throttle estimation by default.
75 * Otherwise enable adaptive estimation by default.
76 */
77#ifdef GUIDANCE_V_NOMINAL_HOVER_THROTTLE
78# ifndef GUIDANCE_V_ADAPT_THROTTLE_ENABLED
79# define GUIDANCE_V_ADAPT_THROTTLE_ENABLED FALSE
80# endif
81#else
82# ifndef GUIDANCE_V_ADAPT_THROTTLE_ENABLED
83# define GUIDANCE_V_ADAPT_THROTTLE_ENABLED TRUE
84# endif
85#endif
87
88#ifndef GUIDANCE_V_MIN_ERR_Z
89#define GUIDANCE_V_MIN_ERR_Z POS_BFP_OF_REAL(-10.)
90#endif
91
92#ifndef GUIDANCE_V_MAX_ERR_Z
93#define GUIDANCE_V_MAX_ERR_Z POS_BFP_OF_REAL(10.)
94#endif
95
96#ifndef GUIDANCE_V_MIN_ERR_ZD
97#define GUIDANCE_V_MIN_ERR_ZD SPEED_BFP_OF_REAL(-10.)
98#endif
99
100#ifndef GUIDANCE_V_MAX_ERR_ZD
101#define GUIDANCE_V_MAX_ERR_ZD SPEED_BFP_OF_REAL(10.)
102#endif
103
104#ifndef GUIDANCE_V_MAX_SUM_ERR
105#define GUIDANCE_V_MAX_SUM_ERR 2000000
106#endif
107
108#ifndef GUIDANCE_V_MAX_CMD
109#define GUIDANCE_V_MAX_CMD 0.9*MAX_PPRZ
110#endif
111
112
113
116#ifndef GUIDANCE_PID_USE_AS_DEFAULT
117#define GUIDANCE_PID_USE_AS_DEFAULT TRUE
118#endif
119
120/*
121 * external variables
122 */
123
125
126/*
127 * internal variables
128 */
129
133
137
138
139#if PERIODIC_TELEMETRY
141
142static void send_hover_loop(struct transport_tx *trans, struct link_device *dev)
143{
144 struct NedCoor_i *pos = stateGetPositionNed_i();
145 struct NedCoor_i *speed = stateGetSpeedNed_i();
146 struct NedCoor_i *accel = stateGetAccelNed_i();
150 &(pos->x), &(pos->y),
151 &(speed->x), &(speed->y),
152 &(accel->x), &(accel->y),
162}
163
181
182#endif
183
210
211/* with a pgain of 100 and a scale of 2,
212 * you get an angle of 5.6 degrees for 1m pos error */
213#define GH_GAIN_SCALE 2
214
219{
220 /* maximum bank angle: default 20 deg, max 40 deg*/
224
225 /* run PID */
226 int32_t pd_x =
229 int32_t pd_y =
233 ((guidance_pid.kv * gh->ref.speed.x) >> (INT32_SPEED_FRAC - GH_GAIN_SCALE)) + /* speed feedforward gain */
234 ((guidance_pid.ka * gh->ref.accel.x) >> (INT32_ACCEL_FRAC - GH_GAIN_SCALE)); /* acceleration feedforward gain */
236 ((guidance_pid.kv * gh->ref.speed.y) >> (INT32_SPEED_FRAC - GH_GAIN_SCALE)) + /* speed feedforward gain */
237 ((guidance_pid.ka * gh->ref.accel.y) >> (INT32_ACCEL_FRAC - GH_GAIN_SCALE)); /* acceleration feedforward gain */
238
239 /* trim max bank angle from PD */
241
242 /* Update pos & speed error integral, zero it if not in_flight.
243 * Integrate twice as fast when not only POS but also SPEED are wrong,
244 * but do not integrate POS errors when the SPEED is already catching up.
245 */
246 if (in_flight) {
247 /* ANGLE_FRAC (12) * GAIN (8) * LOOP_FREQ (9) -> INTEGRATOR HIGH RES ANGLE_FRAX (28) */
250 /* saturate it */
253 /* add it to the command */
256 } else {
258 }
259
260 /* compute a better approximation of force commands by taking thrust into account */
261 if (guidance_pid.approx_force_by_thrust && in_flight) {
263 // FIXME strong coupling with guidance_v here !!!
271 }
272
274
276}
277
279{
280 /* compute position error */
282 /* saturate it */
284
285 /* compute speed error */
287 /* saturate it */
289
290 /* run PID */
291 return guidance_pid_h_run(in_flight, gh);
292}
293
295{
296 /* cancel position error */
298
299 /* compute speed error */
301 /* saturate it */
303
304 /* run PID */
305 return guidance_pid_h_run(in_flight, gh);
306}
307
309{
310 struct StabilizationSetpoint sp = { 0 };
311 // TODO
312 return sp;
313}
314
319#define FF_CMD_FRAC 18
320
322{
323 /* compute the error to our reference */
324 int32_t err_z = gv->z_ref - stateGetPositionNed_i()->z;
326 int32_t err_zd = gv->zd_ref - stateGetSpeedNed_i()->z;
328
329 if (in_flight) {
332 } else {
334 }
335
336 /* our nominal command : (g + zdd)*m */
340 } else {
341 /* use the fixed nominal throttle */
342 inv_m = BFP_OF_REAL(9.81 / (gv->nominal_throttle * MAX_PPRZ), FF_CMD_FRAC);
343 }
344
346 (gv->zdd_ref << (FF_CMD_FRAC - INT32_ACCEL_FRAC));
347
349 /* feed forward command */
351
352 /* bound the nominal command to GUIDANCE_V_MAX_CMD */
354
355
356 /* our error feed back command */
357 /* z-axis pointing down -> positive error means we need less thrust */
359 ((-guidance_pid.v_kp * err_z) >> 7) +
360 ((-guidance_pid.v_kd * err_zd) >> 16) +
362
364
365 /* bound the result */
367
369}
370
372{
373 return guidance_pid_v_run(in_flight, gv);
374}
375
377{
378 return guidance_pid_v_run(in_flight, gv);
379}
380
382{
383 // TODO
384 struct ThrustSetpoint sp;
386 return sp;
387}
388
390{
391 /* set nav_heading to current heading */
393}
394
396{
398}
399
409
415
416
418{
419 return &guidance_pid_pos_err;
420}
421
422#if GUIDANCE_PID_USE_AS_DEFAULT
423// guidance pid control function is implementing the default functions of guidance
424
425void guidance_h_run_enter(void)
426{
428}
429
430void guidance_v_run_enter(void)
431{
433}
434
436{
437 return guidance_pid_h_run_pos(in_flight, gh);
438}
439
441{
442 return guidance_pid_h_run_speed(in_flight, gh);
443}
444
446{
447 return guidance_pid_h_run_accel(in_flight, gh);
448}
449
451{
452 return guidance_pid_v_run_pos(in_flight, gv);
453}
454
456{
457 return guidance_pid_v_run_speed(in_flight, gv);
458}
459
461{
462 return guidance_pid_v_run_accel(in_flight, gv);
463}
464
465#endif
466
#define UNUSED(x)
#define Min(x, y)
Definition esc_dshot.c:109
float psi
in radians
#define VECT2_DIFF(_c, _a, _b)
#define VECT2_STRIM(_v, _min, _max)
#define INT32_POS_FRAC
#define ANGLE_BFP_OF_REAL(_af)
#define INT32_TRIG_FRAC
#define INT32_SPEED_FRAC
#define INT32_ANGLE_PI_2
#define INT32_ACCEL_FRAC
#define INT32_ANGLE_FRAC
#define BFP_OF_REAL(_vr, _frac)
#define INT_VECT2_ZERO(_v)
int32_t z
Down.
int32_t y
East.
int32_t x
North.
vector in North East Down coordinates
static struct NedCoor_i * stateGetAccelNed_i(void)
Get acceleration in NED coordinates (int).
Definition state.h:1177
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition state.h:1306
static struct NedCoor_i * stateGetPositionNed_i(void)
Get position in local NED coordinates (int).
Definition state.h:794
static struct NedCoor_i * stateGetSpeedNed_i(void)
Get ground speed in local NED coordinates (int).
Definition state.h:1004
void guidance_v_run_enter(void)
struct ThrustSetpoint guidance_v_run_pos(bool in_flight UNUSED, struct VerticalGuidance *gv)
struct ThrustSetpoint guidance_v_run_speed(bool in_flight UNUSED, struct VerticalGuidance *gv)
struct ThrustSetpoint guidance_v_run_accel(bool in_flight UNUSED, struct VerticalGuidance *gv)
void guidance_pid_set_h_igain(uint32_t igain)
settings handler
#define GUIDANCE_V_MAX_SUM_ERR
struct ThrustSetpoint guidance_pid_v_run_accel(bool in_flight UNUSED, struct VerticalGuidance *gv UNUSED)
void guidance_pid_init(void)
#define GUIDANCE_H_VGAIN
int32_t guidance_pid_v_ff_cmd
feed-forward command
struct StabilizationSetpoint guidance_pid_h_run_accel(bool in_flight UNUSED, struct HorizontalGuidance *gh UNUSED)
struct Int32Vect2 guidance_pid_trim_att_integrator
struct StabilizationSetpoint guidance_pid_h_run_pos(bool in_flight, struct HorizontalGuidance *gh)
static void send_hover_loop(struct transport_tx *trans, struct link_device *dev)
struct StabilizationSetpoint guidance_pid_h_run_speed(bool in_flight, struct HorizontalGuidance *gh)
int32_t guidance_pid_v_fb_cmd
feed-back command
#define MAX_SPEED_ERR
struct ThrustSetpoint guidance_pid_v_run_pos(bool in_flight, struct VerticalGuidance *gv)
#define GUIDANCE_V_MIN_ERR_Z
const struct Int32Vect2 * guidance_pid_get_h_pos_err(void)
Gets the position error.
static struct ThrustSetpoint guidance_pid_v_run(bool in_flight, struct VerticalGuidance *gv)
void guidance_pid_set_v_igain(uint32_t igain)
#define MAX_POS_ERR
#define GUIDANCE_H_AGAIN
#define GUIDANCE_V_MAX_CMD
#define GUIDANCE_V_ADAPT_THROTTLE_ENABLED
#define FF_CMD_FRAC
run vertical control loop for position and speed control
struct ThrustSetpoint guidance_pid_v_run_speed(bool in_flight, struct VerticalGuidance *gv)
static void send_vert_loop(struct transport_tx *trans, struct link_device *dev)
static struct StabilizationSetpoint guidance_pid_h_run(bool in_flight, struct HorizontalGuidance *gh)
run horizontal control loop for position and speed control
struct GuidancePID guidance_pid
Guidance PID structyre.
#define GH_GAIN_SCALE
#define GUIDANCE_H_THRUST_CMD_FILTER
#define GUIDANCE_V_MIN_ERR_ZD
#define GUIDANCE_V_MAX_ERR_ZD
int32_t guidance_pid_z_sum_err
accumulator for I-gain
struct Int32Vect2 guidance_pid_pos_err
#define GUIDANCE_H_APPROX_FORCE_BY_THRUST
void guidance_pid_v_enter(void)
#define GUIDANCE_V_MAX_ERR_Z
struct Int32Vect2 guidance_pid_speed_err
void guidance_pid_h_enter(void)
Guidance controller with PID for rotorcrafts.
int32_t v_ki
int32_t cmd_thrust
bool approx_force_by_thrust
bool adapt_throttle_enabled
int32_t v_kp
struct Int32Vect2 cmd_earth
int32_t v_kd
int32_t gv_adapt_P
Covariance.
int32_t gv_adapt_X
State of the estimator.
int32_t gv_adapt_Xmeas
Measurement.
Adaptation block of the vertical guidance.
#define GV_ADAPT_X_FRAC
uint16_t foo
Definition main_demo5.c:58
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
#define MAX_PPRZ
Definition paparazzi.h:8
struct HorizontalGuidance guidance_h
Definition guidance_h.c:45
void guidance_h_run_enter(void)
struct StabilizationSetpoint guidance_h_run_pos(bool in_flight, struct HorizontalGuidance *gh)
struct StabilizationSetpoint guidance_h_run_speed(bool in_flight, struct HorizontalGuidance *gh)
struct StabilizationSetpoint guidance_h_run_accel(bool in_flight, struct HorizontalGuidance *gh)
#define GUIDANCE_H_MAX_BANK
Max bank controlled by guidance.
Definition guidance_h.h:64
struct HorizontalGuidanceSetpoint sp
setpoints
Definition guidance_h.h:108
struct VerticalGuidance guidance_v
Definition guidance_v.c:60
int32_t z_sp
altitude setpoint in meters (input).
Definition guidance_v.h:50
int32_t zd_sp
vertical speed setpoint in meter/s (input).
Definition guidance_v.h:56
int32_t z_ref
altitude reference in meters.
Definition guidance_v.h:62
int32_t zd_ref
vertical speed reference in meter/s.
Definition guidance_v.h:68
int32_t thrust_coeff
Definition guidance_v.h:104
int32_t zdd_ref
vertical acceleration reference in meter/s^2.
Definition guidance_v.h:74
struct RotorcraftNavigation nav
Definition navigation.c:51
float heading
heading setpoint (in radians)
Definition navigation.h:133
struct Stabilization stabilization
struct StabilizationSetpoint stab_sp_from_ltp_i(struct Int32Vect2 *vect, int32_t heading)
struct ThrustSetpoint th_sp_from_thrust_i(int32_t thrust, uint8_t axis)
General stabilization interface for rotorcrafts.
#define THRUST_SP_SET_ZERO(_sp)
#define THRUST_AXIS_Z
int32_t cmd[COMMANDS_NB]
output command vector, range from [-MAX_PPRZ:MAX_PPRZ] (store for messages)
API to get/set the generic vehicle states.
static const struct usb_device_descriptor dev
Definition usb_ser_hw.c:74
struct Int32Vect2 pos
horizontal position setpoint in NED.
Definition guidance_h.h:73
Stabilization setpoint.
union StabilizationSetpoint::@278 sp
Thrust setpoint // TODO to a setpoint header Structure to store the desired thrust vector with differ...
union ThrustSetpoint::@284 sp
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
int int32_t
Typedef defining 32 bit int type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.