新手入门
- 积分
- 11
- 金钱
- 11
- 注册时间
- 2019-11-4
- 在线时间
- 5 小时
|
1金钱
首先我使用了CubeMX成功配置了I2C,并且能够顺利读取 写入数据到MOU9250中。然后我继续移植了正点原子的九轴飞行器的DMP的内容。重新编写了MPU9250.h和MPU9250.c
然后使用BYPASS模式时,可以顺利读取速度 加速度 和 磁力计的数据。
但是一旦使用DMP库内的mpu_get_compass_reg函数来读取磁力计时,就读不到任何信息。
如下是我的初始化函数,和原子哥代码中的初始化过程一模一样:如下:
- uint8_t mpu_dmp_init(void)
- {
- printf("\r\n0\r\n");
- uint8_t res=0;
- struct int_param_s int_param;
- unsigned char accel_fsr;
- unsigned short gyro_rate, gyro_fsr;
- unsigned short compass_fsr;
- if(mpu_init(&int_param)==0) //初始化MPU9250
- {
- res=inv_init_mpl(); //初始化MPL
- if(res)return 1;
- inv_enable_quaternion();
- inv_enable_9x_sensor_fusion();
- inv_enable_fast_nomot();
- inv_enable_gyro_tc();
- inv_enable_vector_compass_cal();
- inv_enable_magnetic_disturbance();
- inv_enable_eMPL_outputs();
- res=inv_start_mpl(); //开启MPL
- if(res)return 1;
- res=mpu_set_sensors(INV_XYZ_GYRO|INV_XYZ_ACCEL|INV_XYZ_COMPASS);//设置所需要的传感器
- if(res)return 2;
- res=mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL); //设置FIFO
- if(res)return 3;
- res=mpu_set_sample_rate(DEFAULT_MPU_HZ); //设置采样率
- if(res)return 4;
- res=mpu_set_compass_sample_rate(1000/COMPASS_READ_MS); //设置磁力计采样率
- if(res)return 5;
- mpu_get_sample_rate(&gyro_rate);
- mpu_get_gyro_fsr(&gyro_fsr);
- mpu_get_accel_fsr(&accel_fsr);
- mpu_get_compass_fsr(&compass_fsr);
- inv_set_gyro_sample_rate(1000000L/gyro_rate);
- inv_set_accel_sample_rate(1000000L/gyro_rate);
- inv_set_compass_sample_rate(COMPASS_READ_MS*1000L);
- inv_set_gyro_orientation_and_scale(
- inv_orientation_matrix_to_scalar(gyro_orientation),(long)gyro_fsr<<15);
- inv_set_accel_orientation_and_scale(
- inv_orientation_matrix_to_scalar(gyro_orientation),(long)accel_fsr<<15);
- inv_set_compass_orientation_and_scale(
- inv_orientation_matrix_to_scalar(comp_orientation),(long)compass_fsr<<15);
-
- res=dmp_load_motion_driver_firmware(); //加载dmp固件
- if(res)return 6;
- res=dmp_set_orientation(inv_orientation_matrix_to_scalar(gyro_orientation));//设置陀螺仪方向
- if(res)return 7;
- 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 8;
- res=dmp_set_fifo_rate(DEFAULT_MPU_HZ); //设置DMP输出速率(最大不超过200Hz)
- if(res)return 9;
- res=run_self_test(); //自检
- if(res)return 10;
- res=mpu_set_dmp_state(1); //使能DMP
- if(res)return 11;
- }
- return 0;
- }
复制代码 初始化后,main.c里这么调用的:- while (1)
- {
- printf("\r\n初始化-------\r\n");
- recv = mpu_dmp_init();
- if (recv == 0)
- {
- break;
- }
- else
- {
- printf("\r\n初始化失败\r\n");
- }
- }
复制代码- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- printf("\r\n******************************************\r\n");
- if ((recv = mpu_get_gyro_reg(gyro, 0)) != 0)
- {
- printf("\r\nGet speed wrong\r\n");
- }
- if ((recv = mpu_get_accel_reg(accel, 0)) != 0)
- {
- printf("\r\nGet accel wrong\r\n");
- }
- if ((recv = mpu_get_compass_reg(magnet, 0)) != 0)
- {
- printf("\r\nGet magnet wrong\r\n");
- }
- printf("\r\nThis is speed:vx=%d,vy=%d,vz=%d\r\n", gyro[X], gyro[Y], gyro[Z]);
- printf("\r\nThis is accel:ax=%d,ay=%d,az=%d\r\n", accel[X], accel[Y], accel[Z]);
- printf("\r\nThis is magnet:mx=%d,my=%d,mz=%d\r\n", magnet[X], magnet[Y], magnet[Z]);
- printf("\r\n******************************************\r\n");
- HAL_Delay(100);
-
- }
复制代码 无法读出磁力计数据
但是用bypass可以读出:
- if ((recv = MPU_Get_Magnetometer(&magnet[0], &magnet[1], &magnet[2])) != 0)
- {
- printf("\r\nGet magnet wrong\r\n");
- }
复制代码- int8_t MPU_Get_Magnetometer(short *mx, short *my, short *mz)
- {
- uint8_t buf[6];
- int8_t res;
- mpu_set_bypass(1);
- if ((res = MPU_Read_Bytes(AK8963_ADDR, MAG_XOUT_L, 6, buf)) != 0)
- {
- return res;
- }
- HAL_Delay(100);
- if (res == 0)
- {
- *mx = ((uint16_t)buf[1] << 8) | buf[0];
- *my = ((uint16_t)buf[3] << 8) | buf[2];
- *mz = ((uint16_t)buf[5] << 8) | buf[4];
- }
- if ((res = MPU_Write_Byte(AK8963_ADDR, MAG_CNTL1, 0X11)) != 0)
- {
- return res;
- }
- mpu_set_bypass(0);
- HAL_Delay(100);
- return res;
- }
复制代码 最后发现确实是MASTER 模式没有成功。因为我在主函数中写了一小段测试代码:
- int8_t recv = 0;
- uint8_t uc = 0X20;
- MPU_Write_Bytes(0XD0, 0X6A, 1, &uc);
- uint8_t mstctrol = 0X0D;
- MPU_Write_Bytes(0XD0,0X24 , 1, &mstctrol);
- uint8_t m0 = 255;
- MPU_Read_Bytes(0XD0, 0X24, 1, &m0);
- printf("\r\nM0 = %d\r\n", m0);
- uint8_t u0 = 255;
- MPU_Read_Bytes(0XD0, 0X6A, 1, &u0);
- printf("\r\nU0= %d\r\n", u0);
- mpu_set_bypass(1);
- uint8_t c1 = 255;
- MPU_Read_Bytes(0X18, 0X0A, 1, &c1);
- printf("\r\nC1 = %d\r\n", c1);
- mpu_set_bypass(0);
- uint8_t addddr = 0X18;//0X0C两个都试了
- recv = MPU_Write_Bytes(0XD0, 0X28, 1, &addddr);
- uint8_t control_reg = 0X0A;
- recv = MPU_Write_Bytes(0XD0, 0X29, 1, &control_reg);
- uint8_t CONBIT = 0X80 | 1;
- recv = MPU_Write_Bytes(0XD0, 0X30, 1, &CONBIT);
- uint8_t modechoice = 0X11;
- recv = MPU_Write_Bytes(0XD0, 0X64, 1, &modechoice);
- uint8_t delyamode = 0X03;
- recv = MPU_Write_Bytes(0XD0, 0X67, 1, &delyamode);
- HAL_Delay(1000);
- mpu_set_bypass(1);
- c1 = 255;
- MPU_Read_Bytes(0X18, 0X0A, 1, &c1);
- printf("\r\nC1 2222 = %d\r\n", c1);
- mpu_set_bypass(0);
复制代码 就是先把磁力计CONTROL1和SLAVE1联系在一起。然后给SLAVE1_d0写入0X11,再用bypass模式读取磁力计CONTROL1寄存器。发现根本没有成功。MASTER模式下的写入失败!!!!!!。我基本上快疯了。这个问题搞了我两天没有一点点头绪。有咩有大佬知道原因?帮助我解决一下,如果大佬帮我解决了。马上支付宝100元红包(学生党 么得钱 哎 见谅)。
|
|