3 axis gyroscope & 3 axis accelerometer
Dependents: sgam-lib sgam_mdw_NUCLEOF429ZI_impl
Fork of MPU6050 by
Usage
#include "mbed.h" #include "MPU6050.h" DigitalOut myled(LED1); Serial pc(USBTX, USBRX); I2C i2c(I2C_SDA, I2C_SCL); MPU6050 mpu(i2c); int16_t ax, ay, az; int16_t gx, gy, gz; int main() { pc.printf("MPU6050 test\n\n"); pc.printf("MPU6050 initialize \n"); mpu.initialize(); pc.printf("MPU6050 testConnection \n"); bool mpu6050TestResult = mpu.testConnection(); if(mpu6050TestResult) { pc.printf("MPU6050 test passed \n"); } else { pc.printf("MPU6050 test failed \n"); } while(1) { wait(1); mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); //writing current accelerometer and gyro position pc.printf("%d;%d;%d;%d;%d;%d\n",ax,ay,az,gx,gy,gz); } }
Revision 2:8c562a8fed36, committed 2014-09-12
- Comitter:
- Yihui Xiong
- Date:
- Fri Sep 12 09:28:14 2014 +0800
- Parent:
- 1:1e0baaf91e96
- Commit message:
- update
Changed in this revision
diff -r 1e0baaf91e96 -r 8c562a8fed36 I2Cdev.cpp --- a/I2Cdev.cpp Wed May 08 00:34:55 2013 +0000 +++ b/I2Cdev.cpp Fri Sep 12 09:28:14 2014 +0800 @@ -5,14 +5,8 @@ #include "I2Cdev.h" -#define useDebugSerial -I2Cdev::I2Cdev(): debugSerial(USBTX, USBRX), i2c(I2C_SDA,I2C_SCL) -{ - -} - -I2Cdev::I2Cdev(PinName i2cSda, PinName i2cScl): debugSerial(USBTX, USBRX), i2c(i2cSda,i2cScl) +I2Cdev::I2Cdev(I2C &interface) : i2c(interface) { } @@ -254,18 +248,31 @@ bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data) { - i2c.start(); - i2c.write(devAddr<<1); - i2c.write(regAddr); - for(int i = 0; i < length; i++) { - i2c.write(data[i]); + int len = length + 1; + char *buf = (char*)malloc(len); + buf[0] = regAddr; + for (int i = 0; i < length; i++) { + buf[i + 1] = data[i]; } - i2c.stop(); + i2c.write(devAddr << 1 , buf, len); + + free (buf); return true; } bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data) { + int len = length * 2 + 1; + char *buf = (char*)malloc(len); + buf[0] = regAddr; + for (int i = 0; i < length; i++) { + uint16_t word = data[i]; + buf[2*i + 1] = word >> 8; + buf[2*i + 2] = word & 0xFF; + } + i2c.write(devAddr << 1 , buf, len); + + free (buf); return true; }
diff -r 1e0baaf91e96 -r 8c562a8fed36 I2Cdev.h --- a/I2Cdev.h Wed May 08 00:34:55 2013 +0000 +++ b/I2Cdev.h Fri Sep 12 09:28:14 2014 +0800 @@ -9,16 +9,12 @@ #include "mbed.h" -#define I2C_SDA p28 -#define I2C_SCL p27 class I2Cdev { private: I2C i2c; - Serial debugSerial; public: - I2Cdev(); - I2Cdev(PinName i2cSda, PinName i2cScl); + I2Cdev(I2C &interface); int8_t readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout()); int8_t readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout()); @@ -41,4 +37,4 @@ static uint16_t readTimeout(void); }; -#endif \ No newline at end of file +#endif
diff -r 1e0baaf91e96 -r 8c562a8fed36 MPU6050.cpp --- a/MPU6050.cpp Wed May 08 00:34:55 2013 +0000 +++ b/MPU6050.cpp Fri Sep 12 09:28:14 2014 +0800 @@ -42,28 +42,20 @@ #include "MPU6050.h" -#define useDebugSerial +#define LOG(args...) // printf(args) //instead of using pgmspace.h typedef const unsigned char prog_uchar; #define pgm_read_byte_near(x) (*(prog_uchar*)x) #define pgm_read_byte(x) (*(prog_uchar*)x) -/** Default constructor, uses default I2C address. - * @see MPU6050_DEFAULT_ADDRESS - */ -MPU6050::MPU6050() : debugSerial(USBTX, USBRX) -{ - devAddr = MPU6050_DEFAULT_ADDRESS; -} - /** Specific address constructor. * @param address I2C address * @see MPU6050_DEFAULT_ADDRESS * @see MPU6050_ADDRESS_AD0_LOW * @see MPU6050_ADDRESS_AD0_HIGH */ -MPU6050::MPU6050(uint8_t address) : debugSerial(USBTX, USBRX) +MPU6050::MPU6050(I2C &i2c, uint8_t address) : i2Cdev(i2c) { devAddr = address; } @@ -77,19 +69,14 @@ */ void MPU6050::initialize() { - -#ifdef useDebugSerial - debugSerial.printf("MPU6050::initialize start\n"); -#endif + LOG("MPU6050::initialize start\n"); setClockSource(MPU6050_CLOCK_PLL_XGYRO); setFullScaleGyroRange(MPU6050_GYRO_FS_250); setFullScaleAccelRange(MPU6050_ACCEL_FS_2); setSleepEnabled(false); // thanks to Jack Elston for pointing this one out! -#ifdef useDebugSerial - debugSerial.printf("MPU6050::initialize end\n"); -#endif + LOG("MPU6050::initialize end\n"); } /** Verify the I2C connection. @@ -98,13 +85,12 @@ */ bool MPU6050::testConnection() { -#ifdef useDebugSerial - debugSerial.printf("MPU6050::testConnection start\n"); -#endif + LOG("MPU6050::testConnection start\n"); + uint8_t deviceId = getDeviceID(); -#ifdef useDebugSerial - debugSerial.printf("DeviceId = %d\n",deviceId); -#endif + + LOG("DeviceId = %d\n",deviceId); + return deviceId == 0x34; } @@ -3429,4 +3415,4 @@ void MPU6050::setDMPConfig2(uint8_t config) { i2Cdev.writeByte(devAddr, MPU6050_RA_DMP_CFG_2, config); -} \ No newline at end of file +}
diff -r 1e0baaf91e96 -r 8c562a8fed36 MPU6050.h --- a/MPU6050.h Wed May 08 00:34:55 2013 +0000 +++ b/MPU6050.h Fri Sep 12 09:28:14 2014 +0800 @@ -48,7 +48,7 @@ #define MPU6050_ADDRESS_AD0_LOW 0x68 // address pin low (GND), default for InvenSense evaluation board #define MPU6050_ADDRESS_AD0_HIGH 0x69 // address pin high (VCC) -#define MPU6050_DEFAULT_ADDRESS MPU6050_ADDRESS_AD0_LOW +#define MPU6050_DEFAULT_ADDRESS MPU6050_ADDRESS_AD0_HIGH #define MPU6050_RA_XG_OFFS_TC 0x00 //[7] PWR_MODE, [6:1] XG_OFFS_TC, [0] OTP_BNK_VLD #define MPU6050_RA_YG_OFFS_TC 0x01 //[7] PWR_MODE, [6:1] YG_OFFS_TC, [0] OTP_BNK_VLD @@ -405,10 +405,8 @@ class MPU6050 { private: I2Cdev i2Cdev; - Serial debugSerial; public: - MPU6050(); - MPU6050(uint8_t address); + MPU6050(I2C &i2c, uint8_t address = MPU6050_DEFAULT_ADDRESS); void initialize(); bool testConnection(); @@ -991,4 +989,4 @@ uint8_t buffer[14]; }; -#endif /* _MPU6050_H_ */ \ No newline at end of file +#endif /* _MPU6050_H_ */
diff -r 1e0baaf91e96 -r 8c562a8fed36 helper_3dmath.h --- a/helper_3dmath.h Wed May 08 00:34:55 2013 +0000 +++ b/helper_3dmath.h Fri Sep 12 09:28:14 2014 +0800 @@ -213,4 +213,4 @@ } }; -#endif /* _HELPER_3DMATH_H_ */ \ No newline at end of file +#endif /* _HELPER_3DMATH_H_ */