Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for mpu9250 gyro/acc/mag chip #20

Open
wants to merge 2 commits into
base: upstream_shared
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 89 additions & 3 deletions Sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ void Mag_init() {
delay(100);
}

#if !defined(MPU6050_I2C_AUX_MASTER)
#if !defined(MPU6050_I2C_AUX_MASTER || MPU9250)
void Device_Mag_getADC() {
i2c_getSixRawADC(MAG_ADDRESS,MAG_DATA_REGISTER);
MAG_ORIENTATION( ((rawADC[0]<<8) | rawADC[1]) ,
Expand Down Expand Up @@ -1063,7 +1063,7 @@ static void Mag_init() {
delay(100);
}

#if !defined(MPU6050_I2C_AUX_MASTER)
#if !defined(MPU6050_I2C_AUX_MASTER || MPU9250)
static void Device_Mag_getADC() {
getADC();
}
Expand Down Expand Up @@ -1303,6 +1303,92 @@ void Gyro_getADC () {
// End Of I2C Gyroscope and Accelerometer LSM330
// ************************************************************************************************************

// ************************************************************************************************************
// I2C Gyroscope/Accelerometer/Compass MPU9250
// ************************************************************************************************************
#if defined(MPU9250)

#if !defined(MPU9250_ADDRESS)
#define MPU9250_ADDRESS 0x68 // address pin AD0 low (GND), default for FreeIMU v0.4 and InvenSense evaluation board
//#define MPU9250_ADDRESS 0x69 // address pin AD0 high (VCC)
//The MAG acquisition
#define MAG_ADDRESS 0x0C
#define MAG_DATA_REGISTER 0x03
#endif


////////////////////////////////////
// Gyro start //
////////////////////////////////////

void Gyro_init() {
TWBR = ((F_CPU / 400000L) - 16) / 2; // change the I2C clock rate to 400kHz
i2c_writeReg(MPU9250_ADDRESS, 0x6B, 0x80); //PWR_MGMT_1 -- DEVICE_RESET 1
delay(5);
i2c_writeReg(MPU9250_ADDRESS, 0x6B, 0x03); //PWR_MGMT_1 -- SLEEP 0; CYCLE 0; TEMP_DIS 0; CLKSEL 3 (PLL with Z Gyro reference)
i2c_writeReg(MPU9250_ADDRESS, 0x1A, GYRO_DLPF_CFG); //CONFIG -- EXT_SYNC_SET 0 (disable input pin for data sync) ; default DLPF_CFG = 0 => ACC bandwidth = 260Hz GYRO bandwidth = 256Hz)
i2c_writeReg(MPU9250_ADDRESS, 0x1B, 0x18); //GYRO_CONFIG -- FS_SEL = 3: Full scale set to 2000 deg/sec
// enable I2C bypass for AUX I2C, allways on because we have a magnetometer on board
i2c_writeReg(MPU9250_ADDRESS, 0x37, 0x02); //INT_PIN_CFG -- INT_LEVEL=0 ; INT_OPEN=0 ; LATCH_INT_EN=0 ; INT_RD_CLEAR=0 ; FSYNC_INT_LEVEL=0 ; FSYNC_INT_EN=0 ; I2C_BYPASS_EN=1 ; CLKOUT_EN=0
}

void Gyro_getADC () {
i2c_getSixRawADC(MPU9250_ADDRESS, 0x43);
GYRO_ORIENTATION( ((rawADC[0]<<8) | rawADC[1])/4 , // range: +/- 8192; +/- 2000 deg/sec
((rawADC[2]<<8) | rawADC[3])/4 ,
((rawADC[4]<<8) | rawADC[5])/4 );
GYRO_Common();
}

////////////////////////////////////
// Gyro end //
////////////////////////////////////


////////////////////////////////////
// ACC start //
////////////////////////////////////
void ACC_init () {
i2c_writeReg(MPU9250_ADDRESS, 0x1C, 0x10);
}

void ACC_getADC () {
i2c_getSixRawADC(MPU9250_ADDRESS, 0x3B);
ACC_ORIENTATION( ((rawADC[0]<<8) | rawADC[1])/8 ,
((rawADC[2]<<8) | rawADC[3])/8 ,
((rawADC[4]<<8) | rawADC[5])/8 );
ACC_Common();
}

////////////////////////////////////
// ACC end //
////////////////////////////////////


////////////////////////////////////
// MAG start //
////////////////////////////////////
void Mag_init() {
delay(100);
i2c_writeReg(MAG_ADDRESS,0x0a,0x01); //Start the first conversion
delay(100);
}
void Device_Mag_getADC() {
i2c_getSixRawADC(MAG_ADDRESS, 0x03);
MAG_ORIENTATION( ((rawADC[1]<<8) | rawADC[0]) ,
((rawADC[3]<<8) | rawADC[2]) ,
((rawADC[5]<<8) | rawADC[4]) );
//Start another meassurement
i2c_writeReg(MAG_ADDRESS,0x0a,0x01);
}
////////////////////////////////////
// MAG end //
////////////////////////////////////

#endif
// ************************************************************************************************************
// End Of I2C Gyroscope/Accelerometer/Compass MPU9250
// ************************************************************************************************************

#if defined(WMP)
// ************************************************************************************************************
Expand Down Expand Up @@ -1536,4 +1622,4 @@ void initSensors() {
initS();
if (i2c_errors_count == 0) break; // no error during init => init ok
}
}
}
6 changes: 3 additions & 3 deletions Sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void i2c_read_reg_to_buf(uint8_t add, uint8_t reg, uint8_t *buf, uint8_t size);
#if defined(ADCACC)
#define ACC_1G 75
#endif
#if defined(MPU6050)
#if defined(MPU6050) || defined(MPU9250)
#if defined(FREEIMUv04)
#define ACC_1G 255
#else
Expand All @@ -54,7 +54,7 @@ void i2c_read_reg_to_buf(uint8_t add, uint8_t reg, uint8_t *buf, uint8_t size);
#if defined(ITG3050)
#define GYRO_SCALE (4 / 16.0 * PI / 180.0 / 1000000.0) //16.4 LSB = 1 deg/s -- 16.0 apparently gives beter results than 16.4 (empirical)
#endif
#if defined(MPU6050) || defined(MPU3050)
#if defined(MPU6050) || defined(MPU3050) || defined(MPU9250)
#define GYRO_SCALE (4 / 16.4 * PI / 180.0 / 1000000.0) //16.4 LSB = 1 deg/s
#endif
#if defined(ITG3200)
Expand All @@ -67,4 +67,4 @@ void i2c_read_reg_to_buf(uint8_t add, uint8_t reg, uint8_t *buf, uint8_t size);
#define GYRO_SCALE (1.0f/200e6f)
#endif

#endif /* SENSORS_H_ */
#endif /* SENSORS_H_ */
3 changes: 2 additions & 1 deletion config.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
//#define MPU3050
//#define L3G4200D
//#define MPU6050 //combo + ACC
//#define MPU9250
//#define LSM330 //combo + ACC

/* I2C accelerometer */
Expand Down Expand Up @@ -1228,4 +1229,4 @@ Also note, that maqgnetic declination changes with time, so recheck your value e
/*************************************************************************************************/

#endif /* CONFIG_H_ */

8 changes: 4 additions & 4 deletions def.h
Original file line number Diff line number Diff line change
Expand Up @@ -1668,19 +1668,19 @@
/*************** Sensor Type definitions ********************/
/**************************************************************************************/

#if defined(ADXL345) || defined(BMA020) || defined(BMA180) || defined(BMA280) || defined(MMA7455) || defined(ADCACC) || defined(LIS3LV02) || defined(LSM303DLx_ACC) || defined(MPU6050) || defined(LSM330) || defined(MMA8451Q)
#if defined(ADXL345) || defined(BMA020) || defined(BMA180) || defined(BMA280) || defined(MMA7455) || defined(ADCACC) || defined(LIS3LV02) || defined(LSM303DLx_ACC) || defined(MPU6050) || defined(LSM330) || defined(MMA8451Q) || defined(MPU9250)
#define ACC 1
#else
#define ACC 0
#endif

#if defined(HMC5883) || defined(HMC5843) || defined(AK8975) || defined(MAG3110)
#if defined(HMC5883) || defined(HMC5843) || defined(AK8975) || defined(MAG3110) || defined(MPU9250)
#define MAG 1
#else
#define MAG 0
#endif

#if defined(ITG3200) || defined(ITG3050) || defined(L3G4200D) || defined(MPU6050) || defined(LSM330) || defined(MPU3050) || defined(WMP)
#if defined(ITG3200) || defined(ITG3050) || defined(L3G4200D) || defined(MPU6050) || defined(LSM330) || defined(MPU3050) || defined(WMP) || defined(MPU9250)
#define GYRO 1
#else
#define GYRO 0
Expand Down Expand Up @@ -2029,4 +2029,4 @@
#error "you use one feature that is no longer supported or has undergone a name change"
#endif

#endif /* DEF_H_ */
#endif /* DEF_H_ */