MPU9255上的磁力计没有数据

Posted

技术标签:

【中文标题】MPU9255上的磁力计没有数据【英文标题】:No data from magnetometer on MPU9255 【发布时间】:2017-06-07 10:03:09 【问题描述】:

我在从 MPU9255 上的磁力计读取数据时遇到问题。加速度计和陀螺仪工作正常,但我没有收到来自磁力计的任何数据。我正在研究 STM32F103 目标。

有人知道下面的代码有什么问题吗?

uint8_t Settings = 0x00;
uint8_t SettingsM = 0x00;   
uint8_t SettingsL = 0x22;   //mode2 16bit 
//settings accelerometer and gyroscope
HAL_I2C_Mem_Write(&hi2c1, 0xD0, 27, I2C_MEMADD_SIZE_8BIT, &Settings,1,1000);
HAL_I2C_Mem_Write(&hi2c1, 0xD0, 28, I2C_MEMADD_SIZE_8BIT, &Settings,1,1000);

//Power Down mode
HAL_I2C_Mem_Write(&hi2c1, 0x0C, 0x0A, I2C_MEMADD_SIZE_8BIT, &SettingsM,1,1000);
HAL_Delay(100);
//16 bit output mode 2
HAL_I2C_Mem_Write(&hi2c1, 0x0C, 0x0A, I2C_MEMADD_SIZE_8BIT, &SettingsL,1,1000);
while (1)

    //A X
    HAL_I2C_Mem_Read(&hi2c1, 0xD0, 59, I2C_MEMADD_SIZE_8BIT, &DAHx,1,100);
    HAL_I2C_Mem_Read(&hi2c1, 0xD0, 60, I2C_MEMADD_SIZE_8BIT, &DALx,1,100);
    //A Y
    HAL_I2C_Mem_Read(&hi2c1, 0xD0, 61, I2C_MEMADD_SIZE_8BIT, &DAHy,1,100);
    HAL_I2C_Mem_Read(&hi2c1, 0xD0, 62, I2C_MEMADD_SIZE_8BIT, &DALy,1,100);
    //A Z
    HAL_I2C_Mem_Read(&hi2c1, 0xD0, 63, I2C_MEMADD_SIZE_8BIT, &DAHz,1,100);
    HAL_I2C_Mem_Read(&hi2c1, 0xD0, 64, I2C_MEMADD_SIZE_8BIT, &DALz,1,100);
    //Temp
    HAL_I2C_Mem_Read(&hi2c1, 0xD0, 65, I2C_MEMADD_SIZE_8BIT, &DHT,1,100);
    HAL_I2C_Mem_Read(&hi2c1, 0xD0, 66, I2C_MEMADD_SIZE_8BIT, &DLT,1,100);
    //G X
    HAL_I2C_Mem_Read(&hi2c1, 0xD0, 67, I2C_MEMADD_SIZE_8BIT, &DGHx,1,100);
    HAL_I2C_Mem_Read(&hi2c1, 0xD0, 68, I2C_MEMADD_SIZE_8BIT, &DGLx,1,100);
    //G Y
    HAL_I2C_Mem_Read(&hi2c1, 0xD0, 69, I2C_MEMADD_SIZE_8BIT, &DGHy,1,100);
    HAL_I2C_Mem_Read(&hi2c1, 0xD0, 70, I2C_MEMADD_SIZE_8BIT, &DGLy,1,100);
    //G Z
    HAL_I2C_Mem_Read(&hi2c1, 0xD0, 71, I2C_MEMADD_SIZE_8BIT, &DGHz,1,100);
    HAL_I2C_Mem_Read(&hi2c1, 0xD0, 72, I2C_MEMADD_SIZE_8BIT, &DGLz,1,100);

    do
    
        HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x02, I2C_MEMADD_SIZE_8BIT, &ST1,1,100);
    
    while (!(ST1&0x01));

    //0x0C - adress AK8963, 0x03-0x08 => HLx-HHz
    HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x03, I2C_MEMADD_SIZE_8BIT, &HLx,1,100);
    HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x04, I2C_MEMADD_SIZE_8BIT, &HHx,1,100);
    HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x05, I2C_MEMADD_SIZE_8BIT, &HLy,1,100);
    HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x06, I2C_MEMADD_SIZE_8BIT, &HHy,1,100);
    HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x07, I2C_MEMADD_SIZE_8BIT, &HLz,1,100);
    HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x08, I2C_MEMADD_SIZE_8BIT, &HHz,1,100);

    HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x09, I2C_MEMADD_SIZE_8BIT, &H,1,100);

    //accelerometer
    Ax = (int16_t)(DAHx << 8 | DALx);
    Ay = (int16_t)(DAHy << 8 | DALy);
    Az = (int16_t)(DAHz << 8 | DALz);
    //temperature
    T = (int16_t)(DHT << 8 | DLT);
    //gyroscope
    Gx = (int16_t)(DGHx << 8 | DGLx);
    Gy = (int16_t)(DGHy << 8 | DGLy);
    Gz = (int16_t)(DGHz << 8 | DGLz);
    //magnetometer
    Hx= (int16_t)(HHx << 8 | HLx);
    Hy= (int16_t)(HHy << 8 | HLy);
    Hz= (int16_t)(HHz << 8 | HLz);

    Ax_g = (float)Ax/16384.0;
    Ay_g = (float)Ay/16384.0;
    Az_g = (float)Az/16384.0;
    Gx_g = (float)Gx/131.0;
    Gy_g = (float)Gy/131.0;
    Gz_g = (float)Gz/131.0;

    arx = (180.0/3.141592) * atanf(Ax_g / sqrtf(Ay_g*Ay_g + Az_g*Az_g));
    ary = (180.0/3.141592) * atanf(Ay_g / sqrtf((Ax_g*Ax_g + Az_g*Az_g)));

    rx = (0.96 * arx) + (0.04 * Gx_g);
    ry = (0.96 * ary) + (0.04 * Gy_g);

【问题讨论】:

我没有检查代码,但仅供参考,磁力计是 MPU 内部的设备,具有不同的 I2C 地址。 你能详细说明I do not receive any data from magnetometer吗?失败案例是什么?你得到全零吗?你从来没有看到状态寄存器中设置了 DRDY 位吗? 【参考方案1】:

您需要在 MPU9255 上启用 I2C 旁路模式才能与 AK8963 直接通信。在程序开始时设置 INT Pin / Bypass Enable Configuration 寄存器(寄存器 55)中的BYPASS_EN 位:

uint8_t BypassEnable = 0x02;
HAL_I2C_Mem_Write(&hi2c1, 0xD0, 55, I2C_MEMADD_SIZE_8BIT, &BypassEnable,1,1000);

查看这里的工作示例:Arduino Sensor 10DOF MPU9255 cannot fetch magnetometer data

【讨论】:

以上是关于MPU9255上的磁力计没有数据的主要内容,如果未能解决你的问题,请参考以下文章

[Motion]MPU9250的基本框架

[Motion]MPU9250的基本性能参数

从 sensehats IMU 上的磁力计收集数据

航空航天专用Everspin非易失性MRAM存储器

使用 javascript 访问 nativescript 磁力计数据

有没有办法把MPU-6050的原始数据转换成0-359的旋转数据?