GPS1_kevin projet c MPU9250

Dependencies:   mbed ST_401_84MHZ

Committer:
kekette
Date:
Thu Dec 12 15:34:50 2019 +0000
Revision:
0:507d1a0c6655
test

Who changed what in which revision?

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