123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- #include <AP_HAL/AP_HAL.h>
- #include "AP_YawController.h"
- extern const AP_HAL::HAL& hal;
- const AP_Param::GroupInfo AP_YawController::var_info[] = {
-
-
-
-
-
-
- AP_GROUPINFO("SLIP", 0, AP_YawController, _K_A, 0),
-
-
-
-
-
-
- AP_GROUPINFO("INT", 1, AP_YawController, _K_I, 0),
-
-
-
-
-
-
- AP_GROUPINFO("DAMP", 2, AP_YawController, _K_D, 0),
-
-
-
-
-
-
- AP_GROUPINFO("RLL", 3, AP_YawController, _K_FF, 1),
-
-
-
-
-
-
-
- AP_GROUPINFO("IMAX", 5, AP_YawController, _imax, 1500),
- AP_GROUPEND
- };
- int32_t AP_YawController::get_servo_out(float scaler, bool disable_integrator)
- {
- uint32_t tnow = AP_HAL::millis();
- uint32_t dt = tnow - _last_t;
- if (_last_t == 0 || dt > 1000) {
- dt = 0;
- }
- _last_t = tnow;
-
- int16_t aspd_min = aparm.airspeed_min;
- if (aspd_min < 1) {
- aspd_min = 1;
- }
-
- float delta_time = (float) dt / 1000.0f;
-
-
- float aspeed;
- float rate_offset;
- float bank_angle = _ahrs.roll;
-
- if (fabsf(bank_angle) < 1.5707964f) {
- bank_angle = constrain_float(bank_angle,-1.3962634f,1.3962634f);
- }
- if (!_ahrs.airspeed_estimate(&aspeed)) {
-
- aspeed = 0.5f*(float(aspd_min) + float(aparm.airspeed_max));
- }
- rate_offset = (GRAVITY_MSS / MAX(aspeed , float(aspd_min))) * sinf(bank_angle) * _K_FF;
-
- float omega_z = _ahrs.get_gyro().z;
-
-
- float accel_y = AP::ins().get_accel().y;
-
-
- float rate_hp_in = ToDeg(omega_z - rate_offset);
-
-
-
-
-
- float rate_hp_out = 0.9960080f * _last_rate_hp_out + rate_hp_in - _last_rate_hp_in;
- _last_rate_hp_out = rate_hp_out;
- _last_rate_hp_in = rate_hp_in;
-
- float integ_in = - _K_I * (_K_A * accel_y + rate_hp_out);
-
-
-
-
- if (!disable_integrator && _K_D > 0) {
-
- if (aspeed > float(aspd_min))
- {
-
- if (_last_out < -45) {
- _integrator += MAX(integ_in * delta_time , 0);
- } else if (_last_out > 45) {
-
- _integrator += MIN(integ_in * delta_time , 0);
- } else {
- _integrator += integ_in * delta_time;
- }
- }
- } else {
- _integrator = 0;
- }
- if (_K_D < 0.0001f) {
-
- return 0;
- }
-
-
- float intLimScaled = _imax * 0.01f / (_K_D * scaler * scaler);
-
- _integrator = constrain_float(_integrator, -intLimScaled, intLimScaled);
-
-
-
- if (_K_D > _K_D_last && _K_D > 0) {
- _integrator = _K_D_last/_K_D * _integrator;
- }
- _K_D_last = _K_D;
-
-
-
-
-
- _pid_info.I = _K_D * _integrator * scaler * scaler;
- _pid_info.D = _K_D * (-rate_hp_out) * scaler * scaler;
- _last_out = _pid_info.I + _pid_info.D;
-
- return constrain_float(_last_out * 100, -4500, 4500);
- }
- void AP_YawController::reset_I()
- {
- _integrator = 0;
- }
|