新手入门
- 积分
- 11
- 金钱
- 11
- 注册时间
- 2016-10-29
- 在线时间
- 1 小时
|
void delay(uint32_t ms)
{
uint16_t i;
while(ms--)
{
for(i = 0;i < 1000;i++);
}
}
void mpu6050Init(void)
{
// bool ack;
// uint8_t sig;
i2cWrite(MPU6050_ADDRESS, 0x14, 0xb6); //reset
delay(10);
i2cWrite(MPU6050_ADDRESS, 0x11, 0x00); //normal modes
i2cWrite(MPU6050_ADDRESS, 0x10, BW_47HZ);
i2cWrite(MPU6050_ADDRESS, 0X0F, 0X00); //+-2000 DEG/SEC 16.4lsb
i2cWrite(MPU6050_ADDRESS, 0X09, 0X40); //any_int any motion intrrupt status enable
// i2cWrite(MPU6050_ADDRESS, 0X32, 0Xff);
#if 0
delay(10);
i2cWrite(BMA280_ADDRESS, 0x10, ACC_BW_62_5hz); //set BW to 15,63Hz 0x09
delay(5);
i2cWrite(BMA280_ADDRESS, 0x0F, 0x08); //set range to 8G 8
i2cWrite(BMA280_ADDRESS, 0x11, 0X00);
//acc_1G = 255; //118
acc_1G = 510; //510
#endif
}
//=====================================================
bool mpu6050Detect (void)
{
bool ack;
uint8_t sig;
delay(50);
i2cWrite(MPU6050_ADDRESS, 0x14, 0xb6); // sofeware reset
i2cWrite(BMA280_ADDRESS, 0x14, 0xb6); //sofeware reset
delay(350);
i2cWrite(BMA280_ADDRESS, 0x34, 0x04);
i2cWrite(MPU6050_ADDRESS, 0x34, 0x04);
// read acc_id
ack = i2cRead(BMA280_ADDRESS, 0x00, 1, &sig);
if (!ack)
return false;
ack = i2cRead(MPU6050_ADDRESS, 0x00, 1, &sig);
if (!ack)
return false;
mpu6050Init();
MC3210AccRead(gyroADC);
//MC3210AccAlign(gyroADC);
//mpu6050GyroRead(accADC);
//mpu6050GyroAlign(accADC);
return true ;
}
//==============================================================================
void mpu6050GyroRead(int16_t * gyroData)
{
uint8_t buf[6];
// uint8_t axis = 0 ;
i2cRead(MPU6050_ADDRESS, 0x02, 6, buf);
gyroData[0] = (buf[1] << 8) | buf[0];
gyroData[1] = (buf[3] << 8) | buf[2];
gyroData[2] = (buf[5] << 8) | buf[4];
a123[0] = (buf[1] << 8) | buf[0];
a123[1] = (buf[3] << 8) | buf[2];
a123[2] = (buf[5] << 8) | buf[4];
// gyroData[0] = gyroData[0]>>1 ;
// gyroData[1] = gyroData[1]>>1 ;
// gyroData[2] = gyroData[2]>>1 ;
}
void mpu6050GyroAlign(int16_t * gyroData)
{
int16_t temp[2];
temp[0] = gyroData[0];
temp[1] = gyroData[1];
/*gyroData[0] = -temp[1] / 4;
gyroData[1] = temp[0] / 4;
gyroData[2] = -gyroData[2] / 4;*/
gyroData[0] = -temp[0] / 4;
gyroData[1] = temp[1] / 4;
gyroData[2] = -gyroData[2] / 4;
}
//==============================================================================
#if 0
void Gyro_getADC(void)
{
// uint8_t axis1 =0 ;
mpu6050GyroRead(gyroADC);
mpu6050GyroAlign(gyroADC);
Gyro_calibe();
}
#endif
//===========================================================================================
void Gyro_calibe(void)
{
// static int16_t previousGyroADC[3] = { 0, 0, 0 };
// static int32_t g[3];
uint8_t axis;
/* if (calibratingG > 0) {
for (axis = 0; axis < 3; axis++) {
if (calibratingG == 1000)
g[axis] = 0;
g[axis] += gyroADC[axis];
gyroADC[axis] = 0;
gyroZero[axis] = 0;
if (calibratingG == 1) {
gyroZero[axis] = g[axis] / 1000;
blinkLED(10, 15, 1);
}
}
calibratingG--;
} */
for (axis = 0; axis < 3; axis++) {
gyroADC[axis] =gyroADC[axis] - gyroZero[axis];
gyroADC[axis] = constrain(gyroADC[axis], previousGyroADC[axis] -800, previousGyroADC[axis] + 800);
previousGyroADC[axis] = gyroADC[axis];
}
}
//==============================================================
void Acc_calibe(void)
{
static int32_t a[3] = {0,0,0};
uint32_t axis;
// static int16_t x ;
int16_t calibratingA = 0;
if (calibratingA > 0) {
for (axis = 0; axis < 3; axis++) {
// Reset a[axis] at start of calibration
if (calibratingA == 800) //400
a[axis] = 0;
// Sum up 400 readings
a[axis] += accADC[axis];
// Clear global variables for next reading
accADC[axis] = 0;
//cfg.accZero[axis] = 0;
accZero[axis] = 0;
}
// Calculate average, shift Z down by acc_1G and store values in EEPROM at end of calibration
if (calibratingA == 1) {
accZero[ROLL] = a[ROLL] / 800; //Íù×óΪ¼Ó
accZero[PITCH] = a[PITCH] / 800;
accZero[YAW] = a[YAW] / 800 - acc_1G; // for nunchuk 200=1G
// cfg.accZero[ROLL] = 20;
// cfg.accZero[PITCH] = -51;
// cfg.accZero[YAW] = 384 - acc_1G;
// SYNC_FLASH = 0xaa ;
// Write_flash () ;
calibe_fg = 0 ;
// cfg.accTrim[ROLL] = 0;
// cfg.accTrim[PITCH] = 0;
accTrim[ROLL] = 0;
accTrim[PITCH] = 0;
}
calibratingA--;
}
accADC[ROLL] -= accZero[ROLL];
accADC[PITCH] -= accZero[PITCH];
accADC[YAW] -= accZero[YAW];
}
//===============================================================================
void MC3210AccRead(int16_t * accData)
{
uint8_t buf[6] ;// SHAKE_TH;
// bool ack=0;
i2cRead(BMA280_ADDRESS, 0x02, 6, buf);
accData[0] = (buf[1] << 8) | buf[0];
accData[1] = (buf[3] << 8) | buf[2];
accData[2] = (buf[5] << 8) | buf[4];
accData[0] = accData[0] >>4;
accData[1] = accData[1] >>4 ;
accData[2] = accData[2] >>4 ;
}
#define BMA2XX_EEPROM_CTRL_REG 0x33
#define BMA2XX_OFFSET_CTRL_REG 0x36
#define BMA2XX_OFFSET_PARAMS_REG 0x37
#define BMA2XX_OFFSET_X 0x38
#define BMA2XX_OFFSET_Y 0x39
#define BMA2XX_OFFSET_Z 0x3A
void MC3210AccAlign(int16_t * accData)
{
int16_t temp[2];
temp[0] = accData[0];
temp[1] = accData[1];
accData[0] = temp[1] ;
accData[1] = temp[0] ;
accData[2] = accData[2] ;
}
|
|