2011-11-16 21:49:56 -04:00
|
|
|
/*
|
|
|
|
SITL handling
|
|
|
|
|
|
|
|
This simulates a compass
|
|
|
|
|
|
|
|
Andrew Tridgell November 2011
|
|
|
|
*/
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <AP_Math.h>
|
|
|
|
#include <AP_Compass.h>
|
|
|
|
#include "desktop.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2012-03-02 23:15:26 -04:00
|
|
|
#define MAG_OFS_X 5.0
|
|
|
|
#define MAG_OFS_Y 13.0
|
2012-02-13 20:15:27 -04:00
|
|
|
#define MAG_OFS_Z -18.0
|
|
|
|
|
2012-03-20 19:05:58 -03:00
|
|
|
// declination in Canberra (degrees)
|
|
|
|
#define MAG_DECLINATION 12.1
|
|
|
|
|
|
|
|
// inclination in Canberra (degrees)
|
|
|
|
#define MAG_INCLINATION -66
|
|
|
|
|
|
|
|
// magnetic field strength in Canberra as observed
|
|
|
|
// using an APM1 with 5883L compass
|
|
|
|
#define MAG_FIELD_STRENGTH 818
|
|
|
|
|
2012-03-28 06:47:33 -03:00
|
|
|
#define MAG_NOISE 5
|
|
|
|
|
2011-11-16 21:49:56 -04:00
|
|
|
/*
|
|
|
|
given a magnetic heading, and roll, pitch, yaw values,
|
|
|
|
calculate consistent magnetometer components
|
|
|
|
|
|
|
|
All angles are in radians
|
|
|
|
*/
|
2012-03-20 19:05:58 -03:00
|
|
|
static Vector3f heading_to_mag(float roll, float pitch, float yaw)
|
2011-11-16 21:49:56 -04:00
|
|
|
{
|
2012-03-20 19:05:58 -03:00
|
|
|
Vector3f Bearth, m;
|
|
|
|
Matrix3f R;
|
2011-11-16 21:49:56 -04:00
|
|
|
|
2012-03-20 19:05:58 -03:00
|
|
|
// Bearth is the magnetic field in Canberra. We need to adjust
|
|
|
|
// it for inclination and declination
|
|
|
|
Bearth(MAG_FIELD_STRENGTH, 0, 0);
|
|
|
|
R.from_euler(0, -ToRad(MAG_INCLINATION), ToRad(MAG_DECLINATION));
|
|
|
|
Bearth = R * Bearth;
|
2011-11-16 21:49:56 -04:00
|
|
|
|
2012-03-20 19:05:58 -03:00
|
|
|
// create a rotation matrix for the given attitude
|
|
|
|
R.from_euler(roll, pitch, yaw);
|
2011-11-16 21:49:56 -04:00
|
|
|
|
2012-03-20 19:05:58 -03:00
|
|
|
// convert the earth frame magnetic vector to body frame, and
|
|
|
|
// apply the offsets
|
|
|
|
m = R.transposed() * Bearth - Vector3f(MAG_OFS_X, MAG_OFS_Y, MAG_OFS_Z);
|
2012-03-28 06:47:33 -03:00
|
|
|
return m + (rand_vec3f() * MAG_NOISE);
|
2011-11-16 21:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
setup the compass with new input
|
|
|
|
all inputs are in degrees
|
|
|
|
*/
|
2012-03-20 19:05:58 -03:00
|
|
|
void sitl_update_compass(float roll, float pitch, float yaw)
|
2011-11-16 21:49:56 -04:00
|
|
|
{
|
|
|
|
extern AP_Compass_HIL compass;
|
2012-03-20 19:05:58 -03:00
|
|
|
Vector3f m = heading_to_mag(ToRad(roll),
|
2011-11-16 21:49:56 -04:00
|
|
|
ToRad(pitch),
|
|
|
|
ToRad(yaw));
|
2012-03-20 19:05:58 -03:00
|
|
|
compass.setHIL(m.x, m.y, m.z);
|
2011-11-16 21:49:56 -04:00
|
|
|
}
|