Calculating the angle of two MPU6050 simultaneously


hello!

i looking way measure angles 2 mpu6050 on same adruino simultaneously. attached sketch using.

mpu#1 works fine , nice data. however, data output mpu#2. values around 134 , not change when moving mpu. did wring calculation or wiring? ado (0x69??) of mpu#2 not connected gnd, connecting gnd not help.

i hope can help!


////////////////////////////////////////////////////////////////
// wiring of hardware
//
// arduino - mpu1 - mpu2
// a4 - sda1 - sda2
// a5 - scl1 - scl2
// 3.3 - vcc1 - vcc2- ado2
// gnd - gnd1 - gnd2
//
///////////////////////////////////////////////////////////////




#include<wire.h>
const int mpu_addr=0x68;
const int mpu_addr1=0x69;
double acx,acy,acz,tmp,gyx,gyy,gyz;
double acx1,acy1,acz1,tmp1,gyx1,gyy1,gyz1;
uint32_t timer;
double companglex, compangley;
double companglex1, compangley1;
#define degconvert 57.2957786



void setup() {
  // set mpu 6050:
  wire.begin();
  #if arduino >= 157
  wire.setclock(400000ul); // set i2c frequency 400khz
  #else
    twbr = ((f_cpu / 400000ul) - 16) / 2; // set i2c frequency 400khz
  #endif

 
  wire.begintransmission(mpu_addr);
  wire.begintransmission(mpu_addr1);
  wire.write(0x6b); 
  wire.write(0);     
  wire.endtransmission(true);
  serial.begin(9600);
  serial.println("cleardata");
  serial.println("label, time, companglex, compangley, companglex1, compangley1");
  serial.println("initializing i2c devices...");
  delay(100);

  //setup starting angle
  //1) collect data
  wire.begintransmission(mpu_addr);
  wire.begintransmission(mpu_addr1);
  wire.write(0x3b); 
  wire.endtransmission(false);
  wire.requestfrom(mpu_addr,14,true); 
  wire.requestfrom(mpu_addr1,14,true); 
  acx=wire.read()<<8|wire.read();     
  acy=wire.read()<<8|wire.read(); 
  acz=wire.read()<<8|wire.read(); 
  tmp=wire.read()<<8|wire.read();
  gyx=wire.read()<<8|wire.read(); 
  gyy=wire.read()<<8|wire.read(); 
  gyz=wire.read()<<8|wire.read(); 
  acx1=wire.read()<<8|wire.read();     
  acy1=wire.read()<<8|wire.read(); 
  acz1=wire.read()<<8|wire.read(); 
  tmp1=wire.read()<<8|wire.read(); 
  gyx1=wire.read()<<8|wire.read(); 
  gyy1=wire.read()<<8|wire.read(); 
  gyz1=wire.read()<<8|wire.read(); 
 

  double roll = atan2(acy, acz)*degconvert;
  double pitch = atan2(-acx, acz)*degconvert;
  double roll1 = atan2(acy1, acz1)*degconvert;
  double pitch1 = atan2(-acx1, acz1)*degconvert;
 
 
  double gyroxangle = roll;
  double gyroyangle = pitch;
  double companglex = roll;
  double compangley = pitch;
  double gyroxangle1 = roll1;
  double gyroyangle1 = pitch1;
  double companglex1 = roll1;
  double compangley1 = pitch1;

  //start timer
  timer = micros();
}

void loop() {
  wire.begintransmission(mpu_addr);
  wire.begintransmission(mpu_addr1);
  wire.write(0x3b); 
  wire.endtransmission(false);
  wire.requestfrom(mpu_addr,14,true);
  wire.requestfrom(mpu_addr1,14,true);
  acx=wire.read()<<8|wire.read();       
  acy=wire.read()<<8|wire.read(); 
  acz=wire.read()<<8|wire.read(); 
  tmp=wire.read()<<8|wire.read(); 
  gyx=wire.read()<<8|wire.read(); 
  gyy=wire.read()<<8|wire.read(); 
  gyz=wire.read()<<8|wire.read(); 
  acx1=wire.read()<<8|wire.read();     
  acy1=wire.read()<<8|wire.read(); 
  acz1=wire.read()<<8|wire.read(); 
  tmp1=wire.read()<<8|wire.read(); 
  gyx1=wire.read()<<8|wire.read(); 
  gyy1=wire.read()<<8|wire.read();
  gyz1=wire.read()<<8|wire.read();
 
  double dt = (double)(micros() - timer) / 1000000;
  double dt1 = (double)(micros() - timer) / 1000000;
  timer = micros();
 
  double roll = atan2(acy, acz)*degconvert;
  double pitch = atan2(-acx, acz)*degconvert;
  double roll1 = atan2(acy1, acz1)*degconvert;
  double pitch1 = atan2(-acx1, acz1)*degconvert;
 
  double gyroxrate = gyx/131.0;
  double gyroyrate = gyy/131.0;
  double gyroxrate1 = gyx1/131.0;
  double gyroyrate1 = gyy1/131.0;
 
  companglex = 0.99 * (companglex + gyroxrate * dt) + 0.01 * roll;
  compangley = 0.99 * (compangley + gyroyrate * dt) + 0.01 * pitch;
  companglex1 = 0.99 * (companglex1 + gyroxrate1 * dt1) + 0.01 * roll1;
  compangley1 = 0.99 * (compangley1 + gyroyrate1 * dt1) + 0.01 * pitch1;
 
  serial.print("data, time,"); serial.print (companglex, 4); 
 serial.print(","); serial.print (compangley, 4);
 serial.print(","); serial.print (companglex1, 4); 
 serial.print(","); serial.println (compangley1, 4);
 }


Arduino Forum > Using Arduino > Sensors > Calculating the angle of two MPU6050 simultaneously


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts