简体   繁体   中英

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

for my project for school that decides if I pass this year or not I have to work with an MPU-6050 and an Arduino Due. The MPU works via I2C and ive got that part working. I can get values and its linked. But there is one problem, with my code it seems that I am not getting the right values! Or that I am processing them wrongly. I will show you the Arduino C code and my code and maybe someone knows what I am doing wrong.

Here is the arduino code with output:

#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);
}

With output:

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

Now this seems to be fine. But my c++ code is not working properly at all it seems. Here it is:

#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);
 }
}

To the people wondering what hwlib is, its a library provided by school. So you see the read and write methods of hwlib and if you are wondering how they look:

  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();      
    } 

Now, it seems that it writes the data to the byte array that I send with the function. And with that i get the data and put it on the screen with hwlib::cout. It works just like the normal std::cout but the teacher said to use it.

Output:

651601120216235240
65522552160248235192
65200120184235240
642280321100235240
64244010812423616
650255208132235224
654004018235224
641320280208235224
654255236102360
65480480200235224

I will continue trying to figure it out because I can not fail this project haha, but if someone has an answer and they can tell me what im doing wrong and how I should be doing it that would make my year, litterally.

Thankyou in advance!

The values are 2 byte long, but you output one byte at a time. This only works for hex, but not for decimals.

Note that 16640=0x4100, and 0x41=65, so (at least) the first two digits of each line are correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM