2012-03-03 00:49:38 -04:00
|
|
|
#ifndef AP_Quaternion_h
|
|
|
|
#define AP_Quaternion_h
|
|
|
|
|
|
|
|
#include <AP_Math.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <AP_Compass.h>
|
|
|
|
#include <AP_GPS.h>
|
|
|
|
#include <AP_IMU.h>
|
|
|
|
|
|
|
|
#if defined(ARDUINO) && ARDUINO >= 100
|
|
|
|
#include "Arduino.h"
|
|
|
|
#else
|
|
|
|
#include "WProgram.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class AP_Quaternion
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Constructor
|
2012-03-07 00:16:21 -04:00
|
|
|
AP_Quaternion(IMU *imu, GPS *&gps) :
|
2012-03-03 00:49:38 -04:00
|
|
|
_imu(imu),
|
2012-03-07 00:16:21 -04:00
|
|
|
_gps(gps)
|
2012-03-03 00:49:38 -04:00
|
|
|
{
|
|
|
|
// reference direction of flux in earth frame
|
|
|
|
b_x = 0;
|
|
|
|
b_z = -1;
|
|
|
|
|
2012-03-08 18:02:46 -04:00
|
|
|
// limit the drift to the drift rate reported by the
|
|
|
|
// sensor driver
|
|
|
|
gyroMeasDrift = imu->get_gyro_drift_rate();
|
|
|
|
|
2012-03-03 00:49:38 -04:00
|
|
|
// scaled gyro drift limits
|
|
|
|
beta = sqrt(3.0f / 4.0f) * gyroMeasError;
|
|
|
|
zeta = sqrt(3.0f / 4.0f) * gyroMeasDrift;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accessors
|
|
|
|
void set_centripetal(bool b) {_centripetal = b;}
|
|
|
|
bool get_centripetal(void) {return _centripetal;}
|
|
|
|
void set_compass(Compass *compass);
|
|
|
|
|
|
|
|
// Methods
|
2012-03-03 01:00:57 -04:00
|
|
|
void update(void);
|
2012-03-03 00:49:38 -04:00
|
|
|
|
|
|
|
// Euler angles (radians)
|
|
|
|
float roll;
|
|
|
|
float pitch;
|
|
|
|
float yaw;
|
|
|
|
|
|
|
|
// integer Euler angles (Degrees * 100)
|
|
|
|
int32_t roll_sensor;
|
|
|
|
int32_t pitch_sensor;
|
|
|
|
int32_t yaw_sensor;
|
|
|
|
|
|
|
|
|
2012-03-03 03:32:50 -04:00
|
|
|
// compatibility methods with DCM
|
|
|
|
void update_DCM(void) { update(); }
|
|
|
|
void update_DCM_fast(void) { update(); }
|
2012-03-04 20:27:45 -04:00
|
|
|
Vector3f get_gyro(void) {
|
|
|
|
// notice the sign reversals here
|
|
|
|
return Vector3f(-_gyro_corrected.x, -_gyro_corrected.y, _gyro_corrected.z);
|
|
|
|
}
|
2012-03-08 03:14:40 -04:00
|
|
|
Vector3f get_gyro_drift(void) {
|
2012-03-04 20:27:45 -04:00
|
|
|
// notice the sign reversals here
|
|
|
|
return Vector3f(-gyro_bias.x, -gyro_bias.y, gyro_bias.z);
|
|
|
|
}
|
2012-03-03 03:32:50 -04:00
|
|
|
float get_accel_weight(void) { return 0; }
|
|
|
|
float get_renorm_val(void) { return 0; }
|
|
|
|
float get_health(void) { return 0; }
|
|
|
|
void matrix_reset(void) { }
|
|
|
|
uint8_t gyro_sat_count;
|
|
|
|
uint8_t renorm_range_count;
|
|
|
|
uint8_t renorm_blowup_count;
|
2012-03-03 08:32:31 -04:00
|
|
|
float get_error_rp(void);
|
|
|
|
float get_error_yaw(void);
|
2012-03-07 00:16:21 -04:00
|
|
|
Matrix3f get_dcm_matrix(void) {
|
|
|
|
Matrix3f ret;
|
|
|
|
quaternion_to_rotation_matrix(q, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-03-03 03:32:50 -04:00
|
|
|
|
2012-03-03 00:49:38 -04:00
|
|
|
private:
|
|
|
|
void update_IMU(float deltat, Vector3f &gyro, Vector3f &accel);
|
|
|
|
void update_MARG(float deltat, Vector3f &gyro, Vector3f &accel, Vector3f &mag);
|
2012-03-08 18:02:04 -04:00
|
|
|
void update_drift(float deltat, Vector3f &mag);
|
2012-03-03 00:49:38 -04:00
|
|
|
|
|
|
|
bool _have_initial_yaw;
|
|
|
|
|
|
|
|
// Methods
|
|
|
|
void accel_adjust(void);
|
|
|
|
|
|
|
|
// members
|
|
|
|
Compass * _compass;
|
2012-03-07 00:16:21 -04:00
|
|
|
// time in microseconds of last compass update
|
|
|
|
uint32_t _compass_last_update;
|
2012-03-03 00:49:38 -04:00
|
|
|
|
|
|
|
// note: we use ref-to-pointer here so that our caller can change the GPS without our noticing
|
|
|
|
// IMU under us without our noticing.
|
|
|
|
GPS *&_gps;
|
|
|
|
IMU *_imu;
|
|
|
|
|
|
|
|
// true if we are doing centripetal acceleration correction
|
|
|
|
bool _centripetal;
|
|
|
|
|
2012-03-07 03:31:35 -04:00
|
|
|
// maximum gyroscope measurement error in rad/s (set to 7 degrees/second)
|
2012-03-08 18:02:04 -04:00
|
|
|
static const float gyroMeasError = 20.0 * (M_PI/180.0);
|
2012-03-03 00:49:38 -04:00
|
|
|
|
2012-03-08 18:02:04 -04:00
|
|
|
float gyroMeasDrift;
|
2012-03-03 00:49:38 -04:00
|
|
|
|
|
|
|
float beta;
|
|
|
|
float zeta;
|
|
|
|
|
|
|
|
// quaternion elements
|
2012-03-05 00:07:04 -04:00
|
|
|
Quaternion q;
|
2012-03-03 00:49:38 -04:00
|
|
|
|
2012-03-05 00:07:04 -04:00
|
|
|
// magnetic flux estimates. These are used for the automatic
|
|
|
|
// magnetometer calibration
|
2012-03-03 00:49:38 -04:00
|
|
|
float b_x;
|
|
|
|
float b_z;
|
|
|
|
|
|
|
|
// estimate gyroscope biases error
|
|
|
|
Vector3f gyro_bias;
|
2012-03-03 03:32:50 -04:00
|
|
|
|
2012-03-04 20:27:45 -04:00
|
|
|
// the current corrected gyro vector
|
|
|
|
Vector3f _gyro_corrected;
|
2012-03-03 08:32:31 -04:00
|
|
|
|
2012-03-07 00:16:21 -04:00
|
|
|
// accel and gyro accumulators for drift correction
|
|
|
|
Vector3f _gyro_sum;
|
|
|
|
Vector3f _accel_sum;
|
|
|
|
uint32_t _sum_count;
|
|
|
|
|
2012-03-03 08:32:31 -04:00
|
|
|
// estimate of error
|
|
|
|
float _error_rp_sum;
|
|
|
|
uint16_t _error_rp_count;
|
|
|
|
float _error_yaw_sum;
|
|
|
|
uint16_t _error_yaw_count;
|
2012-03-03 00:49:38 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|