2012-08-21 23:08:14 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
|
|
|
|
// Code by Jon Challinger
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and / or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
2013-01-10 15:22:37 -04:00
|
|
|
#include <AP_Math.h>
|
2012-10-12 14:53:42 -03:00
|
|
|
#include <AP_HAL.h>
|
2012-08-21 23:08:14 -03:00
|
|
|
#include "AP_YawController.h"
|
|
|
|
|
2012-10-12 14:53:42 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2012-08-21 23:08:14 -03:00
|
|
|
const AP_Param::GroupInfo AP_YawController::var_info[] PROGMEM = {
|
|
|
|
AP_GROUPINFO("P", 0, AP_YawController, _kp, 0),
|
|
|
|
AP_GROUPINFO("I", 1, AP_YawController, _ki, 0),
|
|
|
|
AP_GROUPINFO("IMAX", 2, AP_YawController, _imax, 0),
|
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
2013-01-01 22:53:26 -04:00
|
|
|
// Low pass filter cut frequency for derivative calculation.
|
|
|
|
// FCUT macro computes a frequency cut based on an acceptable delay.
|
2013-01-10 14:42:24 -04:00
|
|
|
#define FCUT(d) (1 / ( 2 * 3.14f * (d) ) )
|
|
|
|
const float AP_YawController::_fCut = FCUT(0.5f);
|
2013-01-01 22:53:26 -04:00
|
|
|
|
2012-08-21 23:08:14 -03:00
|
|
|
int32_t AP_YawController::get_servo_out(float scaler, bool stick_movement)
|
|
|
|
{
|
2012-10-12 14:53:42 -03:00
|
|
|
uint32_t tnow = hal.scheduler->millis();
|
2012-08-21 23:08:14 -03:00
|
|
|
uint32_t dt = tnow - _last_t;
|
|
|
|
if (_last_t == 0 || dt > 1000) {
|
|
|
|
dt = 0;
|
|
|
|
}
|
|
|
|
_last_t = tnow;
|
|
|
|
|
2012-11-05 00:31:19 -04:00
|
|
|
if(_ins == NULL) { // can't control without a reference
|
2012-08-21 23:08:14 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-10 14:42:24 -04:00
|
|
|
float delta_time = (float) dt / 1000.0f;
|
2012-08-21 23:08:14 -03:00
|
|
|
|
|
|
|
if(stick_movement) {
|
|
|
|
if(!_stick_movement) {
|
|
|
|
_stick_movement_begin = tnow;
|
|
|
|
} else {
|
|
|
|
if(_stick_movement_begin < tnow-333) {
|
|
|
|
_freeze_start_time = tnow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_stick_movement = stick_movement;
|
|
|
|
|
2012-11-05 00:31:19 -04:00
|
|
|
Vector3f accels = _ins->get_accel();
|
2012-08-21 23:08:14 -03:00
|
|
|
|
2012-10-12 14:53:42 -03:00
|
|
|
// I didn't pull 512 out of a hat - it is a (very) loose approximation of
|
2013-01-10 14:42:24 -04:00
|
|
|
// 100*ToDeg(asinf(-accels.y/9.81f))
|
2012-10-12 14:53:42 -03:00
|
|
|
// which, with a P of 1.0, would mean that your rudder angle would be
|
|
|
|
// equal to your roll angle when
|
2012-08-21 23:08:14 -03:00
|
|
|
// the plane is still. Thus we have an (approximate) unit to go by.
|
|
|
|
float error = 512 * -accels.y;
|
|
|
|
|
|
|
|
// strongly filter the error
|
2013-01-10 14:42:24 -04:00
|
|
|
float RC = 1/(2*PI*_fCut);
|
2012-08-21 23:08:14 -03:00
|
|
|
error = _last_error +
|
|
|
|
(delta_time / (RC + delta_time)) * (error - _last_error);
|
|
|
|
_last_error = error;
|
|
|
|
// integrator
|
|
|
|
if(_freeze_start_time < (tnow - 2000)) {
|
2013-01-10 14:42:24 -04:00
|
|
|
if ((fabsf(_ki) > 0) && (dt > 0)) {
|
2012-08-21 23:08:14 -03:00
|
|
|
_integrator += (error * _ki) * scaler * delta_time;
|
|
|
|
if (_integrator < -_imax) _integrator = -_imax;
|
|
|
|
else if (_integrator > _imax) _integrator = _imax;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_integrator = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error * _kp * scaler) + _integrator;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_YawController::reset_I()
|
|
|
|
{
|
|
|
|
_integrator = 0;
|
|
|
|
}
|