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/APM_Control/AR_AttitudeControl.cpp
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/1415#include <AP_AHRS/AP_AHRS.h>16#include <AP_Math/AP_Math.h>17#include <AP_HAL/AP_HAL.h>18#include "AR_AttitudeControl.h"19#include <AP_GPS/AP_GPS.h>2021// attitude control default definition22#define AR_ATTCONTROL_STEER_ANG_P 2.00f23#define AR_ATTCONTROL_STEER_RATE_FF 0.20f24#define AR_ATTCONTROL_STEER_RATE_P 0.20f25#define AR_ATTCONTROL_STEER_RATE_I 0.20f26#define AR_ATTCONTROL_STEER_RATE_IMAX 1.00f27#define AR_ATTCONTROL_STEER_RATE_D 0.00f28#define AR_ATTCONTROL_STEER_RATE_FILT 10.00f29#define AR_ATTCONTROL_STEER_RATE_MAX 120.0f30#define AR_ATTCONTROL_STEER_ACCEL_MAX 120.0f31#define AR_ATTCONTROL_THR_SPEED_P 0.20f32#define AR_ATTCONTROL_THR_SPEED_I 0.20f33#define AR_ATTCONTROL_THR_SPEED_IMAX 1.00f34#define AR_ATTCONTROL_THR_SPEED_D 0.00f35#define AR_ATTCONTROL_THR_SPEED_FILT 10.00f36#define AR_ATTCONTROL_PITCH_THR_P 1.80f37#define AR_ATTCONTROL_PITCH_THR_I 1.50f38#define AR_ATTCONTROL_PITCH_THR_D 0.03f39#define AR_ATTCONTROL_PITCH_THR_IMAX 1.0f40#define AR_ATTCONTROL_PITCH_THR_FILT 10.0f41#define AR_ATTCONTROL_BAL_PITCH_FF 0.4f42#define AR_ATTCONTROL_PITCH_LIM_TC 0.5f // pitch limit default time constant43#define AR_ATTCONTROL_PITCH_RELAX_RATIO 0.5f // pitch limit relaxed 2x slower than it is limited44#define AR_ATTCONTROL_PITCH_LIM_THR_THRESH 0.60 // pitch limiting starts if throttle exceeds 60%45#define AR_ATTCONTROL_DT 0.02f46#define AR_ATTCONTROL_TIMEOUT_MS 20047#define AR_ATTCONTROL_HEEL_SAIL_P 1.0f48#define AR_ATTCONTROL_HEEL_SAIL_I 0.1f49#define AR_ATTCONTROL_HEEL_SAIL_D 0.0f50#define AR_ATTCONTROL_HEEL_SAIL_IMAX 1.0f51#define AR_ATTCONTROL_HEEL_SAIL_FILT 10.0f52#define AR_ATTCONTROL_DT 0.02f5354// throttle/speed control maximum acceleration/deceleration (in m/s) (_ACCEL_MAX parameter default)55#define AR_ATTCONTROL_THR_ACCEL_MAX 1.00f5657// minimum speed in m/s58#define AR_ATTCONTROL_STEER_SPEED_MIN 1.0f5960// speed (in m/s) at or below which vehicle is considered stopped (_STOP_SPEED parameter default)61#define AR_ATTCONTROL_STOP_SPEED_DEFAULT 0.1f6263extern const AP_HAL::HAL& hal;6465AR_AttitudeControl *AR_AttitudeControl::_singleton;6667const AP_Param::GroupInfo AR_AttitudeControl::var_info[] = {6869// @Param: _STR_RAT_P70// @DisplayName: Steering control rate P gain71// @Description: Steering control rate P gain. Converts the turn rate error (in radians/sec) to a steering control output (in the range -1 to +1)72// @Range: 0.000 2.00073// @Increment: 0.00174// @User: Standard7576// @Param: _STR_RAT_I77// @DisplayName: Steering control I gain78// @Description: Steering control I gain. Corrects long term error between the desired turn rate (in rad/s) and actual79// @Range: 0.000 2.00080// @Increment: 0.00181// @User: Standard8283// @Param: _STR_RAT_IMAX84// @DisplayName: Steering control I gain maximum85// @Description: Steering control I gain maximum. Constrains the steering output (range -1 to +1) that the I term will generate86// @Range: 0.000 1.00087// @Increment: 0.0188// @User: Standard8990// @Param: _STR_RAT_D91// @DisplayName: Steering control D gain92// @Description: Steering control D gain. Compensates for short-term change in desired turn rate vs actual93// @Range: 0.000 0.40094// @Increment: 0.00195// @User: Standard9697// @Param: _STR_RAT_FF98// @DisplayName: Steering control feed forward99// @Description: Steering control feed forward100// @Range: 0.000 3.000101// @Increment: 0.001102// @User: Standard103104// @Param: _STR_RAT_FILT105// @DisplayName: Steering control filter frequency106// @Description: Steering control input filter. Lower values reduce noise but add delay.107// @Range: 0.000 100.000108// @Increment: 0.1109// @Units: Hz110// @User: Standard111112// @Param: _STR_RAT_FLTT113// @DisplayName: Steering control Target filter frequency in Hz114// @Description: Target filter frequency in Hz115// @Range: 0.000 100.000116// @Increment: 0.1117// @Units: Hz118// @User: Standard119120// @Param: _STR_RAT_FLTE121// @DisplayName: Steering control Error filter frequency in Hz122// @Description: Error filter frequency in Hz123// @Range: 0.000 100.000124// @Increment: 0.1125// @Units: Hz126// @User: Standard127128// @Param: _STR_RAT_FLTD129// @DisplayName: Steering control Derivative term filter frequency in Hz130// @Description: Derivative filter frequency in Hz131// @Range: 0.000 100.000132// @Increment: 0.1133// @Units: Hz134// @User: Standard135136// @Param: _STR_RAT_SMAX137// @DisplayName: Steering slew rate limit138// @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.139// @Range: 0 200140// @Increment: 0.5141// @User: Advanced142143// @Param: _STR_RAT_PDMX144// @DisplayName: Steering control PD sum maximum145// @Description: Steering control PD sum maximum. The maximum/minimum value that the sum of the P and D term can output146// @Range: 0.000 1.000147// @Increment: 0.01148149// @Param: _STR_RAT_D_FF150// @DisplayName: Steering control Derivative FeedForward Gain151// @Description: FF D Gain which produces an output that is proportional to the rate of change of the target152// @Range: 0 0.03153// @Increment: 0.001154// @User: Advanced155156// @Param: _STR_RAT_NTF157// @DisplayName: Steering control Target notch filter index158// @Description: Steering control Target notch filter index159// @Range: 1 8160// @User: Advanced161162// @Param: _STR_RAT_NEF163// @DisplayName: Steering control Error notch filter index164// @Description: Steering control Error notch filter index165// @Range: 1 8166// @User: Advanced167168AP_SUBGROUPINFO(_steer_rate_pid, "_STR_RAT_", 1, AR_AttitudeControl, AC_PID),169170// @Param: _SPEED_P171// @DisplayName: Speed control P gain172// @Description: Speed control P gain. Converts the error between the desired speed (in m/s) and actual speed to a motor output (in the range -1 to +1)173// @Range: 0.010 2.000174// @Increment: 0.01175// @User: Standard176177// @Param: _SPEED_I178// @DisplayName: Speed control I gain179// @Description: Speed control I gain. Corrects long term error between the desired speed (in m/s) and actual speed180// @Range: 0.000 2.000181// @Increment: 0.01182// @User: Standard183184// @Param: _SPEED_IMAX185// @DisplayName: Speed control I gain maximum186// @Description: Speed control I gain maximum. Constrains the maximum motor output (range -1 to +1) that the I term will generate187// @Range: 0.000 1.000188// @Increment: 0.01189// @User: Standard190191// @Param: _SPEED_D192// @DisplayName: Speed control D gain193// @Description: Speed control D gain. Compensates for short-term change in desired speed vs actual194// @Range: 0.000 0.400195// @Increment: 0.001196// @User: Standard197198// @Param: _SPEED_FF199// @DisplayName: Speed control feed forward200// @Description: Speed control feed forward201// @Range: 0.000 0.500202// @Increment: 0.001203// @User: Standard204205// @Param: _SPEED_FILT206// @DisplayName: Speed control filter frequency207// @Description: Speed control input filter. Lower values reduce noise but add delay.208// @Range: 0.000 100.000209// @Increment: 0.1210// @Units: Hz211// @User: Standard212213// @Param: _SPEED_FLTT214// @DisplayName: Speed control Target filter frequency in Hz215// @Description: Target filter frequency in Hz216// @Range: 0.000 100.000217// @Increment: 0.1218// @Units: Hz219// @User: Standard220221// @Param: _SPEED_FLTE222// @DisplayName: Speed control Error filter frequency in Hz223// @Description: Error filter frequency in Hz224// @Range: 0.000 100.000225// @Increment: 0.1226// @Units: Hz227// @User: Standard228229// @Param: _SPEED_FLTD230// @DisplayName: Speed control Derivative term filter frequency in Hz231// @Description: Derivative filter frequency in Hz232// @Range: 0.000 100.000233// @Increment: 0.1234// @Units: Hz235// @User: Standard236237// @Param: _SPEED_SMAX238// @DisplayName: Speed control slew rate limit239// @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.240// @Range: 0 200241// @Increment: 0.5242// @User: Advanced243244// @Param: _SPEED_PDMX245// @DisplayName: Speed control PD sum maximum246// @Description: Speed control PD sum maximum. The maximum/minimum value that the sum of the P and D term can output247// @Range: 0.000 1.000248// @Increment: 0.01249250// @Param: _SPEED_D_FF251// @DisplayName: Speed control Derivative FeedForward Gain252// @Description: FF D Gain which produces an output that is proportional to the rate of change of the target253// @Range: 0 0.03254// @Increment: 0.001255// @User: Advanced256257// @Param: _SPEED_NTF258// @DisplayName: Speed control Target notch filter index259// @Description: Speed control Target notch filter index260// @Range: 1 8261// @User: Advanced262263// @Param: _SPEED_NEF264// @DisplayName: Speed control Error notch filter index265// @Description: Speed control Error notch filter index266// @Range: 1 8267// @User: Advanced268269AP_SUBGROUPINFO(_throttle_speed_pid, "_SPEED_", 2, AR_AttitudeControl, AC_PID),270271// @Param: _ACCEL_MAX272// @DisplayName: Speed control acceleration (and deceleration) maximum in m/s/s273// @Description: Speed control acceleration (and deceleration) maximum in m/s/s. 0 to disable acceleration limiting274// @Range: 0.0 10.0275// @Increment: 0.1276// @Units: m/s/s277// @User: Standard278AP_GROUPINFO("_ACCEL_MAX", 3, AR_AttitudeControl, _throttle_accel_max, AR_ATTCONTROL_THR_ACCEL_MAX),279280// @Param: _BRAKE281// @DisplayName: Speed control brake enable/disable282// @Description: Speed control brake enable/disable. Allows sending a reversed output to the motors to slow the vehicle.283// @Values: 0:Disable,1:Enable284// @User: Standard285AP_GROUPINFO("_BRAKE", 4, AR_AttitudeControl, _brake_enable, 1),286287// @Param: _STOP_SPEED288// @DisplayName: Speed control stop speed289// @Description: Speed control stop speed. Motor outputs to zero once vehicle speed falls below this value290// @Range: 0.00 0.50291// @Increment: 0.01292// @Units: m/s293// @User: Standard294AP_GROUPINFO("_STOP_SPEED", 5, AR_AttitudeControl, _stop_speed, AR_ATTCONTROL_STOP_SPEED_DEFAULT),295296// @Param: _STR_ANG_P297// @DisplayName: Steering control angle P gain298// @Description: Steering control angle P gain. Converts the error between the desired heading/yaw (in radians) and actual heading/yaw to a desired turn rate (in rad/sec)299// @Range: 1.000 10.000300// @Increment: 0.1301// @User: Standard302AP_SUBGROUPINFO(_steer_angle_p, "_STR_ANG_", 6, AR_AttitudeControl, AC_P),303304// @Param: _STR_ACC_MAX305// @DisplayName: Steering control angular acceleration maximum306// @Description: Steering control angular acceleration maximum (in deg/s/s). 0 to disable acceleration limiting307// @Range: 0 1000308// @Increment: 0.1309// @Units: deg/s/s310// @User: Standard311AP_GROUPINFO("_STR_ACC_MAX", 7, AR_AttitudeControl, _steer_accel_max, AR_ATTCONTROL_STEER_ACCEL_MAX),312313// @Param: _STR_RAT_MAX314// @DisplayName: Steering control rotation rate maximum315// @Description: Steering control rotation rate maximum in deg/s. 0 to remove rate limiting316// @Range: 0 1000317// @Increment: 0.1318// @Units: deg/s319// @User: Standard320AP_GROUPINFO("_STR_RAT_MAX", 8, AR_AttitudeControl, _steer_rate_max, AR_ATTCONTROL_STEER_RATE_MAX),321322// @Param: _DECEL_MAX323// @DisplayName: Speed control deceleration maximum in m/s/s324// @Description: Speed control and deceleration maximum in m/s/s. 0 to use ATC_ACCEL_MAX for deceleration325// @Range: 0.0 10.0326// @Increment: 0.1327// @Units: m/s/s328// @User: Standard329AP_GROUPINFO("_DECEL_MAX", 9, AR_AttitudeControl, _throttle_decel_max, 0.00f),330331// @Param: _BAL_P332// @DisplayName: Pitch control P gain333// @Description: Pitch control P gain for BalanceBots. Converts the error between the desired pitch (in radians) and actual pitch to a motor output (in the range -1 to +1)334// @Range: 0.000 2.000335// @Increment: 0.01336// @User: Standard337338// @Param: _BAL_I339// @DisplayName: Pitch control I gain340// @Description: Pitch control I gain for BalanceBots. Corrects long term error between the desired pitch (in radians) and actual pitch341// @Range: 0.000 2.000342// @Increment: 0.01343// @User: Standard344345// @Param: _BAL_IMAX346// @DisplayName: Pitch control I gain maximum347// @Description: Pitch control I gain maximum. Constrains the maximum motor output (range -1 to +1) that the I term will generate348// @Range: 0.000 1.000349// @Increment: 0.01350// @User: Standard351352// @Param: _BAL_D353// @DisplayName: Pitch control D gain354// @Description: Pitch control D gain. Compensates for short-term change in desired pitch vs actual355// @Range: 0.000 0.100356// @Increment: 0.001357// @User: Standard358359// @Param: _BAL_FF360// @DisplayName: Pitch control feed forward361// @Description: Pitch control feed forward362// @Range: 0.000 0.500363// @Increment: 0.001364// @User: Standard365366// @Param: _BAL_FILT367// @DisplayName: Pitch control filter frequency368// @Description: Pitch control input filter. Lower values reduce noise but add delay.369// @Range: 0.000 100.000370// @Increment: 0.1371// @Units: Hz372// @User: Standard373374// @Param: _BAL_FLTT375// @DisplayName: Pitch control Target filter frequency in Hz376// @Description: Pitch control Target filter frequency in Hz377// @Range: 0.000 100.000378// @Increment: 0.1379// @Units: Hz380// @User: Standard381382// @Param: _BAL_FLTE383// @DisplayName: Pitch control Error filter frequency in Hz384// @Description: Pitch control Error filter frequency in Hz385// @Range: 0.000 100.000386// @Increment: 0.1387// @Units: Hz388// @User: Standard389390// @Param: _BAL_FLTD391// @DisplayName: Pitch control Derivative term filter frequency in Hz392// @Description: Pitch control Derivative filter frequency in Hz393// @Range: 0.000 100.000394// @Increment: 0.1395// @Units: Hz396// @User: Standard397398// @Param: _BAL_SMAX399// @DisplayName: Pitch control slew rate limit400// @Description: Pitch control 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.401// @Range: 0 200402// @Increment: 0.5403// @User: Advanced404405// @Param: _BAL_PDMX406// @DisplayName: Pitch control PD sum maximum407// @Description: Pitch control PD sum maximum. The maximum/minimum value that the sum of the P and D term can output408// @Range: 0.000 1.000409// @Increment: 0.01410411// @Param: _BAL_D_FF412// @DisplayName: Pitch control Derivative FeedForward Gain413// @Description: FF D Gain which produces an output that is proportional to the rate of change of the target414// @Range: 0 0.03415// @Increment: 0.001416// @User: Advanced417418// @Param: _BAL_NTF419// @DisplayName: Pitch control Target notch filter index420// @Description: Pitch control Target notch filter index421// @Range: 1 8422// @User: Advanced423424// @Param: _BAL_NEF425// @DisplayName: Pitch control Error notch filter index426// @Description: Pitch control Error notch filter index427// @Range: 1 8428// @User: Advanced429430AP_SUBGROUPINFO(_pitch_to_throttle_pid, "_BAL_", 10, AR_AttitudeControl, AC_PID),431432// @Param: _BAL_PIT_FF433// @DisplayName: Pitch control feed forward from current pitch angle434// @Description: Pitch control feed forward from current pitch angle435// @Range: 0.0 1.0436// @Increment: 0.01437// @User: Standard438AP_GROUPINFO("_BAL_PIT_FF", 11, AR_AttitudeControl, _pitch_to_throttle_ff, AR_ATTCONTROL_BAL_PITCH_FF),439440// @Param: _SAIL_P441// @DisplayName: Sail Heel control P gain442// @Description: Sail Heel control P gain for sailboats. Converts the error between the desired heel angle (in radians) and actual heel to a main sail output (in the range -1 to +1)443// @Range: 0.000 2.000444// @Increment: 0.01445// @User: Standard446447// @Param: _SAIL_I448// @DisplayName: Sail Heel control I gain449// @Description: Sail Heel control I gain for sailboats. Corrects long term error between the desired heel angle (in radians) and actual450// @Range: 0.000 2.000451// @Increment: 0.01452// @User: Standard453454// @Param: _SAIL_IMAX455// @DisplayName: Sail Heel control I gain maximum456// @Description: Sail Heel control I gain maximum. Constrains the maximum I term contribution to the main sail output (range -1 to +1)457// @Range: 0.000 1.000458// @Increment: 0.01459// @User: Standard460461// @Param: _SAIL_D462// @DisplayName: Sail Heel control D gain463// @Description: Sail Heel control D gain. Compensates for short-term change in desired heel angle vs actual464// @Range: 0.000 0.100465// @Increment: 0.001466// @User: Standard467468// @Param: _SAIL_FF469// @DisplayName: Sail Heel control feed forward470// @Description: Sail Heel control feed forward471// @Range: 0.000 0.500472// @Increment: 0.001473// @User: Standard474475// @Param: _SAIL_FILT476// @DisplayName: Sail Heel control filter frequency477// @Description: Sail Heel control input filter. Lower values reduce noise but add delay.478// @Range: 0.000 100.000479// @Increment: 0.1480// @Units: Hz481// @User: Standard482483// @Param: _SAIL_FLTT484// @DisplayName: Sail Heel Target filter frequency in Hz485// @Description: Target filter frequency in Hz486// @Range: 0.000 100.000487// @Increment: 0.1488// @Units: Hz489// @User: Standard490491// @Param: _SAIL_FLTE492// @DisplayName: Sail Heel Error filter frequency in Hz493// @Description: Error filter frequency in Hz494// @Range: 0.000 100.000495// @Increment: 0.1496// @Units: Hz497// @User: Standard498499// @Param: _SAIL_FLTD500// @DisplayName: Sail Heel Derivative term filter frequency in Hz501// @Description: Derivative filter frequency in Hz502// @Range: 0.000 100.000503// @Increment: 0.1504// @Units: Hz505// @User: Standard506507// @Param: _SAIL_SMAX508// @DisplayName: Sail heel slew rate limit509// @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.510// @Range: 0 200511// @Increment: 0.5512// @User: Advanced513514// @Param: _SAIL_PDMX515// @DisplayName: Sail Heel control PD sum maximum516// @Description: Sail Heel control PD sum maximum. The maximum/minimum value that the sum of the P and D term can output517// @Range: 0.000 1.000518// @Increment: 0.01519520// @Param: _SAIL_D_FF521// @DisplayName: Sail Heel Derivative FeedForward Gain522// @Description: FF D Gain which produces an output that is proportional to the rate of change of the target523// @Range: 0 0.03524// @Increment: 0.001525// @User: Advanced526527// @Param: _SAIL_NTF528// @DisplayName: Sail Heel Target notch filter index529// @Description: Sail Heel Target notch filter index530// @Range: 1 8531// @User: Advanced532533// @Param: _SAIL_NEF534// @DisplayName: Sail Heel Error notch filter index535// @Description: Sail Heel Error notch filter index536// @Range: 1 8537// @User: Advanced538539AP_SUBGROUPINFO(_sailboat_heel_pid, "_SAIL_", 12, AR_AttitudeControl, AC_PID),540541// @Param: _TURN_MAX_G542// @DisplayName: Turning maximum G force543// @Description: The maximum turning acceleration (in units of gravities) that the rover can handle while remaining stable. The navigation code will keep the lateral acceleration below this level to avoid rolling over or slipping the wheels in turns544// @Units: gravities545// @Range: 0.1 10546// @Increment: 0.01547// @User: Standard548AP_GROUPINFO("_TURN_MAX_G", 13, AR_AttitudeControl, _turn_lateral_G_max, 0.6f),549550// @Param: _BAL_LIM_TC551// @DisplayName: Pitch control limit time constant552// @Description: Pitch control limit time constant to protect against falling. Lower values limit pitch more quickly, higher values limit more slowly. Set to 0 to disable553// @Range: 0.0 5.0554// @Increment: 0.01555// @User: Standard556AP_GROUPINFO("_BAL_LIM_TC", 14, AR_AttitudeControl, _pitch_limit_tc, AR_ATTCONTROL_PITCH_LIM_TC),557558// @Param: _BAL_LIM_THR559// @DisplayName: Pitch control limit throttle threshold560// @Description: Pitch control limit throttle threshold. Pitch angle will be limited if throttle crosses this threshold (from 0 to 1)561// @Range: 0.0 1.0562// @Increment: 0.01563// @User: Standard564AP_GROUPINFO("_BAL_LIM_THR", 15, AR_AttitudeControl, _pitch_limit_throttle_thresh, AR_ATTCONTROL_PITCH_LIM_THR_THRESH),565566AP_GROUPEND567};568569AR_AttitudeControl::AR_AttitudeControl() :570_steer_angle_p(AR_ATTCONTROL_STEER_ANG_P),571_steer_rate_pid(AR_ATTCONTROL_STEER_RATE_P, AR_ATTCONTROL_STEER_RATE_I, AR_ATTCONTROL_STEER_RATE_D, AR_ATTCONTROL_STEER_RATE_FF, AR_ATTCONTROL_STEER_RATE_IMAX, 0.0f, AR_ATTCONTROL_STEER_RATE_FILT, 0.0f),572_throttle_speed_pid(AR_ATTCONTROL_THR_SPEED_P, AR_ATTCONTROL_THR_SPEED_I, AR_ATTCONTROL_THR_SPEED_D, 0.0f, AR_ATTCONTROL_THR_SPEED_IMAX, 0.0f, AR_ATTCONTROL_THR_SPEED_FILT, 0.0f),573_pitch_to_throttle_pid(AR_ATTCONTROL_PITCH_THR_P, AR_ATTCONTROL_PITCH_THR_I, AR_ATTCONTROL_PITCH_THR_D, 0.0f, AR_ATTCONTROL_PITCH_THR_IMAX, 0.0f, AR_ATTCONTROL_PITCH_THR_FILT, 0.0f),574_sailboat_heel_pid(AR_ATTCONTROL_HEEL_SAIL_P, AR_ATTCONTROL_HEEL_SAIL_I, AR_ATTCONTROL_HEEL_SAIL_D, 0.0f, AR_ATTCONTROL_HEEL_SAIL_IMAX, 0.0f, AR_ATTCONTROL_HEEL_SAIL_FILT, 0.0f)575{576_singleton = this;577AP_Param::setup_object_defaults(this, var_info);578}579580// return a steering servo output from -1.0 to +1.0 given a desired lateral acceleration rate in m/s/s.581// positive lateral acceleration is to the right.582float AR_AttitudeControl::get_steering_out_lat_accel(float desired_accel, bool motor_limit_left, bool motor_limit_right, float dt)583{584// record desired accel for reporting purposes585_steer_lat_accel_last_ms = AP_HAL::millis();586_desired_lat_accel = desired_accel;587588// get speed forward589float speed;590if (!get_forward_speed(speed)) {591// we expect caller will not try to control heading using rate control without a valid speed estimate592// on failure to get speed we do not attempt to steer593return 0.0f;594}595596const float desired_rate = get_turn_rate_from_lat_accel(desired_accel, speed);597598return get_steering_out_rate(desired_rate, motor_limit_left, motor_limit_right, dt);599}600601// return a steering servo output from -1 to +1 given a heading in radians602// set rate_max_rads to a non-zero number to apply a limit on the desired turn rate603// return value is normally in range -1.0 to +1.0 but can be higher or lower604float AR_AttitudeControl::get_steering_out_heading(float heading_rad, float rate_max_rads, bool motor_limit_left, bool motor_limit_right, float dt)605{606// calculate the desired turn rate (in radians) from the angle error (also in radians)607float desired_rate = get_turn_rate_from_heading(heading_rad, rate_max_rads);608609return get_steering_out_rate(desired_rate, motor_limit_left, motor_limit_right, dt);610}611612// return a desired turn-rate given a desired heading in radians613float AR_AttitudeControl::get_turn_rate_from_heading(float heading_rad, float rate_max_rads) const614{615const float yaw_error = wrap_PI(heading_rad - AP::ahrs().get_yaw());616617// Calculate the desired turn rate (in radians) from the angle error (also in radians)618float desired_rate = _steer_angle_p.get_p(yaw_error);619620// limit desired_rate if a custom pivot turn rate is selected, otherwise use ATC_STR_RAT_MAX621if (is_positive(rate_max_rads)) {622desired_rate = constrain_float(desired_rate, -rate_max_rads, rate_max_rads);623}624625// if acceleration limit is provided, ensure rate can be slowed to zero in time to stop at heading_rad (i.e. avoid overshoot)626if (is_positive(_steer_accel_max)) {627const float steer_accel_rate_max_rads = safe_sqrt(2.0 * fabsf(yaw_error) * radians(_steer_accel_max));628desired_rate = constrain_float(desired_rate, -steer_accel_rate_max_rads, steer_accel_rate_max_rads);629}630631return desired_rate;632}633634// return a steering servo output given a desired yaw rate in radians/sec.635// positive yaw is to the right636// return value is normally in range -1.0 to +1.0 but can be higher or lower637// also sets steering_limit_left and steering_limit_right flags638float AR_AttitudeControl::get_steering_out_rate(float desired_rate, bool motor_limit_left, bool motor_limit_right, float dt)639{640// sanity check dt641dt = constrain_float(dt, 0.0f, 1.0f);642643// update steering limit flags used by higher level controllers (e.g. position controller)644_steering_limit_left = motor_limit_left;645_steering_limit_right = motor_limit_right;646647// if not called recently, reset input filter and desired turn rate to actual turn rate (used for accel limiting)648const uint32_t now = AP_HAL::millis();649if ((_steer_turn_last_ms == 0) || ((now - _steer_turn_last_ms) > AR_ATTCONTROL_TIMEOUT_MS)) {650_steer_rate_pid.reset_filter();651_steer_rate_pid.reset_I();652_desired_turn_rate = AP::ahrs().get_yaw_rate_earth();653}654_steer_turn_last_ms = now;655656// acceleration limit desired turn rate657if (is_positive(_steer_accel_max)) {658const float change_max = radians(_steer_accel_max) * dt;659if (desired_rate <= _desired_turn_rate - change_max) {660_steering_limit_left = true;661}662if (desired_rate >= _desired_turn_rate + change_max) {663_steering_limit_right = true;664}665desired_rate = constrain_float(desired_rate, _desired_turn_rate - change_max, _desired_turn_rate + change_max);666}667_desired_turn_rate = desired_rate;668669// rate limit desired turn rate670if (is_positive(_steer_rate_max)) {671const float steer_rate_max_rad = radians(_steer_rate_max);672if (_desired_turn_rate <= -steer_rate_max_rad) {673_steering_limit_left = true;674}675if (_desired_turn_rate >= steer_rate_max_rad) {676_steering_limit_right = true;677}678_desired_turn_rate = constrain_float(_desired_turn_rate, -steer_rate_max_rad, steer_rate_max_rad);679}680681// G limit based on speed682float speed;683if (get_forward_speed(speed)) {684// do not limit to less than 1 deg/s685const float turn_rate_max = MAX(get_turn_rate_from_lat_accel(get_turn_lat_accel_max(), fabsf(speed)), radians(1.0f));686if (_desired_turn_rate <= -turn_rate_max) {687_steering_limit_left = true;688}689if (_desired_turn_rate >= turn_rate_max) {690_steering_limit_right = true;691}692_desired_turn_rate = constrain_float(_desired_turn_rate, -turn_rate_max, turn_rate_max);693}694695// update pid to calculate output to motors696float output = _steer_rate_pid.update_all(_desired_turn_rate, AP::ahrs().get_yaw_rate_earth(), dt, (motor_limit_left || motor_limit_right));697output += _steer_rate_pid.get_ff();698// constrain and return final output699return output;700}701702// get latest desired turn rate in rad/sec (recorded during calls to get_steering_out_rate)703float AR_AttitudeControl::get_desired_turn_rate() const704{705// return zero if no recent calls to turn rate controller706if ((_steer_turn_last_ms == 0) || ((AP_HAL::millis() - _steer_turn_last_ms) > AR_ATTCONTROL_TIMEOUT_MS)) {707return 0.0f;708}709return _desired_turn_rate;710}711712// get latest desired lateral acceleration in m/s/s (recorded during calls to get_steering_out_lat_accel)713float AR_AttitudeControl::get_desired_lat_accel() const714{715// return zero if no recent calls to lateral acceleration controller716if ((_steer_lat_accel_last_ms == 0) || ((AP_HAL::millis() - _steer_lat_accel_last_ms) > AR_ATTCONTROL_TIMEOUT_MS)) {717return 0.0f;718}719return _desired_lat_accel;720}721722// get actual lateral acceleration in m/s/s. returns true on success723bool AR_AttitudeControl::get_lat_accel(float &lat_accel) const724{725float speed;726if (!get_forward_speed(speed)) {727return false;728}729lat_accel = speed * AP::ahrs().get_yaw_rate_earth();730return true;731}732733// calculate the turn rate in rad/sec given a lateral acceleration (in m/s/s) and speed (in m/s)734float AR_AttitudeControl::get_turn_rate_from_lat_accel(float lat_accel, float speed) const735{736// enforce minimum speed to stop oscillations when first starting to move737if (fabsf(speed) < AR_ATTCONTROL_STEER_SPEED_MIN) {738if (is_negative(speed)) {739speed = -AR_ATTCONTROL_STEER_SPEED_MIN;740} else {741speed = AR_ATTCONTROL_STEER_SPEED_MIN;742}743}744745return lat_accel / speed;746}747748// return a throttle output from -1 to +1 given a desired speed in m/s (use negative speeds to travel backwards)749// motor_limit should be true if motors have hit their upper or lower limits750// cruise speed should be in m/s, cruise throttle should be a number from -1 to +1751float AR_AttitudeControl::get_throttle_out_speed(float desired_speed, bool motor_limit_low, bool motor_limit_high, float cruise_speed, float cruise_throttle, float dt)752{753// sanity check dt754dt = constrain_float(dt, 0.0f, 1.0f);755756// get speed forward757float speed;758if (!get_forward_speed(speed)) {759// we expect caller will not try to control heading using rate control without a valid speed estimate760// on failure to get speed we do not attempt to steer761return 0.0f;762}763764// if not called recently, reset input filter and desired speed to actual speed (used for accel limiting)765if (!speed_control_active()) {766_throttle_speed_pid.reset_filter();767_throttle_speed_pid.reset_I();768_desired_speed = speed;769}770_speed_last_ms = AP_HAL::millis();771772// acceleration limit desired speed773_desired_speed = get_desired_speed_accel_limited(desired_speed, dt);774775// calculate base throttle (protect against divide by zero)776float throttle_base = 0.0f;777if (is_positive(cruise_speed) && is_positive(cruise_throttle)) {778throttle_base = _desired_speed * (cruise_throttle / cruise_speed);779}780781// calculate final output782float throttle_out = _throttle_speed_pid.update_all(_desired_speed, speed, dt, (motor_limit_low || motor_limit_high || _throttle_limit_low || _throttle_limit_high));783throttle_out += _throttle_speed_pid.get_ff();784throttle_out += throttle_base;785786// update PID info for reporting purposes787_throttle_speed_pid_info = _throttle_speed_pid.get_pid_info();788_throttle_speed_pid_info.FF += throttle_base;789790// clear local limit flags used to stop i-term build-up as we stop reversed outputs going to motors791_throttle_limit_low = false;792_throttle_limit_high = false;793794// protect against reverse output being sent to the motors unless braking has been enabled795if (!_brake_enable) {796// if both desired speed and actual speed are positive, do not allow negative values797if ((_desired_speed >= 0.0f) && (throttle_out <= 0.0f)) {798throttle_out = 0.0f;799_throttle_limit_low = true;800} else if ((_desired_speed <= 0.0f) && (throttle_out >= 0.0f)) {801throttle_out = 0.0f;802_throttle_limit_high = true;803}804}805806// final output throttle in range -1 to 1807return throttle_out;808}809810// return a throttle output from -1 to +1 to perform a controlled stop. returns true once the vehicle has stopped811float AR_AttitudeControl::get_throttle_out_stop(bool motor_limit_low, bool motor_limit_high, float cruise_speed, float cruise_throttle, float dt, bool &stopped)812{813// get current system time814const uint32_t now = AP_HAL::millis();815816// if we were stopped in the last 300ms, assume we are still stopped817bool _stopped = (_stop_last_ms != 0) && (now - _stop_last_ms) < 300;818819// get deceleration limited speed820float desired_speed_limited = get_desired_speed_accel_limited(0.0f, dt);821822// get speed forward823float speed;824if (!get_forward_speed(speed)) {825// could not get speed so assume stopped826_stopped = true;827} else {828// if desired speed is zero and vehicle drops below _stop_speed consider it stopped829if (is_zero(desired_speed_limited) && fabsf(speed) <= fabsf(_stop_speed)) {830_stopped = true;831}832}833834// set stopped status for caller835stopped = _stopped;836837// if stopped return zero838if (stopped) {839// update last time we thought we were stopped840_stop_last_ms = now;841// set last time speed controller was run so accelerations are limited842_speed_last_ms = now;843// reset filters and I-term844_throttle_speed_pid.reset_filter();845_throttle_speed_pid.reset_I();846// ensure desired speed is zero847_desired_speed = 0.0f;848return 0.0f;849}850851// clear stopped system time852_stop_last_ms = 0;853// run speed controller to bring vehicle to stop854return get_throttle_out_speed(desired_speed_limited, motor_limit_low, motor_limit_high, cruise_speed, cruise_throttle, dt);855}856857// balancebot pitch to throttle controller858// returns a throttle output from -1 to +1 given a desired pitch angle (in radians)859// pitch_max should be the user defined max pitch angle (in radians)860// motor_limit should be true if the motors have hit their upper or lower limit861float AR_AttitudeControl::get_throttle_out_from_pitch(float desired_pitch, float pitch_max, bool motor_limit, float dt)862{863// sanity check dt864dt = constrain_float(dt, 0.0f, 1.0f);865866// if not called recently, reset input filter867const uint32_t now = AP_HAL::millis();868if ((_balance_last_ms == 0) || ((now - _balance_last_ms) > AR_ATTCONTROL_TIMEOUT_MS)) {869_pitch_to_throttle_pid.reset_filter();870_pitch_to_throttle_pid.reset_I();871_pitch_limit_low = -pitch_max;872_pitch_limit_high = pitch_max;873}874_balance_last_ms = now;875876// limit desired pitch to protect against falling877const bool pitch_limit_active = (_pitch_limit_tc >= 0.01) && (_pitch_limit_throttle_thresh > 0);878if (pitch_limit_active) {879desired_pitch = constrain_float(desired_pitch, _pitch_limit_low, _pitch_limit_high);880_pitch_limited = (desired_pitch <= _pitch_limit_low || desired_pitch >= _pitch_limit_high);881} else {882_pitch_limited = false;883}884885// initialise output to feed forward from current pitch angle886const float pitch_rad = AP::ahrs().get_pitch();887float output = sinf(pitch_rad) * _pitch_to_throttle_ff;888889// add regular PID control890output += _pitch_to_throttle_pid.update_all(desired_pitch, pitch_rad, dt, motor_limit);891output += _pitch_to_throttle_pid.get_ff();892893// update pitch limits for next iteration894// note: pitch is positive when leaning backwards, negative when leaning forward895if (pitch_limit_active) {896const float pitch_limit_incr = 1.0/_pitch_limit_tc * dt * pitch_max;897const float pitch_relax_incr = pitch_limit_incr * AR_ATTCONTROL_PITCH_RELAX_RATIO;898if (output <= -_pitch_limit_throttle_thresh) {899// very low negative throttle output means we must lower pitch_high (e.g. reduce leaning backwards)900_pitch_limit_high = MAX(_pitch_limit_high - pitch_limit_incr, 0);901} else {902_pitch_limit_high = MIN(_pitch_limit_high + pitch_relax_incr, pitch_max);903}904if (output >= _pitch_limit_throttle_thresh) {905// very high positive throttle output means we must raise pitch_low (reduce leaning forwards)906_pitch_limit_low = MIN(_pitch_limit_low + pitch_limit_incr, 0);907} else {908_pitch_limit_low = MAX(_pitch_limit_low - pitch_relax_incr, -pitch_max);909}910}911912// constrain and return final output913return output;914}915916// get latest desired pitch in radians for reporting purposes917float AR_AttitudeControl::get_desired_pitch() const918{919// if not called recently, return 0920if ((_balance_last_ms == 0) || ((AP_HAL::millis() - _balance_last_ms) > AR_ATTCONTROL_TIMEOUT_MS)) {921return 0.0f;922}923924return _pitch_to_throttle_pid.get_pid_info().target;925}926927// Sailboat heel(roll) angle controller releases sail to keep at maximum heel angle928// but does not attempt to reach maximum heel angle, ie only lets sails out, does not pull them in929float AR_AttitudeControl::get_sail_out_from_heel(float desired_heel, float dt)930{931// sanity check dt932dt = constrain_float(dt, 0.0f, 1.0f);933934// if not called recently, reset input filter935const uint32_t now = AP_HAL::millis();936if ((_heel_controller_last_ms == 0) || ((now - _heel_controller_last_ms) > AR_ATTCONTROL_TIMEOUT_MS)) {937_sailboat_heel_pid.reset_filter();938_sailboat_heel_pid.reset_I();939}940_heel_controller_last_ms = now;941942_sailboat_heel_pid.update_all(desired_heel, fabsf(AP::ahrs().get_roll()), dt);943944// get feed-forward945const float ff = _sailboat_heel_pid.get_ff();946947// get p, constrain to be zero or negative948float p = _sailboat_heel_pid.get_p();949if (is_positive(p)) {950p = 0.0f;951}952953// get i, constrain to be zero or negative954float i = _sailboat_heel_pid.get_i();955if (is_positive(i)) {956i = 0.0f;957_sailboat_heel_pid.reset_I();958}959960// get d961const float d = _sailboat_heel_pid.get_d();962963// constrain and return final output964return (ff + p + i + d) * -1.0f;965}966967// get the slew rate value for speed and steering for oscillation detection in lua scripts968void AR_AttitudeControl::get_srate(float &steering_srate, float &speed_srate)969{970steering_srate = get_steering_rate_pid().get_pid_info().slew_rate;971speed_srate = _throttle_speed_pid_info.slew_rate;972}973974// get forward speed in m/s (earth-frame horizontal velocity but only along vehicle x-axis). returns true on success975bool AR_AttitudeControl::get_forward_speed(float &speed) const976{977Vector3f velocity;978const AP_AHRS &_ahrs = AP::ahrs();979if (!_ahrs.get_velocity_NED(velocity)) {980// use less accurate GPS, assuming entire length is along forward/back axis of vehicle981if (AP::gps().status() >= AP_GPS::GPS_OK_FIX_3D) {982if (abs(wrap_180_cd(_ahrs.yaw_sensor - AP::gps().ground_course_cd())) <= 9000) {983speed = AP::gps().ground_speed();984} else {985speed = -AP::gps().ground_speed();986}987return true;988} else {989return false;990}991}992// calculate forward speed velocity into body frame993speed = velocity.x*_ahrs.cos_yaw() + velocity.y*_ahrs.sin_yaw();994return true;995}996997float AR_AttitudeControl::get_decel_max() const998{999if (is_positive(_throttle_decel_max)) {1000return _throttle_decel_max;1001} else {1002return _throttle_accel_max;1003}1004}10051006// check if speed controller active1007bool AR_AttitudeControl::speed_control_active() const1008{1009// active if there have been recent calls to speed controller1010if ((_speed_last_ms == 0) || ((AP_HAL::millis() - _speed_last_ms) > AR_ATTCONTROL_TIMEOUT_MS)) {1011return false;1012}1013return true;1014}10151016// get latest desired speed recorded during call to get_throttle_out_speed. For reporting purposes only1017float AR_AttitudeControl::get_desired_speed() const1018{1019// return zero if no recent calls to speed controller1020if (!speed_control_active()) {1021return 0.0f;1022}1023return _desired_speed;1024}10251026// get acceleration limited desired speed1027float AR_AttitudeControl::get_desired_speed_accel_limited(float desired_speed, float dt) const1028{1029// return input value if no recent calls to speed controller1030// apply no limiting when ATC_ACCEL_MAX is set to zero1031if (!speed_control_active() || !is_positive(_throttle_accel_max)) {1032return desired_speed;1033}10341035// sanity check dt1036dt = constrain_float(dt, 0.0f, 1.0f);10371038// use previous desired speed as basis for accel limiting1039float speed_prev = _desired_speed;10401041// if no recent calls to speed controller limit based on current speed1042if (!speed_control_active()) {1043get_forward_speed(speed_prev);1044}10451046// acceleration limit desired speed1047float speed_change_max;1048if (fabsf(desired_speed) < fabsf(_desired_speed) && is_positive(_throttle_decel_max)) {1049speed_change_max = _throttle_decel_max * dt;1050} else {1051speed_change_max = _throttle_accel_max * dt;1052}1053return constrain_float(desired_speed, speed_prev - speed_change_max, speed_prev + speed_change_max);1054}10551056// get minimum stopping distance (in meters) given a speed (in m/s)1057float AR_AttitudeControl::get_stopping_distance(float speed) const1058{1059// get maximum vehicle deceleration1060const float decel_max = get_decel_max();10611062if ((decel_max <= 0.0f) || is_zero(speed)) {1063return 0.0f;1064}10651066// assume linear deceleration1067return 0.5f * sq(speed) / decel_max;1068}10691070// relax I terms of throttle and steering controllers1071void AR_AttitudeControl::relax_I()1072{1073_steer_rate_pid.reset_I();1074_throttle_speed_pid.reset_I();1075_pitch_to_throttle_pid.reset_I();1076}10771078void AR_AttitudeControl::set_notch_sample_rate(float sample_rate)1079{1080#if AP_FILTER_ENABLED1081_steer_rate_pid.set_notch_sample_rate(sample_rate);1082_throttle_speed_pid.set_notch_sample_rate(sample_rate);1083_pitch_to_throttle_pid.set_notch_sample_rate(sample_rate);1084#endif1085}108610871088