2013-09-08 21:17:02 -03:00
/*
This program 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 .
This program 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 this program . If not , see < http : //www.gnu.org/licenses/>.
*/
// Code by Andrew Tridgell
// Based upon the roll controller by Paul Riseborough and Jon Challinger
//
2021-07-20 08:15:06 -03:00
# include <AP_AHRS/AP_AHRS.h>
2015-08-11 03:28:41 -03:00
# include <AP_Math/AP_Math.h>
# include <AP_HAL/AP_HAL.h>
2013-09-08 21:17:02 -03:00
# include "AP_SteerController.h"
extern const AP_HAL : : HAL & hal ;
2015-10-25 14:03:46 -03:00
const AP_Param : : GroupInfo AP_SteerController : : var_info [ ] = {
2014-01-08 22:34:10 -04:00
// @Param: TCONST
2013-09-08 21:17:02 -03:00
// @DisplayName: Steering Time Constant
2013-10-06 19:28:07 -03:00
// @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.
2013-09-08 21:17:02 -03:00
// @Range: 0.4 1.0
2017-05-02 10:39:35 -03:00
// @Units: s
2013-09-08 21:17:02 -03:00
// @Increment: 0.1
// @User: Advanced
2013-09-23 18:37:12 -03:00
AP_GROUPINFO ( " TCONST " , 0 , AP_SteerController , _tau , 0.75f ) ,
2013-09-08 21:17:02 -03:00
// @Param: P
// @DisplayName: Steering turning gain
// @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
// @Range: 0.1 10.0
// @Increment: 0.1
2020-09-13 19:42:53 -03:00
// @User: Standard
2013-09-08 21:17:02 -03:00
AP_GROUPINFO ( " P " , 1 , AP_SteerController , _K_P , 1.8f ) ,
// @Param: I
// @DisplayName: Integrator Gain
// @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.
// @Range: 0 1.0
// @Increment: 0.05
2020-09-13 19:42:53 -03:00
// @User: Standard
2013-09-08 21:17:02 -03:00
AP_GROUPINFO ( " I " , 3 , AP_SteerController , _K_I , 0.2f ) ,
// @Param: D
// @DisplayName: Damping Gain
// @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.
// @Range: 0 0.1
// @Increment: 0.01
2020-09-13 19:42:53 -03:00
// @User: Standard
2013-09-15 19:26:29 -03:00
AP_GROUPINFO ( " D " , 4 , AP_SteerController , _K_D , 0.005f ) ,
2013-09-08 21:17:02 -03:00
// @Param: IMAX
// @DisplayName: Integrator limit
// @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.
// @Range: 0 4500
2021-05-20 10:08:46 -03:00
// @Increment: 10
2017-05-02 10:39:35 -03:00
// @Units: cdeg
2013-09-08 21:17:02 -03:00
// @User: Advanced
AP_GROUPINFO ( " IMAX " , 5 , AP_SteerController , _imax , 1500 ) ,
2013-09-29 20:03:52 -03:00
// @Param: MINSPD
// @DisplayName: Minimum speed
2015-09-15 21:11:20 -03:00
// @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.
2013-09-29 20:03:52 -03:00
// @Range: 0 5
// @Increment: 0.1
2017-05-02 10:39:35 -03:00
// @Units: m/s
2020-09-13 19:42:53 -03:00
// @User: Standard
2013-10-04 23:56:28 -03:00
AP_GROUPINFO ( " MINSPD " , 6 , AP_SteerController , _minspeed , 1.0f ) ,
2013-09-29 20:03:52 -03:00
2015-06-08 08:08:14 -03:00
// @Param: FF
// @DisplayName: Steering feed forward
// @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.
// @Range: 0.0 10.0
// @Increment: 0.1
2020-09-13 19:42:53 -03:00
// @User: Standard
2015-06-08 08:08:14 -03:00
AP_GROUPINFO ( " FF " , 7 , AP_SteerController , _K_FF , 0 ) ,
2016-12-13 21:47:22 -04:00
// @Param: DRTSPD
// @DisplayName: Derating speed
// @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.
// @Range: 0.0 30.0
// @Increment: 0.1
2017-05-02 10:39:35 -03:00
// @Units: m/s
2016-12-13 21:47:22 -04:00
// @User: Advanced
AP_GROUPINFO ( " DRTSPD " , 8 , AP_SteerController , _deratespeed , 0 ) ,
// @Param: DRTFCT
// @DisplayName: Derating factor
// @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.
// @Range: 0.0 50.0
// @Increment: 0.1
2017-05-02 10:39:35 -03:00
// @Units: deg/m/s
2016-12-13 21:47:22 -04:00
// @User: Advanced
AP_GROUPINFO ( " DRTFCT " , 9 , AP_SteerController , _deratefactor , 10 ) ,
// @Param: DRTMIN
// @DisplayName: Minimum angle of wheel
// @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.
2021-05-20 10:08:46 -03:00
// @Range: 0 4500
// @Increment: 10
2017-05-02 10:39:35 -03:00
// @Units: cdeg
2016-12-13 21:47:22 -04:00
// @User: Advanced
AP_GROUPINFO ( " DRTMIN " , 10 , AP_SteerController , _mindegree , 4500 ) ,
2013-09-08 21:17:02 -03:00
AP_GROUPEND
} ;
/*
2013-10-03 08:54:11 -03:00
steering rate controller . Returns servo out - 4500 to 4500 given
desired yaw rate in degrees / sec . Positive yaw rate means clockwise yaw .
2013-09-08 21:17:02 -03:00
*/
2013-10-03 08:54:11 -03:00
int32_t AP_SteerController : : get_steering_out_rate ( float desired_rate )
2013-09-08 21:17:02 -03:00
{
2015-11-19 23:05:52 -04:00
uint32_t tnow = AP_HAL : : millis ( ) ;
2013-09-08 21:17:02 -03:00
uint32_t dt = tnow - _last_t ;
if ( _last_t = = 0 | | dt > 1000 ) {
dt = 0 ;
}
_last_t = tnow ;
2021-07-20 08:15:06 -03:00
AP_AHRS & _ahrs = AP : : ahrs ( ) ;
2013-09-08 21:17:02 -03:00
float speed = _ahrs . groundspeed ( ) ;
2013-09-29 20:03:52 -03:00
if ( speed < _minspeed ) {
2016-12-13 21:47:22 -04:00
// assume a minimum speed. This stops oscillations when first starting to move
2013-09-29 20:03:52 -03:00
speed = _minspeed ;
2013-09-08 21:17:02 -03:00
}
// this is a linear approximation of the inverse steering
// equation for a ground vehicle. It returns steering as an angle from -45 to 45
float scaler = 1.0f / speed ;
2019-06-27 06:31:52 -03:00
_pid_info . target = desired_rate ;
2015-06-08 08:08:14 -03:00
2013-09-08 21:17:02 -03:00
// Calculate the steering rate error (deg/sec) and apply gain scaler
2016-05-31 20:54:59 -03:00
// We do this in earth frame to allow for rover leaning over in hard corners
2016-07-14 04:49:13 -03:00
float yaw_rate_earth = ToDeg ( _ahrs . get_yaw_rate_earth ( ) ) ;
if ( _reverse ) {
2017-08-03 09:41:07 -03:00
yaw_rate_earth * = - 1.0f ;
2016-07-14 04:49:13 -03:00
}
2019-01-27 01:42:16 -04:00
_pid_info . actual = yaw_rate_earth ;
2016-07-14 04:49:13 -03:00
float rate_error = ( desired_rate - yaw_rate_earth ) * scaler ;
2013-09-08 21:17:02 -03:00
// Calculate equivalent gains so that values for K_P and K_I can be taken across from the old PID law
// No conversion is required for K_D
float ki_rate = _K_I * _tau * 45.0f ;
2015-11-27 13:11:58 -04:00
float kp_ff = MAX ( ( _K_P - _K_I * _tau ) * _tau - _K_D , 0 ) * 45.0f ;
2015-06-08 08:08:14 -03:00
float k_ff = _K_FF * 45.0f ;
2013-09-08 21:17:02 -03:00
float delta_time = ( float ) dt * 0.001f ;
2016-12-13 21:47:22 -04:00
// Multiply yaw rate error by _ki_rate and integrate
// Don't integrate if in stabilize mode as the integrator will wind up against the pilots inputs
2013-10-05 05:11:35 -03:00
if ( ki_rate > 0 & & speed > = _minspeed ) {
2013-09-08 21:17:02 -03:00
// only integrate if gain and time step are positive.
if ( dt > 0 ) {
2013-09-11 02:05:13 -03:00
float integrator_delta = rate_error * ki_rate * delta_time * scaler ;
2013-09-08 21:17:02 -03:00
// prevent the integrator from increasing if steering defln demand is above the upper limit
if ( _last_out < - 45 ) {
2015-11-27 13:11:58 -04:00
integrator_delta = MAX ( integrator_delta , 0 ) ;
2013-09-08 21:17:02 -03:00
} else if ( _last_out > 45 ) {
// prevent the integrator from decreasing if steering defln demand is below the lower limit
2015-11-27 13:11:58 -04:00
integrator_delta = MIN ( integrator_delta , 0 ) ;
2013-09-08 21:17:02 -03:00
}
2015-06-08 08:08:14 -03:00
_pid_info . I + = integrator_delta ;
2013-09-08 21:17:02 -03:00
}
} else {
2015-06-08 08:08:14 -03:00
_pid_info . I = 0 ;
2013-09-08 21:17:02 -03:00
}
// Scale the integration limit
2013-09-11 02:05:13 -03:00
float intLimScaled = _imax * 0.01f ;
2013-09-08 21:17:02 -03:00
// Constrain the integrator state
2015-06-08 08:08:14 -03:00
_pid_info . I = constrain_float ( _pid_info . I , - intLimScaled , intLimScaled ) ;
_pid_info . D = rate_error * _K_D * 4.0f ;
2015-06-08 20:50:08 -03:00
_pid_info . P = ( ToRad ( desired_rate ) * kp_ff ) * scaler ;
_pid_info . FF = ( ToRad ( desired_rate ) * k_ff ) * scaler ;
2013-09-08 21:17:02 -03:00
2016-12-13 21:47:22 -04:00
// Calculate the demanded control surface deflection
_last_out = _pid_info . D + _pid_info . FF + _pid_info . P + _pid_info . I ;
2013-09-08 21:17:02 -03:00
2016-12-13 21:47:22 -04:00
float derate_constraint = 4500 ;
// Calculate required constrain based on speed
if ( ! is_zero ( _deratespeed ) & & speed > _deratespeed ) {
derate_constraint = 4500 - ( speed - _deratespeed ) * _deratefactor * 100 ;
if ( derate_constraint < _mindegree ) {
derate_constraint = _mindegree ;
}
}
// Convert to centi-degrees and constrain
return constrain_float ( _last_out * 100 , - derate_constraint , derate_constraint ) ;
2013-09-08 21:17:02 -03:00
}
2013-10-03 08:54:11 -03:00
/*
lateral acceleration controller . Returns servo value - 4500 to 4500
given a desired lateral acceleration
*/
int32_t AP_SteerController : : get_steering_out_lat_accel ( float desired_accel )
{
2021-07-20 08:15:06 -03:00
float speed = AP : : ahrs ( ) . groundspeed ( ) ;
2013-10-03 08:54:11 -03:00
if ( speed < _minspeed ) {
// assume a minimum speed. This reduces osciallations when first starting to move
speed = _minspeed ;
}
// Calculate the desired steering rate given desired_accel and speed
float desired_rate = ToDeg ( desired_accel / speed ) ;
2016-07-14 04:49:13 -03:00
if ( _reverse ) {
desired_rate * = - 1 ;
}
2013-10-03 08:54:11 -03:00
return get_steering_out_rate ( desired_rate ) ;
}
/*
return a steering servo value from - 4500 to 4500 given an angular
steering error in centidegrees .
*/
int32_t AP_SteerController : : get_steering_out_angle_error ( int32_t angle_err )
{
2014-07-08 07:26:20 -03:00
if ( _tau < 0.1f ) {
2022-07-05 00:08:56 -03:00
_tau . set ( 0.1f ) ;
2013-10-03 08:54:11 -03:00
}
// Calculate the desired steering rate (deg/sec) from the angle error
float desired_rate = angle_err * 0.01f / _tau ;
return get_steering_out_rate ( desired_rate ) ;
}
2013-09-08 21:17:02 -03:00
void AP_SteerController : : reset_I ( )
{
2015-06-08 08:08:14 -03:00
_pid_info . I = 0 ;
2013-09-08 21:17:02 -03:00
}