Jump to content
I2Cdevlib Forums

Weather station + MPU6050


Recommended Posts

Hi to all!

 

I am using the MPU-6050 sensor, among others, as a part of my graduation project.

I am trying to use the MPU6050 together with the BMP180 (and some other simpler sensors), and with some intuition I could manage to make them work together by just taking the raw data from the MPU6050. All my attempts to use the needed libraries to acquire the yaw/pitch/roll data format and take into account the DMP and the fifo buffer the sensor itself has while using other sensors in the same program have failed, maybe because I don't have so much experience with programming. I attach my code, because maybe you could help me to make it work. The MPU6050 example works, as far as I don't mix it with other sensors.

 

I use different functions as part of the main code, like these ones:

 

 

void initMPU6050()
{
    // initialize MPU6050
    Wire.beginTransmission(MPU6050_Address);
    accelgyro.initialize();
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
    Wire.endTransmission(MPU6050_Address);
 
}

 

void readAccelgyro()

 

{
  
    int16_t ax, ay, az;
    int16_t gx, gy, gz;
    
    Wire.beginTransmission(MPU6050_Address);
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    if (!dmpReady) return;
 
    // wait for MPU interrupt or extra packet(s) available
    while (!mpuInterrupt && fifoCount < packetSize) {
        // other program behavior stuff here
        // .
        // .
        // .
        // if you are really paranoid you can frequently test in between other
        // stuff to see if mpuInterrupt is true, and if so, "break;" from the
        // while() loop to immediately process the MPU data
        // .
        // .
        // .
    }
 
    // reset interrupt flag and get INT_STATUS byte
    mpuInterrupt = false;
    mpuIntStatus = accelgyro.getIntStatus();
 
    // get current FIFO count
    fifoCount = accelgyro.getFIFOCount();
 
    // check for overflow (this should never happen unless our code is too inefficient)
    if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
        // reset so we can continue cleanly
        accelgyro.resetFIFO();
        Serial.println(F("FIFO overflow!"));
 
    // otherwise, check for DMP data ready interrupt (this should happen frequently)
    } else if (mpuIntStatus & 0x02) {
        // wait for correct available data length, should be a VERY short wait
        while (fifoCount < packetSize) fifoCount = accelgyro.getFIFOCount();
 
        // read a packet from FIFO
        accelgyro.getFIFOBytes(fifoBuffer, packetSize);
        
        // track FIFO count here in case there is > 1 packet available
        // (this lets us immediately read more without waiting for an interrupt)
        fifoCount -= packetSize;
        
        accelgyro.dmpGetQuaternion(&q, fifoBuffer);
        accelgyro.dmpGetGravity(&gravity, &q);
        accelgyro.dmpGetYawPitchRoll(ypr, &q, &gravity);}
       
    Wire.endTransmission(MPU6050_Address);
    
    //store data
    data.ax = ax;
    data.ay = ay;
    data.az = az;
    data.gx = gx;
    data.gy = gy;
    data.gz = gz;
    data.yaw =  ypr[0] * 180/M_PI;
    data.pitch = ypr[1] * 180/M_PI;
    data.roll = ypr[2] * 180/M_PI;
}

 

 

But all I get from the ypr variables are zeros.

 

Any help would be really appreciated.

Kind regards,

María. 

 

 

Link to comment
Share on other sites

Here's an update: I've changed one of the functions:

 

 

void initMPU6050()

{
    // initialize MPU6050
    Wire.beginTransmission(MPU6050_Address);
    accelgyro.initialize();
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
    devStatus = accelgyro.dmpInitialize();
 
    // make sure it worked (returns 0 if so)
    if (devStatus == 0) {
        // turn on the DMP, now that it's ready
        Serial.println(F("Enabling DMP..."));
        accelgyro.setDMPEnabled(true);
 
        // enable Arduino interrupt detection
        Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
        attachInterrupt(0, dmpDataReady, RISING);
        mpuIntStatus = accelgyro.getIntStatus();
 
        // set our DMP Ready flag so the main loop() function knows it's okay to use it
        Serial.println(F("DMP ready! Waiting for first interrupt..."));
        dmpReady = true;
 
        // get expected DMP packet size for later comparison
        packetSize = accelgyro.dmpGetFIFOPacketSize();
    } else {
        // ERROR!
        // 1 = initial memory load failed
        // 2 = DMP configuration updates failed
        // (if it's going to break, usually the code will be 1)
        Serial.print(F("DMP Initialization failed (code "));
        Serial.print(devStatus);
        Serial.println(F(")"));
    }
    
    Wire.endTransmission(MPU6050_Address);
 
}
 

 

 

But now the yaw doesn't work. There's almost no variation when I move the breadboard where the MPU is. Any thoughts at all?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...