2013-05-02 08:31:17 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2013-08-29 02:34:34 -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/>.
|
|
|
|
*/
|
|
|
|
|
2011-02-14 00:27:07 -04:00
|
|
|
/*
|
2012-08-17 03:19:22 -03:00
|
|
|
* AP_Compass_HIL.cpp - Arduino Library for HIL model of HMC5843 I2C Magnetometer
|
|
|
|
* Code by James Goppert. DIYDrones.com
|
|
|
|
*
|
|
|
|
*/
|
2011-02-14 00:27:07 -04:00
|
|
|
|
|
|
|
|
2012-10-11 17:48:39 -03:00
|
|
|
#include <AP_HAL.h>
|
2011-02-14 00:27:07 -04:00
|
|
|
#include "AP_Compass_HIL.h"
|
|
|
|
|
2012-10-11 17:48:39 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2013-05-02 01:59:33 -03:00
|
|
|
// constructor
|
|
|
|
AP_Compass_HIL::AP_Compass_HIL() : Compass()
|
|
|
|
{
|
2013-05-02 08:31:17 -03:00
|
|
|
product_id = AP_COMPASS_TYPE_HIL;
|
|
|
|
_setup_earth_field();
|
2013-05-02 01:59:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// setup _Bearth
|
|
|
|
void AP_Compass_HIL::_setup_earth_field(void)
|
|
|
|
{
|
2013-05-02 08:31:17 -03:00
|
|
|
// assume a earth field strength of 400
|
|
|
|
_Bearth(400, 0, 0);
|
|
|
|
|
|
|
|
// rotate _Bearth for inclination and declination. -66 degrees
|
|
|
|
// is the inclination in Canberra, Australia
|
|
|
|
Matrix3f R;
|
|
|
|
R.from_euler(0, ToRad(66), _declination.get());
|
|
|
|
_Bearth = R * _Bearth;
|
2013-05-02 01:59:33 -03:00
|
|
|
}
|
|
|
|
|
2011-02-14 00:27:07 -04:00
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
|
|
|
|
2011-12-28 05:31:36 -04:00
|
|
|
bool AP_Compass_HIL::read()
|
2011-02-14 00:27:07 -04:00
|
|
|
{
|
2014-07-31 22:19:53 -03:00
|
|
|
_field[0] = _hil_mag;
|
|
|
|
|
|
|
|
apply_corrections(_field[0],0);
|
2013-03-01 10:58:55 -04:00
|
|
|
|
2012-08-17 03:19:22 -03:00
|
|
|
// values set by setHIL function
|
2012-10-11 17:48:39 -03:00
|
|
|
last_update = hal.scheduler->micros(); // record time of update
|
2012-08-17 03:19:22 -03:00
|
|
|
return true;
|
2011-02-14 00:27:07 -04:00
|
|
|
}
|
|
|
|
|
2013-05-02 01:59:33 -03:00
|
|
|
#define MAG_OFS_X 5.0
|
|
|
|
#define MAG_OFS_Y 13.0
|
|
|
|
#define MAG_OFS_Z -18.0
|
|
|
|
|
2011-02-14 00:27:07 -04:00
|
|
|
// Update raw magnetometer values from HIL data
|
|
|
|
//
|
2013-05-02 01:59:33 -03:00
|
|
|
void AP_Compass_HIL::setHIL(float roll, float pitch, float yaw)
|
2011-02-14 00:27:07 -04:00
|
|
|
{
|
2013-05-02 08:31:17 -03:00
|
|
|
Matrix3f R;
|
2013-05-02 01:59:33 -03:00
|
|
|
|
2013-05-02 08:31:17 -03:00
|
|
|
// create a rotation matrix for the given attitude
|
|
|
|
R.from_euler(roll, pitch, yaw);
|
2013-05-02 01:59:33 -03:00
|
|
|
|
2013-05-02 08:31:17 -03:00
|
|
|
if (_last_declination != _declination.get()) {
|
|
|
|
_setup_earth_field();
|
|
|
|
_last_declination = _declination.get();
|
|
|
|
}
|
2013-05-02 01:59:33 -03:00
|
|
|
|
2013-05-02 08:31:17 -03:00
|
|
|
// convert the earth frame magnetic vector to body frame, and
|
|
|
|
// apply the offsets
|
|
|
|
_hil_mag = R.mul_transpose(_Bearth);
|
|
|
|
_hil_mag -= Vector3f(MAG_OFS_X, MAG_OFS_Y, MAG_OFS_Z);
|
2013-05-01 23:27:35 -03:00
|
|
|
|
2013-05-02 08:31:17 -03:00
|
|
|
// apply default board orientation for this compass type. This is
|
|
|
|
// a noop on most boards
|
|
|
|
_hil_mag.rotate(MAG_BOARD_ORIENTATION);
|
2013-05-01 23:27:35 -03:00
|
|
|
|
2013-05-02 08:31:17 -03:00
|
|
|
// add user selectable orientation
|
2014-09-24 10:29:03 -03:00
|
|
|
_hil_mag.rotate((enum Rotation)_orientation[0].get());
|
2013-05-01 23:27:35 -03:00
|
|
|
|
2014-09-24 10:29:03 -03:00
|
|
|
if (!_external[0]) {
|
2013-08-30 01:02:09 -03:00
|
|
|
// and add in AHRS_ORIENTATION setting if not an external compass
|
|
|
|
_hil_mag.rotate(_board_orientation);
|
|
|
|
}
|
2013-05-01 23:27:35 -03:00
|
|
|
|
2013-12-09 02:46:41 -04:00
|
|
|
_healthy[0] = true;
|
2011-02-14 00:27:07 -04:00
|
|
|
}
|
2012-08-26 00:42:00 -03:00
|
|
|
|
2013-12-29 07:55:20 -04:00
|
|
|
// Update raw magnetometer values from HIL mag vector
|
|
|
|
//
|
2014-05-12 06:44:15 -03:00
|
|
|
void AP_Compass_HIL::setHIL(const Vector3f &mag)
|
2013-12-29 07:55:20 -04:00
|
|
|
{
|
|
|
|
_hil_mag.x = mag.x;
|
|
|
|
_hil_mag.y = mag.y;
|
|
|
|
_hil_mag.z = mag.z;
|
|
|
|
_healthy[0] = true;
|
|
|
|
}
|
|
|
|
|
2014-05-12 06:44:15 -03:00
|
|
|
const Vector3f& AP_Compass_HIL::getHIL() const {
|
|
|
|
return _hil_mag;
|
|
|
|
}
|
|
|
|
|
2012-08-26 00:42:00 -03:00
|
|
|
void AP_Compass_HIL::accumulate(void)
|
|
|
|
{
|
2013-05-02 08:31:17 -03:00
|
|
|
// nothing to do
|
2012-08-26 00:42:00 -03:00
|
|
|
}
|