Tisham Dhar / Xadow_MPU9150AHRS

This program is designed to run on a set of Xadow M0 modules to create a Hotshoe IMU which outputs GPS and Orientation data to Nikon cameras, as well as triggering the camera at set intervals.

Dependencies:   MBed_Adafruit-GPS-Library SC16IS750 SDFileSystem SSD1308_128x64_I2C USBDevice mbed BMP085

Fork of MPU9150AHRS by Kris Winer

/media/uploads/whatnick/20151022_004759.jpg

Committer:
whatnick
Date:
Tue Dec 01 04:49:10 2015 +0000
Revision:
10:46b5acee9472
Parent:
0:39935bb3c1a1
Minor text changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
onehorse0:39935bb3c1a1 1#ifndef MPU9150_H
onehorse0:39935bb3c1a1 2#define MPU9150_H
onehorse0:39935bb3c1a1 3
onehorse0:39935bb3c1a1 4#include "mbed.h"
whatnick9:b7062c55d36c 5
whatnick9:b7062c55d36c 6//#define DEBUG
whatnick9:b7062c55d36c 7
whatnick9:b7062c55d36c 8#ifdef DEBUG
whatnick9:b7062c55d36c 9#include "USBSerial.h" // To use USB virtual serial, a driver is needed, check http://mbed.org/handbook/USBSerial
whatnick9:b7062c55d36c 10#define LOG(args...) pc.printf(args)
whatnick9:b7062c55d36c 11USBSerial pc;
whatnick9:b7062c55d36c 12#else
whatnick9:b7062c55d36c 13#define LOG(args...)
whatnick9:b7062c55d36c 14#endif
onehorse0:39935bb3c1a1 15
onehorse0:39935bb3c1a1 16// Define registers per MPU6050, Register Map and Descriptions, Rev 4.2, 08/19/2013 6 DOF Motion sensor fusion device
onehorse0:39935bb3c1a1 17// Invensense Inc., www.invensense.com
onehorse0:39935bb3c1a1 18// See also MPU-9150 Register Map and Descriptions, Revision 4.0, RM-MPU-9150A-00, 9/12/2012 for registers not listed in
onehorse0:39935bb3c1a1 19// above document; the MPU6050 and MPU 9150 are virtually identical but the latter has an on-board magnetic sensor
onehorse0:39935bb3c1a1 20//
onehorse0:39935bb3c1a1 21//Magnetometer Registers
onehorse0:39935bb3c1a1 22#define WHO_AM_I_AK8975A 0x00 // should return 0x48
onehorse0:39935bb3c1a1 23#define INFO 0x01
onehorse0:39935bb3c1a1 24#define AK8975A_ST1 0x02 // data ready status bit 0
onehorse0:39935bb3c1a1 25#define AK8975A_ADDRESS 0x0C<<1
onehorse0:39935bb3c1a1 26#define AK8975A_XOUT_L 0x03 // data
onehorse0:39935bb3c1a1 27#define AK8975A_XOUT_H 0x04
onehorse0:39935bb3c1a1 28#define AK8975A_YOUT_L 0x05
onehorse0:39935bb3c1a1 29#define AK8975A_YOUT_H 0x06
onehorse0:39935bb3c1a1 30#define AK8975A_ZOUT_L 0x07
onehorse0:39935bb3c1a1 31#define AK8975A_ZOUT_H 0x08
onehorse0:39935bb3c1a1 32#define AK8975A_ST2 0x09 // Data overflow bit 3 and data read error status bit 2
onehorse0:39935bb3c1a1 33#define AK8975A_CNTL 0x0A // Power down (0000), single-measurement (0001), self-test (1000) and Fuse ROM (1111) modes on bits 3:0
onehorse0:39935bb3c1a1 34#define AK8975A_ASTC 0x0C // Self test control
onehorse0:39935bb3c1a1 35#define AK8975A_ASAX 0x10 // Fuse ROM x-axis sensitivity adjustment value
onehorse0:39935bb3c1a1 36#define AK8975A_ASAY 0x11 // Fuse ROM y-axis sensitivity adjustment value
onehorse0:39935bb3c1a1 37#define AK8975A_ASAZ 0x12 // Fuse ROM z-axis sensitivity adjustment value
onehorse0:39935bb3c1a1 38
onehorse0:39935bb3c1a1 39#define XGOFFS_TC 0x00 // Bit 7 PWR_MODE, bits 6:1 XG_OFFS_TC, bit 0 OTP_BNK_VLD
onehorse0:39935bb3c1a1 40#define YGOFFS_TC 0x01
onehorse0:39935bb3c1a1 41#define ZGOFFS_TC 0x02
onehorse0:39935bb3c1a1 42#define X_FINE_GAIN 0x03 // [7:0] fine gain
onehorse0:39935bb3c1a1 43#define Y_FINE_GAIN 0x04
onehorse0:39935bb3c1a1 44#define Z_FINE_GAIN 0x05
onehorse0:39935bb3c1a1 45#define XA_OFFSET_H 0x06 // User-defined trim values for accelerometer
onehorse0:39935bb3c1a1 46#define XA_OFFSET_L_TC 0x07
onehorse0:39935bb3c1a1 47#define YA_OFFSET_H 0x08
onehorse0:39935bb3c1a1 48#define YA_OFFSET_L_TC 0x09
onehorse0:39935bb3c1a1 49#define ZA_OFFSET_H 0x0A
onehorse0:39935bb3c1a1 50#define ZA_OFFSET_L_TC 0x0B
onehorse0:39935bb3c1a1 51#define SELF_TEST_X 0x0D
onehorse0:39935bb3c1a1 52#define SELF_TEST_Y 0x0E
onehorse0:39935bb3c1a1 53#define SELF_TEST_Z 0x0F
onehorse0:39935bb3c1a1 54#define SELF_TEST_A 0x10
onehorse0:39935bb3c1a1 55#define XG_OFFS_USRH 0x13 // User-defined trim values for gyroscope, populate with calibration routine
onehorse0:39935bb3c1a1 56#define XG_OFFS_USRL 0x14
onehorse0:39935bb3c1a1 57#define YG_OFFS_USRH 0x15
onehorse0:39935bb3c1a1 58#define YG_OFFS_USRL 0x16
onehorse0:39935bb3c1a1 59#define ZG_OFFS_USRH 0x17
onehorse0:39935bb3c1a1 60#define ZG_OFFS_USRL 0x18
onehorse0:39935bb3c1a1 61#define SMPLRT_DIV 0x19
onehorse0:39935bb3c1a1 62#define CONFIG 0x1A
onehorse0:39935bb3c1a1 63#define GYRO_CONFIG 0x1B
onehorse0:39935bb3c1a1 64#define ACCEL_CONFIG 0x1C
onehorse0:39935bb3c1a1 65#define FF_THR 0x1D // Free-fall
onehorse0:39935bb3c1a1 66#define FF_DUR 0x1E // Free-fall
onehorse0:39935bb3c1a1 67#define MOT_THR 0x1F // Motion detection threshold bits [7:0]
onehorse0:39935bb3c1a1 68#define MOT_DUR 0x20 // Duration counter threshold for motion interrupt generation, 1 kHz rate, LSB = 1 ms
onehorse0:39935bb3c1a1 69#define ZMOT_THR 0x21 // Zero-motion detection threshold bits [7:0]
onehorse0:39935bb3c1a1 70#define ZRMOT_DUR 0x22 // Duration counter threshold for zero motion interrupt generation, 16 Hz rate, LSB = 64 ms
onehorse0:39935bb3c1a1 71#define FIFO_EN 0x23
onehorse0:39935bb3c1a1 72#define I2C_MST_CTRL 0x24
onehorse0:39935bb3c1a1 73#define I2C_SLV0_ADDR 0x25
onehorse0:39935bb3c1a1 74#define I2C_SLV0_REG 0x26
onehorse0:39935bb3c1a1 75#define I2C_SLV0_CTRL 0x27
onehorse0:39935bb3c1a1 76#define I2C_SLV1_ADDR 0x28
onehorse0:39935bb3c1a1 77#define I2C_SLV1_REG 0x29
onehorse0:39935bb3c1a1 78#define I2C_SLV1_CTRL 0x2A
onehorse0:39935bb3c1a1 79#define I2C_SLV2_ADDR 0x2B
onehorse0:39935bb3c1a1 80#define I2C_SLV2_REG 0x2C
onehorse0:39935bb3c1a1 81#define I2C_SLV2_CTRL 0x2D
onehorse0:39935bb3c1a1 82#define I2C_SLV3_ADDR 0x2E
onehorse0:39935bb3c1a1 83#define I2C_SLV3_REG 0x2F
onehorse0:39935bb3c1a1 84#define I2C_SLV3_CTRL 0x30
onehorse0:39935bb3c1a1 85#define I2C_SLV4_ADDR 0x31
onehorse0:39935bb3c1a1 86#define I2C_SLV4_REG 0x32
onehorse0:39935bb3c1a1 87#define I2C_SLV4_DO 0x33
onehorse0:39935bb3c1a1 88#define I2C_SLV4_CTRL 0x34
onehorse0:39935bb3c1a1 89#define I2C_SLV4_DI 0x35
onehorse0:39935bb3c1a1 90#define I2C_MST_STATUS 0x36
onehorse0:39935bb3c1a1 91#define INT_PIN_CFG 0x37
onehorse0:39935bb3c1a1 92#define INT_ENABLE 0x38
onehorse0:39935bb3c1a1 93#define DMP_INT_STATUS 0x39 // Check DMP interrupt
onehorse0:39935bb3c1a1 94#define INT_STATUS 0x3A
onehorse0:39935bb3c1a1 95#define ACCEL_XOUT_H 0x3B
onehorse0:39935bb3c1a1 96#define ACCEL_XOUT_L 0x3C
onehorse0:39935bb3c1a1 97#define ACCEL_YOUT_H 0x3D
onehorse0:39935bb3c1a1 98#define ACCEL_YOUT_L 0x3E
onehorse0:39935bb3c1a1 99#define ACCEL_ZOUT_H 0x3F
onehorse0:39935bb3c1a1 100#define ACCEL_ZOUT_L 0x40
onehorse0:39935bb3c1a1 101#define TEMP_OUT_H 0x41
onehorse0:39935bb3c1a1 102#define TEMP_OUT_L 0x42
onehorse0:39935bb3c1a1 103#define GYRO_XOUT_H 0x43
onehorse0:39935bb3c1a1 104#define GYRO_XOUT_L 0x44
onehorse0:39935bb3c1a1 105#define GYRO_YOUT_H 0x45
onehorse0:39935bb3c1a1 106#define GYRO_YOUT_L 0x46
onehorse0:39935bb3c1a1 107#define GYRO_ZOUT_H 0x47
onehorse0:39935bb3c1a1 108#define GYRO_ZOUT_L 0x48
onehorse0:39935bb3c1a1 109#define EXT_SENS_DATA_00 0x49
onehorse0:39935bb3c1a1 110#define EXT_SENS_DATA_01 0x4A
onehorse0:39935bb3c1a1 111#define EXT_SENS_DATA_02 0x4B
onehorse0:39935bb3c1a1 112#define EXT_SENS_DATA_03 0x4C
onehorse0:39935bb3c1a1 113#define EXT_SENS_DATA_04 0x4D
onehorse0:39935bb3c1a1 114#define EXT_SENS_DATA_05 0x4E
onehorse0:39935bb3c1a1 115#define EXT_SENS_DATA_06 0x4F
onehorse0:39935bb3c1a1 116#define EXT_SENS_DATA_07 0x50
onehorse0:39935bb3c1a1 117#define EXT_SENS_DATA_08 0x51
onehorse0:39935bb3c1a1 118#define EXT_SENS_DATA_09 0x52
onehorse0:39935bb3c1a1 119#define EXT_SENS_DATA_10 0x53
onehorse0:39935bb3c1a1 120#define EXT_SENS_DATA_11 0x54
onehorse0:39935bb3c1a1 121#define EXT_SENS_DATA_12 0x55
onehorse0:39935bb3c1a1 122#define EXT_SENS_DATA_13 0x56
onehorse0:39935bb3c1a1 123#define EXT_SENS_DATA_14 0x57
onehorse0:39935bb3c1a1 124#define EXT_SENS_DATA_15 0x58
onehorse0:39935bb3c1a1 125#define EXT_SENS_DATA_16 0x59
onehorse0:39935bb3c1a1 126#define EXT_SENS_DATA_17 0x5A
onehorse0:39935bb3c1a1 127#define EXT_SENS_DATA_18 0x5B
onehorse0:39935bb3c1a1 128#define EXT_SENS_DATA_19 0x5C
onehorse0:39935bb3c1a1 129#define EXT_SENS_DATA_20 0x5D
onehorse0:39935bb3c1a1 130#define EXT_SENS_DATA_21 0x5E
onehorse0:39935bb3c1a1 131#define EXT_SENS_DATA_22 0x5F
onehorse0:39935bb3c1a1 132#define EXT_SENS_DATA_23 0x60
onehorse0:39935bb3c1a1 133#define MOT_DETECT_STATUS 0x61
onehorse0:39935bb3c1a1 134#define I2C_SLV0_DO 0x63
onehorse0:39935bb3c1a1 135#define I2C_SLV1_DO 0x64
onehorse0:39935bb3c1a1 136#define I2C_SLV2_DO 0x65
onehorse0:39935bb3c1a1 137#define I2C_SLV3_DO 0x66
onehorse0:39935bb3c1a1 138#define I2C_MST_DELAY_CTRL 0x67
onehorse0:39935bb3c1a1 139#define SIGNAL_PATH_RESET 0x68
onehorse0:39935bb3c1a1 140#define MOT_DETECT_CTRL 0x69
onehorse0:39935bb3c1a1 141#define USER_CTRL 0x6A // Bit 7 enable DMP, bit 3 reset DMP
onehorse0:39935bb3c1a1 142#define PWR_MGMT_1 0x6B // Device defaults to the SLEEP mode
onehorse0:39935bb3c1a1 143#define PWR_MGMT_2 0x6C
onehorse0:39935bb3c1a1 144#define DMP_BANK 0x6D // Activates a specific bank in the DMP
onehorse0:39935bb3c1a1 145#define DMP_RW_PNT 0x6E // Set read/write pointer to a specific start address in specified DMP bank
onehorse0:39935bb3c1a1 146#define DMP_REG 0x6F // Register in DMP from which to read or to which to write
onehorse0:39935bb3c1a1 147#define DMP_REG_1 0x70
onehorse0:39935bb3c1a1 148#define DMP_REG_2 0x71
onehorse0:39935bb3c1a1 149#define FIFO_COUNTH 0x72
onehorse0:39935bb3c1a1 150#define FIFO_COUNTL 0x73
onehorse0:39935bb3c1a1 151#define FIFO_R_W 0x74
onehorse0:39935bb3c1a1 152#define WHO_AM_I_MPU9150 0x75 // Should return 0x68
onehorse0:39935bb3c1a1 153
onehorse0:39935bb3c1a1 154
onehorse0:39935bb3c1a1 155// Using the GY-9150 breakout board, ADO is set to 0
onehorse0:39935bb3c1a1 156// Seven-bit device address is 110100 for ADO = 0 and 110101 for ADO = 1
onehorse0:39935bb3c1a1 157// mbed uses the eight-bit device address, so shift seven-bit addresses left by one!
onehorse0:39935bb3c1a1 158#define ADO 0
onehorse0:39935bb3c1a1 159#if ADO
onehorse0:39935bb3c1a1 160#define MPU9150_ADDRESS 0x69<<1 // Device address when ADO = 1
onehorse0:39935bb3c1a1 161#else
onehorse0:39935bb3c1a1 162#define MPU9150_ADDRESS 0x68<<1 // Device address when ADO = 0
onehorse0:39935bb3c1a1 163#endif
onehorse0:39935bb3c1a1 164
onehorse0:39935bb3c1a1 165// Set initial input parameters
onehorse0:39935bb3c1a1 166enum Ascale {
onehorse0:39935bb3c1a1 167 AFS_2G = 0,
onehorse0:39935bb3c1a1 168 AFS_4G,
onehorse0:39935bb3c1a1 169 AFS_8G,
onehorse0:39935bb3c1a1 170 AFS_16G
onehorse0:39935bb3c1a1 171};
onehorse0:39935bb3c1a1 172
onehorse0:39935bb3c1a1 173enum Gscale {
onehorse0:39935bb3c1a1 174 GFS_250DPS = 0,
onehorse0:39935bb3c1a1 175 GFS_500DPS,
onehorse0:39935bb3c1a1 176 GFS_1000DPS,
onehorse0:39935bb3c1a1 177 GFS_2000DPS
onehorse0:39935bb3c1a1 178};
onehorse0:39935bb3c1a1 179
onehorse0:39935bb3c1a1 180uint8_t Ascale = AFS_2G; // AFS_2G, AFS_4G, AFS_8G, AFS_16G
onehorse0:39935bb3c1a1 181uint8_t Gscale = GFS_250DPS; // GFS_250DPS, GFS_500DPS, GFS_1000DPS, GFS_2000DPS
onehorse0:39935bb3c1a1 182float aRes, gRes, mRes; // scale resolutions per LSB for the sensors
onehorse0:39935bb3c1a1 183
onehorse0:39935bb3c1a1 184//Set up I2C, (SDA,SCL)
onehorse0:39935bb3c1a1 185I2C i2c(I2C_SDA, I2C_SCL);
onehorse0:39935bb3c1a1 186
onehorse0:39935bb3c1a1 187DigitalOut myled(LED1);
onehorse0:39935bb3c1a1 188
onehorse0:39935bb3c1a1 189// Pin definitions
onehorse0:39935bb3c1a1 190int intPin = 12; // These can be changed, 2 and 3 are the Arduinos ext int pins
onehorse0:39935bb3c1a1 191
onehorse0:39935bb3c1a1 192int16_t accelCount[3]; // Stores the 16-bit signed accelerometer sensor output
onehorse0:39935bb3c1a1 193int16_t gyroCount[3]; // Stores the 16-bit signed gyro sensor output
onehorse0:39935bb3c1a1 194int16_t magCount[3]; // Stores the 16-bit signed magnetometer sensor output
onehorse0:39935bb3c1a1 195float magCalibration[3] = {0, 0, 0}, magbias[3] = {0, 0, 0}; // Factory mag calibration and mag bias
whatnick9:b7062c55d36c 196float gyroBias[3] = {0, 0, 0}, accelBias[3] = {0, 0, 0}, magBias[3] = {0, 0, 0}; ; // Bias corrections for gyro, accelerometer and mag
onehorse0:39935bb3c1a1 197float ax, ay, az, gx, gy, gz, mx, my, mz; // variables to hold latest sensor data values
onehorse0:39935bb3c1a1 198int16_t tempCount; // Stores the real internal chip temperature in degrees Celsius
onehorse0:39935bb3c1a1 199float temperature;
onehorse0:39935bb3c1a1 200float SelfTest[6];
onehorse0:39935bb3c1a1 201
onehorse0:39935bb3c1a1 202int delt_t = 0; // used to control display output rate
onehorse0:39935bb3c1a1 203int count = 0; // used to control display output rate
onehorse0:39935bb3c1a1 204
onehorse0:39935bb3c1a1 205// parameters for 6 DoF sensor fusion calculations
onehorse0:39935bb3c1a1 206float PI = 3.14159265358979323846f;
onehorse0:39935bb3c1a1 207float GyroMeasError = PI * (60.0f / 180.0f); // gyroscope measurement error in rads/s (start at 60 deg/s), then reduce after ~10 s to 3
onehorse0:39935bb3c1a1 208float beta = sqrt(3.0f / 4.0f) * GyroMeasError; // compute beta
onehorse0:39935bb3c1a1 209float GyroMeasDrift = PI * (1.0f / 180.0f); // gyroscope measurement drift in rad/s/s (start at 0.0 deg/s/s)
onehorse0:39935bb3c1a1 210float zeta = sqrt(3.0f / 4.0f) * GyroMeasDrift; // compute zeta, the other free parameter in the Madgwick scheme usually set to a small or zero value
onehorse0:39935bb3c1a1 211#define Kp 2.0f * 5.0f // these are the free parameters in the Mahony filter and fusion scheme, Kp for proportional feedback, Ki for integral
onehorse0:39935bb3c1a1 212#define Ki 0.0f
onehorse0:39935bb3c1a1 213
onehorse0:39935bb3c1a1 214float pitch, yaw, roll;
onehorse0:39935bb3c1a1 215float deltat = 0.0f; // integration interval for both filter schemes
onehorse0:39935bb3c1a1 216int lastUpdate = 0, firstUpdate = 0, Now = 0; // used to calculate integration interval // used to calculate integration interval
onehorse0:39935bb3c1a1 217float q[4] = {1.0f, 0.0f, 0.0f, 0.0f}; // vector to hold quaternion
onehorse0:39935bb3c1a1 218float eInt[3] = {0.0f, 0.0f, 0.0f}; // vector to hold integral error for Mahony method
onehorse0:39935bb3c1a1 219
onehorse0:39935bb3c1a1 220class MPU9150 {
onehorse0:39935bb3c1a1 221
onehorse0:39935bb3c1a1 222 protected:
onehorse0:39935bb3c1a1 223
onehorse0:39935bb3c1a1 224 public:
onehorse0:39935bb3c1a1 225 //===================================================================================================================
onehorse0:39935bb3c1a1 226//====== Set of useful function to access acceleratio, gyroscope, and temperature data
onehorse0:39935bb3c1a1 227//===================================================================================================================
onehorse0:39935bb3c1a1 228
onehorse0:39935bb3c1a1 229 void writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
onehorse0:39935bb3c1a1 230{
onehorse0:39935bb3c1a1 231 char data_write[2];
onehorse0:39935bb3c1a1 232 data_write[0] = subAddress;
onehorse0:39935bb3c1a1 233 data_write[1] = data;
onehorse0:39935bb3c1a1 234 i2c.write(address, data_write, 2, 0);
onehorse0:39935bb3c1a1 235}
onehorse0:39935bb3c1a1 236
onehorse0:39935bb3c1a1 237 char readByte(uint8_t address, uint8_t subAddress)
onehorse0:39935bb3c1a1 238{
onehorse0:39935bb3c1a1 239 char data[1]; // `data` will store the register data
onehorse0:39935bb3c1a1 240 char data_write[1];
onehorse0:39935bb3c1a1 241 data_write[0] = subAddress;
onehorse0:39935bb3c1a1 242 i2c.write(address, data_write, 1, 1); // no stop
onehorse0:39935bb3c1a1 243 i2c.read(address, data, 1, 0);
onehorse0:39935bb3c1a1 244 return data[0];
onehorse0:39935bb3c1a1 245}
onehorse0:39935bb3c1a1 246
onehorse0:39935bb3c1a1 247 void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest)
onehorse0:39935bb3c1a1 248{
onehorse0:39935bb3c1a1 249 char data[14];
onehorse0:39935bb3c1a1 250 char data_write[1];
onehorse0:39935bb3c1a1 251 data_write[0] = subAddress;
onehorse0:39935bb3c1a1 252 i2c.write(address, data_write, 1, 1); // no stop
onehorse0:39935bb3c1a1 253 i2c.read(address, data, count, 0);
onehorse0:39935bb3c1a1 254 for(int ii = 0; ii < count; ii++) {
onehorse0:39935bb3c1a1 255 dest[ii] = data[ii];
onehorse0:39935bb3c1a1 256 }
onehorse0:39935bb3c1a1 257}
onehorse0:39935bb3c1a1 258
onehorse0:39935bb3c1a1 259
onehorse0:39935bb3c1a1 260void getGres() {
onehorse0:39935bb3c1a1 261 switch (Gscale)
onehorse0:39935bb3c1a1 262 {
onehorse0:39935bb3c1a1 263 // Possible gyro scales (and their register bit settings) are:
onehorse0:39935bb3c1a1 264 // 250 DPS (00), 500 DPS (01), 1000 DPS (10), and 2000 DPS (11).
onehorse0:39935bb3c1a1 265 // Here's a bit of an algorith to calculate DPS/(ADC tick) based on that 2-bit value:
onehorse0:39935bb3c1a1 266 case GFS_250DPS:
onehorse0:39935bb3c1a1 267 gRes = 250.0/32768.0;
onehorse0:39935bb3c1a1 268 break;
onehorse0:39935bb3c1a1 269 case GFS_500DPS:
onehorse0:39935bb3c1a1 270 gRes = 500.0/32768.0;
onehorse0:39935bb3c1a1 271 break;
onehorse0:39935bb3c1a1 272 case GFS_1000DPS:
onehorse0:39935bb3c1a1 273 gRes = 1000.0/32768.0;
onehorse0:39935bb3c1a1 274 break;
onehorse0:39935bb3c1a1 275 case GFS_2000DPS:
onehorse0:39935bb3c1a1 276 gRes = 2000.0/32768.0;
onehorse0:39935bb3c1a1 277 break;
onehorse0:39935bb3c1a1 278 }
onehorse0:39935bb3c1a1 279}
onehorse0:39935bb3c1a1 280
onehorse0:39935bb3c1a1 281
onehorse0:39935bb3c1a1 282void getAres() {
onehorse0:39935bb3c1a1 283 switch (Ascale)
onehorse0:39935bb3c1a1 284 {
onehorse0:39935bb3c1a1 285 // Possible accelerometer scales (and their register bit settings) are:
onehorse0:39935bb3c1a1 286 // 2 Gs (00), 4 Gs (01), 8 Gs (10), and 16 Gs (11).
onehorse0:39935bb3c1a1 287 // Here's a bit of an algorith to calculate DPS/(ADC tick) based on that 2-bit value:
onehorse0:39935bb3c1a1 288 case AFS_2G:
onehorse0:39935bb3c1a1 289 aRes = 2.0/32768.0;
onehorse0:39935bb3c1a1 290 break;
onehorse0:39935bb3c1a1 291 case AFS_4G:
onehorse0:39935bb3c1a1 292 aRes = 4.0/32768.0;
onehorse0:39935bb3c1a1 293 break;
onehorse0:39935bb3c1a1 294 case AFS_8G:
onehorse0:39935bb3c1a1 295 aRes = 8.0/32768.0;
onehorse0:39935bb3c1a1 296 break;
onehorse0:39935bb3c1a1 297 case AFS_16G:
onehorse0:39935bb3c1a1 298 aRes = 16.0/32768.0;
onehorse0:39935bb3c1a1 299 break;
onehorse0:39935bb3c1a1 300 }
onehorse0:39935bb3c1a1 301}
onehorse0:39935bb3c1a1 302
onehorse0:39935bb3c1a1 303
onehorse0:39935bb3c1a1 304void readAccelData(int16_t * destination)
onehorse0:39935bb3c1a1 305{
onehorse0:39935bb3c1a1 306 uint8_t rawData[6]; // x/y/z accel register data stored here
onehorse0:39935bb3c1a1 307 readBytes(MPU9150_ADDRESS, ACCEL_XOUT_H, 6, &rawData[0]); // Read the six raw data registers into data array
onehorse0:39935bb3c1a1 308 destination[0] = (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value
onehorse0:39935bb3c1a1 309 destination[1] = (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ;
onehorse0:39935bb3c1a1 310 destination[2] = (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ;
onehorse0:39935bb3c1a1 311}
onehorse0:39935bb3c1a1 312
onehorse0:39935bb3c1a1 313void readGyroData(int16_t * destination)
onehorse0:39935bb3c1a1 314{
onehorse0:39935bb3c1a1 315 uint8_t rawData[6]; // x/y/z gyro register data stored here
onehorse0:39935bb3c1a1 316 readBytes(MPU9150_ADDRESS, GYRO_XOUT_H, 6, &rawData[0]); // Read the six raw data registers sequentially into data array
onehorse0:39935bb3c1a1 317 destination[0] = (int16_t)(((int16_t)rawData[0] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value
onehorse0:39935bb3c1a1 318 destination[1] = (int16_t)(((int16_t)rawData[2] << 8) | rawData[3]) ;
onehorse0:39935bb3c1a1 319 destination[2] = (int16_t)(((int16_t)rawData[4] << 8) | rawData[5]) ;
onehorse0:39935bb3c1a1 320}
onehorse0:39935bb3c1a1 321
onehorse0:39935bb3c1a1 322void readMagData(int16_t * destination)
onehorse0:39935bb3c1a1 323{
onehorse0:39935bb3c1a1 324 uint8_t rawData[6]; // x/y/z gyro register data stored here
onehorse0:39935bb3c1a1 325 writeByte(AK8975A_ADDRESS, AK8975A_CNTL, 0x01); // toggle enable data read from magnetometer, no continuous read mode!
onehorse0:39935bb3c1a1 326 wait(0.01);
onehorse0:39935bb3c1a1 327 // Only accept a new magnetometer data read if the data ready bit is set and
onehorse0:39935bb3c1a1 328 // if there are no sensor overflow or data read errors
onehorse0:39935bb3c1a1 329 if(readByte(AK8975A_ADDRESS, AK8975A_ST1) & 0x01) { // wait for magnetometer data ready bit to be set
onehorse0:39935bb3c1a1 330 readBytes(AK8975A_ADDRESS, AK8975A_XOUT_L, 6, &rawData[0]); // Read the six raw data registers sequentially into data array
onehorse0:39935bb3c1a1 331 destination[0] = ((int16_t)rawData[1] << 8) | rawData[0] ; // Turn the MSB and LSB into a signed 16-bit value
onehorse0:39935bb3c1a1 332 destination[1] = ((int16_t)rawData[3] << 8) | rawData[2] ;
onehorse0:39935bb3c1a1 333 destination[2] = ((int16_t)rawData[5] << 8) | rawData[4] ;
onehorse0:39935bb3c1a1 334 }
onehorse0:39935bb3c1a1 335}
onehorse0:39935bb3c1a1 336
onehorse0:39935bb3c1a1 337void initAK8975A(float * destination)
onehorse0:39935bb3c1a1 338{
onehorse0:39935bb3c1a1 339 uint8_t rawData[3]; // x/y/z gyro register data stored here
onehorse0:39935bb3c1a1 340 writeByte(AK8975A_ADDRESS, AK8975A_CNTL, 0x00); // Power down
onehorse0:39935bb3c1a1 341 wait(0.01);
onehorse0:39935bb3c1a1 342 writeByte(AK8975A_ADDRESS, AK8975A_CNTL, 0x0F); // Enter Fuse ROM access mode
onehorse0:39935bb3c1a1 343 wait(0.01);
onehorse0:39935bb3c1a1 344 readBytes(AK8975A_ADDRESS, AK8975A_ASAX, 3, &rawData[0]); // Read the x-, y-, and z-axis calibration values
onehorse0:39935bb3c1a1 345 destination[0] = (float)(rawData[0] - 128)/256.0f + 1.0f; // Return x-axis sensitivity adjustment values
onehorse0:39935bb3c1a1 346 destination[1] = (float)(rawData[1] - 128)/256.0f + 1.0f;
onehorse0:39935bb3c1a1 347 destination[2] = (float)(rawData[2] - 128)/256.0f + 1.0f;
onehorse0:39935bb3c1a1 348}
onehorse0:39935bb3c1a1 349
whatnick9:b7062c55d36c 350void magcalMPU9150(float * dest1)
whatnick9:b7062c55d36c 351{
whatnick9:b7062c55d36c 352 uint16_t ii = 0, sample_count = 0;
whatnick9:b7062c55d36c 353 int32_t mag_bias[3] = {0, 0, 0};
whatnick9:b7062c55d36c 354 int16_t mag_max[3] = {0, 0, 0}, mag_min[3] = {0, 0, 0}, mag_temp[3] = {0, 0, 0};
whatnick9:b7062c55d36c 355
whatnick9:b7062c55d36c 356 LOG("Mag Calibration: Wave device in a figure eight until done!");
whatnick9:b7062c55d36c 357 wait(4);
whatnick9:b7062c55d36c 358
whatnick9:b7062c55d36c 359 sample_count = 64;
whatnick9:b7062c55d36c 360 for(ii = 0; ii < sample_count; ii++) {
whatnick9:b7062c55d36c 361 readMagData(mag_temp); // Read the mag data
whatnick9:b7062c55d36c 362 for (int jj = 0; jj < 3; jj++) {
whatnick9:b7062c55d36c 363 if (ii == 0) {
whatnick9:b7062c55d36c 364 mag_max[jj] = mag_temp[jj]; // Offsets may be large enough that mag_temp[i] may not be bipolar!
whatnick9:b7062c55d36c 365 mag_min[jj] = mag_temp[jj]; // This prevents max or min being pinned to 0 if the values are unipolar...
whatnick9:b7062c55d36c 366 } else {
whatnick9:b7062c55d36c 367 if(mag_temp[jj] > mag_max[jj]) mag_max[jj] = mag_temp[jj];
whatnick9:b7062c55d36c 368 if(mag_temp[jj] < mag_min[jj]) mag_min[jj] = mag_temp[jj];
whatnick9:b7062c55d36c 369 }
whatnick9:b7062c55d36c 370 }
whatnick9:b7062c55d36c 371 wait_ms(135); // at 8 Hz ODR, new mag data is available every 125 ms
whatnick9:b7062c55d36c 372 }
whatnick9:b7062c55d36c 373
whatnick9:b7062c55d36c 374// Serial.println("mag x min/max:"); Serial.println(mag_max[0]); Serial.println(mag_min[0]);
whatnick9:b7062c55d36c 375// Serial.println("mag y min/max:"); Serial.println(mag_max[1]); Serial.println(mag_min[1]);
whatnick9:b7062c55d36c 376// Serial.println("mag z min/max:"); Serial.println(mag_max[2]); Serial.println(mag_min[2]);
whatnick9:b7062c55d36c 377
whatnick9:b7062c55d36c 378 mag_bias[0] = (mag_max[0] + mag_min[0])/2; // get average x mag bias in counts
whatnick9:b7062c55d36c 379 mag_bias[1] = (mag_max[1] + mag_min[1])/2; // get average y mag bias in counts
whatnick9:b7062c55d36c 380 mag_bias[2] = (mag_max[2] + mag_min[2])/2; // get average z mag bias in counts
whatnick9:b7062c55d36c 381
whatnick9:b7062c55d36c 382 dest1[0] = (float) mag_bias[0]*mRes*magCalibration[0]; // save mag biases in G for main program
whatnick9:b7062c55d36c 383 dest1[1] = (float) mag_bias[1]*mRes*magCalibration[1];
whatnick9:b7062c55d36c 384 dest1[2] = (float) mag_bias[2]*mRes*magCalibration[2];
whatnick9:b7062c55d36c 385
whatnick9:b7062c55d36c 386 LOG("Mag Calibration done!");
whatnick9:b7062c55d36c 387}
whatnick9:b7062c55d36c 388
onehorse0:39935bb3c1a1 389int16_t readTempData()
onehorse0:39935bb3c1a1 390{
onehorse0:39935bb3c1a1 391 uint8_t rawData[2]; // x/y/z gyro register data stored here
onehorse0:39935bb3c1a1 392 readBytes(MPU9150_ADDRESS, TEMP_OUT_H, 2, &rawData[0]); // Read the two raw data registers sequentially into data array
onehorse0:39935bb3c1a1 393 return ((int16_t)rawData[0] << 8) | rawData[1] ; // Turn the MSB and LSB into a 16-bit value
onehorse0:39935bb3c1a1 394}
onehorse0:39935bb3c1a1 395
onehorse0:39935bb3c1a1 396void resetMPU9150() {
onehorse0:39935bb3c1a1 397 // reset device
onehorse0:39935bb3c1a1 398 writeByte(MPU9150_ADDRESS, PWR_MGMT_1, 0x80); // Write a one to bit 7 reset bit; toggle reset device
onehorse0:39935bb3c1a1 399 wait(0.1);
onehorse0:39935bb3c1a1 400 }
onehorse0:39935bb3c1a1 401
onehorse0:39935bb3c1a1 402
onehorse0:39935bb3c1a1 403
onehorse0:39935bb3c1a1 404void initMPU9150()
onehorse0:39935bb3c1a1 405{
onehorse0:39935bb3c1a1 406 // Initialize MPU9150 device
onehorse0:39935bb3c1a1 407 // wake up device
onehorse0:39935bb3c1a1 408 writeByte(MPU9150_ADDRESS, PWR_MGMT_1, 0x00); // Clear sleep mode bit (6), enable all sensors
onehorse0:39935bb3c1a1 409 wait(0.1); // Delay 100 ms for PLL to get established on x-axis gyro; should check for PLL ready interrupt
onehorse0:39935bb3c1a1 410
onehorse0:39935bb3c1a1 411 // get stable time source
onehorse0:39935bb3c1a1 412 writeByte(MPU9150_ADDRESS, PWR_MGMT_1, 0x01); // Set clock source to be PLL with x-axis gyroscope reference, bits 2:0 = 001
onehorse0:39935bb3c1a1 413
onehorse0:39935bb3c1a1 414 // Configure Gyro and Accelerometer
onehorse0:39935bb3c1a1 415 // Disable FSYNC and set accelerometer and gyro bandwidth to 44 and 42 Hz, respectively;
onehorse0:39935bb3c1a1 416 // DLPF_CFG = bits 2:0 = 010; this sets the sample rate at 1 kHz for both
onehorse0:39935bb3c1a1 417 // Maximum delay is 4.9 ms which is just over a 200 Hz maximum rate
onehorse0:39935bb3c1a1 418 writeByte(MPU9150_ADDRESS, CONFIG, 0x03);
onehorse0:39935bb3c1a1 419
onehorse0:39935bb3c1a1 420 // Set sample rate = gyroscope output rate/(1 + SMPLRT_DIV)
onehorse0:39935bb3c1a1 421 writeByte(MPU9150_ADDRESS, SMPLRT_DIV, 0x04); // Use a 200 Hz rate; the same rate set in CONFIG above
onehorse0:39935bb3c1a1 422
onehorse0:39935bb3c1a1 423 // Set gyroscope full scale range
onehorse0:39935bb3c1a1 424 // Range selects FS_SEL and AFS_SEL are 0 - 3, so 2-bit values are left-shifted into positions 4:3
onehorse0:39935bb3c1a1 425 uint8_t c = readByte(MPU9150_ADDRESS, GYRO_CONFIG);
onehorse0:39935bb3c1a1 426 writeByte(MPU9150_ADDRESS, GYRO_CONFIG, c & ~0xE0); // Clear self-test bits [7:5]
onehorse0:39935bb3c1a1 427 writeByte(MPU9150_ADDRESS, GYRO_CONFIG, c & ~0x18); // Clear AFS bits [4:3]
onehorse0:39935bb3c1a1 428 writeByte(MPU9150_ADDRESS, GYRO_CONFIG, c | Gscale << 3); // Set full scale range for the gyro
onehorse0:39935bb3c1a1 429
onehorse0:39935bb3c1a1 430 // Set accelerometer configuration
onehorse0:39935bb3c1a1 431 c = readByte(MPU9150_ADDRESS, ACCEL_CONFIG);
onehorse0:39935bb3c1a1 432 writeByte(MPU9150_ADDRESS, ACCEL_CONFIG, c & ~0xE0); // Clear self-test bits [7:5]
onehorse0:39935bb3c1a1 433 writeByte(MPU9150_ADDRESS, ACCEL_CONFIG, c & ~0x18); // Clear AFS bits [4:3]
onehorse0:39935bb3c1a1 434 writeByte(MPU9150_ADDRESS, ACCEL_CONFIG, c | Ascale << 3); // Set full scale range for the accelerometer
onehorse0:39935bb3c1a1 435
onehorse0:39935bb3c1a1 436 // The accelerometer, gyro, and thermometer are set to 1 kHz sample rates,
onehorse0:39935bb3c1a1 437 // but all these rates are further reduced by a factor of 5 to 200 Hz because of the SMPLRT_DIV setting
onehorse0:39935bb3c1a1 438
onehorse0:39935bb3c1a1 439 // Configure Interrupts and Bypass Enable
onehorse0:39935bb3c1a1 440 // Set interrupt pin active high, push-pull, and clear on read of INT_STATUS, enable I2C_BYPASS_EN so additional chips
onehorse0:39935bb3c1a1 441 // can join the I2C bus and all can be controlled by the Arduino as master
onehorse0:39935bb3c1a1 442 writeByte(MPU9150_ADDRESS, INT_PIN_CFG, 0x22);
onehorse0:39935bb3c1a1 443 writeByte(MPU9150_ADDRESS, INT_ENABLE, 0x01); // Enable data ready (bit 0) interrupt
onehorse0:39935bb3c1a1 444}
onehorse0:39935bb3c1a1 445
onehorse0:39935bb3c1a1 446// Function which accumulates gyro and accelerometer data after device initialization. It calculates the average
onehorse0:39935bb3c1a1 447// of the at-rest readings and then loads the resulting offsets into accelerometer and gyro bias registers.
onehorse0:39935bb3c1a1 448void calibrateMPU9150(float * dest1, float * dest2)
onehorse0:39935bb3c1a1 449{
onehorse0:39935bb3c1a1 450 uint8_t data[12]; // data array to hold accelerometer and gyro x, y, z, data
onehorse0:39935bb3c1a1 451 uint16_t ii, packet_count, fifo_count;
onehorse0:39935bb3c1a1 452 int32_t gyro_bias[3] = {0, 0, 0}, accel_bias[3] = {0, 0, 0};
onehorse0:39935bb3c1a1 453
onehorse0:39935bb3c1a1 454// reset device, reset all registers, clear gyro and accelerometer bias registers
onehorse0:39935bb3c1a1 455 writeByte(MPU9150_ADDRESS, PWR_MGMT_1, 0x80); // Write a one to bit 7 reset bit; toggle reset device
onehorse0:39935bb3c1a1 456 wait(0.1);
onehorse0:39935bb3c1a1 457
onehorse0:39935bb3c1a1 458// get stable time source
onehorse0:39935bb3c1a1 459// Set clock source to be PLL with x-axis gyroscope reference, bits 2:0 = 001
onehorse0:39935bb3c1a1 460 writeByte(MPU9150_ADDRESS, PWR_MGMT_1, 0x01);
onehorse0:39935bb3c1a1 461 writeByte(MPU9150_ADDRESS, PWR_MGMT_2, 0x00);
onehorse0:39935bb3c1a1 462 wait(0.2);
onehorse0:39935bb3c1a1 463
onehorse0:39935bb3c1a1 464// Configure device for bias calculation
onehorse0:39935bb3c1a1 465 writeByte(MPU9150_ADDRESS, INT_ENABLE, 0x00); // Disable all interrupts
onehorse0:39935bb3c1a1 466 writeByte(MPU9150_ADDRESS, FIFO_EN, 0x00); // Disable FIFO
onehorse0:39935bb3c1a1 467 writeByte(MPU9150_ADDRESS, PWR_MGMT_1, 0x00); // Turn on internal clock source
onehorse0:39935bb3c1a1 468 writeByte(MPU9150_ADDRESS, I2C_MST_CTRL, 0x00); // Disable I2C master
onehorse0:39935bb3c1a1 469 writeByte(MPU9150_ADDRESS, USER_CTRL, 0x00); // Disable FIFO and I2C master modes
onehorse0:39935bb3c1a1 470 writeByte(MPU9150_ADDRESS, USER_CTRL, 0x0C); // Reset FIFO and DMP
onehorse0:39935bb3c1a1 471 wait(0.015);
onehorse0:39935bb3c1a1 472
onehorse0:39935bb3c1a1 473// Configure MPU9150 gyro and accelerometer for bias calculation
onehorse0:39935bb3c1a1 474 writeByte(MPU9150_ADDRESS, CONFIG, 0x01); // Set low-pass filter to 188 Hz
onehorse0:39935bb3c1a1 475 writeByte(MPU9150_ADDRESS, SMPLRT_DIV, 0x00); // Set sample rate to 1 kHz
onehorse0:39935bb3c1a1 476 writeByte(MPU9150_ADDRESS, GYRO_CONFIG, 0x00); // Set gyro full-scale to 250 degrees per second, maximum sensitivity
onehorse0:39935bb3c1a1 477 writeByte(MPU9150_ADDRESS, ACCEL_CONFIG, 0x00); // Set accelerometer full-scale to 2 g, maximum sensitivity
onehorse0:39935bb3c1a1 478
onehorse0:39935bb3c1a1 479 uint16_t gyrosensitivity = 131; // = 131 LSB/degrees/sec
onehorse0:39935bb3c1a1 480 uint16_t accelsensitivity = 16384; // = 16384 LSB/g
onehorse0:39935bb3c1a1 481
onehorse0:39935bb3c1a1 482// Configure FIFO to capture accelerometer and gyro data for bias calculation
onehorse0:39935bb3c1a1 483 writeByte(MPU9150_ADDRESS, USER_CTRL, 0x40); // Enable FIFO
onehorse0:39935bb3c1a1 484 writeByte(MPU9150_ADDRESS, FIFO_EN, 0x78); // Enable gyro and accelerometer sensors for FIFO (max size 1024 bytes in MPU9150)
onehorse0:39935bb3c1a1 485 wait(0.08); // accumulate 80 samples in 80 milliseconds = 960 bytes
onehorse0:39935bb3c1a1 486
onehorse0:39935bb3c1a1 487// At end of sample accumulation, turn off FIFO sensor read
onehorse0:39935bb3c1a1 488 writeByte(MPU9150_ADDRESS, FIFO_EN, 0x00); // Disable gyro and accelerometer sensors for FIFO
onehorse0:39935bb3c1a1 489 readBytes(MPU9150_ADDRESS, FIFO_COUNTH, 2, &data[0]); // read FIFO sample count
onehorse0:39935bb3c1a1 490 fifo_count = ((uint16_t)data[0] << 8) | data[1];
onehorse0:39935bb3c1a1 491 packet_count = fifo_count/12;// How many sets of full gyro and accelerometer data for averaging
onehorse0:39935bb3c1a1 492
onehorse0:39935bb3c1a1 493 for (ii = 0; ii < packet_count; ii++) {
onehorse0:39935bb3c1a1 494 int16_t accel_temp[3] = {0, 0, 0}, gyro_temp[3] = {0, 0, 0};
onehorse0:39935bb3c1a1 495 readBytes(MPU9150_ADDRESS, FIFO_R_W, 12, &data[0]); // read data for averaging
onehorse0:39935bb3c1a1 496 accel_temp[0] = (int16_t) (((int16_t)data[0] << 8) | data[1] ) ; // Form signed 16-bit integer for each sample in FIFO
onehorse0:39935bb3c1a1 497 accel_temp[1] = (int16_t) (((int16_t)data[2] << 8) | data[3] ) ;
onehorse0:39935bb3c1a1 498 accel_temp[2] = (int16_t) (((int16_t)data[4] << 8) | data[5] ) ;
onehorse0:39935bb3c1a1 499 gyro_temp[0] = (int16_t) (((int16_t)data[6] << 8) | data[7] ) ;
onehorse0:39935bb3c1a1 500 gyro_temp[1] = (int16_t) (((int16_t)data[8] << 8) | data[9] ) ;
onehorse0:39935bb3c1a1 501 gyro_temp[2] = (int16_t) (((int16_t)data[10] << 8) | data[11]) ;
onehorse0:39935bb3c1a1 502
onehorse0:39935bb3c1a1 503 accel_bias[0] += (int32_t) accel_temp[0]; // Sum individual signed 16-bit biases to get accumulated signed 32-bit biases
onehorse0:39935bb3c1a1 504 accel_bias[1] += (int32_t) accel_temp[1];
onehorse0:39935bb3c1a1 505 accel_bias[2] += (int32_t) accel_temp[2];
onehorse0:39935bb3c1a1 506 gyro_bias[0] += (int32_t) gyro_temp[0];
onehorse0:39935bb3c1a1 507 gyro_bias[1] += (int32_t) gyro_temp[1];
onehorse0:39935bb3c1a1 508 gyro_bias[2] += (int32_t) gyro_temp[2];
onehorse0:39935bb3c1a1 509
onehorse0:39935bb3c1a1 510}
onehorse0:39935bb3c1a1 511 accel_bias[0] /= (int32_t) packet_count; // Normalize sums to get average count biases
onehorse0:39935bb3c1a1 512 accel_bias[1] /= (int32_t) packet_count;
onehorse0:39935bb3c1a1 513 accel_bias[2] /= (int32_t) packet_count;
onehorse0:39935bb3c1a1 514 gyro_bias[0] /= (int32_t) packet_count;
onehorse0:39935bb3c1a1 515 gyro_bias[1] /= (int32_t) packet_count;
onehorse0:39935bb3c1a1 516 gyro_bias[2] /= (int32_t) packet_count;
onehorse0:39935bb3c1a1 517
onehorse0:39935bb3c1a1 518 if(accel_bias[2] > 0L) {accel_bias[2] -= (int32_t) accelsensitivity;} // Remove gravity from the z-axis accelerometer bias calculation
onehorse0:39935bb3c1a1 519 else {accel_bias[2] += (int32_t) accelsensitivity;}
onehorse0:39935bb3c1a1 520
onehorse0:39935bb3c1a1 521// Construct the gyro biases for push to the hardware gyro bias registers, which are reset to zero upon device startup
onehorse0:39935bb3c1a1 522 data[0] = (-gyro_bias[0]/4 >> 8) & 0xFF; // Divide by 4 to get 32.9 LSB per deg/s to conform to expected bias input format
onehorse0:39935bb3c1a1 523 data[1] = (-gyro_bias[0]/4) & 0xFF; // Biases are additive, so change sign on calculated average gyro biases
onehorse0:39935bb3c1a1 524 data[2] = (-gyro_bias[1]/4 >> 8) & 0xFF;
onehorse0:39935bb3c1a1 525 data[3] = (-gyro_bias[1]/4) & 0xFF;
onehorse0:39935bb3c1a1 526 data[4] = (-gyro_bias[2]/4 >> 8) & 0xFF;
onehorse0:39935bb3c1a1 527 data[5] = (-gyro_bias[2]/4) & 0xFF;
onehorse0:39935bb3c1a1 528
onehorse0:39935bb3c1a1 529/// Push gyro biases to hardware registers
onehorse0:39935bb3c1a1 530 writeByte(MPU9150_ADDRESS, XG_OFFS_USRH, data[0]);
onehorse0:39935bb3c1a1 531 writeByte(MPU9150_ADDRESS, XG_OFFS_USRL, data[1]);
onehorse0:39935bb3c1a1 532 writeByte(MPU9150_ADDRESS, YG_OFFS_USRH, data[2]);
onehorse0:39935bb3c1a1 533 writeByte(MPU9150_ADDRESS, YG_OFFS_USRL, data[3]);
onehorse0:39935bb3c1a1 534 writeByte(MPU9150_ADDRESS, ZG_OFFS_USRH, data[4]);
onehorse0:39935bb3c1a1 535 writeByte(MPU9150_ADDRESS, ZG_OFFS_USRL, data[5]);
onehorse0:39935bb3c1a1 536
onehorse0:39935bb3c1a1 537 dest1[0] = (float) gyro_bias[0]/(float) gyrosensitivity; // construct gyro bias in deg/s for later manual subtraction
onehorse0:39935bb3c1a1 538 dest1[1] = (float) gyro_bias[1]/(float) gyrosensitivity;
onehorse0:39935bb3c1a1 539 dest1[2] = (float) gyro_bias[2]/(float) gyrosensitivity;
onehorse0:39935bb3c1a1 540
onehorse0:39935bb3c1a1 541// Construct the accelerometer biases for push to the hardware accelerometer bias registers. These registers contain
onehorse0:39935bb3c1a1 542// factory trim values which must be added to the calculated accelerometer biases; on boot up these registers will hold
onehorse0:39935bb3c1a1 543// non-zero values. In addition, bit 0 of the lower byte must be preserved since it is used for temperature
onehorse0:39935bb3c1a1 544// compensation calculations. Accelerometer bias registers expect bias input as 2048 LSB per g, so that
onehorse0:39935bb3c1a1 545// the accelerometer biases calculated above must be divided by 8.
onehorse0:39935bb3c1a1 546
onehorse0:39935bb3c1a1 547 int32_t accel_bias_reg[3] = {0, 0, 0}; // A place to hold the factory accelerometer trim biases
onehorse0:39935bb3c1a1 548 readBytes(MPU9150_ADDRESS, XA_OFFSET_H, 2, &data[0]); // Read factory accelerometer trim values
onehorse0:39935bb3c1a1 549 accel_bias_reg[0] = (int16_t) ((int16_t)data[0] << 8) | data[1];
onehorse0:39935bb3c1a1 550 readBytes(MPU9150_ADDRESS, YA_OFFSET_H, 2, &data[0]);
onehorse0:39935bb3c1a1 551 accel_bias_reg[1] = (int16_t) ((int16_t)data[0] << 8) | data[1];
onehorse0:39935bb3c1a1 552 readBytes(MPU9150_ADDRESS, ZA_OFFSET_H, 2, &data[0]);
onehorse0:39935bb3c1a1 553 accel_bias_reg[2] = (int16_t) ((int16_t)data[0] << 8) | data[1];
onehorse0:39935bb3c1a1 554
onehorse0:39935bb3c1a1 555 uint32_t mask = 1uL; // Define mask for temperature compensation bit 0 of lower byte of accelerometer bias registers
onehorse0:39935bb3c1a1 556 uint8_t mask_bit[3] = {0, 0, 0}; // Define array to hold mask bit for each accelerometer bias axis
onehorse0:39935bb3c1a1 557
onehorse0:39935bb3c1a1 558 for(ii = 0; ii < 3; ii++) {
onehorse0:39935bb3c1a1 559 if(accel_bias_reg[ii] & mask) mask_bit[ii] = 0x01; // If temperature compensation bit is set, record that fact in mask_bit
onehorse0:39935bb3c1a1 560 }
onehorse0:39935bb3c1a1 561
onehorse0:39935bb3c1a1 562 // Construct total accelerometer bias, including calculated average accelerometer bias from above
onehorse0:39935bb3c1a1 563 accel_bias_reg[0] -= (accel_bias[0]/8); // Subtract calculated averaged accelerometer bias scaled to 2048 LSB/g (16 g full scale)
onehorse0:39935bb3c1a1 564 accel_bias_reg[1] -= (accel_bias[1]/8);
onehorse0:39935bb3c1a1 565 accel_bias_reg[2] -= (accel_bias[2]/8);
onehorse0:39935bb3c1a1 566
onehorse0:39935bb3c1a1 567 data[0] = (accel_bias_reg[0] >> 8) & 0xFF;
onehorse0:39935bb3c1a1 568 data[1] = (accel_bias_reg[0]) & 0xFF;
onehorse0:39935bb3c1a1 569 data[1] = data[1] | mask_bit[0]; // preserve temperature compensation bit when writing back to accelerometer bias registers
onehorse0:39935bb3c1a1 570 data[2] = (accel_bias_reg[1] >> 8) & 0xFF;
onehorse0:39935bb3c1a1 571 data[3] = (accel_bias_reg[1]) & 0xFF;
onehorse0:39935bb3c1a1 572 data[3] = data[3] | mask_bit[1]; // preserve temperature compensation bit when writing back to accelerometer bias registers
onehorse0:39935bb3c1a1 573 data[4] = (accel_bias_reg[2] >> 8) & 0xFF;
onehorse0:39935bb3c1a1 574 data[5] = (accel_bias_reg[2]) & 0xFF;
onehorse0:39935bb3c1a1 575 data[5] = data[5] | mask_bit[2]; // preserve temperature compensation bit when writing back to accelerometer bias registers
onehorse0:39935bb3c1a1 576
onehorse0:39935bb3c1a1 577// Apparently this is not working for the acceleration biases in the MPU-9250
onehorse0:39935bb3c1a1 578// Are we handling the temperature correction bit properly?
onehorse0:39935bb3c1a1 579// Push accelerometer biases to hardware registers
onehorse0:39935bb3c1a1 580 writeByte(MPU9150_ADDRESS, XA_OFFSET_H, data[0]);
onehorse0:39935bb3c1a1 581 writeByte(MPU9150_ADDRESS, XA_OFFSET_L_TC, data[1]);
onehorse0:39935bb3c1a1 582 writeByte(MPU9150_ADDRESS, YA_OFFSET_H, data[2]);
onehorse0:39935bb3c1a1 583 writeByte(MPU9150_ADDRESS, YA_OFFSET_L_TC, data[3]);
onehorse0:39935bb3c1a1 584 writeByte(MPU9150_ADDRESS, ZA_OFFSET_H, data[4]);
onehorse0:39935bb3c1a1 585 writeByte(MPU9150_ADDRESS, ZA_OFFSET_L_TC, data[5]);
onehorse0:39935bb3c1a1 586
onehorse0:39935bb3c1a1 587// Output scaled accelerometer biases for manual subtraction in the main program
onehorse0:39935bb3c1a1 588 dest2[0] = (float)accel_bias[0]/(float)accelsensitivity;
onehorse0:39935bb3c1a1 589 dest2[1] = (float)accel_bias[1]/(float)accelsensitivity;
onehorse0:39935bb3c1a1 590 dest2[2] = (float)accel_bias[2]/(float)accelsensitivity;
onehorse0:39935bb3c1a1 591}
onehorse0:39935bb3c1a1 592
onehorse0:39935bb3c1a1 593
onehorse0:39935bb3c1a1 594// Accelerometer and gyroscope self test; check calibration wrt factory settings
onehorse0:39935bb3c1a1 595void MPU9150SelfTest(float * destination) // Should return percent deviation from factory trim values, +/- 14 or less deviation is a pass
onehorse0:39935bb3c1a1 596{
onehorse0:39935bb3c1a1 597 uint8_t rawData[4] = {0, 0, 0, 0};
onehorse0:39935bb3c1a1 598 uint8_t selfTest[6];
onehorse0:39935bb3c1a1 599 float factoryTrim[6];
onehorse0:39935bb3c1a1 600
onehorse0:39935bb3c1a1 601 // Configure the accelerometer for self-test
onehorse0:39935bb3c1a1 602 writeByte(MPU9150_ADDRESS, ACCEL_CONFIG, 0xF0); // Enable self test on all three axes and set accelerometer range to +/- 8 g
onehorse0:39935bb3c1a1 603 writeByte(MPU9150_ADDRESS, GYRO_CONFIG, 0xE0); // Enable self test on all three axes and set gyro range to +/- 250 degrees/s
onehorse0:39935bb3c1a1 604 wait(0.25); // Delay a while to let the device execute the self-test
onehorse0:39935bb3c1a1 605 rawData[0] = readByte(MPU9150_ADDRESS, SELF_TEST_X); // X-axis self-test results
onehorse0:39935bb3c1a1 606 rawData[1] = readByte(MPU9150_ADDRESS, SELF_TEST_Y); // Y-axis self-test results
onehorse0:39935bb3c1a1 607 rawData[2] = readByte(MPU9150_ADDRESS, SELF_TEST_Z); // Z-axis self-test results
onehorse0:39935bb3c1a1 608 rawData[3] = readByte(MPU9150_ADDRESS, SELF_TEST_A); // Mixed-axis self-test results
onehorse0:39935bb3c1a1 609 // Extract the acceleration test results first
onehorse0:39935bb3c1a1 610 selfTest[0] = (rawData[0] >> 3) | (rawData[3] & 0x30) >> 4 ; // XA_TEST result is a five-bit unsigned integer
onehorse0:39935bb3c1a1 611 selfTest[1] = (rawData[1] >> 3) | (rawData[3] & 0x0C) >> 4 ; // YA_TEST result is a five-bit unsigned integer
onehorse0:39935bb3c1a1 612 selfTest[2] = (rawData[2] >> 3) | (rawData[3] & 0x03) >> 4 ; // ZA_TEST result is a five-bit unsigned integer
onehorse0:39935bb3c1a1 613 // Extract the gyration test results first
onehorse0:39935bb3c1a1 614 selfTest[3] = rawData[0] & 0x1F ; // XG_TEST result is a five-bit unsigned integer
onehorse0:39935bb3c1a1 615 selfTest[4] = rawData[1] & 0x1F ; // YG_TEST result is a five-bit unsigned integer
onehorse0:39935bb3c1a1 616 selfTest[5] = rawData[2] & 0x1F ; // ZG_TEST result is a five-bit unsigned integer
onehorse0:39935bb3c1a1 617 // Process results to allow final comparison with factory set values
onehorse0:39935bb3c1a1 618 factoryTrim[0] = (4096.0f*0.34f)*(pow( (0.92f/0.34f) , ((selfTest[0] - 1.0f)/30.0f))); // FT[Xa] factory trim calculation
onehorse0:39935bb3c1a1 619 factoryTrim[1] = (4096.0f*0.34f)*(pow( (0.92f/0.34f) , ((selfTest[1] - 1.0f)/30.0f))); // FT[Ya] factory trim calculation
onehorse0:39935bb3c1a1 620 factoryTrim[2] = (4096.0f*0.34f)*(pow( (0.92f/0.34f) , ((selfTest[2] - 1.0f)/30.0f))); // FT[Za] factory trim calculation
onehorse0:39935bb3c1a1 621 factoryTrim[3] = ( 25.0f*131.0f)*(pow( 1.046f , (selfTest[3] - 1.0f) )); // FT[Xg] factory trim calculation
onehorse0:39935bb3c1a1 622 factoryTrim[4] = (-25.0f*131.0f)*(pow( 1.046f , (selfTest[4] - 1.0f) )); // FT[Yg] factory trim calculation
onehorse0:39935bb3c1a1 623 factoryTrim[5] = ( 25.0f*131.0f)*(pow( 1.046f , (selfTest[5] - 1.0f) )); // FT[Zg] factory trim calculation
onehorse0:39935bb3c1a1 624
onehorse0:39935bb3c1a1 625 // Output self-test results and factory trim calculation if desired
onehorse0:39935bb3c1a1 626 // Serial.println(selfTest[0]); Serial.println(selfTest[1]); Serial.println(selfTest[2]);
onehorse0:39935bb3c1a1 627 // Serial.println(selfTest[3]); Serial.println(selfTest[4]); Serial.println(selfTest[5]);
onehorse0:39935bb3c1a1 628 // Serial.println(factoryTrim[0]); Serial.println(factoryTrim[1]); Serial.println(factoryTrim[2]);
onehorse0:39935bb3c1a1 629 // Serial.println(factoryTrim[3]); Serial.println(factoryTrim[4]); Serial.println(factoryTrim[5]);
onehorse0:39935bb3c1a1 630
onehorse0:39935bb3c1a1 631 // Report results as a ratio of (STR - FT)/FT; the change from Factory Trim of the Self-Test Response
onehorse0:39935bb3c1a1 632 // To get to percent, must multiply by 100 and subtract result from 100
onehorse0:39935bb3c1a1 633 for (int i = 0; i < 6; i++) {
onehorse0:39935bb3c1a1 634 destination[i] = 100.0f + 100.0f*(selfTest[i] - factoryTrim[i])/factoryTrim[i]; // Report percent differences
onehorse0:39935bb3c1a1 635 }
onehorse0:39935bb3c1a1 636
onehorse0:39935bb3c1a1 637}
onehorse0:39935bb3c1a1 638
onehorse0:39935bb3c1a1 639
onehorse0:39935bb3c1a1 640
onehorse0:39935bb3c1a1 641// Implementation of Sebastian Madgwick's "...efficient orientation filter for... inertial/magnetic sensor arrays"
onehorse0:39935bb3c1a1 642// (see http://www.x-io.co.uk/category/open-source/ for examples and more details)
onehorse0:39935bb3c1a1 643// which fuses acceleration, rotation rate, and magnetic moments to produce a quaternion-based estimate of absolute
onehorse0:39935bb3c1a1 644// device orientation -- which can be converted to yaw, pitch, and roll. Useful for stabilizing quadcopters, etc.
onehorse0:39935bb3c1a1 645// The performance of the orientation filter is at least as good as conventional Kalman-based filtering algorithms
onehorse0:39935bb3c1a1 646// but is much less computationally intensive---it can be performed on a 3.3 V Pro Mini operating at 8 MHz!
onehorse0:39935bb3c1a1 647 void MadgwickQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz)
onehorse0:39935bb3c1a1 648 {
onehorse0:39935bb3c1a1 649 float q1 = q[0], q2 = q[1], q3 = q[2], q4 = q[3]; // short name local variable for readability
onehorse0:39935bb3c1a1 650 float norm;
onehorse0:39935bb3c1a1 651 float hx, hy, _2bx, _2bz;
onehorse0:39935bb3c1a1 652 float s1, s2, s3, s4;
onehorse0:39935bb3c1a1 653 float qDot1, qDot2, qDot3, qDot4;
onehorse0:39935bb3c1a1 654
onehorse0:39935bb3c1a1 655 // Auxiliary variables to avoid repeated arithmetic
onehorse0:39935bb3c1a1 656 float _2q1mx;
onehorse0:39935bb3c1a1 657 float _2q1my;
onehorse0:39935bb3c1a1 658 float _2q1mz;
onehorse0:39935bb3c1a1 659 float _2q2mx;
onehorse0:39935bb3c1a1 660 float _4bx;
onehorse0:39935bb3c1a1 661 float _4bz;
onehorse0:39935bb3c1a1 662 float _2q1 = 2.0f * q1;
onehorse0:39935bb3c1a1 663 float _2q2 = 2.0f * q2;
onehorse0:39935bb3c1a1 664 float _2q3 = 2.0f * q3;
onehorse0:39935bb3c1a1 665 float _2q4 = 2.0f * q4;
onehorse0:39935bb3c1a1 666 float _2q1q3 = 2.0f * q1 * q3;
onehorse0:39935bb3c1a1 667 float _2q3q4 = 2.0f * q3 * q4;
onehorse0:39935bb3c1a1 668 float q1q1 = q1 * q1;
onehorse0:39935bb3c1a1 669 float q1q2 = q1 * q2;
onehorse0:39935bb3c1a1 670 float q1q3 = q1 * q3;
onehorse0:39935bb3c1a1 671 float q1q4 = q1 * q4;
onehorse0:39935bb3c1a1 672 float q2q2 = q2 * q2;
onehorse0:39935bb3c1a1 673 float q2q3 = q2 * q3;
onehorse0:39935bb3c1a1 674 float q2q4 = q2 * q4;
onehorse0:39935bb3c1a1 675 float q3q3 = q3 * q3;
onehorse0:39935bb3c1a1 676 float q3q4 = q3 * q4;
onehorse0:39935bb3c1a1 677 float q4q4 = q4 * q4;
onehorse0:39935bb3c1a1 678
onehorse0:39935bb3c1a1 679 // Normalise accelerometer measurement
onehorse0:39935bb3c1a1 680 norm = sqrt(ax * ax + ay * ay + az * az);
onehorse0:39935bb3c1a1 681 if (norm == 0.0f) return; // handle NaN
onehorse0:39935bb3c1a1 682 norm = 1.0f/norm;
onehorse0:39935bb3c1a1 683 ax *= norm;
onehorse0:39935bb3c1a1 684 ay *= norm;
onehorse0:39935bb3c1a1 685 az *= norm;
onehorse0:39935bb3c1a1 686
onehorse0:39935bb3c1a1 687 // Normalise magnetometer measurement
onehorse0:39935bb3c1a1 688 norm = sqrt(mx * mx + my * my + mz * mz);
onehorse0:39935bb3c1a1 689 if (norm == 0.0f) return; // handle NaN
onehorse0:39935bb3c1a1 690 norm = 1.0f/norm;
onehorse0:39935bb3c1a1 691 mx *= norm;
onehorse0:39935bb3c1a1 692 my *= norm;
onehorse0:39935bb3c1a1 693 mz *= norm;
onehorse0:39935bb3c1a1 694
onehorse0:39935bb3c1a1 695 // Reference direction of Earth's magnetic field
onehorse0:39935bb3c1a1 696 _2q1mx = 2.0f * q1 * mx;
onehorse0:39935bb3c1a1 697 _2q1my = 2.0f * q1 * my;
onehorse0:39935bb3c1a1 698 _2q1mz = 2.0f * q1 * mz;
onehorse0:39935bb3c1a1 699 _2q2mx = 2.0f * q2 * mx;
onehorse0:39935bb3c1a1 700 hx = mx * q1q1 - _2q1my * q4 + _2q1mz * q3 + mx * q2q2 + _2q2 * my * q3 + _2q2 * mz * q4 - mx * q3q3 - mx * q4q4;
onehorse0:39935bb3c1a1 701 hy = _2q1mx * q4 + my * q1q1 - _2q1mz * q2 + _2q2mx * q3 - my * q2q2 + my * q3q3 + _2q3 * mz * q4 - my * q4q4;
onehorse0:39935bb3c1a1 702 _2bx = sqrt(hx * hx + hy * hy);
onehorse0:39935bb3c1a1 703 _2bz = -_2q1mx * q3 + _2q1my * q2 + mz * q1q1 + _2q2mx * q4 - mz * q2q2 + _2q3 * my * q4 - mz * q3q3 + mz * q4q4;
onehorse0:39935bb3c1a1 704 _4bx = 2.0f * _2bx;
onehorse0:39935bb3c1a1 705 _4bz = 2.0f * _2bz;
onehorse0:39935bb3c1a1 706
onehorse0:39935bb3c1a1 707 // Gradient decent algorithm corrective step
onehorse0:39935bb3c1a1 708 s1 = -_2q3 * (2.0f * q2q4 - _2q1q3 - ax) + _2q2 * (2.0f * q1q2 + _2q3q4 - ay) - _2bz * q3 * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (-_2bx * q4 + _2bz * q2) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + _2bx * q3 * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
onehorse0:39935bb3c1a1 709 s2 = _2q4 * (2.0f * q2q4 - _2q1q3 - ax) + _2q1 * (2.0f * q1q2 + _2q3q4 - ay) - 4.0f * q2 * (1.0f - 2.0f * q2q2 - 2.0f * q3q3 - az) + _2bz * q4 * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (_2bx * q3 + _2bz * q1) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + (_2bx * q4 - _4bz * q2) * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
onehorse0:39935bb3c1a1 710 s3 = -_2q1 * (2.0f * q2q4 - _2q1q3 - ax) + _2q4 * (2.0f * q1q2 + _2q3q4 - ay) - 4.0f * q3 * (1.0f - 2.0f * q2q2 - 2.0f * q3q3 - az) + (-_4bx * q3 - _2bz * q1) * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (_2bx * q2 + _2bz * q4) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + (_2bx * q1 - _4bz * q3) * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
onehorse0:39935bb3c1a1 711 s4 = _2q2 * (2.0f * q2q4 - _2q1q3 - ax) + _2q3 * (2.0f * q1q2 + _2q3q4 - ay) + (-_4bx * q4 + _2bz * q2) * (_2bx * (0.5f - q3q3 - q4q4) + _2bz * (q2q4 - q1q3) - mx) + (-_2bx * q1 + _2bz * q3) * (_2bx * (q2q3 - q1q4) + _2bz * (q1q2 + q3q4) - my) + _2bx * q2 * (_2bx * (q1q3 + q2q4) + _2bz * (0.5f - q2q2 - q3q3) - mz);
onehorse0:39935bb3c1a1 712 norm = sqrt(s1 * s1 + s2 * s2 + s3 * s3 + s4 * s4); // normalise step magnitude
onehorse0:39935bb3c1a1 713 norm = 1.0f/norm;
onehorse0:39935bb3c1a1 714 s1 *= norm;
onehorse0:39935bb3c1a1 715 s2 *= norm;
onehorse0:39935bb3c1a1 716 s3 *= norm;
onehorse0:39935bb3c1a1 717 s4 *= norm;
onehorse0:39935bb3c1a1 718
onehorse0:39935bb3c1a1 719 // Compute rate of change of quaternion
onehorse0:39935bb3c1a1 720 qDot1 = 0.5f * (-q2 * gx - q3 * gy - q4 * gz) - beta * s1;
onehorse0:39935bb3c1a1 721 qDot2 = 0.5f * (q1 * gx + q3 * gz - q4 * gy) - beta * s2;
onehorse0:39935bb3c1a1 722 qDot3 = 0.5f * (q1 * gy - q2 * gz + q4 * gx) - beta * s3;
onehorse0:39935bb3c1a1 723 qDot4 = 0.5f * (q1 * gz + q2 * gy - q3 * gx) - beta * s4;
onehorse0:39935bb3c1a1 724
onehorse0:39935bb3c1a1 725 // Integrate to yield quaternion
onehorse0:39935bb3c1a1 726 q1 += qDot1 * deltat;
onehorse0:39935bb3c1a1 727 q2 += qDot2 * deltat;
onehorse0:39935bb3c1a1 728 q3 += qDot3 * deltat;
onehorse0:39935bb3c1a1 729 q4 += qDot4 * deltat;
onehorse0:39935bb3c1a1 730 norm = sqrt(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4); // normalise quaternion
onehorse0:39935bb3c1a1 731 norm = 1.0f/norm;
onehorse0:39935bb3c1a1 732 q[0] = q1 * norm;
onehorse0:39935bb3c1a1 733 q[1] = q2 * norm;
onehorse0:39935bb3c1a1 734 q[2] = q3 * norm;
onehorse0:39935bb3c1a1 735 q[3] = q4 * norm;
onehorse0:39935bb3c1a1 736
onehorse0:39935bb3c1a1 737 }
onehorse0:39935bb3c1a1 738
onehorse0:39935bb3c1a1 739
onehorse0:39935bb3c1a1 740
onehorse0:39935bb3c1a1 741 // Similar to Madgwick scheme but uses proportional and integral filtering on the error between estimated reference vectors and
onehorse0:39935bb3c1a1 742 // measured ones.
onehorse0:39935bb3c1a1 743 void MahonyQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz)
onehorse0:39935bb3c1a1 744 {
onehorse0:39935bb3c1a1 745 float q1 = q[0], q2 = q[1], q3 = q[2], q4 = q[3]; // short name local variable for readability
onehorse0:39935bb3c1a1 746 float norm;
onehorse0:39935bb3c1a1 747 float hx, hy, bx, bz;
onehorse0:39935bb3c1a1 748 float vx, vy, vz, wx, wy, wz;
onehorse0:39935bb3c1a1 749 float ex, ey, ez;
onehorse0:39935bb3c1a1 750 float pa, pb, pc;
onehorse0:39935bb3c1a1 751
onehorse0:39935bb3c1a1 752 // Auxiliary variables to avoid repeated arithmetic
onehorse0:39935bb3c1a1 753 float q1q1 = q1 * q1;
onehorse0:39935bb3c1a1 754 float q1q2 = q1 * q2;
onehorse0:39935bb3c1a1 755 float q1q3 = q1 * q3;
onehorse0:39935bb3c1a1 756 float q1q4 = q1 * q4;
onehorse0:39935bb3c1a1 757 float q2q2 = q2 * q2;
onehorse0:39935bb3c1a1 758 float q2q3 = q2 * q3;
onehorse0:39935bb3c1a1 759 float q2q4 = q2 * q4;
onehorse0:39935bb3c1a1 760 float q3q3 = q3 * q3;
onehorse0:39935bb3c1a1 761 float q3q4 = q3 * q4;
onehorse0:39935bb3c1a1 762 float q4q4 = q4 * q4;
onehorse0:39935bb3c1a1 763
onehorse0:39935bb3c1a1 764 // Normalise accelerometer measurement
onehorse0:39935bb3c1a1 765 norm = sqrt(ax * ax + ay * ay + az * az);
onehorse0:39935bb3c1a1 766 if (norm == 0.0f) return; // handle NaN
onehorse0:39935bb3c1a1 767 norm = 1.0f / norm; // use reciprocal for division
onehorse0:39935bb3c1a1 768 ax *= norm;
onehorse0:39935bb3c1a1 769 ay *= norm;
onehorse0:39935bb3c1a1 770 az *= norm;
onehorse0:39935bb3c1a1 771
onehorse0:39935bb3c1a1 772 // Normalise magnetometer measurement
onehorse0:39935bb3c1a1 773 norm = sqrt(mx * mx + my * my + mz * mz);
onehorse0:39935bb3c1a1 774 if (norm == 0.0f) return; // handle NaN
onehorse0:39935bb3c1a1 775 norm = 1.0f / norm; // use reciprocal for division
onehorse0:39935bb3c1a1 776 mx *= norm;
onehorse0:39935bb3c1a1 777 my *= norm;
onehorse0:39935bb3c1a1 778 mz *= norm;
onehorse0:39935bb3c1a1 779
onehorse0:39935bb3c1a1 780 // Reference direction of Earth's magnetic field
onehorse0:39935bb3c1a1 781 hx = 2.0f * mx * (0.5f - q3q3 - q4q4) + 2.0f * my * (q2q3 - q1q4) + 2.0f * mz * (q2q4 + q1q3);
onehorse0:39935bb3c1a1 782 hy = 2.0f * mx * (q2q3 + q1q4) + 2.0f * my * (0.5f - q2q2 - q4q4) + 2.0f * mz * (q3q4 - q1q2);
onehorse0:39935bb3c1a1 783 bx = sqrt((hx * hx) + (hy * hy));
onehorse0:39935bb3c1a1 784 bz = 2.0f * mx * (q2q4 - q1q3) + 2.0f * my * (q3q4 + q1q2) + 2.0f * mz * (0.5f - q2q2 - q3q3);
onehorse0:39935bb3c1a1 785
onehorse0:39935bb3c1a1 786 // Estimated direction of gravity and magnetic field
onehorse0:39935bb3c1a1 787 vx = 2.0f * (q2q4 - q1q3);
onehorse0:39935bb3c1a1 788 vy = 2.0f * (q1q2 + q3q4);
onehorse0:39935bb3c1a1 789 vz = q1q1 - q2q2 - q3q3 + q4q4;
onehorse0:39935bb3c1a1 790 wx = 2.0f * bx * (0.5f - q3q3 - q4q4) + 2.0f * bz * (q2q4 - q1q3);
onehorse0:39935bb3c1a1 791 wy = 2.0f * bx * (q2q3 - q1q4) + 2.0f * bz * (q1q2 + q3q4);
onehorse0:39935bb3c1a1 792 wz = 2.0f * bx * (q1q3 + q2q4) + 2.0f * bz * (0.5f - q2q2 - q3q3);
onehorse0:39935bb3c1a1 793
onehorse0:39935bb3c1a1 794 // Error is cross product between estimated direction and measured direction of gravity
onehorse0:39935bb3c1a1 795 ex = (ay * vz - az * vy) + (my * wz - mz * wy);
onehorse0:39935bb3c1a1 796 ey = (az * vx - ax * vz) + (mz * wx - mx * wz);
onehorse0:39935bb3c1a1 797 ez = (ax * vy - ay * vx) + (mx * wy - my * wx);
onehorse0:39935bb3c1a1 798 if (Ki > 0.0f)
onehorse0:39935bb3c1a1 799 {
onehorse0:39935bb3c1a1 800 eInt[0] += ex; // accumulate integral error
onehorse0:39935bb3c1a1 801 eInt[1] += ey;
onehorse0:39935bb3c1a1 802 eInt[2] += ez;
onehorse0:39935bb3c1a1 803 }
onehorse0:39935bb3c1a1 804 else
onehorse0:39935bb3c1a1 805 {
onehorse0:39935bb3c1a1 806 eInt[0] = 0.0f; // prevent integral wind up
onehorse0:39935bb3c1a1 807 eInt[1] = 0.0f;
onehorse0:39935bb3c1a1 808 eInt[2] = 0.0f;
onehorse0:39935bb3c1a1 809 }
onehorse0:39935bb3c1a1 810
onehorse0:39935bb3c1a1 811 // Apply feedback terms
onehorse0:39935bb3c1a1 812 gx = gx + Kp * ex + Ki * eInt[0];
onehorse0:39935bb3c1a1 813 gy = gy + Kp * ey + Ki * eInt[1];
onehorse0:39935bb3c1a1 814 gz = gz + Kp * ez + Ki * eInt[2];
onehorse0:39935bb3c1a1 815
onehorse0:39935bb3c1a1 816 // Integrate rate of change of quaternion
onehorse0:39935bb3c1a1 817 pa = q2;
onehorse0:39935bb3c1a1 818 pb = q3;
onehorse0:39935bb3c1a1 819 pc = q4;
onehorse0:39935bb3c1a1 820 q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * deltat);
onehorse0:39935bb3c1a1 821 q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * deltat);
onehorse0:39935bb3c1a1 822 q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * deltat);
onehorse0:39935bb3c1a1 823 q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * deltat);
onehorse0:39935bb3c1a1 824
onehorse0:39935bb3c1a1 825 // Normalise quaternion
onehorse0:39935bb3c1a1 826 norm = sqrt(q1 * q1 + q2 * q2 + q3 * q3 + q4 * q4);
onehorse0:39935bb3c1a1 827 norm = 1.0f / norm;
onehorse0:39935bb3c1a1 828 q[0] = q1 * norm;
onehorse0:39935bb3c1a1 829 q[1] = q2 * norm;
onehorse0:39935bb3c1a1 830 q[2] = q3 * norm;
onehorse0:39935bb3c1a1 831 q[3] = q4 * norm;
onehorse0:39935bb3c1a1 832
onehorse0:39935bb3c1a1 833 }
onehorse0:39935bb3c1a1 834 };
onehorse0:39935bb3c1a1 835#endif