mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-24 16:55:36 +03:00
136 lines
5.3 KiB
C
136 lines
5.3 KiB
C
/*
|
|
* This file is part of Cleanflight.
|
|
*
|
|
* Cleanflight is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Cleanflight is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#ifndef USE_OSD_SLAVE
|
|
#include <stdbool.h>
|
|
#include "common/time.h"
|
|
#include "config/parameter_group.h"
|
|
|
|
#define MAX_PID_PROCESS_DENOM 16
|
|
#define PID_CONTROLLER_BETAFLIGHT 1
|
|
#define PID_MIXER_SCALING 1000.0f
|
|
#define PID_SERVO_MIXER_SCALING 0.7f
|
|
#define PIDSUM_LIMIT 500
|
|
#define PIDSUM_LIMIT_YAW 400
|
|
|
|
// 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
|
|
#define PTERM_SCALE 0.032029f
|
|
#define ITERM_SCALE 0.244381f
|
|
#define DTERM_SCALE 0.000529f
|
|
|
|
typedef enum {
|
|
PID_ROLL,
|
|
PID_PITCH,
|
|
PID_YAW,
|
|
PID_ALT,
|
|
PID_POS,
|
|
PID_POSR,
|
|
PID_NAVR,
|
|
PID_LEVEL,
|
|
PID_MAG,
|
|
PID_VEL,
|
|
PID_ITEM_COUNT
|
|
} pidIndex_e;
|
|
|
|
typedef enum {
|
|
SUPEREXPO_YAW_OFF = 0,
|
|
SUPEREXPO_YAW_ON,
|
|
SUPEREXPO_YAW_ALWAYS
|
|
} pidSuperExpoYaw_e;
|
|
|
|
typedef enum {
|
|
PID_STABILISATION_OFF = 0,
|
|
PID_STABILISATION_ON
|
|
} pidStabilisationState_e;
|
|
|
|
typedef enum {
|
|
PID_CRASH_RECOVERY_OFF = 0,
|
|
PID_CRASH_RECOVERY_ON,
|
|
PID_CRASH_RECOVERY_BEEP
|
|
} pidCrashRecovery_e;
|
|
|
|
typedef struct pid8_s {
|
|
uint8_t P;
|
|
uint8_t I;
|
|
uint8_t D;
|
|
} pid8_t;
|
|
|
|
typedef struct pidProfile_s {
|
|
pid8_t pid[PID_ITEM_COUNT];
|
|
|
|
uint16_t yaw_lpf_hz; // Additional yaw filter when yaw axis too noisy
|
|
uint16_t dterm_lpf_hz; // Delta Filter in hz
|
|
uint16_t dterm_notch_hz; // Biquad dterm notch hz
|
|
uint16_t dterm_notch_cutoff; // Biquad dterm notch low cutoff
|
|
uint8_t dterm_filter_type; // Filter selection for dterm
|
|
uint8_t itermWindupPointPercent; // Experimental ITerm windup threshold, percent motor saturation
|
|
uint16_t pidSumLimit;
|
|
uint16_t pidSumLimitYaw;
|
|
uint8_t vbatPidCompensation; // Scale PIDsum to battery voltage
|
|
uint8_t pidAtMinThrottle; // Disable/Enable pids on zero throttle. Normally even without airmode P and D would be active.
|
|
uint8_t levelAngleLimit; // Max angle in degrees in level mode
|
|
uint8_t levelSensitivity; // Angle mode sensitivity reflected in degrees assuming user using full stick
|
|
|
|
uint8_t horizon_tilt_effect; // inclination factor for Horizon mode
|
|
uint8_t horizon_tilt_expert_mode; // OFF or ON
|
|
|
|
// Betaflight PID controller parameters
|
|
uint16_t itermThrottleThreshold; // max allowed throttle delta before iterm accelerated in ms
|
|
uint16_t itermAcceleratorGain; // Iterm Accelerator Gain when itermThrottlethreshold is hit
|
|
uint8_t setpointRelaxRatio; // Setpoint weight relaxation effect
|
|
uint8_t dtermSetpointWeight; // Setpoint weight for Dterm (0= measurement, 1= full error, 1 > aggressive derivative)
|
|
uint16_t yawRateAccelLimit; // yaw accel limiter for deg/sec/ms
|
|
uint16_t rateAccelLimit; // accel limiter roll/pitch deg/sec/ms
|
|
uint16_t crash_dthreshold; // dterm crash value
|
|
uint16_t crash_gthreshold; // gyro crash value
|
|
uint16_t crash_setpoint_threshold; // setpoint must be below this value to detect crash, so flips and rolls are not interpreted as crashes
|
|
uint16_t crash_time; // ms
|
|
uint16_t crash_delay; // ms
|
|
uint8_t crash_recovery_angle; // degrees
|
|
uint8_t crash_recovery_rate; // degree/second
|
|
pidCrashRecovery_e crash_recovery; // off, on, on and beeps when it is in crash recovery mode
|
|
} pidProfile_t;
|
|
|
|
PG_DECLARE_ARRAY(pidProfile_t, MAX_PROFILE_COUNT, pidProfiles);
|
|
|
|
typedef struct pidConfig_s {
|
|
uint8_t pid_process_denom; // Processing denominator for PID controller vs gyro sampling rate
|
|
} pidConfig_t;
|
|
|
|
PG_DECLARE(pidConfig_t, pidConfig);
|
|
|
|
union rollAndPitchTrims_u;
|
|
void pidController(const pidProfile_t *pidProfile, const union rollAndPitchTrims_u *angleTrim, timeUs_t currentTimeUs);
|
|
|
|
extern float axisPID_P[3], axisPID_I[3], axisPID_D[3];
|
|
bool airmodeWasActivated;
|
|
extern uint32_t targetPidLooptime;
|
|
|
|
// PIDweight is a scale factor for PIDs which is derived from the throttle and TPA setting, and 100 = 100% scale means no PID reduction
|
|
extern uint8_t PIDweight[3];
|
|
|
|
void pidResetErrorGyroState(void);
|
|
void pidStabilisationState(pidStabilisationState_e pidControllerState);
|
|
void pidSetTargetLooptime(uint32_t pidLooptime);
|
|
void pidSetItermAccelerator(float newItermAccelerator);
|
|
void pidInitFilters(const pidProfile_t *pidProfile);
|
|
void pidInitConfig(const pidProfile_t *pidProfile);
|
|
void pidInit(const pidProfile_t *pidProfile);
|
|
|
|
#endif
|