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/>.
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
30 // Scaling factors for Pids for better tunable range in configurator for betaflight pid controller. The scaling is based on legacy pid controller or previous float
31 #define PTERM_SCALE 0.032029f
32 #define ITERM_SCALE 0.244381f
33 #define DTERM_SCALE 0.000529f
50 PID_CONTROLLER_LEGACY
= 0, // Legacy PID controller. Old INT / Rewrite with 2.9 status. Fastest performance....least math. Will stay same in the future
51 PID_CONTROLLER_BETAFLIGHT
, // Betaflight PID controller. Old luxfloat -> float evolution. More math added and maintained in the future
53 } pidControllerType_e
;
57 DELTA_FROM_MEASUREMENT
61 SUPEREXPO_YAW_OFF
= 0,
72 PID_STABILISATION_OFF
= 0,
74 } pidStabilisationState_e
;
76 typedef struct pidProfile_s
{
77 uint8_t pidController
; // 1 = rewrite betaflight evolved from http://www.multiwii.com/forum/viewtopic.php?f=8&t=3671, 2 = Betaflight PIDc (Evolved Luxfloat)
79 uint8_t P8
[PID_ITEM_COUNT
];
80 uint8_t I8
[PID_ITEM_COUNT
];
81 uint8_t D8
[PID_ITEM_COUNT
];
83 uint8_t dterm_filter_type
; // Filter selection for dterm
84 uint16_t dterm_lpf_hz
; // Delta Filter in hz
85 uint16_t yaw_lpf_hz
; // Additional yaw filter when yaw axis too noisy
86 uint16_t dterm_notch_hz
; // Biquad dterm notch hz
87 uint16_t dterm_notch_cutoff
; // Biquad dterm notch low cutoff
88 uint8_t deltaMethod
; // Alternative delta Calculation
89 uint16_t rollPitchItermIgnoreRate
; // Experimental threshold for resetting iterm for pitch and roll on certain rates
90 uint16_t yawItermIgnoreRate
; // Experimental threshold for resetting iterm for yaw on certain rates
92 uint8_t dterm_average_count
; // Configurable delta count for dterm
93 uint8_t vbatPidCompensation
; // Scale PIDsum to battery voltage
94 uint8_t zeroThrottleStabilisation
; // Disable/Enable zero throttle stabilisation. Normally even without airmode P and D would be active.
96 // Betaflight PID controller parameters
97 uint8_t toleranceBand
; // Error tolerance area where toleranceBandReduction is applied under certain circumstances
98 uint8_t toleranceBandReduction
; // Lowest possible P and D reduction in percentage
99 uint8_t zeroCrossAllowanceCount
; // Amount of bouncebacks within tolerance band allowed before reduction kicks in
100 uint8_t itermThrottleGain
; // Throttle coupling to iterm. Quick throttle changes will bump iterm
101 uint8_t ptermSetpointWeight
; // Setpoint weight for Pterm (lower means more PV tracking)
102 uint8_t dtermSetpointWeight
; // Setpoint weight for Dterm (0= measurement, 1= full error, 1 > agressive derivative)
103 uint16_t yawRateAccelLimit
; // yaw accel limiter for deg/sec/ms
104 uint16_t rateAccelLimit
; // accel limiter roll/pitch deg/sec/ms
107 uint8_t gtune_lolimP
[3]; // [0..200] Lower limit of P during G tune
108 uint8_t gtune_hilimP
[3]; // [0..200] Higher limit of P during G tune. 0 Disables tuning for that axis.
109 uint8_t gtune_pwr
; // [0..10] Strength of adjustment
110 uint16_t gtune_settle_time
; // [200..1000] Settle time in ms
111 uint8_t gtune_average_cycles
; // [8..128] Number of looptime cycles used for gyro average calculation
115 struct controlRateConfig_s
;
116 union rollAndPitchTrims_u
;
118 typedef void (*pidControllerFuncPtr
)(const pidProfile_t
*pidProfile
, uint16_t max_angle_inclination
,
119 const union rollAndPitchTrims_u
*angleTrim
, const struct rxConfig_s
*rxConfig
); // pid controller function prototype
121 extern int16_t axisPID
[XYZ_AXIS_COUNT
];
122 extern int32_t axisPID_P
[3], axisPID_I
[3], axisPID_D
[3];
123 bool airmodeWasActivated
;
124 extern uint32_t targetPidLooptime
;
126 void pidSetController(pidControllerType_e type
);
127 void pidResetErrorGyroState(void);
128 void pidStabilisationState(pidStabilisationState_e pidControllerState
);
129 void setTargetPidLooptime(uint8_t pidProcessDenom
);