int init(){ open_bus(); unsigned char whoami=0; i2c_read(MPU6050_ADDR, MPU6050_WHO_AM_I, 1, &whoami); if(!silent_flag) { printf("WHO_AM_I: %x\n", whoami); } struct int_param_s int_param; if(!silent_flag) { printf("MPU init: %i\n", mpu_init(&int_param)); printf("MPU sensor init: %i\n", mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL)); printf("MPU configure fifo: %i\n", mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL)); printf("DMP firmware: %i\n ",dmp_load_motion_driver_firmware()); printf("DMP orientation: %i\n ",dmp_set_orientation( inv_orientation_matrix_to_scalar(gyro_orientation))); } unsigned short dmp_features = DMP_FEATURE_6X_LP_QUAT | DMP_FEATURE_TAP | DMP_FEATURE_SEND_RAW_ACCEL | DMP_FEATURE_SEND_CAL_GYRO | DMP_FEATURE_GYRO_CAL; if(!silent_flag) { printf("DMP feature enable: %i\n", dmp_enable_feature(dmp_features)); printf("DMP set fifo rate: %i\n", dmp_set_fifo_rate(DEFAULT_MPU_HZ)); printf("DMP enable %i\n", mpu_set_dmp_state(1)); } if(!no_interrupt_flag) { mpu_set_int_level(1); // Interrupt is low when firing dmp_set_interrupt_mode(DMP_INT_CONTINUOUS); // Fire interrupt on new FIFO value } return 0; }
uint8_t MPU_Init(void) { GPIO_InitTypeDef gpio; NVIC_InitTypeDef nvic; EXTI_InitTypeDef exti; int res=0; IIC_Init(); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); gpio.GPIO_Pin = GPIO_Pin_5; gpio.GPIO_Mode = GPIO_Mode_IN; gpio.GPIO_OType = GPIO_OType_PP; gpio.GPIO_PuPd = GPIO_PuPd_UP; gpio.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOB, &gpio); SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB,GPIO_PinSource5); exti.EXTI_Line = EXTI_Line5; exti.EXTI_Mode = EXTI_Mode_Interrupt; exti.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿中断 exti.EXTI_LineCmd = ENABLE; EXTI_Init(&exti); nvic.NVIC_IRQChannel = EXTI9_5_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = ITP_MPU_EXTI9_5_PREEMPTION; nvic.NVIC_IRQChannelSubPriority = ITP_MPU_EXTI9_5_SUB; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); if(mpu_init()==0) //初始化MPU6050 { res=mpu_set_sensors(INV_XYZ_GYRO|INV_XYZ_ACCEL);//设置所需要的传感器 if(res)return 1; res=mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL);//设置FIFO if(res)return 2; res=mpu_set_sample_rate(DEFAULT_MPU_HZ); //设置采样率 if(res)return 3; res=dmp_load_motion_driver_firmware(); //加载dmp固件 if(res)return 4; res=dmp_set_orientation(inv_orientation_matrix_to_scalar(gyro_orientation));//设置陀螺仪方向 if(res)return 5; res=dmp_enable_feature(DMP_FEATURE_6X_LP_QUAT|DMP_FEATURE_TAP| //设置dmp功能 DMP_FEATURE_ANDROID_ORIENT|DMP_FEATURE_SEND_RAW_ACCEL|DMP_FEATURE_SEND_CAL_GYRO| DMP_FEATURE_GYRO_CAL); if(res)return 6; res=dmp_set_fifo_rate(DEFAULT_MPU_HZ); //设置DMP输出速率(最大不超过200Hz) if(res)return 7; res=mpu_set_int_level(1); if(res)return 8; res=dmp_set_interrupt_mode(DMP_INT_CONTINUOUS); if(res)return 9; res=run_self_test(); //自检 if(res)return 10; res=mpu_set_dmp_state(1); //使能DMP if(res)return 11; } else return 12; return 0; }
// Private int8_t IMUTask::MPUSetup() { // TODO: should check here to see if IMU is already setup and just init the lib int8_t result = 0; struct int_param_s int_param; int_param.cb = IMU_interrupt; int_param.pin = MPU_INT; int_param.arg = FALLING; unsigned char accel_fsr; unsigned short gyro_rate, gyro_fsr; unsigned long timestamp; result = mpu_init(&int_param); debug::log("IMUTask: mpu_init returned " + String(result)); if (result != 0) { return -1; } /* Wake up all sensors. */ result = mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL); debug::log("IMUTask: mpu_set_sensors returned " + String(result)); result = mpu_set_sample_rate(IMU_DEFAULT_MPU_HZ); debug::log("IMUTask: mpu_set_sample_rate returned " + String(result)); debug::log("Pre laod"); result = dmp_load_motion_driver_firmware(); debug::log("IMUTask: dmp_load_motion_driver_firmware returned " + String(result)); if (result != 0) { return -2; } result = dmp_set_orientation( inv_orientation_matrix_to_scalar(gyro_orientation) ); debug::log("IMUTask: dmp_set_orientation returned " + String(result)); if (result != 0) { return -3; } /* * Known Bug - * DMP when enabled will sample sensor data at 200Hz and output to FIFO at the rate * specified in the dmp_set_fifo_rate API. The DMP will then sent an interrupt once * a sample has been put into the FIFO. Therefore if the dmp_set_fifo_rate is at 25Hz * there will be a 25Hz interrupt from the MPU device. * * There is a known issue in which if you do not enable DMP_FEATURE_TAP * then the interrupts will be at 200Hz even if fifo rate * is set at a different rate. To avoid this issue include the DMP_FEATURE_TAP */ unsigned short dmp_features = DMP_FEATURE_TAP | // Don't remove this, see above DMP_FEATURE_ANDROID_ORIENT; // we use this for the screen // Disableing the rest for now as we dont yet have a use for them // DMP_FEATURE_SEND_RAW_ACCEL | // DMP_FEATURE_SEND_RAW_GYRO; result = dmp_enable_feature(dmp_features); if (result != 0) { debug::log("IMUTask: mp_enable_feature returned " + String(result)); return -4; } result = dmp_set_fifo_rate(IMU_DEFAULT_MPU_HZ); if (result != 0) { debug::log("IMUTask: dmp_set_fifo_rate returned " + String(result)); return -5; } dmp_register_tap_cb(IMU_tap_cb); dmp_register_android_orient_cb(IMU_android_orient_cb); dmp_set_interrupt_mode(DMP_INT_GESTURE); return 0; }