Update target.c
[betaflight.git] / src / main / flight / pid.h
blob64182f8256ae957a8bbdb28e95e35680d14d7256
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
17 #include "rx/rx.h"
19 #pragma once
21 #define GYRO_I_MAX 256 // Gyro I limiter
22 #define YAW_P_LIMIT_MIN 100 // Maximum value for yaw P limiter
23 #define YAW_P_LIMIT_MAX 500 // Maximum value for yaw P limiter
24 #define YAW_JUMP_PREVENTION_LIMIT_LOW 80
25 #define YAW_JUMP_PREVENTION_LIMIT_HIGH 400
27 #define DYNAMIC_PTERM_STICK_THRESHOLD 400
29 typedef enum {
30 PIDROLL,
31 PIDPITCH,
32 PIDYAW,
33 PIDALT,
34 PIDPOS,
35 PIDPOSR,
36 PIDNAVR,
37 PIDLEVEL,
38 PIDMAG,
39 PIDVEL,
40 PID_ITEM_COUNT
41 } pidIndex_e;
43 typedef enum {
44 PID_CONTROLLER_LEGACY = 0, // Legacy PID controller. Old INT / Rewrite with 2.9 status. Fastest performance....least math. Will stay same in the future
45 PID_CONTROLLER_BETAFLIGHT, // Betaflight PID controller. Old luxfloat -> float evolution. More math added and maintained in the future
46 PID_COUNT
47 } pidControllerType_e;
49 typedef enum {
50 DELTA_FROM_ERROR = 0,
51 DELTA_FROM_MEASUREMENT
52 } pidDeltaType_e;
54 typedef enum {
55 SUPEREXPO_YAW_OFF = 0,
56 SUPEREXPO_YAW_ON,
57 SUPEREXPO_YAW_ALWAYS
58 } pidSuperExpoYaw_e;
60 typedef enum {
61 NEGATIVE_ERROR = 0,
62 POSITIVE_ERROR
63 } pidErrorPolarity_e;
65 typedef enum {
66 PID_STABILISATION_OFF = 0,
67 PID_STABILISATION_ON
68 } pidStabilisationState_e;
70 typedef struct pidProfile_s {
71 uint8_t pidController; // 1 = rewrite betaflight evolved from http://www.multiwii.com/forum/viewtopic.php?f=8&t=3671, 2 = Betaflight PIDc (Evolved Luxfloat)
73 uint8_t P8[PID_ITEM_COUNT];
74 uint8_t I8[PID_ITEM_COUNT];
75 uint8_t D8[PID_ITEM_COUNT];
77 uint16_t dterm_lpf_hz; // Delta Filter in hz
78 uint16_t yaw_lpf_hz; // Additional yaw filter when yaw axis too noisy
79 uint8_t deltaMethod; // Alternative delta Calculation
80 uint16_t rollPitchItermIgnoreRate; // Experimental threshold for resetting iterm for pitch and roll on certain rates
81 uint16_t yawItermIgnoreRate; // Experimental threshold for resetting iterm for yaw on certain rates
82 uint16_t yaw_p_limit;
83 uint8_t dterm_average_count; // Configurable delta count for dterm
84 uint8_t vbatPidCompensation; // Scale PIDsum to battery voltage
85 uint8_t zeroThrottleStabilisation; // Disable/Enable zero throttle stabilisation. Normally even without airmode P and D would be active.
87 // Betaflight PID controller parameters
88 uint8_t toleranceBand; // Error tolerance area where toleranceBandReduction is applied under certain circumstances
89 uint8_t toleranceBandReduction; // Lowest possible P and D reduction in percentage
90 uint8_t zeroCrossAllowanceCount; // Amount of bouncebacks within tolerance band allowed before reduction kicks in
91 uint16_t accelerationLimitPercent; // Percentage that motor is allowed to increase or decrease in a period of 1ms
92 uint8_t itermThrottleGain; // Throttle coupling to iterm. Quick throttle changes will bump iterm
94 #ifdef GTUNE
95 uint8_t gtune_lolimP[3]; // [0..200] Lower limit of P during G tune
96 uint8_t gtune_hilimP[3]; // [0..200] Higher limit of P during G tune. 0 Disables tuning for that axis.
97 uint8_t gtune_pwr; // [0..10] Strength of adjustment
98 uint16_t gtune_settle_time; // [200..1000] Settle time in ms
99 uint8_t gtune_average_cycles; // [8..128] Number of looptime cycles used for gyro average calculation
100 #endif
101 } pidProfile_t;
103 struct controlRateConfig_s;
104 union rollAndPitchTrims_u;
105 struct rxConfig_s;
106 typedef void (*pidControllerFuncPtr)(const pidProfile_t *pidProfile, uint16_t max_angle_inclination,
107 const union rollAndPitchTrims_u *angleTrim, const struct rxConfig_s *rxConfig); // pid controller function prototype
109 extern int16_t axisPID[XYZ_AXIS_COUNT];
110 extern int32_t axisPID_P[3], axisPID_I[3], axisPID_D[3];
111 bool airmodeWasActivated;
112 extern uint32_t targetPidLooptime;
114 void pidSetController(pidControllerType_e type);
115 void pidResetErrorGyroState(void);
116 void pidStabilisationState(pidStabilisationState_e pidControllerState);
117 void setTargetPidLooptime(uint8_t pidProcessDenom);