Getting/Processing 使用 Arduino Due 从 MPU-6050 获得的正确值

Getting/Processing the right values U get from the MPU-6050 with Arduino Due

对于决定我今年是否通过的学校项目,我必须使用 MPU-6050 和 Arduino Due。 MPU 通过 I2C 工作,我已经让那部分工作了。我可以获得值及其链接。但是有一个问题,我的代码似乎没有得到正确的值!或者我错误地处理了它们。我将向您展示 Arduino C 代码和我的代码,也许有人知道我做错了什么。

这是带有输出的 arduino 代码:

#include<Wire.h>
const int MPU_addr=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(9600);
}
void loop(){
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    
  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  Serial.print(" | GyY = "); Serial.print(GyY);
  Serial.print("\n");
  //Serial.print(" | GyZ = "); Serial.println(GyZ);
  delay(100);
}

输出:

 | AcX = 16676
 | AcX = 16628
 | AcX = 16740
 | AcX = 16692
 | AcX = 16660
 | AcX = 16676
 | AcX = 16780
 | AcX = 16684
 | AcX = 16612
 | AcX = 16644
 | AcX = 16588

现在看来可以了。但我的 C++ 代码似乎根本无法正常工作。这是:

#include "hwlib.hpp"
#include <iomanip>

int main( void ){

 // kill the watchdog
 WDT->WDT_MR = WDT_MR_WDDIS;

 namespace target = hwlib::target;
// int16_t zaxis, yaxis;
 byte adress = 0x68;

 auto scl = target::pin_oc{ target::pins::scl };
 auto sda = target::pin_oc{ target::pins::sda };

 auto i2c_bus = hwlib::i2c_bus_bit_banged_scl_sda{ scl,sda };


 for(;;){
    byte data[8] = {0x3B};
    i2c_bus.write(adress, data, 1);
    i2c_bus.read(adress, data, 8);
    hwlib::cout  << (int16_t)data[0] << (int16_t)data[1]  << (int16_t)data[2]<< (int16_t)data[3] << (int16_t)data[4] << (int16_t)data[5] << (int16_t)data[6] << (int16_t)data[7] << "\n";
    hwlib::wait_ms(500);
 }
}

想知道 hwlib 是什么的人,它是学校提供的图书馆。所以你看到了 hwlib 的 read 和 write 方法,如果你想知道它们的样子:

  void write( fast_byte a, const byte data[], fast_byte n ) override {
       write_start();
       write_byte( a << 1 );
       for( fast_byte i = 0; i < n; i++ ){
          read_ack();
          write_byte( data[ i ] );
      }               
       read_ack();
       write_stop();      
    }

    //
    void read( fast_byte a, byte data[], fast_byte n ) override {
       write_start();
       write_byte( ( a << 1 ) | 0x01 );    
       read_ack();
       for( fast_byte i = 0; i < n; i++ ){
          if( i > 0 ){
             write_ack();
          }   
          data[ i ] = read_byte();
       }               
       write_stop();      
    } 

现在,它似乎将数据写入我用函数发送的字节数组。然后我得到数据并用 hwlib::cout 把它放在屏幕上。它和普通的一样工作std::cout但是老师说要用它。

输出:

651601120216235240
65522552160248235192
65200120184235240
642280321100235240
64244010812423616
650255208132235224
654004018235224
641320280208235224
654255236102360
65480480200235224

我会继续努力弄清楚,因为我不能让这个项目失败哈哈,但如果有人有答案,他们可以告诉我我做错了什么,我应该怎么做,这将使我的一年,胡乱地。

提前致谢!

这些值是 2 个字节长,但您一次输出一个字节。这仅适用于十六进制,但不适用于小数。

注意 16640=0x4100,0x41=65,所以(至少)每行的前两位数字是正确的。