2012-10-30 23:56:25 -03:00
|
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2010-10-30 13:17:16 -03:00
|
|
|
|
/*
|
2012-08-21 23:19:51 -03:00
|
|
|
|
* APM_AHRS_DCM.cpp
|
|
|
|
|
*
|
|
|
|
|
* AHRS system using DCM matrices
|
|
|
|
|
*
|
|
|
|
|
* Based on DCM code by Doug Weibel, Jordi Mu<EFBFBD>oz and Jose Julio. DIYDrones.com
|
|
|
|
|
*
|
|
|
|
|
* Adapted for the general ArduPilot AHRS interface by Andrew Tridgell
|
2013-08-26 03:55:30 -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/>.
|
2012-08-21 23:19:51 -03:00
|
|
|
|
*/
|
2012-03-11 04:59:53 -03:00
|
|
|
|
#include <AP_AHRS.h>
|
2012-11-14 12:10:15 -04:00
|
|
|
|
#include <AP_HAL.h>
|
|
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
2014-03-30 08:00:25 -03:00
|
|
|
|
// this is the speed in m/s above which we first get a yaw lock with
|
2012-02-24 20:30:59 -04:00
|
|
|
|
// the GPS
|
2014-03-30 08:00:25 -03:00
|
|
|
|
#define GPS_SPEED_MIN 3
|
2010-10-24 15:37:10 -03:00
|
|
|
|
|
2012-06-27 22:09:22 -03:00
|
|
|
|
// the limit (in degrees/second) beyond which we stop integrating
|
|
|
|
|
// omega_I. At larger spin rates the DCM PI controller can get 'dizzy'
|
|
|
|
|
// which results in false gyro drift. See
|
|
|
|
|
// http://gentlenav.googlecode.com/files/fastRotations.pdf
|
|
|
|
|
#define SPIN_RATE_LIMIT 20
|
|
|
|
|
|
2012-04-16 07:51:46 -03:00
|
|
|
|
|
2012-03-07 00:09:17 -04:00
|
|
|
|
// run a full DCM update round
|
2010-10-24 15:37:10 -03:00
|
|
|
|
void
|
2012-03-11 04:59:53 -03:00
|
|
|
|
AP_AHRS_DCM::update(void)
|
2010-10-24 15:37:10 -03:00
|
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
|
float delta_t;
|
2011-09-15 00:04:08 -03:00
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// tell the IMU to grab some data
|
2013-11-03 23:34:32 -04:00
|
|
|
|
_ins.update();
|
2011-06-12 20:49:01 -03:00
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// ask the IMU how much time this sensor reading represents
|
2013-11-03 23:34:32 -04:00
|
|
|
|
delta_t = _ins.get_delta_time();
|
2011-09-15 00:04:08 -03:00
|
|
|
|
|
2012-11-13 00:33:10 -04:00
|
|
|
|
// if the update call took more than 0.2 seconds then discard it,
|
|
|
|
|
// otherwise we may move too far. This happens when arming motors
|
|
|
|
|
// in ArduCopter
|
2013-01-10 14:42:24 -04:00
|
|
|
|
if (delta_t > 0.2f) {
|
2014-02-26 18:41:28 -04:00
|
|
|
|
memset(&_ra_sum[0], 0, sizeof(_ra_sum));
|
2012-11-13 00:33:10 -04:00
|
|
|
|
_ra_deltat = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// Integrate the DCM matrix using gyro inputs
|
|
|
|
|
matrix_update(delta_t);
|
2011-09-11 15:03:55 -03:00
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// Normalize the DCM matrix
|
|
|
|
|
normalize();
|
2011-09-11 15:03:55 -03:00
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// Perform drift correction
|
|
|
|
|
drift_correction(delta_t);
|
2010-10-24 15:37:10 -03:00
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// paranoid check for bad values in the DCM matrix
|
|
|
|
|
check_matrix();
|
2010-11-14 22:15:16 -04:00
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// Calculate pitch, roll, yaw for stabilization and navigation
|
|
|
|
|
euler_angles();
|
2014-02-08 02:52:20 -04:00
|
|
|
|
|
|
|
|
|
// update trig values including _cos_roll, cos_pitch
|
|
|
|
|
update_trig();
|
2010-11-17 17:20:20 -04:00
|
|
|
|
}
|
2011-06-12 20:49:01 -03:00
|
|
|
|
|
2012-03-07 00:09:17 -04:00
|
|
|
|
// update the DCM matrix using only the gyros
|
2011-06-12 20:49:01 -03:00
|
|
|
|
void
|
2012-03-11 04:59:53 -03:00
|
|
|
|
AP_AHRS_DCM::matrix_update(float _G_Dt)
|
2010-10-24 15:37:10 -03:00
|
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// note that we do not include the P terms in _omega. This is
|
|
|
|
|
// because the spin_rate is calculated from _omega.length(),
|
|
|
|
|
// and including the P terms would give positive feedback into
|
|
|
|
|
// the _P_gain() calculation, which can lead to a very large P
|
|
|
|
|
// value
|
2014-02-26 18:41:28 -04:00
|
|
|
|
_omega.zero();
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
2014-02-26 18:41:28 -04:00
|
|
|
|
// average across all healthy gyros. This reduces noise on systems
|
|
|
|
|
// with more than one gyro
|
|
|
|
|
uint8_t healthy_count = 0;
|
|
|
|
|
for (uint8_t i=0; i<_ins.get_gyro_count(); i++) {
|
|
|
|
|
if (_ins.get_gyro_health(i)) {
|
|
|
|
|
_omega += _ins.get_gyro(i);
|
|
|
|
|
healthy_count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (healthy_count > 1) {
|
|
|
|
|
_omega /= healthy_count;
|
|
|
|
|
}
|
|
|
|
|
_omega += _omega_I;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
_dcm_matrix.rotate((_omega + _omega_P + _omega_yaw_P) * _G_Dt);
|
2012-06-27 05:16:41 -03:00
|
|
|
|
}
|
|
|
|
|
|
2012-06-27 22:09:22 -03:00
|
|
|
|
|
2011-12-13 06:32:50 -04:00
|
|
|
|
/*
|
2012-08-21 23:19:51 -03:00
|
|
|
|
* reset the DCM matrix and omega. Used on ground start, and on
|
|
|
|
|
* extreme errors in the matrix
|
2011-12-13 06:32:50 -04:00
|
|
|
|
*/
|
|
|
|
|
void
|
2012-03-11 04:59:53 -03:00
|
|
|
|
AP_AHRS_DCM::reset(bool recover_eulers)
|
2011-12-13 06:32:50 -04:00
|
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// reset the integration terms
|
|
|
|
|
_omega_I.zero();
|
|
|
|
|
_omega_P.zero();
|
|
|
|
|
_omega_yaw_P.zero();
|
|
|
|
|
_omega.zero();
|
|
|
|
|
|
|
|
|
|
// if the caller wants us to try to recover to the current
|
|
|
|
|
// attitude then calculate the dcm matrix from the current
|
|
|
|
|
// roll/pitch/yaw values
|
|
|
|
|
if (recover_eulers && !isnan(roll) && !isnan(pitch) && !isnan(yaw)) {
|
|
|
|
|
_dcm_matrix.from_euler(roll, pitch, yaw);
|
|
|
|
|
} else {
|
|
|
|
|
// otherwise make it flat
|
|
|
|
|
_dcm_matrix.from_euler(0, 0, 0);
|
|
|
|
|
}
|
2011-12-13 06:32:50 -04:00
|
|
|
|
}
|
2010-10-24 15:37:10 -03:00
|
|
|
|
|
2013-11-22 21:37:23 -04:00
|
|
|
|
// reset the current attitude, used by HIL
|
2013-11-23 06:44:56 -04:00
|
|
|
|
void AP_AHRS_DCM::reset_attitude(const float &_roll, const float &_pitch, const float &_yaw)
|
2013-11-22 21:37:23 -04:00
|
|
|
|
{
|
2013-11-23 06:44:56 -04:00
|
|
|
|
_dcm_matrix.from_euler(_roll, _pitch, _yaw);
|
2013-11-22 21:37:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-22 17:00:25 -04:00
|
|
|
|
/*
|
2012-08-21 23:19:51 -03:00
|
|
|
|
* check the DCM matrix for pathological values
|
2012-02-22 17:00:25 -04:00
|
|
|
|
*/
|
|
|
|
|
void
|
2012-03-11 04:59:53 -03:00
|
|
|
|
AP_AHRS_DCM::check_matrix(void)
|
2012-02-22 17:00:25 -04:00
|
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
|
if (_dcm_matrix.is_nan()) {
|
|
|
|
|
//Serial.printf("ERROR: DCM matrix NAN\n");
|
|
|
|
|
reset(true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// some DCM matrix values can lead to an out of range error in
|
|
|
|
|
// the pitch calculation via asin(). These NaN values can
|
|
|
|
|
// feed back into the rest of the DCM matrix via the
|
|
|
|
|
// error_course value.
|
2013-01-10 14:42:24 -04:00
|
|
|
|
if (!(_dcm_matrix.c.x < 1.0f &&
|
|
|
|
|
_dcm_matrix.c.x > -1.0f)) {
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// We have an invalid matrix. Force a normalisation.
|
|
|
|
|
normalize();
|
|
|
|
|
|
|
|
|
|
if (_dcm_matrix.is_nan() ||
|
2013-01-10 14:42:24 -04:00
|
|
|
|
fabsf(_dcm_matrix.c.x) > 10) {
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// normalisation didn't fix the problem! We're
|
|
|
|
|
// in real trouble. All we can do is reset
|
|
|
|
|
//Serial.printf("ERROR: DCM matrix error. _dcm_matrix.c.x=%f\n",
|
|
|
|
|
// _dcm_matrix.c.x);
|
|
|
|
|
reset(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-22 17:00:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2012-03-07 02:12:42 -04:00
|
|
|
|
// renormalise one vector component of the DCM matrix
|
|
|
|
|
// this will return false if renormalization fails
|
2012-03-07 00:09:17 -04:00
|
|
|
|
bool
|
2012-03-11 04:59:53 -03:00
|
|
|
|
AP_AHRS_DCM::renorm(Vector3f const &a, Vector3f &result)
|
2010-10-24 15:37:10 -03:00
|
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
|
float renorm_val;
|
|
|
|
|
|
|
|
|
|
// numerical errors will slowly build up over time in DCM,
|
|
|
|
|
// causing inaccuracies. We can keep ahead of those errors
|
|
|
|
|
// using the renormalization technique from the DCM IMU paper
|
|
|
|
|
// (see equations 18 to 21).
|
|
|
|
|
|
|
|
|
|
// For APM we don't bother with the taylor expansion
|
|
|
|
|
// optimisation from the paper as on our 2560 CPU the cost of
|
|
|
|
|
// the sqrt() is 44 microseconds, and the small time saving of
|
|
|
|
|
// the taylor expansion is not worth the potential of
|
|
|
|
|
// additional error buildup.
|
|
|
|
|
|
|
|
|
|
// Note that we can get significant renormalisation values
|
|
|
|
|
// when we have a larger delta_t due to a glitch eleswhere in
|
|
|
|
|
// APM, such as a I2c timeout or a set of EEPROM writes. While
|
|
|
|
|
// we would like to avoid these if possible, if it does happen
|
|
|
|
|
// we don't want to compound the error by making DCM less
|
|
|
|
|
// accurate.
|
|
|
|
|
|
2013-01-10 14:42:24 -04:00
|
|
|
|
renorm_val = 1.0f / a.length();
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
|
|
|
|
// keep the average for reporting
|
|
|
|
|
_renorm_val_sum += renorm_val;
|
|
|
|
|
_renorm_val_count++;
|
|
|
|
|
|
2013-01-10 14:42:24 -04:00
|
|
|
|
if (!(renorm_val < 2.0f && renorm_val > 0.5f)) {
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// this is larger than it should get - log it as a warning
|
2013-01-10 14:42:24 -04:00
|
|
|
|
if (!(renorm_val < 1.0e6f && renorm_val > 1.0e-6f)) {
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// we are getting values which are way out of
|
|
|
|
|
// range, we will reset the matrix and hope we
|
|
|
|
|
// can recover our attitude using drift
|
|
|
|
|
// correction before we hit the ground!
|
|
|
|
|
//Serial.printf("ERROR: DCM renormalisation error. renorm_val=%f\n",
|
|
|
|
|
// renorm_val);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = a * renorm_val;
|
|
|
|
|
return true;
|
2012-03-07 00:09:17 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*************************************************
|
2012-08-21 23:19:51 -03:00
|
|
|
|
* Direction Cosine Matrix IMU: Theory
|
|
|
|
|
* William Premerlani and Paul Bizard
|
|
|
|
|
*
|
|
|
|
|
* Numerical errors will gradually reduce the orthogonality conditions expressed by equation 5
|
|
|
|
|
* to approximations rather than identities. In effect, the axes in the two frames of reference no
|
|
|
|
|
* longer describe a rigid body. Fortunately, numerical error accumulates very slowly, so it is a
|
|
|
|
|
* simple matter to stay ahead of it.
|
|
|
|
|
* We call the process of enforcing the orthogonality conditions <EFBFBD>renormalization<EFBFBD>.
|
|
|
|
|
*/
|
2012-03-07 00:09:17 -04:00
|
|
|
|
void
|
2012-03-11 04:59:53 -03:00
|
|
|
|
AP_AHRS_DCM::normalize(void)
|
2012-03-07 00:09:17 -04:00
|
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
|
float error;
|
|
|
|
|
Vector3f t0, t1, t2;
|
|
|
|
|
|
|
|
|
|
error = _dcm_matrix.a * _dcm_matrix.b; // eq.18
|
|
|
|
|
|
|
|
|
|
t0 = _dcm_matrix.a - (_dcm_matrix.b * (0.5f * error)); // eq.19
|
|
|
|
|
t1 = _dcm_matrix.b - (_dcm_matrix.a * (0.5f * error)); // eq.19
|
|
|
|
|
t2 = t0 % t1; // c= a x b // eq.20
|
|
|
|
|
|
|
|
|
|
if (!renorm(t0, _dcm_matrix.a) ||
|
|
|
|
|
!renorm(t1, _dcm_matrix.b) ||
|
|
|
|
|
!renorm(t2, _dcm_matrix.c)) {
|
|
|
|
|
// Our solution is blowing up and we will force back
|
|
|
|
|
// to last euler angles
|
2014-05-15 04:09:18 -03:00
|
|
|
|
_last_failure_ms = hal.scheduler->millis();
|
2012-08-21 23:19:51 -03:00
|
|
|
|
reset(true);
|
|
|
|
|
}
|
2010-10-24 15:37:10 -03:00
|
|
|
|
}
|
|
|
|
|
|
2012-03-07 00:09:17 -04:00
|
|
|
|
|
2012-06-27 22:09:22 -03:00
|
|
|
|
// produce a yaw error value. The returned value is proportional
|
|
|
|
|
// to sin() of the current heading error in earth frame
|
|
|
|
|
float
|
|
|
|
|
AP_AHRS_DCM::yaw_error_compass(void)
|
2010-10-24 15:37:10 -03:00
|
|
|
|
{
|
2013-12-08 23:10:49 -04:00
|
|
|
|
const Vector3f &mag = _compass->get_field();
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// get the mag vector in the earth frame
|
2013-05-05 00:51:45 -03:00
|
|
|
|
Vector2f rb = _dcm_matrix.mulXY(mag);
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
|
|
|
|
rb.normalize();
|
|
|
|
|
if (rb.is_inf()) {
|
|
|
|
|
// not a valid vector
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
2012-03-23 02:22:56 -03:00
|
|
|
|
|
2013-05-04 12:25:59 -03:00
|
|
|
|
// update vector holding earths magnetic field (if required)
|
|
|
|
|
if( _last_declination != _compass->get_declination() ) {
|
|
|
|
|
_last_declination = _compass->get_declination();
|
|
|
|
|
_mag_earth.x = cosf(_last_declination);
|
|
|
|
|
_mag_earth.y = sinf(_last_declination);
|
|
|
|
|
}
|
2012-03-23 02:22:56 -03:00
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// calculate the error term in earth frame
|
2013-05-05 00:51:45 -03:00
|
|
|
|
// calculate the Z component of the cross product of rb and _mag_earth
|
|
|
|
|
return rb % _mag_earth;
|
2012-06-27 22:09:22 -03:00
|
|
|
|
}
|
2012-06-19 01:47:09 -03:00
|
|
|
|
|
2012-07-04 03:53:40 -03:00
|
|
|
|
// the _P_gain raises the gain of the PI controller
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// when we are spinning fast. See the fastRotations
|
|
|
|
|
// paper from Bill.
|
2012-06-27 22:09:22 -03:00
|
|
|
|
float
|
|
|
|
|
AP_AHRS_DCM::_P_gain(float spin_rate)
|
|
|
|
|
{
|
2013-07-12 23:36:25 -03:00
|
|
|
|
if (spin_rate < ToRad(50)) {
|
2013-01-10 14:42:24 -04:00
|
|
|
|
return 1.0f;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
}
|
2013-07-12 23:36:25 -03:00
|
|
|
|
if (spin_rate > ToRad(500)) {
|
2013-01-10 14:42:24 -04:00
|
|
|
|
return 10.0f;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
}
|
2013-07-12 23:36:25 -03:00
|
|
|
|
return spin_rate/ToRad(50);
|
2012-06-27 22:09:22 -03:00
|
|
|
|
}
|
2012-06-19 01:47:09 -03:00
|
|
|
|
|
2014-01-18 03:18:12 -04:00
|
|
|
|
// _yaw_gain reduces the gain of the PI controller applied to heading errors
|
|
|
|
|
// when observability from change of velocity is good (eg changing speed or turning)
|
|
|
|
|
// This reduces unwanted roll and pitch coupling due to compass errors for planes.
|
|
|
|
|
// High levels of noise on _accel_ef will cause the gain to drop and could lead to
|
|
|
|
|
// increased heading drift during straight and level flight, however some gain is
|
|
|
|
|
// always available. TODO check the necessity of adding adjustable acc threshold
|
|
|
|
|
// and/or filtering accelerations before getting magnitude
|
|
|
|
|
float
|
2014-02-26 18:41:28 -04:00
|
|
|
|
AP_AHRS_DCM::_yaw_gain(void) const
|
2014-01-18 03:18:12 -04:00
|
|
|
|
{
|
2014-02-26 18:41:28 -04:00
|
|
|
|
float VdotEFmag = pythagorous2(_accel_ef[_active_accel_instance].x,
|
|
|
|
|
_accel_ef[_active_accel_instance].y);
|
2014-01-18 03:18:12 -04:00
|
|
|
|
if (VdotEFmag <= 4.0f) {
|
|
|
|
|
return 0.2f*(4.5f - VdotEFmag);
|
|
|
|
|
}
|
|
|
|
|
return 0.1f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-08-11 02:55:53 -03:00
|
|
|
|
// return true if we have and should use GPS
|
2013-07-07 22:25:40 -03:00
|
|
|
|
bool AP_AHRS_DCM::have_gps(void) const
|
2012-08-11 02:55:53 -03:00
|
|
|
|
{
|
2014-03-30 08:00:25 -03:00
|
|
|
|
if (_gps.status() <= AP_GPS::NO_FIX || !_gps_use) {
|
2012-08-21 23:19:51 -03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2012-08-11 02:55:53 -03:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-28 23:48:25 -03:00
|
|
|
|
// return true if we should use the compass for yaw correction
|
2013-10-21 23:06:27 -03:00
|
|
|
|
bool AP_AHRS_DCM::use_compass(void)
|
2013-03-28 23:48:25 -03:00
|
|
|
|
{
|
|
|
|
|
if (!_compass || !_compass->use_for_yaw()) {
|
|
|
|
|
// no compass available
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-05-06 01:31:49 -03:00
|
|
|
|
if (!_flags.fly_forward || !have_gps()) {
|
2013-03-28 23:48:25 -03:00
|
|
|
|
// we don't have any alterative to the compass
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-03-30 08:00:25 -03:00
|
|
|
|
if (_gps.ground_speed() < GPS_SPEED_MIN) {
|
2013-03-28 23:48:25 -03:00
|
|
|
|
// we are not going fast enough to use the GPS
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if the current yaw differs from the GPS yaw by more than 45
|
|
|
|
|
// degrees and the estimated wind speed is less than 80% of the
|
|
|
|
|
// ground speed, then switch to GPS navigation. This will help
|
|
|
|
|
// prevent flyaways with very bad compass offsets
|
2014-03-30 08:00:25 -03:00
|
|
|
|
int32_t error = abs(wrap_180_cd(yaw_sensor - _gps.ground_course_cd()));
|
|
|
|
|
if (error > 4500 && _wind.length() < _gps.ground_speed()*0.8f) {
|
2013-10-21 23:06:27 -03:00
|
|
|
|
if (hal.scheduler->millis() - _last_consistent_heading > 2000) {
|
|
|
|
|
// start using the GPS for heading if the compass has been
|
|
|
|
|
// inconsistent with the GPS for 2 seconds
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
_last_consistent_heading = hal.scheduler->millis();
|
2013-03-28 23:48:25 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// use the compass
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-27 22:09:22 -03:00
|
|
|
|
// yaw drift correction using the compass or GPS
|
|
|
|
|
// this function prodoces the _omega_yaw_P vector, and also
|
|
|
|
|
// contributes to the _omega_I.z long term yaw drift estimate
|
|
|
|
|
void
|
|
|
|
|
AP_AHRS_DCM::drift_correction_yaw(void)
|
|
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
|
bool new_value = false;
|
|
|
|
|
float yaw_error;
|
|
|
|
|
float yaw_deltat;
|
|
|
|
|
|
2013-03-28 23:48:25 -03:00
|
|
|
|
if (use_compass()) {
|
2013-06-24 23:43:53 -03:00
|
|
|
|
/*
|
|
|
|
|
we are using compass for yaw
|
|
|
|
|
*/
|
2012-08-21 23:19:51 -03:00
|
|
|
|
if (_compass->last_update != _compass_last_update) {
|
2013-01-10 14:42:24 -04:00
|
|
|
|
yaw_deltat = (_compass->last_update - _compass_last_update) * 1.0e-6f;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
_compass_last_update = _compass->last_update;
|
|
|
|
|
// we force an additional compass read()
|
|
|
|
|
// here. This has the effect of throwing away
|
|
|
|
|
// the first compass value, which can be bad
|
2013-05-06 01:31:49 -03:00
|
|
|
|
if (!_flags.have_initial_yaw && _compass->read()) {
|
2012-08-21 23:19:51 -03:00
|
|
|
|
float heading = _compass->calculate_heading(_dcm_matrix);
|
|
|
|
|
_dcm_matrix.from_euler(roll, pitch, heading);
|
|
|
|
|
_omega_yaw_P.zero();
|
2013-05-06 01:31:49 -03:00
|
|
|
|
_flags.have_initial_yaw = true;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
}
|
|
|
|
|
new_value = true;
|
|
|
|
|
yaw_error = yaw_error_compass();
|
2013-11-03 22:57:06 -04:00
|
|
|
|
|
|
|
|
|
// also update the _gps_last_update, so if we later
|
|
|
|
|
// disable the compass due to significant yaw error we
|
|
|
|
|
// don't suddenly change yaw with a reset
|
2014-03-30 08:00:25 -03:00
|
|
|
|
_gps_last_update = _gps.last_fix_time_ms();
|
2012-08-21 23:19:51 -03:00
|
|
|
|
}
|
2013-05-06 01:31:49 -03:00
|
|
|
|
} else if (_flags.fly_forward && have_gps()) {
|
2013-06-24 23:43:53 -03:00
|
|
|
|
/*
|
|
|
|
|
we are using GPS for yaw
|
|
|
|
|
*/
|
2014-03-30 08:00:25 -03:00
|
|
|
|
if (_gps.last_fix_time_ms() != _gps_last_update &&
|
|
|
|
|
_gps.ground_speed() >= GPS_SPEED_MIN) {
|
|
|
|
|
yaw_deltat = (_gps.last_fix_time_ms() - _gps_last_update) * 1.0e-3f;
|
|
|
|
|
_gps_last_update = _gps.last_fix_time_ms();
|
2013-06-24 23:43:53 -03:00
|
|
|
|
new_value = true;
|
2014-03-30 08:00:25 -03:00
|
|
|
|
float gps_course_rad = ToRad(_gps.ground_course_cd() * 0.01f);
|
2013-11-03 22:57:51 -04:00
|
|
|
|
float yaw_error_rad = wrap_PI(gps_course_rad - yaw);
|
2013-06-24 23:43:53 -03:00
|
|
|
|
yaw_error = sinf(yaw_error_rad);
|
|
|
|
|
|
|
|
|
|
/* reset yaw to match GPS heading under any of the
|
|
|
|
|
following 3 conditions:
|
|
|
|
|
|
|
|
|
|
1) if we have reached GPS_SPEED_MIN and have never had
|
|
|
|
|
yaw information before
|
|
|
|
|
|
|
|
|
|
2) if the last time we got yaw information from the GPS
|
|
|
|
|
is more than 20 seconds ago, which means we may have
|
|
|
|
|
suffered from considerable gyro drift
|
|
|
|
|
|
|
|
|
|
3) if we are over 3*GPS_SPEED_MIN (which means 9m/s)
|
|
|
|
|
and our yaw error is over 60 degrees, which means very
|
|
|
|
|
poor yaw. This can happen on bungee launch when the
|
|
|
|
|
operator pulls back the plane rapidly enough then on
|
|
|
|
|
release the GPS heading changes very rapidly
|
|
|
|
|
*/
|
|
|
|
|
if (!_flags.have_initial_yaw ||
|
|
|
|
|
yaw_deltat > 20 ||
|
2014-03-30 08:00:25 -03:00
|
|
|
|
(_gps.ground_speed() >= 3*GPS_SPEED_MIN && fabsf(yaw_error_rad) >= 1.047f)) {
|
2013-06-24 23:43:53 -03:00
|
|
|
|
// reset DCM matrix based on current yaw
|
|
|
|
|
_dcm_matrix.from_euler(roll, pitch, gps_course_rad);
|
2012-08-21 23:19:51 -03:00
|
|
|
|
_omega_yaw_P.zero();
|
2013-05-06 01:31:49 -03:00
|
|
|
|
_flags.have_initial_yaw = true;
|
2013-06-24 23:43:53 -03:00
|
|
|
|
yaw_error = 0;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!new_value) {
|
|
|
|
|
// we don't have any new yaw information
|
|
|
|
|
// slowly decay _omega_yaw_P to cope with loss
|
|
|
|
|
// of our yaw source
|
2013-01-10 14:42:24 -04:00
|
|
|
|
_omega_yaw_P *= 0.97f;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// convert the error vector to body frame
|
2013-05-05 00:51:45 -03:00
|
|
|
|
float error_z = _dcm_matrix.c.z * yaw_error;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
|
|
|
|
// the spin rate changes the P gain, and disables the
|
|
|
|
|
// integration at higher rates
|
|
|
|
|
float spin_rate = _omega.length();
|
|
|
|
|
|
|
|
|
|
// update the proportional control to drag the
|
|
|
|
|
// yaw back to the right value. We use a gain
|
|
|
|
|
// that depends on the spin rate. See the fastRotations.pdf
|
|
|
|
|
// paper from Bill Premerlani
|
2014-01-18 03:18:12 -04:00
|
|
|
|
// We also adjust the gain depending on the rate of change of horizontal velocity which
|
|
|
|
|
// is proportional to how observable the heading is from the acceerations and GPS velocity
|
|
|
|
|
// The accelration derived heading will be more reliable in turns than compass or GPS
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
2014-02-26 18:41:28 -04:00
|
|
|
|
_omega_yaw_P.z = error_z * _P_gain(spin_rate) * _kp_yaw * _yaw_gain();
|
2013-05-06 01:31:49 -03:00
|
|
|
|
if (_flags.fast_ground_gains) {
|
2012-08-21 23:19:51 -03:00
|
|
|
|
_omega_yaw_P.z *= 8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// don't update the drift term if we lost the yaw reference
|
|
|
|
|
// for more than 2 seconds
|
2013-01-10 14:42:24 -04:00
|
|
|
|
if (yaw_deltat < 2.0f && spin_rate < ToRad(SPIN_RATE_LIMIT)) {
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// also add to the I term
|
2013-05-05 00:51:45 -03:00
|
|
|
|
_omega_I_sum.z += error_z * _ki_yaw * yaw_deltat;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-10 14:42:24 -04:00
|
|
|
|
_error_yaw_sum += fabsf(yaw_error);
|
2012-08-21 23:19:51 -03:00
|
|
|
|
_error_yaw_count++;
|
2012-06-27 22:09:22 -03:00
|
|
|
|
}
|
2012-03-23 02:22:56 -03:00
|
|
|
|
|
2012-06-27 22:09:22 -03:00
|
|
|
|
|
2013-11-04 00:50:33 -04:00
|
|
|
|
/**
|
2014-02-26 18:41:28 -04:00
|
|
|
|
return an accel vector delayed by AHRS_ACCEL_DELAY samples for a
|
|
|
|
|
specific accelerometer instance
|
2013-11-04 00:50:33 -04:00
|
|
|
|
*/
|
2014-02-26 18:41:28 -04:00
|
|
|
|
Vector3f AP_AHRS_DCM::ra_delayed(uint8_t instance, const Vector3f &ra)
|
2013-11-04 00:50:33 -04:00
|
|
|
|
{
|
|
|
|
|
// get the old element, and then fill it with the new element
|
2014-02-26 18:41:28 -04:00
|
|
|
|
Vector3f ret = _ra_delay_buffer[instance];
|
|
|
|
|
_ra_delay_buffer[instance] = ra;
|
2014-05-15 09:19:54 -03:00
|
|
|
|
if (ret.is_zero()) {
|
|
|
|
|
// use the current vector if the previous vector is exactly
|
|
|
|
|
// zero. This prevents an error on initialisation
|
|
|
|
|
return ra;
|
|
|
|
|
}
|
2013-11-04 00:50:33 -04:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
2012-06-27 22:09:22 -03:00
|
|
|
|
|
|
|
|
|
// perform drift correction. This function aims to update _omega_P and
|
|
|
|
|
// _omega_I with our best estimate of the short term and long term
|
|
|
|
|
// gyro error. The _omega_P value is what pulls our attitude solution
|
|
|
|
|
// back towards the reference vector quickly. The _omega_I term is an
|
|
|
|
|
// attempt to learn the long term drift rate of the gyros.
|
|
|
|
|
//
|
|
|
|
|
// This drift correction implementation is based on a paper
|
|
|
|
|
// by Bill Premerlani from here:
|
|
|
|
|
// http://gentlenav.googlecode.com/files/RollPitchDriftCompensation.pdf
|
|
|
|
|
void
|
|
|
|
|
AP_AHRS_DCM::drift_correction(float deltat)
|
|
|
|
|
{
|
|
|
|
|
Vector3f velocity;
|
|
|
|
|
uint32_t last_correction_time;
|
|
|
|
|
|
|
|
|
|
// perform yaw drift correction if we have a new yaw reference
|
|
|
|
|
// vector
|
|
|
|
|
drift_correction_yaw();
|
|
|
|
|
|
2012-12-12 03:22:56 -04:00
|
|
|
|
// rotate accelerometer values into the earth frame
|
2014-02-26 18:41:28 -04:00
|
|
|
|
for (uint8_t i=0; i<_ins.get_accel_count(); i++) {
|
|
|
|
|
if (_ins.get_accel_health(i)) {
|
|
|
|
|
_accel_ef[i] = _dcm_matrix * _ins.get_accel(i);
|
|
|
|
|
// integrate the accel vector in the earth frame between GPS readings
|
|
|
|
|
_ra_sum[i] += _accel_ef[i] * deltat;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-06-27 22:09:22 -03:00
|
|
|
|
|
|
|
|
|
// keep a sum of the deltat values, so we know how much time
|
|
|
|
|
// we have integrated over
|
|
|
|
|
_ra_deltat += deltat;
|
|
|
|
|
|
2013-05-04 23:47:49 -03:00
|
|
|
|
if (!have_gps() ||
|
2014-03-30 08:00:25 -03:00
|
|
|
|
_gps.status() < AP_GPS::GPS_OK_FIX_3D ||
|
|
|
|
|
_gps.num_sats() < _gps_minsats) {
|
2012-11-05 05:06:44 -04:00
|
|
|
|
// no GPS, or not a good lock. From experience we need at
|
|
|
|
|
// least 6 satellites to get a really reliable velocity number
|
|
|
|
|
// from the GPS.
|
|
|
|
|
//
|
|
|
|
|
// As a fallback we use the fixed wing acceleration correction
|
|
|
|
|
// if we have an airspeed estimate (which we only have if
|
|
|
|
|
// _fly_forward is set), otherwise no correction
|
2013-01-10 14:42:24 -04:00
|
|
|
|
if (_ra_deltat < 0.2f) {
|
2012-06-27 22:09:22 -03:00
|
|
|
|
// not enough time has accumulated
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-08-21 23:19:51 -03:00
|
|
|
|
float airspeed;
|
|
|
|
|
if (_airspeed && _airspeed->use()) {
|
|
|
|
|
airspeed = _airspeed->get_airspeed();
|
|
|
|
|
} else {
|
|
|
|
|
airspeed = _last_airspeed;
|
|
|
|
|
}
|
|
|
|
|
// use airspeed to estimate our ground velocity in
|
|
|
|
|
// earth frame by subtracting the wind
|
|
|
|
|
velocity = _dcm_matrix.colx() * airspeed;
|
|
|
|
|
|
|
|
|
|
// add in wind estimate
|
|
|
|
|
velocity += _wind;
|
|
|
|
|
|
2012-11-14 12:10:15 -04:00
|
|
|
|
last_correction_time = hal.scheduler->millis();
|
2012-08-21 23:19:51 -03:00
|
|
|
|
_have_gps_lock = false;
|
2012-06-27 22:09:22 -03:00
|
|
|
|
} else {
|
2014-03-30 08:00:25 -03:00
|
|
|
|
if (_gps.last_fix_time_ms() == _ra_sum_start) {
|
2012-06-27 22:09:22 -03:00
|
|
|
|
// we don't have a new GPS fix - nothing more to do
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-03-30 08:00:25 -03:00
|
|
|
|
velocity = _gps.velocity();
|
|
|
|
|
last_correction_time = _gps.last_fix_time_ms();
|
2012-08-21 23:19:51 -03:00
|
|
|
|
if (_have_gps_lock == false) {
|
|
|
|
|
// if we didn't have GPS lock in the last drift
|
|
|
|
|
// correction interval then set the velocities equal
|
|
|
|
|
_last_velocity = velocity;
|
|
|
|
|
}
|
|
|
|
|
_have_gps_lock = true;
|
|
|
|
|
|
2013-05-06 21:52:14 -03:00
|
|
|
|
// keep last airspeed estimate for dead-reckoning purposes
|
|
|
|
|
Vector3f airspeed = velocity - _wind;
|
|
|
|
|
airspeed.z = 0;
|
|
|
|
|
_last_airspeed = airspeed.length();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (have_gps()) {
|
|
|
|
|
// use GPS for positioning with any fix, even a 2D fix
|
2014-03-30 08:00:25 -03:00
|
|
|
|
_last_lat = _gps.location().lat;
|
|
|
|
|
_last_lng = _gps.location().lng;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
_position_offset_north = 0;
|
|
|
|
|
_position_offset_east = 0;
|
|
|
|
|
|
2013-05-06 21:52:14 -03:00
|
|
|
|
// once we have a single GPS lock, we can update using
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// dead-reckoning from then on
|
|
|
|
|
_have_position = true;
|
2013-05-06 21:52:14 -03:00
|
|
|
|
} else {
|
|
|
|
|
// update dead-reckoning position estimate
|
|
|
|
|
_position_offset_north += velocity.x * _ra_deltat;
|
|
|
|
|
_position_offset_east += velocity.y * _ra_deltat;
|
2012-06-27 22:09:22 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// see if this is our first time through - in which case we
|
|
|
|
|
// just setup the start times and return
|
|
|
|
|
if (_ra_sum_start == 0) {
|
|
|
|
|
_ra_sum_start = last_correction_time;
|
|
|
|
|
_last_velocity = velocity;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-04 03:53:40 -03:00
|
|
|
|
// equation 9: get the corrected acceleration vector in earth frame. Units
|
2012-06-27 22:09:22 -03:00
|
|
|
|
// are m/s/s
|
2012-07-04 03:53:40 -03:00
|
|
|
|
Vector3f GA_e;
|
2013-05-04 23:47:49 -03:00
|
|
|
|
GA_e = Vector3f(0, 0, -1.0f);
|
2013-05-06 01:31:49 -03:00
|
|
|
|
|
2013-11-04 00:50:33 -04:00
|
|
|
|
bool using_gps_corrections = false;
|
2014-02-26 18:41:28 -04:00
|
|
|
|
float ra_scale = 1.0f/(_ra_deltat*GRAVITY_MSS);
|
|
|
|
|
|
2013-05-06 01:31:49 -03:00
|
|
|
|
if (_flags.correct_centrifugal && (_have_gps_lock || _flags.fly_forward)) {
|
2014-02-26 18:41:28 -04:00
|
|
|
|
float v_scale = gps_gain.get() * ra_scale;
|
2013-05-04 23:47:49 -03:00
|
|
|
|
Vector3f vdelta = (velocity - _last_velocity) * v_scale;
|
|
|
|
|
GA_e += vdelta;
|
|
|
|
|
GA_e.normalize();
|
|
|
|
|
if (GA_e.is_inf()) {
|
|
|
|
|
// wait for some non-zero acceleration information
|
2014-05-15 04:09:18 -03:00
|
|
|
|
_last_failure_ms = hal.scheduler->millis();
|
2013-05-04 23:47:49 -03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2013-11-04 00:50:33 -04:00
|
|
|
|
using_gps_corrections = true;
|
2012-06-27 22:09:22 -03:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-04 03:53:40 -03:00
|
|
|
|
// calculate the error term in earth frame.
|
2014-02-26 18:41:28 -04:00
|
|
|
|
// we do this for each available accelerometer then pick the
|
|
|
|
|
// accelerometer that leads to the smallest error term. This takes
|
|
|
|
|
// advantage of the different sample rates on different
|
|
|
|
|
// accelerometers to dramatically reduce the impact of aliasing
|
|
|
|
|
// due to harmonics of vibrations that match closely the sampling
|
|
|
|
|
// rate of our accelerometers. On the Pixhawk we have the LSM303D
|
|
|
|
|
// running at 800Hz and the MPU6000 running at 1kHz, by combining
|
|
|
|
|
// the two the effects of aliasing are greatly reduced.
|
|
|
|
|
Vector3f error[INS_MAX_INSTANCES];
|
|
|
|
|
Vector3f GA_b[INS_MAX_INSTANCES];
|
|
|
|
|
int8_t besti = -1;
|
|
|
|
|
float best_error = 0;
|
|
|
|
|
for (uint8_t i=0; i<_ins.get_accel_count(); i++) {
|
2014-05-15 09:18:56 -03:00
|
|
|
|
if (!_ins.get_accel_health(i)) {
|
2014-02-26 18:41:28 -04:00
|
|
|
|
// only use healthy sensors
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
_ra_sum[i] *= ra_scale;
|
2013-11-04 00:50:33 -04:00
|
|
|
|
|
2014-02-26 18:41:28 -04:00
|
|
|
|
// get the delayed ra_sum to match the GPS lag
|
|
|
|
|
if (using_gps_corrections) {
|
|
|
|
|
GA_b[i] = ra_delayed(i, _ra_sum[i]);
|
|
|
|
|
} else {
|
|
|
|
|
GA_b[i] = _ra_sum[i];
|
|
|
|
|
}
|
2014-04-21 02:23:01 -03:00
|
|
|
|
if (GA_b[i].is_zero()) {
|
|
|
|
|
// wait for some non-zero acceleration information
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-02-26 18:41:28 -04:00
|
|
|
|
GA_b[i].normalize();
|
|
|
|
|
if (GA_b[i].is_inf()) {
|
|
|
|
|
// wait for some non-zero acceleration information
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
error[i] = GA_b[i] % GA_e;
|
|
|
|
|
float error_length = error[i].length();
|
|
|
|
|
if (besti == -1 || error_length < best_error) {
|
|
|
|
|
besti = i;
|
|
|
|
|
best_error = error_length;
|
|
|
|
|
}
|
2013-11-04 00:50:33 -04:00
|
|
|
|
}
|
2014-02-26 18:41:28 -04:00
|
|
|
|
|
|
|
|
|
if (besti == -1) {
|
|
|
|
|
// no healthy accelerometers!
|
2014-05-15 04:09:18 -03:00
|
|
|
|
_last_failure_ms = hal.scheduler->millis();
|
2013-11-03 22:59:38 -04:00
|
|
|
|
return;
|
2012-07-06 00:03:46 -03:00
|
|
|
|
}
|
2013-11-03 22:59:38 -04:00
|
|
|
|
|
2014-02-26 18:41:28 -04:00
|
|
|
|
_active_accel_instance = besti;
|
2012-07-06 00:03:46 -03:00
|
|
|
|
|
|
|
|
|
#define YAW_INDEPENDENT_DRIFT_CORRECTION 0
|
2012-08-21 23:19:51 -03:00
|
|
|
|
#if YAW_INDEPENDENT_DRIFT_CORRECTION
|
2012-07-04 03:53:40 -03:00
|
|
|
|
// step 2 calculate earth_error_Z
|
|
|
|
|
float earth_error_Z = error.z;
|
|
|
|
|
|
|
|
|
|
// equation 10
|
2012-12-18 22:36:35 -04:00
|
|
|
|
float tilt = pythagorous2(GA_e.x, GA_e.y);
|
2012-07-04 03:53:40 -03:00
|
|
|
|
|
|
|
|
|
// equation 11
|
2014-02-26 18:41:28 -04:00
|
|
|
|
float theta = atan2f(GA_b[besti].y, GA_b[besti].x);
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
2012-07-04 03:53:40 -03:00
|
|
|
|
// equation 12
|
2013-01-10 14:42:24 -04:00
|
|
|
|
Vector3f GA_e2 = Vector3f(cosf(theta)*tilt, sinf(theta)*tilt, GA_e.z);
|
2012-07-04 03:53:40 -03:00
|
|
|
|
|
|
|
|
|
// step 6
|
2014-02-26 18:41:28 -04:00
|
|
|
|
error = GA_b[besti] % GA_e2;
|
2012-07-06 00:03:46 -03:00
|
|
|
|
error.z = earth_error_Z;
|
|
|
|
|
#endif // YAW_INDEPENDENT_DRIFT_CORRECTION
|
2012-07-04 19:11:37 -03:00
|
|
|
|
|
2012-07-19 22:49:40 -03:00
|
|
|
|
// to reduce the impact of two competing yaw controllers, we
|
|
|
|
|
// reduce the impact of the gps/accelerometers on yaw when we are
|
|
|
|
|
// flat, but still allow for yaw correction using the
|
2012-07-20 03:23:56 -03:00
|
|
|
|
// accelerometers at high roll angles as long as we have a GPS
|
2013-03-28 23:48:25 -03:00
|
|
|
|
if (use_compass()) {
|
2013-01-10 14:42:24 -04:00
|
|
|
|
if (have_gps() && gps_gain == 1.0f) {
|
2014-02-26 18:41:28 -04:00
|
|
|
|
error[besti].z *= sinf(fabsf(roll));
|
2012-08-21 23:19:51 -03:00
|
|
|
|
} else {
|
2014-02-26 18:41:28 -04:00
|
|
|
|
error[besti].z = 0;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
}
|
2012-07-04 19:11:37 -03:00
|
|
|
|
}
|
2012-06-27 22:09:22 -03:00
|
|
|
|
|
2013-11-06 22:54:16 -04:00
|
|
|
|
// if ins is unhealthy then stop attitude drift correction and
|
|
|
|
|
// hope the gyros are OK for a while. Just slowly reduce _omega_P
|
|
|
|
|
// to prevent previous bad accels from throwing us off
|
|
|
|
|
if (!_ins.healthy()) {
|
2014-02-26 18:41:28 -04:00
|
|
|
|
error[besti].zero();
|
2013-11-06 22:54:16 -04:00
|
|
|
|
} else {
|
|
|
|
|
// convert the error term to body frame
|
2014-02-26 18:41:28 -04:00
|
|
|
|
error[besti] = _dcm_matrix.mul_transpose(error[besti]);
|
2013-11-06 22:54:16 -04:00
|
|
|
|
}
|
2012-06-27 22:09:22 -03:00
|
|
|
|
|
2014-02-26 18:41:28 -04:00
|
|
|
|
if (error[besti].is_nan() || error[besti].is_inf()) {
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// don't allow bad values
|
|
|
|
|
check_matrix();
|
2014-05-15 04:09:18 -03:00
|
|
|
|
_last_failure_ms = hal.scheduler->millis();
|
2012-08-21 23:19:51 -03:00
|
|
|
|
return;
|
2012-08-18 00:53:59 -03:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-26 18:41:28 -04:00
|
|
|
|
_error_rp_sum += best_error;
|
2012-07-04 03:53:40 -03:00
|
|
|
|
_error_rp_count++;
|
|
|
|
|
|
2012-06-27 22:09:22 -03:00
|
|
|
|
// base the P gain on the spin rate
|
|
|
|
|
float spin_rate = _omega.length();
|
|
|
|
|
|
|
|
|
|
// we now want to calculate _omega_P and _omega_I. The
|
|
|
|
|
// _omega_P value is what drags us quickly to the
|
|
|
|
|
// accelerometer reading.
|
2014-02-26 18:41:28 -04:00
|
|
|
|
_omega_P = error[besti] * _P_gain(spin_rate) * _kp;
|
2013-05-06 01:31:49 -03:00
|
|
|
|
if (_flags.fast_ground_gains) {
|
2012-08-21 23:19:51 -03:00
|
|
|
|
_omega_P *= 8;
|
2012-08-21 02:38:06 -03:00
|
|
|
|
}
|
2012-06-27 22:09:22 -03:00
|
|
|
|
|
2014-03-30 08:00:25 -03:00
|
|
|
|
if (_flags.fly_forward && _gps.status() >= AP_GPS::GPS_OK_FIX_2D &&
|
|
|
|
|
_gps.ground_speed() < GPS_SPEED_MIN &&
|
2013-11-03 23:34:32 -04:00
|
|
|
|
_ins.get_accel().x >= 7 &&
|
2013-04-27 19:03:36 -03:00
|
|
|
|
pitch_sensor > -3000 && pitch_sensor < 3000) {
|
|
|
|
|
// assume we are in a launch acceleration, and reduce the
|
|
|
|
|
// rp gain by 50% to reduce the impact of GPS lag on
|
|
|
|
|
// takeoff attitude when using a catapult
|
|
|
|
|
_omega_P *= 0.5f;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-27 22:09:22 -03:00
|
|
|
|
// accumulate some integrator error
|
|
|
|
|
if (spin_rate < ToRad(SPIN_RATE_LIMIT)) {
|
2014-02-26 18:41:28 -04:00
|
|
|
|
_omega_I_sum += error[besti] * _ki * _ra_deltat;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
_omega_I_sum_time += _ra_deltat;
|
2012-06-27 22:09:22 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_omega_I_sum_time >= 5) {
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// limit the rate of change of omega_I to the hardware
|
|
|
|
|
// reported maximum gyro drift rate. This ensures that
|
|
|
|
|
// short term errors don't cause a buildup of omega_I
|
|
|
|
|
// beyond the physical limits of the device
|
|
|
|
|
float change_limit = _gyro_drift_limit * _omega_I_sum_time;
|
2013-05-01 21:25:40 -03:00
|
|
|
|
_omega_I_sum.x = constrain_float(_omega_I_sum.x, -change_limit, change_limit);
|
|
|
|
|
_omega_I_sum.y = constrain_float(_omega_I_sum.y, -change_limit, change_limit);
|
|
|
|
|
_omega_I_sum.z = constrain_float(_omega_I_sum.z, -change_limit, change_limit);
|
2012-08-21 23:19:51 -03:00
|
|
|
|
_omega_I += _omega_I_sum;
|
|
|
|
|
_omega_I_sum.zero();
|
|
|
|
|
_omega_I_sum_time = 0;
|
2012-06-27 22:09:22 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// zero our accumulator ready for the next GPS step
|
2014-02-26 18:41:28 -04:00
|
|
|
|
memset(&_ra_sum[0], 0, sizeof(_ra_sum));
|
2012-06-27 22:09:22 -03:00
|
|
|
|
_ra_deltat = 0;
|
|
|
|
|
_ra_sum_start = last_correction_time;
|
|
|
|
|
|
|
|
|
|
// remember the velocity for next time
|
|
|
|
|
_last_velocity = velocity;
|
2012-03-23 02:22:56 -03:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-24 15:37:10 -03:00
|
|
|
|
|
2012-08-11 02:55:53 -03:00
|
|
|
|
// update our wind speed estimate
|
2014-02-08 02:31:05 -04:00
|
|
|
|
void AP_AHRS_DCM::estimate_wind(void)
|
2012-08-11 02:55:53 -03:00
|
|
|
|
{
|
2013-05-23 22:21:23 -03:00
|
|
|
|
if (!_flags.wind_estimation) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-02-08 02:31:05 -04:00
|
|
|
|
const Vector3f &velocity = _last_velocity;
|
2013-05-23 22:21:23 -03:00
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// this is based on the wind speed estimation code from MatrixPilot by
|
|
|
|
|
// Bill Premerlani. Adaption for ArduPilot by Jon Challinger
|
|
|
|
|
// See http://gentlenav.googlecode.com/files/WindEstimation.pdf
|
|
|
|
|
Vector3f fuselageDirection = _dcm_matrix.colx();
|
|
|
|
|
Vector3f fuselageDirectionDiff = fuselageDirection - _last_fuse;
|
2012-11-14 12:10:15 -04:00
|
|
|
|
uint32_t now = hal.scheduler->millis();
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
|
|
|
|
// scrap our data and start over if we're taking too long to get a direction change
|
|
|
|
|
if (now - _last_wind_time > 10000) {
|
|
|
|
|
_last_wind_time = now;
|
|
|
|
|
_last_fuse = fuselageDirection;
|
|
|
|
|
_last_vel = velocity;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float diff_length = fuselageDirectionDiff.length();
|
2013-01-10 14:42:24 -04:00
|
|
|
|
if (diff_length > 0.2f) {
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// when turning, use the attitude response to estimate
|
|
|
|
|
// wind speed
|
|
|
|
|
float V;
|
|
|
|
|
Vector3f velocityDiff = velocity - _last_vel;
|
|
|
|
|
|
|
|
|
|
// estimate airspeed it using equation 6
|
|
|
|
|
V = velocityDiff.length() / diff_length;
|
|
|
|
|
|
|
|
|
|
_last_fuse = fuselageDirection;
|
|
|
|
|
_last_vel = velocity;
|
|
|
|
|
|
|
|
|
|
Vector3f fuselageDirectionSum = fuselageDirection + _last_fuse;
|
|
|
|
|
Vector3f velocitySum = velocity + _last_vel;
|
|
|
|
|
|
2013-01-10 14:42:24 -04:00
|
|
|
|
float theta = atan2f(velocityDiff.y, velocityDiff.x) - atan2f(fuselageDirectionDiff.y, fuselageDirectionDiff.x);
|
|
|
|
|
float sintheta = sinf(theta);
|
|
|
|
|
float costheta = cosf(theta);
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
|
|
|
|
Vector3f wind = Vector3f();
|
|
|
|
|
wind.x = velocitySum.x - V * (costheta * fuselageDirectionSum.x - sintheta * fuselageDirectionSum.y);
|
|
|
|
|
wind.y = velocitySum.y - V * (sintheta * fuselageDirectionSum.x + costheta * fuselageDirectionSum.y);
|
|
|
|
|
wind.z = velocitySum.z - V * fuselageDirectionSum.z;
|
2013-01-10 14:42:24 -04:00
|
|
|
|
wind *= 0.5f;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
2013-04-22 19:57:33 -03:00
|
|
|
|
if (wind.length() < _wind.length() + 20) {
|
|
|
|
|
_wind = _wind * 0.95f + wind * 0.05f;
|
|
|
|
|
}
|
2012-08-21 23:19:51 -03:00
|
|
|
|
|
|
|
|
|
_last_wind_time = now;
|
|
|
|
|
} else if (now - _last_wind_time > 2000 && _airspeed && _airspeed->use()) {
|
|
|
|
|
// when flying straight use airspeed to get wind estimate if available
|
|
|
|
|
Vector3f airspeed = _dcm_matrix.colx() * _airspeed->get_airspeed();
|
2013-07-19 22:11:57 -03:00
|
|
|
|
Vector3f wind = velocity - (airspeed * get_EAS2TAS());
|
2013-01-10 14:42:24 -04:00
|
|
|
|
_wind = _wind * 0.92f + wind * 0.08f;
|
2012-09-07 22:27:12 -03:00
|
|
|
|
}
|
2012-08-11 02:55:53 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-17 04:19:42 -04:00
|
|
|
|
// calculate the euler angles and DCM matrix which will be used for high level
|
|
|
|
|
// navigation control. Apply trim such that a positive trim value results in a
|
|
|
|
|
// positive vehicle rotation about that axis (ie a negative offset)
|
2011-06-12 20:49:01 -03:00
|
|
|
|
void
|
2012-03-11 04:59:53 -03:00
|
|
|
|
AP_AHRS_DCM::euler_angles(void)
|
2010-10-24 15:37:10 -03:00
|
|
|
|
{
|
2014-01-17 04:19:42 -04:00
|
|
|
|
_body_dcm_matrix = _dcm_matrix;
|
|
|
|
|
_body_dcm_matrix.rotateXYinv(_trim);
|
|
|
|
|
_body_dcm_matrix.to_euler(&roll, &pitch, &yaw);
|
2011-06-12 20:49:01 -03:00
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
|
roll_sensor = degrees(roll) * 100;
|
|
|
|
|
pitch_sensor = degrees(pitch) * 100;
|
|
|
|
|
yaw_sensor = degrees(yaw) * 100;
|
2010-10-24 15:37:10 -03:00
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
|
if (yaw_sensor < 0)
|
|
|
|
|
yaw_sensor += 36000;
|
2010-10-24 15:37:10 -03:00
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 00:22:39 -04:00
|
|
|
|
/* reporting of DCM state for MAVLink */
|
|
|
|
|
|
|
|
|
|
// average error_roll_pitch since last call
|
2012-03-11 04:59:53 -03:00
|
|
|
|
float AP_AHRS_DCM::get_error_rp(void)
|
2012-03-01 00:22:39 -04:00
|
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
|
if (_error_rp_count == 0) {
|
|
|
|
|
// this happens when telemetry is setup on two
|
|
|
|
|
// serial ports
|
|
|
|
|
return _error_rp_last;
|
|
|
|
|
}
|
|
|
|
|
_error_rp_last = _error_rp_sum / _error_rp_count;
|
|
|
|
|
_error_rp_sum = 0;
|
|
|
|
|
_error_rp_count = 0;
|
|
|
|
|
return _error_rp_last;
|
2012-03-01 00:22:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// average error_yaw since last call
|
2012-03-11 04:59:53 -03:00
|
|
|
|
float AP_AHRS_DCM::get_error_yaw(void)
|
2012-03-01 00:22:39 -04:00
|
|
|
|
{
|
2012-08-21 23:19:51 -03:00
|
|
|
|
if (_error_yaw_count == 0) {
|
|
|
|
|
// this happens when telemetry is setup on two
|
|
|
|
|
// serial ports
|
|
|
|
|
return _error_yaw_last;
|
|
|
|
|
}
|
|
|
|
|
_error_yaw_last = _error_yaw_sum / _error_yaw_count;
|
|
|
|
|
_error_yaw_sum = 0;
|
|
|
|
|
_error_yaw_count = 0;
|
|
|
|
|
return _error_yaw_last;
|
2012-03-01 00:22:39 -04:00
|
|
|
|
}
|
2012-08-10 23:00:31 -03:00
|
|
|
|
|
2012-08-21 23:19:51 -03:00
|
|
|
|
// return our current position estimate using
|
2012-08-10 23:00:31 -03:00
|
|
|
|
// dead-reckoning or GPS
|
2013-08-04 21:16:31 -03:00
|
|
|
|
bool AP_AHRS_DCM::get_position(struct Location &loc)
|
2012-08-10 23:00:31 -03:00
|
|
|
|
{
|
2013-08-04 21:16:31 -03:00
|
|
|
|
loc.lat = _last_lat;
|
|
|
|
|
loc.lng = _last_lng;
|
2014-01-02 07:06:10 -04:00
|
|
|
|
loc.alt = _baro.get_altitude() * 100 + _home.alt;
|
2014-07-24 22:40:16 -03:00
|
|
|
|
loc.flags.relative_alt = 0;
|
|
|
|
|
loc.flags.terrain_alt = 0;
|
2012-08-21 23:19:51 -03:00
|
|
|
|
location_offset(loc, _position_offset_north, _position_offset_east);
|
2014-02-14 18:23:41 -04:00
|
|
|
|
if (_flags.fly_forward && _have_position) {
|
2014-08-06 16:16:21 -03:00
|
|
|
|
location_update(loc, _gps.ground_course_cd() * 0.01f, _gps.ground_speed() * _gps.get_lag());
|
2014-01-02 07:06:10 -04:00
|
|
|
|
}
|
2014-02-14 18:23:41 -04:00
|
|
|
|
return _have_position;
|
2012-08-10 23:00:31 -03:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-24 08:22:58 -03:00
|
|
|
|
// return an airspeed estimate if available
|
2014-02-24 21:43:59 -04:00
|
|
|
|
bool AP_AHRS_DCM::airspeed_estimate(float *airspeed_ret) const
|
2012-08-24 08:22:58 -03:00
|
|
|
|
{
|
2012-09-07 22:27:12 -03:00
|
|
|
|
bool ret = false;
|
2012-08-24 08:22:58 -03:00
|
|
|
|
if (_airspeed && _airspeed->use()) {
|
|
|
|
|
*airspeed_ret = _airspeed->get_airspeed();
|
2013-04-22 19:57:16 -03:00
|
|
|
|
return true;
|
2012-08-24 08:22:58 -03:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-23 22:21:23 -03:00
|
|
|
|
if (!_flags.wind_estimation) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-24 08:22:58 -03:00
|
|
|
|
// estimate it via GPS speed and wind
|
|
|
|
|
if (have_gps()) {
|
|
|
|
|
*airspeed_ret = _last_airspeed;
|
2012-09-07 22:27:12 -03:00
|
|
|
|
ret = true;
|
2012-08-24 08:22:58 -03:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-30 08:00:25 -03:00
|
|
|
|
if (ret && _wind_max > 0 && _gps.status() >= AP_GPS::GPS_OK_FIX_2D) {
|
2012-09-07 22:27:12 -03:00
|
|
|
|
// constrain the airspeed by the ground speed
|
|
|
|
|
// and AHRS_WIND_MAX
|
2014-03-30 08:00:25 -03:00
|
|
|
|
float gnd_speed = _gps.ground_speed();
|
2013-07-19 22:11:57 -03:00
|
|
|
|
float true_airspeed = *airspeed_ret * get_EAS2TAS();
|
|
|
|
|
true_airspeed = constrain_float(true_airspeed,
|
2013-07-10 01:02:14 -03:00
|
|
|
|
gnd_speed - _wind_max,
|
|
|
|
|
gnd_speed + _wind_max);
|
2013-07-19 22:11:57 -03:00
|
|
|
|
*airspeed_ret = true_airspeed / get_EAS2TAS();
|
2012-09-07 22:27:12 -03:00
|
|
|
|
}
|
|
|
|
|
return ret;
|
2012-08-24 08:22:58 -03:00
|
|
|
|
}
|
2014-01-02 07:06:10 -04:00
|
|
|
|
|
2014-03-30 08:00:25 -03:00
|
|
|
|
void AP_AHRS_DCM::set_home(const Location &loc)
|
2014-01-02 07:06:10 -04:00
|
|
|
|
{
|
2014-03-30 08:00:25 -03:00
|
|
|
|
_home = loc;
|
|
|
|
|
_home.options = 0;
|
2014-01-02 07:06:10 -04:00
|
|
|
|
}
|
2014-01-03 20:15:34 -04:00
|
|
|
|
|
2014-05-15 04:09:18 -03:00
|
|
|
|
/*
|
|
|
|
|
check if the AHRS subsystem is healthy
|
|
|
|
|
*/
|
|
|
|
|
bool AP_AHRS_DCM::healthy(void)
|
|
|
|
|
{
|
|
|
|
|
// consider ourselves healthy if there have been no failures for 5 seconds
|
|
|
|
|
return (_last_failure_ms == 0 || hal.scheduler->millis() - _last_failure_ms > 5000);
|
|
|
|
|
}
|