2011-12-28 05:31:36 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2011-02-14 00:25:20 -04:00
|
|
|
#include "Compass.h"
|
|
|
|
|
2012-02-11 07:53:30 -04:00
|
|
|
const AP_Param::GroupInfo Compass::var_info[] PROGMEM = {
|
2012-03-09 22:45:35 -04:00
|
|
|
// index 0 was used for the old orientation matrix
|
2012-02-12 18:55:13 -04:00
|
|
|
AP_GROUPINFO("OFS", 1, Compass, _offset),
|
|
|
|
AP_GROUPINFO("DEC", 2, Compass, _declination),
|
2012-02-24 23:11:07 -04:00
|
|
|
AP_GROUPINFO("LEARN", 3, Compass, _learn), // true if learning calibration
|
|
|
|
AP_GROUPINFO("USE", 4, Compass, _use_for_yaw), // true if used for DCM yaw
|
2012-03-30 00:18:06 -03:00
|
|
|
AP_GROUPINFO("AUTODEC",5, Compass, _auto_declination),
|
2012-02-12 03:23:19 -04:00
|
|
|
AP_GROUPEND
|
2012-02-11 07:53:30 -04:00
|
|
|
};
|
|
|
|
|
2011-02-14 00:25:20 -04:00
|
|
|
// Default constructor.
|
|
|
|
// Note that the Vector/Matrix constructors already implicitly zero
|
|
|
|
// their values.
|
|
|
|
//
|
2012-02-11 07:53:30 -04:00
|
|
|
Compass::Compass(void) :
|
2012-02-29 09:57:35 -04:00
|
|
|
product_id(AP_COMPASS_TYPE_UNKNOWN),
|
2012-02-11 07:53:30 -04:00
|
|
|
_declination (0.0),
|
2012-02-25 02:34:33 -04:00
|
|
|
_learn(1),
|
|
|
|
_use_for_yaw(1),
|
2012-02-29 09:45:49 -04:00
|
|
|
_null_enable(false),
|
2012-03-10 05:03:48 -04:00
|
|
|
_null_init_done(false),
|
2012-03-30 00:18:06 -03:00
|
|
|
_auto_declination(1),
|
2012-03-10 05:03:48 -04:00
|
|
|
_orientation(ROTATION_NONE)
|
2011-02-14 00:25:20 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default init method, just returns success.
|
|
|
|
//
|
|
|
|
bool
|
|
|
|
Compass::init()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-09 22:45:35 -04:00
|
|
|
Compass::set_orientation(enum Rotation rotation)
|
2011-02-14 00:25:20 -04:00
|
|
|
{
|
2012-03-10 05:03:48 -04:00
|
|
|
_orientation = rotation;
|
2011-02-14 00:25:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Compass::set_offsets(const Vector3f &offsets)
|
|
|
|
{
|
|
|
|
_offset.set(offsets);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Compass::save_offsets()
|
|
|
|
{
|
|
|
|
_offset.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3f &
|
|
|
|
Compass::get_offsets()
|
|
|
|
{
|
2012-02-11 07:53:30 -04:00
|
|
|
return _offset;
|
2011-02-14 00:25:20 -04:00
|
|
|
}
|
|
|
|
|
2012-03-30 00:18:06 -03:00
|
|
|
void
|
|
|
|
Compass::set_initial_location(long latitude, long longitude)
|
2012-03-10 19:19:04 -04:00
|
|
|
{
|
2012-03-30 00:18:06 -03:00
|
|
|
// if automatic declination is configured, then compute
|
|
|
|
// the declination based on the initial GPS fix
|
|
|
|
if (_auto_declination) {
|
2012-03-10 19:19:04 -04:00
|
|
|
// Set the declination based on the lat/lng from GPS
|
|
|
|
null_offsets_disable();
|
2012-03-30 00:18:06 -03:00
|
|
|
_declination.set(radians(AP_Declination::get_declination((float)latitude / 10000000, (float)longitude / 10000000)));
|
2012-03-10 19:19:04 -04:00
|
|
|
null_offsets_enable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-14 00:25:20 -04:00
|
|
|
void
|
|
|
|
Compass::set_declination(float radians)
|
|
|
|
{
|
|
|
|
_declination.set_and_save(radians);
|
|
|
|
}
|
|
|
|
|
2011-02-18 23:57:53 -04:00
|
|
|
float
|
|
|
|
Compass::get_declination()
|
|
|
|
{
|
|
|
|
return _declination.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-14 00:25:20 -04:00
|
|
|
void
|
|
|
|
Compass::calculate(float roll, float pitch)
|
|
|
|
{
|
2011-05-08 15:15:29 -03:00
|
|
|
// Note - This function implementation is deprecated
|
|
|
|
// The alternate implementation of this function using the dcm matrix is preferred
|
2011-02-14 00:25:20 -04:00
|
|
|
float headX;
|
|
|
|
float headY;
|
|
|
|
float cos_roll;
|
|
|
|
float sin_roll;
|
|
|
|
float cos_pitch;
|
|
|
|
float sin_pitch;
|
2011-07-08 00:56:04 -03:00
|
|
|
cos_roll = cos(roll);
|
2011-05-08 15:15:29 -03:00
|
|
|
sin_roll = sin(roll);
|
2011-02-14 00:25:20 -04:00
|
|
|
cos_pitch = cos(pitch);
|
2011-05-08 15:15:29 -03:00
|
|
|
sin_pitch = sin(pitch);
|
|
|
|
|
|
|
|
// Tilt compensated magnetic field X component:
|
|
|
|
headX = mag_x*cos_pitch + mag_y*sin_roll*sin_pitch + mag_z*cos_roll*sin_pitch;
|
|
|
|
// Tilt compensated magnetic field Y component:
|
|
|
|
headY = mag_y*cos_roll - mag_z*sin_roll;
|
|
|
|
// magnetic heading
|
|
|
|
heading = atan2(-headY,headX);
|
|
|
|
|
|
|
|
// Declination correction (if supplied)
|
|
|
|
if( fabs(_declination) > 0.0 )
|
|
|
|
{
|
|
|
|
heading = heading + _declination;
|
|
|
|
if (heading > M_PI) // Angle normalization (-180 deg, 180 deg)
|
|
|
|
heading -= (2.0 * M_PI);
|
|
|
|
else if (heading < -M_PI)
|
|
|
|
heading += (2.0 * M_PI);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimization for external DCM use. Calculate normalized components
|
|
|
|
heading_x = cos(heading);
|
|
|
|
heading_y = sin(heading);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Compass::calculate(const Matrix3f &dcm_matrix)
|
|
|
|
{
|
|
|
|
float headX;
|
|
|
|
float headY;
|
2012-02-23 19:42:03 -04:00
|
|
|
float cos_pitch = safe_sqrt(1-(dcm_matrix.c.x*dcm_matrix.c.x));
|
2011-05-08 15:15:29 -03:00
|
|
|
// sin(pitch) = - dcm_matrix(3,1)
|
|
|
|
// cos(pitch)*sin(roll) = - dcm_matrix(3,2)
|
|
|
|
// cos(pitch)*cos(roll) = - dcm_matrix(3,3)
|
2011-02-14 00:25:20 -04:00
|
|
|
|
2012-02-23 19:42:03 -04:00
|
|
|
if (cos_pitch == 0.0) {
|
|
|
|
// we are pointing straight up or down so don't update our
|
|
|
|
// heading using the compass. Wait for the next iteration when
|
|
|
|
// we hopefully will have valid values again.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-14 00:25:20 -04:00
|
|
|
// Tilt compensated magnetic field X component:
|
2011-05-08 15:15:29 -03:00
|
|
|
headX = mag_x*cos_pitch - mag_y*dcm_matrix.c.y*dcm_matrix.c.x/cos_pitch - mag_z*dcm_matrix.c.z*dcm_matrix.c.x/cos_pitch;
|
2011-02-14 00:25:20 -04:00
|
|
|
// Tilt compensated magnetic field Y component:
|
2011-05-08 15:15:29 -03:00
|
|
|
headY = mag_y*dcm_matrix.c.z/cos_pitch - mag_z*dcm_matrix.c.y/cos_pitch;
|
2011-02-14 00:25:20 -04:00
|
|
|
// magnetic heading
|
2011-07-08 00:56:04 -03:00
|
|
|
// 6/4/11 - added constrain to keep bad values from ruining DCM Yaw - Jason S.
|
|
|
|
heading = constrain(atan2(-headY,headX), -3.15, 3.15);
|
2011-02-14 00:25:20 -04:00
|
|
|
|
|
|
|
// Declination correction (if supplied)
|
|
|
|
if( fabs(_declination) > 0.0 )
|
|
|
|
{
|
|
|
|
heading = heading + _declination;
|
|
|
|
if (heading > M_PI) // Angle normalization (-180 deg, 180 deg)
|
|
|
|
heading -= (2.0 * M_PI);
|
|
|
|
else if (heading < -M_PI)
|
|
|
|
heading += (2.0 * M_PI);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimization for external DCM use. Calculate normalized components
|
|
|
|
heading_x = cos(heading);
|
|
|
|
heading_y = sin(heading);
|
2012-02-23 19:42:03 -04:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (isnan(heading_x) || isnan(heading_y)) {
|
|
|
|
Serial.printf("COMPASS: c.x %f c.y %f c.z %f cos_pitch %f mag_x %d mag_y %d mag_z %d headX %f headY %f heading %f heading_x %f heading_y %f\n",
|
|
|
|
dcm_matrix.c.x,
|
|
|
|
dcm_matrix.c.y,
|
|
|
|
dcm_matrix.c.x,
|
|
|
|
cos_pitch,
|
|
|
|
(int)mag_x, (int)mag_y, (int)mag_z,
|
|
|
|
headX, headY,
|
|
|
|
heading,
|
|
|
|
heading_x, heading_y);
|
|
|
|
}
|
|
|
|
#endif
|
2011-02-14 00:25:20 -04:00
|
|
|
}
|
|
|
|
|
2012-03-27 01:16:00 -03:00
|
|
|
|
|
|
|
/*
|
2012-03-28 06:46:11 -03:00
|
|
|
this offset nulling algorithm is inspired by this paper from Bill Premerlani
|
2012-03-27 01:16:00 -03:00
|
|
|
|
|
|
|
http://gentlenav.googlecode.com/files/MagnetometerOffsetNullingRevisited.pdf
|
2012-03-28 06:46:11 -03:00
|
|
|
|
|
|
|
The base algorithm works well, but is quite sensitive to
|
|
|
|
noise. After long discussions with Bill, the following changes were
|
|
|
|
made:
|
|
|
|
|
|
|
|
1) we keep a history buffer that effectively divides the mag
|
|
|
|
vectors into a set of N streams. The algorithm is run on the
|
|
|
|
streams separately
|
|
|
|
|
|
|
|
2) within each stream we only calculate a change when the mag
|
|
|
|
vector has changed by a significant amount.
|
|
|
|
|
|
|
|
This gives us the property that we learn quickly if there is no
|
|
|
|
noise, but still learn correctly (and slowly) in the face of lots of
|
|
|
|
noise.
|
2012-03-27 01:16:00 -03:00
|
|
|
*/
|
2011-02-14 00:25:20 -04:00
|
|
|
void
|
2012-03-27 01:16:00 -03:00
|
|
|
Compass::null_offsets(void)
|
2011-02-14 00:25:20 -04:00
|
|
|
{
|
2012-02-24 23:11:07 -04:00
|
|
|
if (_null_enable == false || _learn == 0) {
|
|
|
|
// auto-calibration is disabled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-27 01:16:00 -03:00
|
|
|
// this gain is set so we converge on the offsets in about 5
|
|
|
|
// minutes with a 10Hz compass
|
2012-03-28 06:46:11 -03:00
|
|
|
const float gain = 0.01;
|
|
|
|
const float max_change = 10.0;
|
|
|
|
const float min_diff = 50.0;
|
|
|
|
Vector3f ofs;
|
|
|
|
|
|
|
|
ofs = _offset.get();
|
2012-03-27 01:16:00 -03:00
|
|
|
|
|
|
|
if (!_null_init_done) {
|
|
|
|
// first time through
|
2011-02-14 00:25:20 -04:00
|
|
|
_null_init_done = true;
|
2012-03-28 06:46:11 -03:00
|
|
|
for (uint8_t i=0; i<_mag_history_size; i++) {
|
|
|
|
// fill the history buffer with the current mag vector,
|
|
|
|
// with the offset removed
|
|
|
|
_mag_history[i] = Vector3i((mag_x+0.5) - ofs.x, (mag_y+0.5) - ofs.y, (mag_z+0.5) - ofs.z);
|
|
|
|
}
|
|
|
|
_mag_history_index = 0;
|
2012-03-27 01:16:00 -03:00
|
|
|
return;
|
2011-02-14 00:25:20 -04:00
|
|
|
}
|
2012-03-27 01:16:00 -03:00
|
|
|
|
2012-03-28 06:46:11 -03:00
|
|
|
Vector3f b1, b2, diff;
|
|
|
|
float length;
|
|
|
|
|
|
|
|
// get a past element
|
|
|
|
b1 = Vector3f(_mag_history[_mag_history_index].x,
|
|
|
|
_mag_history[_mag_history_index].y,
|
|
|
|
_mag_history[_mag_history_index].z);
|
|
|
|
// the history buffer doesn't have the offsets
|
|
|
|
b1 += ofs;
|
|
|
|
|
|
|
|
// get the current vector
|
|
|
|
b2 = Vector3f(mag_x, mag_y, mag_z);
|
|
|
|
|
|
|
|
// calculate the delta for this sample
|
|
|
|
diff = b2 - b1;
|
|
|
|
length = diff.length();
|
|
|
|
if (length < min_diff) {
|
|
|
|
// the mag vector hasn't changed enough - we don't get
|
2012-03-28 08:40:32 -03:00
|
|
|
// enough information from this vector to use it.
|
|
|
|
// Note that we don't put the current vector into the mag
|
|
|
|
// history here. We want to wait for a larger rotation to
|
|
|
|
// build up before calculating an offset change, as accuracy
|
|
|
|
// of the offset change is highly dependent on the size of the
|
|
|
|
// rotation.
|
2012-03-28 06:46:11 -03:00
|
|
|
_mag_history_index = (_mag_history_index + 1) % _mag_history_size;
|
2012-03-27 01:16:00 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-28 06:46:11 -03:00
|
|
|
// put the vector in the history
|
|
|
|
_mag_history[_mag_history_index] = Vector3i((mag_x+0.5) - ofs.x, (mag_y+0.5) - ofs.y, (mag_z+0.5) - ofs.z);
|
|
|
|
_mag_history_index = (_mag_history_index + 1) % _mag_history_size;
|
|
|
|
|
2012-03-27 01:16:00 -03:00
|
|
|
// equation 6 of Bills paper
|
2012-03-28 06:46:11 -03:00
|
|
|
diff = diff * (gain * (b2.length() - b1.length()) / length);
|
2012-03-27 01:16:00 -03:00
|
|
|
|
|
|
|
// limit the change from any one reading. This is to prevent
|
|
|
|
// single crazy readings from throwing off the offsets for a long
|
|
|
|
// time
|
2012-03-28 06:46:11 -03:00
|
|
|
length = diff.length();
|
|
|
|
if (length > max_change) {
|
|
|
|
diff *= max_change / length;
|
2012-03-27 01:16:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the new offsets
|
2012-03-28 06:46:11 -03:00
|
|
|
_offset.set(_offset.get() - diff);
|
2011-02-14 00:25:20 -04:00
|
|
|
}
|
2012-01-12 17:43:39 -04:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Compass::null_offsets_enable(void)
|
|
|
|
{
|
|
|
|
_null_init_done = false;
|
|
|
|
_null_enable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Compass::null_offsets_disable(void)
|
|
|
|
{
|
|
|
|
_null_init_done = false;
|
|
|
|
_null_enable = false;
|
2012-02-11 07:53:30 -04:00
|
|
|
}
|