戊辰寒 发表于 2012-12-28 17:48:09

yawpitchroll角度问题

本帖最后由 戊辰寒 于 2012-12-28 20:47 编辑

设定了陀螺仪跟加速度计的精度,设定了数字高低通滤波器。

测试结果是只有FIFO数据溢出复位以后的第一行数据可信,其他的都乱跳……


如红框所示,分别对应Yaw(Z轴) Pitch(Y轴) Roll(X轴)


图片引自wikipedia.org

请教各位大神如何只提取第一行的数据?


下面是我的程序:

// I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class
// 10/7/2011 by Jeff Rowberg <[email protected]>
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
//   2011-10-07 - initial release

/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2011 Jeff Rowberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#include "Wire.h"

// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 mpu;


#define LED_PIN 13
bool blinkState = false;

bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently in FIFO
uint8_t fifoBuffer; // FIFO storage buffer
int16_t ax, ay, az;
int16_t gx, gy, gz;
int8_t offset_x, offset_y, offset_z;


// orientation/motion vars
Quaternion q; // quaternion container
VectorInt16 aa; // accel sensor measurements
VectorInt16 aaReal; // gravity-free accel sensor measurements
VectorInt16 aaWorld; // world-frame accel sensor measurements
VectorFloat gravity; // gravity vector
float euler; // Euler angle container
float ypr; // yaw/pitch/roll container and gravity vector

// ================================================================
// === INTERRUPT DETECTION ROUTINE ===
// ================================================================

volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
    mpuInterrupt = true;
}



// ================================================================
// === INITIAL SETUP ===
// ================================================================

void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    Wire.begin();

    // initialize serial communication
    // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
    // it's really up to you depending on your project)
    Serial.begin(9600);
    while (!Serial); // wait for Leonardo enumeration, others continue immediately

    // NOTE: 8MHz or slower host processors, like the Teensy @ 3.3v or Ardunio
    // Pro Mini running at 3.3v, cannot handle this baud rate reliably due to
    // the baud timing being too misaligned with processor ticks. You must use
    // 38400 or slower in these cases, or use some kind of external separate
    // crystal solution for the UART timer.
    // initialize device
    Serial.println("Initializing I2C devices...");
    mpu.initialize();

    // verify connection
    Serial.println("Testing device connections...");
    Serial.println(mpu.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");


    //mpu.setXGyroOffset(6);mpu.setYGyroOffset(-2);
    //mpu.setZGyroOffset(0);
   
    /*Serial.print("Offset:\t");
    Serial.print(mpu.getXAccelOffset()); Serial.print("\t");
    Serial.print(mpu.getYAccelOffset()); Serial.print("\t");
    Serial.print(mpu.getZAccelOffset()); Serial.print("\t");
    Serial.print(mpu.getXGyroOffset()); Serial.print("\t");
    Serial.print(mpu.getYGyroOffset()); Serial.print("\t");
    Serial.println(mpu.getZGyroOffset());
    Serial.print("Gain:\t");
    Serial.print(mpu.getXFineGain()); Serial.print("\t");
    Serial.print(mpu.getYFineGain()); Serial.print("\t");
    Serial.println(mpu.getZFineGain());// Serial.print("\t");
    */   


    mpu.setFullScaleGyroRange(3);
    mpu.setFullScaleAccelRange(3);
   
    mpu.setDLPFMode(6);
    mpu.setDHPFMode(1);
      
    // load and configure the DMP
    Serial.println("Initializing DMP...");
    devStatus = mpu.dmpInitialize();
   
    // make sure it worked (returns 0 if so)
    if (devStatus == 0) {
      // turn on the DMP, now that it's ready
      Serial.println("Enabling DMP...");
      mpu.setDMPEnabled(true);

      // enable Arduino interrupt detection
      Serial.println("Enabling interrupt detection (Arduino external interrupt 0)...");
      attachInterrupt(0, dmpDataReady, RISING);
      mpuIntStatus = mpu.getIntStatus();

      // set our DMP Ready flag so the main loop() function knows it's okay to use it
      Serial.println("DMP ready! Waiting for first interrupt...");
      dmpReady = true;

      // get expected DMP packet size for later comparison
      packetSize = mpu.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("DMP Initialization failed (code ");
      Serial.print(devStatus);
      Serial.println(")");
    }

      
    // configure Arduino LED for
    pinMode(LED_PIN, OUTPUT);
}

void loop() {

    // if programming failed, don't try to do anything
    if (!dmpReady) return;

    // wait for MPU interrupt or extra packet(s) available
    if (!mpuInterrupt && fifoCount < packetSize) return;

    // reset interrupt flag and get INT_STATUS byte
    mpuInterrupt = false;
    mpuIntStatus = mpu.getIntStatus();

    // get current FIFO count
    fifoCount = mpu.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
      mpu.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 = mpu.getFIFOCount();

      // read a packet from FIFO
      mpu.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;
   
            /*display Euler angles in degrees
            通过欧拉角度显示偏航*/
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            mpu.dmpGetGravity(&gravity, &q);
            mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
            Serial.print("ypr\t");
            Serial.print(ypr * 180/M_PI);
            Serial.print("\t");
            Serial.print(ypr * 180/M_PI);
            Serial.print("\t");
            Serial.println(ypr * 180/M_PI);


    // read raw accel/gyro measurements from device
    //mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

    // these methods (and a few others) are also available
    //accelgyro.getAcceleration(&ax, &ay, &az);
    //accelgyro.getRotation(&gx, &gy, &gz);
      
    // display tab-separated accel/gyro x/y/z values
    //Serial.print("a/g:\t");
    /*Serial.print(ax); Serial.print(",");
    Serial.print(ay); Serial.print(",");
    Serial.print(az); Serial.print(",");
    Serial.print(gx); Serial.print(",");
    Serial.print(gy); Serial.print(",");
    Serial.println(gz);*/

    // blink LED to indicate activity
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
   }
}

pfhnr 发表于 2013-4-3 16:07:18

解决了吗?我也遇到同样的问题

lb_8848 发表于 2013-5-4 17:24:10

遇到同样的问题了有人解决了么?

mebelove 发表于 2013-5-8 23:32:10

同样的问题,如果添加delay,300ms就可以,但是不理解。这样速率太慢了。

wunanyx 发表于 2013-5-9 15:36:42

采集回来,自己编程

Microsoft 发表于 2013-5-11 21:33:21

我的6050工作一会就停止发送数据了,楼主你碰到这个情况了吗

wujingyu 发表于 2014-10-23 10:58:59

电机运行了吗?可能是电机产生的干扰造成的

wujingyu 发表于 2014-10-23 11:00:27

mebelove 发表于 2013-5-8 23:32 static/image/common/back.gif
同样的问题,如果添加delay,300ms就可以,但是不理解。这样速率太慢了。

请问,你的delay(300)加在什么地方就好了?
页: [1]
查看完整版本: yawpitchroll角度问题