AP_SteerController.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 Andrew Tridgell
  14. // Based upon the roll controller by Paul Riseborough and Jon Challinger
  15. //
  16. #include <AP_Math/AP_Math.h>
  17. #include <AP_HAL/AP_HAL.h>
  18. #include "AP_SteerController.h"
  19. extern const AP_HAL::HAL& hal;
  20. const AP_Param::GroupInfo AP_SteerController::var_info[] = {
  21. // @Param: TCONST
  22. // @DisplayName: Steering Time Constant
  23. // @Description: This controls the time constant in seconds from demanded to achieved steering angle. A value of 0.75 is a good default and will work with nearly all rovers. Ground steering in aircraft needs a bit smaller time constant, and a value of 0.5 is recommended for best ground handling in fixed wing aircraft. A value of 0.75 means that the controller will try to correct any deviation between the desired and actual steering angle in 0.75 seconds. Advanced users may want to reduce this time to obtain a faster response but there is no point setting a time less than the vehicle can achieve.
  24. // @Range: 0.4 1.0
  25. // @Units: s
  26. // @Increment: 0.1
  27. // @User: Advanced
  28. AP_GROUPINFO("TCONST", 0, AP_SteerController, _tau, 0.75f),
  29. // @Param: P
  30. // @DisplayName: Steering turning gain
  31. // @Description: The proportional gain for steering. This should be approximately equal to the diameter of the turning circle of the vehicle at low speed and maximum steering angle
  32. // @Range: 0.1 10.0
  33. // @Increment: 0.1
  34. // @User: User
  35. AP_GROUPINFO("P", 1, AP_SteerController, _K_P, 1.8f),
  36. // @Param: I
  37. // @DisplayName: Integrator Gain
  38. // @Description: This is the gain from the integral of steering angle. Increasing this gain causes the controller to trim out steady offsets due to an out of trim vehicle.
  39. // @Range: 0 1.0
  40. // @Increment: 0.05
  41. // @User: User
  42. AP_GROUPINFO("I", 3, AP_SteerController, _K_I, 0.2f),
  43. // @Param: D
  44. // @DisplayName: Damping Gain
  45. // @Description: This adjusts the damping of the steering control loop. This gain helps to reduce steering jitter with vibration. It should be increased in 0.01 increments as too high a value can lead to a high frequency steering oscillation that could overstress the vehicle.
  46. // @Range: 0 0.1
  47. // @Increment: 0.01
  48. // @User: User
  49. AP_GROUPINFO("D", 4, AP_SteerController, _K_D, 0.005f),
  50. // @Param: IMAX
  51. // @DisplayName: Integrator limit
  52. // @Description: This limits the number of degrees of steering in centi-degrees over which the integrator will operate. At the default setting of 1500 centi-degrees, the integrator will be limited to +- 15 degrees of servo travel. The maximum servo deflection is +- 45 centi-degrees, so the default value represents a 1/3rd of the total control throw which is adequate unless the vehicle is severely out of trim.
  53. // @Range: 0 4500
  54. // @Increment: 1
  55. // @Units: cdeg
  56. // @User: Advanced
  57. AP_GROUPINFO("IMAX", 5, AP_SteerController, _imax, 1500),
  58. // @Param: MINSPD
  59. // @DisplayName: Minimum speed
  60. // @Description: This is the minimum assumed ground speed in meters/second for steering. Having a minimum speed prevents oscillations when the vehicle first starts moving. The vehicle can still drive slower than this limit, but the steering calculations will be done based on this minimum speed.
  61. // @Range: 0 5
  62. // @Increment: 0.1
  63. // @Units: m/s
  64. // @User: User
  65. AP_GROUPINFO("MINSPD", 6, AP_SteerController, _minspeed, 1.0f),
  66. // @Param: FF
  67. // @DisplayName: Steering feed forward
  68. // @Description: The feed forward gain for steering this is the ratio of the achieved turn rate to applied steering. A value of 1 means that the vehicle would yaw at a rate of 45 degrees per second with full steering deflection at 1m/s ground speed.
  69. // @Range: 0.0 10.0
  70. // @Increment: 0.1
  71. // @User: User
  72. AP_GROUPINFO("FF", 7, AP_SteerController, _K_FF, 0),
  73. // @Param: DRTSPD
  74. // @DisplayName: Derating speed
  75. // @Description: Speed after that the maximum degree of steering will start to derate. Set this speed to a maximum speed that a plane can do controlled turn at maximum angle of steering wheel without rolling to wing. If 0 then no derating is used.
  76. // @Range: 0.0 30.0
  77. // @Increment: 0.1
  78. // @Units: m/s
  79. // @User: Advanced
  80. AP_GROUPINFO("DRTSPD", 8, AP_SteerController, _deratespeed, 0),
  81. // @Param: DRTFCT
  82. // @DisplayName: Derating factor
  83. // @Description: Degrees of steering wheel to derate at each additional m/s of speed above "Derating speed". Should be set so that at higher speeds the plane does not roll to the wing in turns.
  84. // @Range: 0.0 50.0
  85. // @Increment: 0.1
  86. // @Units: deg/m/s
  87. // @User: Advanced
  88. AP_GROUPINFO("DRTFCT", 9, AP_SteerController, _deratefactor, 10),
  89. // @Param: DRTMIN
  90. // @DisplayName: Minimum angle of wheel
  91. // @Description: The angle that limits smallest angle of steering wheel at maximum speed. Even if it should derate below, it will stop derating at this angle.
  92. // @Range: 0.0 4500.0
  93. // @Increment: 0.1
  94. // @Units: cdeg
  95. // @User: Advanced
  96. AP_GROUPINFO("DRTMIN", 10, AP_SteerController, _mindegree, 4500),
  97. AP_GROUPEND
  98. };
  99. /*
  100. steering rate controller. Returns servo out -4500 to 4500 given
  101. desired yaw rate in degrees/sec. Positive yaw rate means clockwise yaw.
  102. */
  103. int32_t AP_SteerController::get_steering_out_rate(float desired_rate)
  104. {
  105. uint32_t tnow = AP_HAL::millis();
  106. uint32_t dt = tnow - _last_t;
  107. if (_last_t == 0 || dt > 1000) {
  108. dt = 0;
  109. }
  110. _last_t = tnow;
  111. float speed = _ahrs.groundspeed();
  112. if (speed < _minspeed) {
  113. // assume a minimum speed. This stops oscillations when first starting to move
  114. speed = _minspeed;
  115. }
  116. // this is a linear approximation of the inverse steering
  117. // equation for a ground vehicle. It returns steering as an angle from -45 to 45
  118. float scaler = 1.0f / speed;
  119. _pid_info.target = desired_rate;
  120. // Calculate the steering rate error (deg/sec) and apply gain scaler
  121. // We do this in earth frame to allow for rover leaning over in hard corners
  122. float yaw_rate_earth = ToDeg(_ahrs.get_yaw_rate_earth());
  123. if (_reverse) {
  124. yaw_rate_earth *= -1.0f;
  125. }
  126. _pid_info.actual = yaw_rate_earth;
  127. float rate_error = (desired_rate - yaw_rate_earth) * scaler;
  128. // Calculate equivalent gains so that values for K_P and K_I can be taken across from the old PID law
  129. // No conversion is required for K_D
  130. float ki_rate = _K_I * _tau * 45.0f;
  131. float kp_ff = MAX((_K_P - _K_I * _tau) * _tau - _K_D , 0) * 45.0f;
  132. float k_ff = _K_FF * 45.0f;
  133. float delta_time = (float)dt * 0.001f;
  134. // Multiply yaw rate error by _ki_rate and integrate
  135. // Don't integrate if in stabilize mode as the integrator will wind up against the pilots inputs
  136. if (ki_rate > 0 && speed >= _minspeed) {
  137. // only integrate if gain and time step are positive.
  138. if (dt > 0) {
  139. float integrator_delta = rate_error * ki_rate * delta_time * scaler;
  140. // prevent the integrator from increasing if steering defln demand is above the upper limit
  141. if (_last_out < -45) {
  142. integrator_delta = MAX(integrator_delta , 0);
  143. } else if (_last_out > 45) {
  144. // prevent the integrator from decreasing if steering defln demand is below the lower limit
  145. integrator_delta = MIN(integrator_delta, 0);
  146. }
  147. _pid_info.I += integrator_delta;
  148. }
  149. } else {
  150. _pid_info.I = 0;
  151. }
  152. // Scale the integration limit
  153. float intLimScaled = _imax * 0.01f;
  154. // Constrain the integrator state
  155. _pid_info.I = constrain_float(_pid_info.I, -intLimScaled, intLimScaled);
  156. _pid_info.D = rate_error * _K_D * 4.0f;
  157. _pid_info.P = (ToRad(desired_rate) * kp_ff) * scaler;
  158. _pid_info.FF = (ToRad(desired_rate) * k_ff) * scaler;
  159. // Calculate the demanded control surface deflection
  160. _last_out = _pid_info.D + _pid_info.FF + _pid_info.P + _pid_info.I;
  161. float derate_constraint = 4500;
  162. // Calculate required constrain based on speed
  163. if (!is_zero(_deratespeed) && speed > _deratespeed) {
  164. derate_constraint = 4500 - (speed - _deratespeed) * _deratefactor * 100;
  165. if (derate_constraint < _mindegree) {
  166. derate_constraint = _mindegree;
  167. }
  168. }
  169. // Convert to centi-degrees and constrain
  170. return constrain_float(_last_out * 100, -derate_constraint, derate_constraint);
  171. }
  172. /*
  173. lateral acceleration controller. Returns servo value -4500 to 4500
  174. given a desired lateral acceleration
  175. */
  176. int32_t AP_SteerController::get_steering_out_lat_accel(float desired_accel)
  177. {
  178. float speed = _ahrs.groundspeed();
  179. if (speed < _minspeed) {
  180. // assume a minimum speed. This reduces osciallations when first starting to move
  181. speed = _minspeed;
  182. }
  183. // Calculate the desired steering rate given desired_accel and speed
  184. float desired_rate = ToDeg(desired_accel / speed);
  185. if (_reverse) {
  186. desired_rate *= -1;
  187. }
  188. return get_steering_out_rate(desired_rate);
  189. }
  190. /*
  191. return a steering servo value from -4500 to 4500 given an angular
  192. steering error in centidegrees.
  193. */
  194. int32_t AP_SteerController::get_steering_out_angle_error(int32_t angle_err)
  195. {
  196. if (_tau < 0.1f) {
  197. _tau = 0.1f;
  198. }
  199. // Calculate the desired steering rate (deg/sec) from the angle error
  200. float desired_rate = angle_err * 0.01f / _tau;
  201. return get_steering_out_rate(desired_rate);
  202. }
  203. void AP_SteerController::reset_I()
  204. {
  205. _pid_info.I = 0;
  206. }