Arduino Sensor 10DOF MPU9255 无法获取磁力计数据 [英] Arduino Sensor 10DOF MPU9255 cannot fetch magnetometer data

查看:38
本文介绍了Arduino Sensor 10DOF MPU9255 无法获取磁力计数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有 MPU9255 和 BMP180 的 10 DOF IMU 来收集方向数据.当我运行我下载的 arduino 草图以测试 IMU 的功能时,它完美地收集了来自加速度计和陀螺仪元件的数据,但坚持从磁力计请求数据,而它从未收到过.它成功读取了磁力计数据一次,但除非我重新上传程序,否则永远无法再次读取它.更具体地说,按位和永远不会评估为真,因此程序无法继续,因为来自磁力计的数据未准备好".有问题的代码行是这样的:

I'm using a 10 DOF IMU with an MPU9255 and a BMP180 to collect orientation data. When I run a sketch for arduino that I downloaded to test the IMU's capabilities, it collects the data from the accelerometer and gyroscope elements perfectly, but is stuck with requesting the data from the magnetometer, which it never receives. It reads the magnetometer data succesfully once, but then can never read it again unless I re-upload the program. More specifically, the bitwise and never evaluates to true, so the program cannot proceed, because the data is "not ready" from the magnetometer. The offending lines of code are this:

  do
  {
    I2Cread(MAG_ADDRESS,0x02,1,&ST1);
  }
  while (!(ST1&0x01)); 

这里是完整的代码,以防万一

But here is the full code, just in case

#include <Wire.h>

#define    MPU9250_ADDRESS            0x68
#define    MAG_ADDRESS                0x0C

#define    GYRO_FULL_SCALE_250_DPS    0x00  
#define    GYRO_FULL_SCALE_500_DPS    0x08
#define    GYRO_FULL_SCALE_1000_DPS   0x10
#define    GYRO_FULL_SCALE_2000_DPS   0x18

#define    ACC_FULL_SCALE_2_G        0x00  
#define    ACC_FULL_SCALE_4_G        0x08
#define    ACC_FULL_SCALE_8_G        0x10
#define    ACC_FULL_SCALE_16_G       0x18



// This function read Nbytes bytes from I2C device at address Address. 
// Put read bytes starting at register Register in the Data array. 
void I2Cread(uint8_t Address, uint8_t Register, uint8_t Nbytes, uint8_t* Data)
{
  // Set register address
  Wire.beginTransmission(Address);
  Wire.write(Register);
  Wire.endTransmission();
 if(Register == 0x02){
   Serial.println("Data Not Ready");
 }
  // Read Nbytes
  Wire.requestFrom(Address, Nbytes); 
  uint8_t index=0;
  while (Wire.available())
    Data[index++]=Wire.read();
  Serial.println("Data: " + *Data); 
}


// Write a byte (Data) in device (Address) at register (Register)
void I2CwriteByte(uint8_t Address, uint8_t Register, uint8_t Data)
{
  // Set register address
  Wire.beginTransmission(Address);
  Wire.write(Register);
  Wire.write(Data);
  Wire.endTransmission();
}


// Initializations
void setup()
{
  // Arduino initializations
  Wire.begin();
  Serial.begin(115200);

  // Configure gyroscope range
  I2CwriteByte(MPU9250_ADDRESS,27,GYRO_FULL_SCALE_2000_DPS);
  // Configure accelerometers range
  I2CwriteByte(MPU9250_ADDRESS,28,ACC_FULL_SCALE_16_G);
  // Set by pass mode for the magnetometers
  I2CwriteByte(MPU9250_ADDRESS,0x37,0x02);

  // Request first magnetometer single measurement
  I2CwriteByte(MAG_ADDRESS,0x0A,0x01);


}


long int cpt=0;
// Main loop, read and display data
void loop()
{

  // _______________
  // ::: Counter :::

  // Display data counter
  Serial.println(cpt++,DEC);
  Serial.print ("\t");



  // ____________________________________
  // :::  accelerometer and gyroscope ::: 

  // Read accelerometer and gyroscope
  uint8_t Buf[14];
  I2Cread(MPU9250_ADDRESS,0x3B,14,Buf);


  // Create 16 bits values from 8 bits data

  // Accelerometer
  int16_t ax=-(Buf[0]<<8 | Buf[1]);
  int16_t ay=-(Buf[2]<<8 | Buf[3]);
  int16_t az=Buf[4]<<8 | Buf[5];

  // Gyroscope
  int16_t gx=-(Buf[8]<<8 | Buf[9]);
  int16_t gy=-(Buf[10]<<8 | Buf[11]);
  int16_t gz=Buf[12]<<8 | Buf[13];

    // Display values

  // Accelerometer
  Serial.print (ax,DEC); 
  Serial.print ("\t");
  Serial.print (ay,DEC);
  Serial.print ("\t");
  Serial.print (az,DEC);  
  Serial.print ("\t");

  // Gyroscope
  Serial.print (gx,DEC); 
  Serial.print ("\t");
  Serial.print (gy,DEC);
  Serial.print ("\t");
  Serial.print (gz,DEC);  
  Serial.print ("\t");


  // _____________________
  // :::  Magnetometer ::: 


  // Read register Status 1 and wait for the DRDY: Data Ready
 //Strong suspicion that this do while loop repeats to infinity
  uint8_t ST1;
  do
  {
    I2Cread(MAG_ADDRESS,0x02,1,&ST1);
    //Serial.println("Reading");
  }
  while (!(ST1&0x01)); 

  // Read magnetometer data  
  uint8_t Mag[7];  
  I2Cread(MAG_ADDRESS,0x03,7,Mag);


  // Create 16 bits values from 8 bits data

  // Magnetometer
  int16_t mx=-(Mag[3]<<8 | Mag[2]);
  int16_t my=-(Mag[1]<<8 | Mag[0]);
  int16_t mz=-(Mag[5]<<8 | Mag[4]);


  // Magnetometer
  Serial.print (mx+200,DEC); 
  Serial.print ("\t");
  Serial.print (my-70,DEC);
  Serial.print ("\t");
  Serial.print (mz-700,DEC);  
  Serial.print ("\t");



  // End of line
  Serial.println("");
  delay(100);    
}

推荐答案

啊哈,发现实际问题:每次新读数都需要启动磁力计单次测量:

Aha, found the actual problem: you need to start magnetometer single measurement for every new reading:

  // Create 16 bits values from 8 bits data

  // Magnetometer
  int16_t mx=-(Mag[3]<<8 | Mag[2]);
  int16_t my=-(Mag[1]<<8 | Mag[0]);
  int16_t mz=-(Mag[5]<<8 | Mag[4]);


  // Magnetometer
  Serial.print("MAG:\t");
  Serial.print (mx+200,DEC); 
  Serial.print ("\t");
  Serial.print (my-70,DEC);
  Serial.print ("\t");
  Serial.print (mz-700,DEC);  
  Serial.print ("\t");

  // Request first magnetometer single measurement !!!!!
  I2CwriteByte(MAG_ADDRESS,0x0A,0x01);

这是从你的代码的末尾,我添加了一行启动新的阅读.似乎正在工作.

This is from the end of your code, I added a line that initiates a new reading. Seems to be working.

这篇关于Arduino Sensor 10DOF MPU9255 无法获取磁力计数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆