Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AC_CustomControl/AC_CustomControl_PID.cpp
Views: 1798
#include "AC_CustomControl_config.h"12#if AP_CUSTOMCONTROL_PID_ENABLED34#include "AC_CustomControl_PID.h"5#include "AC_AttitudeControl/AC_AttitudeControl_Multi.h"67// table of user settable parameters8const AP_Param::GroupInfo AC_CustomControl_PID::var_info[] = {9// @Param: ANG_RLL_P10// @DisplayName: Roll axis angle controller P gain11// @Description: Roll axis angle controller P gain. Converts the error between the desired roll angle and actual angle to a desired roll rate12// @Range: 3.000 12.00013// @Range{Sub}: 0.0 12.00014// @User: Standard15AP_SUBGROUPINFO(_p_angle_roll2, "ANG_RLL_", 1, AC_CustomControl_PID, AC_P),1617// @Param: ANG_PIT_P18// @DisplayName: Pitch axis angle controller P gain19// @Description: Pitch axis angle controller P gain. Converts the error between the desired pitch angle and actual angle to a desired pitch rate20// @Range: 3.000 12.00021// @Range{Sub}: 0.0 12.00022// @User: Standard23AP_SUBGROUPINFO(_p_angle_pitch2, "ANG_PIT_", 2, AC_CustomControl_PID, AC_P),2425// @Param: ANG_YAW_P26// @DisplayName: Yaw axis angle controller P gain27// @Description: Yaw axis angle controller P gain. Converts the error between the desired yaw angle and actual angle to a desired yaw rate28// @Range: 3.000 12.00029// @Range{Sub}: 0.0 6.00030// @User: Standard31AP_SUBGROUPINFO(_p_angle_yaw2, "ANG_YAW_", 3, AC_CustomControl_PID, AC_P),323334// @Param: RAT_RLL_P35// @DisplayName: Roll axis rate controller P gain36// @Description: Roll axis rate controller P gain. Corrects in proportion to the difference between the desired roll rate vs actual roll rate37// @Range: 0.01 0.538// @Increment: 0.00539// @User: Standard4041// @Param: RAT_RLL_I42// @DisplayName: Roll axis rate controller I gain43// @Description: Roll axis rate controller I gain. Corrects long-term difference in desired roll rate vs actual roll rate44// @Range: 0.01 2.045// @Increment: 0.0146// @User: Standard4748// @Param: RAT_RLL_IMAX49// @DisplayName: Roll axis rate controller I gain maximum50// @Description: Roll axis rate controller I gain maximum. Constrains the maximum that the I term will output51// @Range: 0 152// @Increment: 0.0153// @User: Standard5455// @Param: RAT_RLL_D56// @DisplayName: Roll axis rate controller D gain57// @Description: Roll axis rate controller D gain. Compensates for short-term change in desired roll rate vs actual roll rate58// @Range: 0.0 0.0559// @Increment: 0.00160// @User: Standard6162// @Param: RAT_RLL_FF63// @DisplayName: Roll axis rate controller feed forward64// @Description: Roll axis rate controller feed forward65// @Range: 0 0.566// @Increment: 0.00167// @User: Standard6869// @Param: RAT_RLL_FLTT70// @DisplayName: Roll axis rate controller target frequency in Hz71// @Description: Roll axis rate controller target frequency in Hz72// @Range: 5 10073// @Increment: 174// @Units: Hz75// @User: Standard7677// @Param: RAT_RLL_FLTE78// @DisplayName: Roll axis rate controller error frequency in Hz79// @Description: Roll axis rate controller error frequency in Hz80// @Range: 0 10081// @Increment: 182// @Units: Hz83// @User: Standard8485// @Param: RAT_RLL_FLTD86// @DisplayName: Roll axis rate controller derivative frequency in Hz87// @Description: Roll axis rate controller derivative frequency in Hz88// @Range: 5 10089// @Increment: 190// @Units: Hz91// @User: Standard9293// @Param: RAT_RLL_SMAX94// @DisplayName: Roll slew rate limit95// @Description: Sets an upper limit on the slew rate produced by the combined P and D gains. If the amplitude of the control action produced by the rate feedback exceeds this value, then the D+P gain is reduced to respect the limit. This limits the amplitude of high frequency oscillations caused by an excessive gain. The limit should be set to no more than 25% of the actuators maximum slew rate to allow for load effects. Note: The gain will not be reduced to less than 10% of the nominal value. A value of zero will disable this feature.96// @Range: 0 20097// @Increment: 0.598// @User: Advanced99100// @Param: RAT_RLL_PDMX101// @DisplayName: Roll axis rate controller PD sum maximum102// @Description: Roll axis rate controller PD sum maximum. The maximum/minimum value that the sum of the P and D term can output103// @Range: 0 1104// @Increment: 0.01105// @User: Advanced106107// @Param: RAT_RLL_D_FF108// @DisplayName: Roll Derivative FeedForward Gain109// @Description: FF D Gain which produces an output that is proportional to the rate of change of the target110// @Range: 0 0.02111// @Increment: 0.0001112// @User: Advanced113114// @Param: RAT_RLL_NTF115// @DisplayName: Roll Target notch filter index116// @Description: Roll Target notch filter index117// @Range: 1 8118// @User: Advanced119120// @Param: RAT_RLL_NEF121// @DisplayName: Roll Error notch filter index122// @Description: Roll Error notch filter index123// @Range: 1 8124// @User: Advanced125126AP_SUBGROUPINFO(_pid_atti_rate_roll, "RAT_RLL_", 4, AC_CustomControl_PID, AC_PID),127128// @Param: RAT_PIT_P129// @DisplayName: Pitch axis rate controller P gain130// @Description: Pitch axis rate controller P gain. Corrects in proportion to the difference between the desired pitch rate vs actual pitch rate131// @Range: 0.01 0.50132// @Increment: 0.005133// @User: Standard134135// @Param: RAT_PIT_I136// @DisplayName: Pitch axis rate controller I gain137// @Description: Pitch axis rate controller I gain. Corrects long-term difference in desired pitch rate vs actual pitch rate138// @Range: 0.01 2.0139// @Increment: 0.01140// @User: Standard141142// @Param: RAT_PIT_IMAX143// @DisplayName: Pitch axis rate controller I gain maximum144// @Description: Pitch axis rate controller I gain maximum. Constrains the maximum that the I term will output145// @Range: 0 1146// @Increment: 0.01147// @User: Standard148149// @Param: RAT_PIT_D150// @DisplayName: Pitch axis rate controller D gain151// @Description: Pitch axis rate controller D gain. Compensates for short-term change in desired pitch rate vs actual pitch rate152// @Range: 0.0 0.05153// @Increment: 0.001154// @User: Standard155156// @Param: RAT_PIT_FF157// @DisplayName: Pitch axis rate controller feed forward158// @Description: Pitch axis rate controller feed forward159// @Range: 0 0.5160// @Increment: 0.001161// @User: Standard162163// @Param: RAT_PIT_FLTT164// @DisplayName: Pitch axis rate controller target frequency in Hz165// @Description: Pitch axis rate controller target frequency in Hz166// @Range: 5 100167// @Increment: 1168// @Units: Hz169// @User: Standard170171// @Param: RAT_PIT_FLTE172// @DisplayName: Pitch axis rate controller error frequency in Hz173// @Description: Pitch axis rate controller error frequency in Hz174// @Range: 0 100175// @Increment: 1176// @Units: Hz177// @User: Standard178179// @Param: RAT_PIT_FLTD180// @DisplayName: Pitch axis rate controller derivative frequency in Hz181// @Description: Pitch axis rate controller derivative frequency in Hz182// @Range: 5 100183// @Increment: 1184// @Units: Hz185// @User: Standard186187// @Param: RAT_PIT_SMAX188// @DisplayName: Pitch slew rate limit189// @Description: Sets an upper limit on the slew rate produced by the combined P and D gains. If the amplitude of the control action produced by the rate feedback exceeds this value, then the D+P gain is reduced to respect the limit. This limits the amplitude of high frequency oscillations caused by an excessive gain. The limit should be set to no more than 25% of the actuators maximum slew rate to allow for load effects. Note: The gain will not be reduced to less than 10% of the nominal value. A value of zero will disable this feature.190// @Range: 0 200191// @Increment: 0.5192// @User: Advanced193194// @Param: RAT_PIT_PDMX195// @DisplayName: Pitch axis rate controller PD sum maximum196// @Description: Pitch axis rate controller PD sum maximum. The maximum/minimum value that the sum of the P and D term can output197// @Range: 0 1198// @Increment: 0.01199// @User: Advanced200201// @Param: RAT_PIT_D_FF202// @DisplayName: Pitch Derivative FeedForward Gain203// @Description: FF D Gain which produces an output that is proportional to the rate of change of the target204// @Range: 0 0.02205// @Increment: 0.0001206// @User: Advanced207208// @Param: RAT_PIT_NTF209// @DisplayName: Pitch Target notch filter index210// @Description: Pitch Target notch filter index211// @Range: 1 8212// @User: Advanced213214// @Param: RAT_PIT_NEF215// @DisplayName: Pitch Error notch filter index216// @Description: Pitch Error notch filter index217// @Range: 1 8218// @User: Advanced219220AP_SUBGROUPINFO(_pid_atti_rate_pitch, "RAT_PIT_", 5, AC_CustomControl_PID, AC_PID),221222223// @Param: RAT_YAW_P224// @DisplayName: Yaw axis rate controller P gain225// @Description: Yaw axis rate controller P gain. Corrects in proportion to the difference between the desired yaw rate vs actual yaw rate226// @Range: 0.10 2.50227// @Increment: 0.005228// @User: Standard229230// @Param: RAT_YAW_I231// @DisplayName: Yaw axis rate controller I gain232// @Description: Yaw axis rate controller I gain. Corrects long-term difference in desired yaw rate vs actual yaw rate233// @Range: 0.010 1.0234// @Increment: 0.01235// @User: Standard236237// @Param: RAT_YAW_IMAX238// @DisplayName: Yaw axis rate controller I gain maximum239// @Description: Yaw axis rate controller I gain maximum. Constrains the maximum that the I term will output240// @Range: 0 1241// @Increment: 0.01242// @User: Standard243244// @Param: RAT_YAW_D245// @DisplayName: Yaw axis rate controller D gain246// @Description: Yaw axis rate controller D gain. Compensates for short-term change in desired yaw rate vs actual yaw rate247// @Range: 0.000 0.02248// @Increment: 0.001249// @User: Standard250251// @Param: RAT_YAW_FF252// @DisplayName: Yaw axis rate controller feed forward253// @Description: Yaw axis rate controller feed forward254// @Range: 0 0.5255// @Increment: 0.001256// @User: Standard257258// @Param: RAT_YAW_FLTT259// @DisplayName: Yaw axis rate controller target frequency in Hz260// @Description: Yaw axis rate controller target frequency in Hz261// @Range: 1 50262// @Increment: 1263// @Units: Hz264// @User: Standard265266// @Param: RAT_YAW_FLTE267// @DisplayName: Yaw axis rate controller error frequency in Hz268// @Description: Yaw axis rate controller error frequency in Hz269// @Range: 0 20270// @Increment: 1271// @Units: Hz272// @User: Standard273274// @Param: RAT_YAW_FLTD275// @DisplayName: Yaw axis rate controller derivative frequency in Hz276// @Description: Yaw axis rate controller derivative frequency in Hz277// @Range: 5 50278// @Increment: 1279// @Units: Hz280// @User: Standard281282// @Param: RAT_YAW_SMAX283// @DisplayName: Yaw slew rate limit284// @Description: Sets an upper limit on the slew rate produced by the combined P and D gains. If the amplitude of the control action produced by the rate feedback exceeds this value, then the D+P gain is reduced to respect the limit. This limits the amplitude of high frequency oscillations caused by an excessive gain. The limit should be set to no more than 25% of the actuators maximum slew rate to allow for load effects. Note: The gain will not be reduced to less than 10% of the nominal value. A value of zero will disable this feature.285// @Range: 0 200286// @Increment: 0.5287// @User: Advanced288289// @Param: RAT_YAW_PDMX290// @DisplayName: Yaw axis rate controller PD sum maximum291// @Description: Yaw axis rate controller PD sum maximum. The maximum/minimum value that the sum of the P and D term can output292// @Range: 0 1293// @Increment: 0.01294// @User: Advanced295296// @Param: RAT_YAW_D_FF297// @DisplayName: Yaw Derivative FeedForward Gain298// @Description: FF D Gain which produces an output that is proportional to the rate of change of the target299// @Range: 0 0.02300// @Increment: 0.0001301// @User: Advanced302303// @Param: RAT_YAW_NTF304// @DisplayName: Yaw Target notch filter index305// @Description: Yaw Target notch filter index306// @Range: 1 8307// @User: Advanced308309// @Param: RAT_YAW_NEF310// @DisplayName: Yaw Error notch filter index311// @Description: Yaw Error notch filter index312// @Range: 1 8313// @User: Advanced314315AP_SUBGROUPINFO(_pid_atti_rate_yaw, "RAT_YAW_", 6, AC_CustomControl_PID, AC_PID),316317AP_GROUPEND318};319320AC_CustomControl_PID::AC_CustomControl_PID(AC_CustomControl& frontend, AP_AHRS_View*& ahrs, AC_AttitudeControl*& att_control, AP_MotorsMulticopter*& motors, float dt) :321AC_CustomControl_Backend(frontend, ahrs, att_control, motors, dt),322_p_angle_roll2(AC_ATTITUDE_CONTROL_ANGLE_P * 0.90f),323_p_angle_pitch2(AC_ATTITUDE_CONTROL_ANGLE_P * 0.90f),324_p_angle_yaw2(AC_ATTITUDE_CONTROL_ANGLE_P * 0.90f),325_pid_atti_rate_roll(AC_ATC_MULTI_RATE_RP_P * 0.90f, AC_ATC_MULTI_RATE_RP_I * 0.90f, AC_ATC_MULTI_RATE_RP_D * 0.90f, 0.0f, AC_ATC_MULTI_RATE_RP_IMAX * 0.90f, AC_ATC_MULTI_RATE_RPY_FILT_HZ * 0.90f, 0.0f, AC_ATC_MULTI_RATE_RPY_FILT_HZ * 0.90f),326_pid_atti_rate_pitch(AC_ATC_MULTI_RATE_RP_P * 0.90f, AC_ATC_MULTI_RATE_RP_I * 0.90f, AC_ATC_MULTI_RATE_RP_D * 0.90f, 0.0f, AC_ATC_MULTI_RATE_RP_IMAX * 0.90f, AC_ATC_MULTI_RATE_RPY_FILT_HZ * 0.90f, 0.0f, AC_ATC_MULTI_RATE_RPY_FILT_HZ * 0.90f),327_pid_atti_rate_yaw(AC_ATC_MULTI_RATE_YAW_P * 0.90f, AC_ATC_MULTI_RATE_YAW_I * 0.90f, AC_ATC_MULTI_RATE_YAW_D * 0.90f, 0.0f, AC_ATC_MULTI_RATE_YAW_IMAX * 0.90f, AC_ATC_MULTI_RATE_RPY_FILT_HZ * 0.90f, AC_ATC_MULTI_RATE_YAW_FILT_HZ * 0.90f, 0.0f)328{329_dt = dt;330AP_Param::setup_object_defaults(this, var_info);331}332333Vector3f AC_CustomControl_PID::update()334{335// reset controller based on spool state336switch (_motors->get_spool_state()) {337case AP_Motors::SpoolState::SHUT_DOWN:338case AP_Motors::SpoolState::GROUND_IDLE:339// We are still at the ground. Reset custom controller to avoid340// build up, ex: integrator341reset();342break;343344case AP_Motors::SpoolState::THROTTLE_UNLIMITED:345case AP_Motors::SpoolState::SPOOLING_UP:346case AP_Motors::SpoolState::SPOOLING_DOWN:347// we are off the ground348break;349}350351// run custom controller after here352Quaternion attitude_body, attitude_target;353_ahrs->get_quat_body_to_ned(attitude_body);354355attitude_target = _att_control->get_attitude_target_quat();356// This vector represents the angular error to rotate the thrust vector using x and y and heading using z357Vector3f attitude_error;358float _thrust_angle, _thrust_error_angle;359_att_control->thrust_heading_rotation_angles(attitude_target, attitude_body, attitude_error, _thrust_angle, _thrust_error_angle);360361// recalculate ang vel feedforward from attitude target model362// rotation from the target frame to the body frame363Quaternion rotation_target_to_body = attitude_body.inverse() * attitude_target;364// target angle velocity vector in the body frame365Vector3f ang_vel_body_feedforward = rotation_target_to_body * _att_control->get_attitude_target_ang_vel();366367// run attitude controller368Vector3f target_rate;369target_rate[0] = _p_angle_roll2.kP() * attitude_error.x + ang_vel_body_feedforward[0];370target_rate[1] = _p_angle_pitch2.kP() * attitude_error.y + ang_vel_body_feedforward[1];371target_rate[2] = _p_angle_yaw2.kP() * attitude_error.z + ang_vel_body_feedforward[2];372373// run rate controller374Vector3f gyro_latest = _ahrs->get_gyro_latest();375Vector3f motor_out;376motor_out.x = _pid_atti_rate_roll.update_all(target_rate[0], gyro_latest[0], _dt, false);377motor_out.y = _pid_atti_rate_pitch.update_all(target_rate[1], gyro_latest[1], _dt, false);378motor_out.z = _pid_atti_rate_yaw.update_all(target_rate[2], gyro_latest[2], _dt, false);379380return motor_out;381}382383// This example uses exact same controller architecture as ArduCopter attitude controller without all the safe guard against saturation.384// The gains are scaled 0.9 times to better detect switch over response.385// Note that integrator are not reset correctly as it is done in reset_main_att_controller inside AC_CustomControl.cpp386// This is done intentionally to demonstrate switch over performance of two exact controller with different reset handling.387void AC_CustomControl_PID::reset(void)388{389_pid_atti_rate_roll.reset_I();390_pid_atti_rate_pitch.reset_I();391_pid_atti_rate_yaw.reset_I();392_pid_atti_rate_roll.reset_filter();393_pid_atti_rate_pitch.reset_filter();394_pid_atti_rate_yaw.reset_filter();395}396397398void AC_CustomControl_PID::set_notch_sample_rate(float sample_rate)399{400#if AP_FILTER_ENABLED401_pid_atti_rate_roll.set_notch_sample_rate(sample_rate);402_pid_atti_rate_pitch.set_notch_sample_rate(sample_rate);403_pid_atti_rate_yaw.set_notch_sample_rate(sample_rate);404#endif405}406407#endif // AP_CUSTOMCONTROL_PID_ENABLED408409410