AP_YawController.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. // Code by Jon Challinger
  14. // Modified by Paul Riseborough to implement a three loop autopilot
  15. // topology
  16. //
  17. #include <AP_HAL/AP_HAL.h>
  18. #include "AP_YawController.h"
  19. extern const AP_HAL::HAL& hal;
  20. const AP_Param::GroupInfo AP_YawController::var_info[] = {
  21. // @Param: SLIP
  22. // @DisplayName: Sideslip control gain
  23. // @Description: Gain from lateral acceleration to demanded yaw rate for aircraft with enough fuselage area to detect lateral acceleration and sideslips. Do not enable for flying wings and gliders. Actively coordinates flight more than just yaw damping. Set after YAW2SRV_DAMP and YAW2SRV_INT are tuned.
  24. // @Range: 0 4
  25. // @Increment: 0.25
  26. // @User: Advanced
  27. AP_GROUPINFO("SLIP", 0, AP_YawController, _K_A, 0),
  28. // @Param: INT
  29. // @DisplayName: Sideslip control integrator
  30. // @Description: Integral gain from lateral acceleration error. Effectively trims rudder to eliminate long-term sideslip.
  31. // @Range: 0 2
  32. // @Increment: 0.25
  33. // @User: Advanced
  34. AP_GROUPINFO("INT", 1, AP_YawController, _K_I, 0),
  35. // @Param: DAMP
  36. // @DisplayName: Yaw damping
  37. // @Description: Gain from yaw rate to rudder. Most effective at yaw damping and should be tuned after KFF_RDDRMIX. Also disables YAW2SRV_INT if set to 0.
  38. // @Range: 0 2
  39. // @Increment: 0.25
  40. // @User: Advanced
  41. AP_GROUPINFO("DAMP", 2, AP_YawController, _K_D, 0),
  42. // @Param: RLL
  43. // @DisplayName: Yaw coordination gain
  44. // @Description: Gain to the yaw rate required to keep it consistent with the turn rate in a coordinated turn. Corrects for yaw tendencies after the turn is established. Increase yaw into the turn by raising. Increase yaw out of the turn by decreasing. Values outside of 0.9-1.1 range indicate airspeed calibration problems.
  45. // @Range: 0.8 1.2
  46. // @Increment: 0.05
  47. // @User: Advanced
  48. AP_GROUPINFO("RLL", 3, AP_YawController, _K_FF, 1),
  49. /*
  50. Note: index 4 should not be used - it was used for an incorrect
  51. AP_Int8 version of the IMAX in the 2.74 release
  52. */
  53. // @Param: IMAX
  54. // @DisplayName: Integrator limit
  55. // @Description: Limit of yaw integrator gain in centi-degrees of servo travel. Servos are assumed to have +/- 4500 centi-degrees of travel, so a value of 1500 allows trim of up to 1/3 of servo travel range.
  56. // @Range: 0 4500
  57. // @Increment: 1
  58. // @User: Advanced
  59. AP_GROUPINFO("IMAX", 5, AP_YawController, _imax, 1500),
  60. AP_GROUPEND
  61. };
  62. int32_t AP_YawController::get_servo_out(float scaler, bool disable_integrator)
  63. {
  64. uint32_t tnow = AP_HAL::millis();
  65. uint32_t dt = tnow - _last_t;
  66. if (_last_t == 0 || dt > 1000) {
  67. dt = 0;
  68. }
  69. _last_t = tnow;
  70. int16_t aspd_min = aparm.airspeed_min;
  71. if (aspd_min < 1) {
  72. aspd_min = 1;
  73. }
  74. float delta_time = (float) dt / 1000.0f;
  75. // Calculate yaw rate required to keep up with a constant height coordinated turn
  76. float aspeed;
  77. float rate_offset;
  78. float bank_angle = _ahrs.roll;
  79. // limit bank angle between +- 80 deg if right way up
  80. if (fabsf(bank_angle) < 1.5707964f) {
  81. bank_angle = constrain_float(bank_angle,-1.3962634f,1.3962634f);
  82. }
  83. if (!_ahrs.airspeed_estimate(&aspeed)) {
  84. // If no airspeed available use average of min and max
  85. aspeed = 0.5f*(float(aspd_min) + float(aparm.airspeed_max));
  86. }
  87. rate_offset = (GRAVITY_MSS / MAX(aspeed , float(aspd_min))) * sinf(bank_angle) * _K_FF;
  88. // Get body rate vector (radians/sec)
  89. float omega_z = _ahrs.get_gyro().z;
  90. // Get the accln vector (m/s^2)
  91. float accel_y = AP::ins().get_accel().y;
  92. // Subtract the steady turn component of rate from the measured rate
  93. // to calculate the rate relative to the turn requirement in degrees/sec
  94. float rate_hp_in = ToDeg(omega_z - rate_offset);
  95. // Apply a high-pass filter to the rate to washout any steady state error
  96. // due to bias errors in rate_offset
  97. // Use a cut-off frequency of omega = 0.2 rad/sec
  98. // Could make this adjustable by replacing 0.9960080 with (1 - omega * dt)
  99. float rate_hp_out = 0.9960080f * _last_rate_hp_out + rate_hp_in - _last_rate_hp_in;
  100. _last_rate_hp_out = rate_hp_out;
  101. _last_rate_hp_in = rate_hp_in;
  102. //Calculate input to integrator
  103. float integ_in = - _K_I * (_K_A * accel_y + rate_hp_out);
  104. // Apply integrator, but clamp input to prevent control saturation and freeze integrator below min FBW speed
  105. // Don't integrate if in stabilise mode as the integrator will wind up against the pilots inputs
  106. // Don't integrate if _K_D is zero as integrator will keep winding up
  107. if (!disable_integrator && _K_D > 0) {
  108. //only integrate if airspeed above min value
  109. if (aspeed > float(aspd_min))
  110. {
  111. // prevent the integrator from increasing if surface defln demand is above the upper limit
  112. if (_last_out < -45) {
  113. _integrator += MAX(integ_in * delta_time , 0);
  114. } else if (_last_out > 45) {
  115. // prevent the integrator from decreasing if surface defln demand is below the lower limit
  116. _integrator += MIN(integ_in * delta_time , 0);
  117. } else {
  118. _integrator += integ_in * delta_time;
  119. }
  120. }
  121. } else {
  122. _integrator = 0;
  123. }
  124. if (_K_D < 0.0001f) {
  125. // yaw damping is disabled, and the integrator is scaled by damping, so return 0
  126. return 0;
  127. }
  128. // Scale the integration limit
  129. float intLimScaled = _imax * 0.01f / (_K_D * scaler * scaler);
  130. // Constrain the integrator state
  131. _integrator = constrain_float(_integrator, -intLimScaled, intLimScaled);
  132. // Protect against increases to _K_D during in-flight tuning from creating large control transients
  133. // due to stored integrator values
  134. if (_K_D > _K_D_last && _K_D > 0) {
  135. _integrator = _K_D_last/_K_D * _integrator;
  136. }
  137. _K_D_last = _K_D;
  138. // Calculate demanded rudder deflection, +Ve deflection yaws nose right
  139. // Save to last value before application of limiter so that integrator limiting
  140. // can detect exceedance next frame
  141. // Scale using inverse dynamic pressure (1/V^2)
  142. _pid_info.I = _K_D * _integrator * scaler * scaler;
  143. _pid_info.D = _K_D * (-rate_hp_out) * scaler * scaler;
  144. _last_out = _pid_info.I + _pid_info.D;
  145. // Convert to centi-degrees and constrain
  146. return constrain_float(_last_out * 100, -4500, 4500);
  147. }
  148. void AP_YawController::reset_I()
  149. {
  150. _integrator = 0;
  151. }