imu for l432kc

Dependencies:   mbed

Committer:
sunninety1
Date:
Sat Dec 08 05:48:54 2018 +0000
Revision:
4:7b04752df27f
Parent:
3:4a1bc31c360f
for l432kc;

Who changed what in which revision?

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