Ported from Arduino Library by Szymon Gaertig: https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050 added Magnetometer support
Fork of MPU6050 by
Revision 2:f8bfb37b2e1f, committed 2013-05-12
- Comitter:
- akode
- Date:
- Sun May 12 10:54:50 2013 +0000
- Parent:
- 1:1e0baaf91e96
- Commit message:
- Added magnetometer support, fixed some compiler warnings
Changed in this revision
diff -r 1e0baaf91e96 -r f8bfb37b2e1f I2Cdev.cpp --- a/I2Cdev.cpp Wed May 08 00:34:55 2013 +0000 +++ b/I2Cdev.cpp Sun May 12 10:54:50 2013 +0000 @@ -7,12 +7,12 @@ #define useDebugSerial -I2Cdev::I2Cdev(): debugSerial(USBTX, USBRX), i2c(I2C_SDA,I2C_SCL) +I2Cdev::I2Cdev(): i2c(I2C_SDA,I2C_SCL), debugSerial(USBTX, USBRX) { } -I2Cdev::I2Cdev(PinName i2cSda, PinName i2cScl): debugSerial(USBTX, USBRX), i2c(i2cSda,i2cScl) +I2Cdev::I2Cdev(PinName i2cSda, PinName i2cScl): i2c(i2cSda,i2cScl), debugSerial(USBTX, USBRX) { }
diff -r 1e0baaf91e96 -r f8bfb37b2e1f MPU6050.cpp --- a/MPU6050.cpp Wed May 08 00:34:55 2013 +0000 +++ b/MPU6050.cpp Sun May 12 10:54:50 2013 +0000 @@ -1882,7 +1882,16 @@ void MPU6050::getMotion9(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz, int16_t* mx, int16_t* my, int16_t* mz) { getMotion6(ax, ay, az, gx, gy, gz); - // TODO: magnetometer integration + + // magnetometer reading + i2Cdev.writeByte(devAddr, MPU6050_RA_INT_PIN_CFG, 0x02); //set i2c bypass enable pin to true to access magnetometer + wait_ms(10); // necessary wait >=6ms + i2Cdev.writeByte(MPU9150_RA_MAG_ADDRESS, 0x0A, 0x01); // enable the magnetometer + wait_ms(10); // necessary wait >=6ms + i2Cdev.readBytes(MPU9150_RA_MAG_ADDRESS, MPU9150_RA_MAG_XOUT_L, 6, buffer); + *mx = (((int16_t)buffer[0]) << 8) | buffer[1]; + *my = (((int16_t)buffer[2]) << 8) | buffer[3]; + *mz = (((int16_t)buffer[4]) << 8) | buffer[5]; } /** Get raw 6-axis motion sensor readings (accel/gyro). * Retrieves all currently available motion sensor values. @@ -3250,8 +3259,8 @@ setMemoryBank(bank); setMemoryStartAddress(address); uint8_t chunkSize; - uint8_t *verifyBuffer; - uint8_t *progBuffer; + uint8_t *verifyBuffer = NULL; + uint8_t *progBuffer = NULL; uint16_t i; uint8_t j; if (verify) verifyBuffer = (uint8_t *)malloc(MPU6050_DMP_MEMORY_CHUNK_SIZE); @@ -3328,7 +3337,8 @@ } bool MPU6050::writeDMPConfigurationSet(const uint8_t *data, uint16_t dataSize, bool useProgMem) { - uint8_t *progBuffer, success, special; + uint8_t success, special; + uint8_t *progBuffer = NULL; uint16_t i, j; if (useProgMem) { progBuffer = (uint8_t *)malloc(8); // assume 8-byte blocks, realloc later if necessary
diff -r 1e0baaf91e96 -r f8bfb37b2e1f MPU6050.h --- a/MPU6050.h Wed May 08 00:34:55 2013 +0000 +++ b/MPU6050.h Sun May 12 10:54:50 2013 +0000 @@ -102,20 +102,32 @@ #define MPU6050_RA_INT_ENABLE 0x38 #define MPU6050_RA_DMP_INT_STATUS 0x39 #define MPU6050_RA_INT_STATUS 0x3A + #define MPU6050_RA_ACCEL_XOUT_H 0x3B #define MPU6050_RA_ACCEL_XOUT_L 0x3C #define MPU6050_RA_ACCEL_YOUT_H 0x3D #define MPU6050_RA_ACCEL_YOUT_L 0x3E #define MPU6050_RA_ACCEL_ZOUT_H 0x3F #define MPU6050_RA_ACCEL_ZOUT_L 0x40 + #define MPU6050_RA_TEMP_OUT_H 0x41 #define MPU6050_RA_TEMP_OUT_L 0x42 + #define MPU6050_RA_GYRO_XOUT_H 0x43 #define MPU6050_RA_GYRO_XOUT_L 0x44 #define MPU6050_RA_GYRO_YOUT_H 0x45 #define MPU6050_RA_GYRO_YOUT_L 0x46 #define MPU6050_RA_GYRO_ZOUT_H 0x47 #define MPU6050_RA_GYRO_ZOUT_L 0x48 + +#define MPU9150_RA_MAG_ADDRESS 0x0C +#define MPU9150_RA_MAG_XOUT_L 0x03 +#define MPU9150_RA_MAG_XOUT_H 0x04 +#define MPU9150_RA_MAG_YOUT_L 0x05 +#define MPU9150_RA_MAG_YOUT_H 0x06 +#define MPU9150_RA_MAG_ZOUT_L 0x07 +#define MPU9150_RA_MAG_ZOUT_H 0x08 + #define MPU6050_RA_EXT_SENS_DATA_00 0x49 #define MPU6050_RA_EXT_SENS_DATA_01 0x4A #define MPU6050_RA_EXT_SENS_DATA_02 0x4B