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.
|
|
|
|
|
|
|
|
#include <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
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
float delta_time = (float) dt / 1000.0;
|
|
|
|
|
|
|
|
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
|
|
|
|
// 100*ToDeg(asin(-accels.y/9.81))
|
|
|
|
// 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
|
|
|
|
float RC = 1/(2*M_PI*_fCut);
|
|
|
|
error = _last_error +
|
|
|
|
(delta_time / (RC + delta_time)) * (error - _last_error);
|
|
|
|
_last_error = error;
|
|
|
|
// integrator
|
|
|
|
if(_freeze_start_time < (tnow - 2000)) {
|
2012-10-12 14:53:42 -03:00
|
|
|
if ((fabs(_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;
|
|
|
|
}
|