极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12998|回复: 1

求助帖:MPU6050 姿态识别

[复制链接]
发表于 2016-8-18 15:49:56 | 显示全部楼层 |阅读模式




Arduino程序:

  1. [code]
  2. #include "Wire.h"
  3. #include "I2Cdev.h"
  4. #include "MPU6050_6Axis_MotionApps20.h"

  5. MPU6050 mpu;
  6. uint8_t devStatus;
  7. uint16_t packetSize;
  8. uint16_t fifoCount;
  9. uint8_t fifoBuffer[64];
  10. Quaternion q;
  11. VectorFloat g;
  12. float YawPitchRoll[3];

  13. void setup() {
  14.     Wire.begin();
  15.     Serial.begin(9600);
  16.     mpu.initialize();
  17.     devStatus = mpu.dmpInitialize();
  18.     mpu.setDMPEnabled(true);
  19.     packetSize = mpu.dmpGetFIFOPacketSize();
  20. }


  21. void loop() {
  22.     fifoCount = mpu.getFIFOCount();
  23.     if (fifoCount == 1024){
  24.       mpu.resetFIFO();
  25.     }
  26.     else {
  27.         while (fifoCount < packetSize)
  28.              fifoCount = mpu.getFIFOCount();
  29.         mpu.getFIFOBytes(fifoBuffer,packetSize);
  30.         fifoCount -= packetSize;
  31.         mpu.dmpGetQuaternion(&q , fifoBuffer);
  32.         mpu.dmpGetGravity(&g , &q);
  33.         mpu.dmpGetYawPitchRoll(YawPitchRoll , &q , &g);

  34.         Serial.print('h');
  35.         Serial.print(',');
  36.         Serial.print(YawPitchRoll[0]);
  37.         Serial.print(',');
  38.         Serial.print(YawPitchRoll[1]);
  39.         Serial.print(',');
  40.         Serial.print(YawPitchRoll[2]);
  41.         Serial.println();
  42.         delay(300);
  43.     }
  44.     mpu.resetFIFO();
  45. }

复制代码
[/code]


Processing程序:
[pre lang="Processing" line="1"]import processing.serial.*;

Serial myPort;  // 创建串口对象myPort
PFont Font;
boolean firstSample = true;
float [] YawPitchRoll = new float[3];        
java.text.DecimalFormat df =new java.text.DecimalFormat("#.00");
void setup()
{
  size(600, 600, P3D);
  myPort = new Serial(this, "COM4", 9600);  
  Font = loadFont("David-30.vlw");
  textFont(Font);
  myPort.bufferUntil('\n');
}

void buildBoxShape() {
  
   
  beginShape(QUADS);
  fill(#CDB38B);
  vertex(-20, -5, 30);
  vertex(20, -5, 30);
  vertex(20, 5, 30);
  vertex(-20, 5, 30);

  //Z-
  fill(#8B795E);
  vertex(-20, -5, -30);
  vertex(20, -5, -30);
  vertex(20, 5, -30);
  vertex(-20, 5, -30);

  //X-
  fill(#FFFACD);
  vertex(-20, -5, -30);
  vertex(-20, -5, 30);
  vertex(-20, 5, 30);
  vertex(-20, 5, -30);

  //X+
  fill(#EEE9BF);
  vertex(20, -5, -30);
  vertex(20, -5, 30);
  vertex(20, 5, 30);
  vertex(20, 5, -30);

  //Y-
  fill(#CDC9A5);
  vertex(-20, -5, -30);
  vertex(20, -5, -30);
  vertex(20, -5, 30);
  vertex(-20, -5, 30);

  //Y+
  fill(#8B8970);
  vertex(-20, 5, -30);
  vertex(20, 5, -30);
  vertex(20, 5, 30);
  vertex(-20, 5, 30);

  endShape();

}

void drawCube() {  
  pushMatrix();
  translate(300, 450, 0);
  scale(4, 4, 4);

  rotateX(YawPitchRoll[2]);
  rotateY(-YawPitchRoll[0]);
  rotateZ(YawPitchRoll[1]);
  buildBoxShape();

  popMatrix();
}

void draw() {  
  float a,b,c,d;
  background(#ffffff);
  fill(#000000);

  //float temp_decoded = 35.0 + ((float) (temp + 13200)) / 280;
  text("Yaw :" + df.format(YawPitchRoll[0]*180/PI) +"°", 20, 50);
  text("Pitch:" + df.format(YawPitchRoll[2]*180/PI)+"°", 20, 150);
  text("Roll :" + df.format(YawPitchRoll[1]*180/PI) +"°", 20, 250);


   //display axes显示轴
  pushMatrix();
  translate(450, 150, 0);
  stroke(#008B8B);
  line(0, 0, 0, 100, 0, 0);
  stroke(#8B008B);
  line(0, 0, 0, 0, 100, 0);
  stroke(#8B0000);
  line(0, 0, 0, 0, 0, 100);
  if (YawPitchRoll[2] < 0){
     a = -sin(YawPitchRoll[2]);
     b = cos(-YawPitchRoll[2]);
  }
  else{
     a = sin(YawPitchRoll[2]);
     b = cos(YawPitchRoll[2]);
  }
  if (YawPitchRoll[0] < 0){
     c = -sin(YawPitchRoll[0]);
     d = cos(-YawPitchRoll[0]);
  }  
  else{
     c = sin(YawPitchRoll[0]);
     d = cos(YawPitchRoll[0]);   
  }

  line(0, 0, 0,100*b*d , 100*b*c, 100*a);
  popMatrix();
  stroke(#FFFFFF);
  drawCube();
}

void serialEvent(Serial p){
    String [] str;
    String inString = p.readString();
    if (inString != null){
      str = inString.split(",");
      if (str[0].charAt(0) == 'h'){
        println("device is working");
        YawPitchRoll[0] = Float.parseFloat(str[1]);
        YawPitchRoll[1] = Float.parseFloat(str[2]);
        YawPitchRoll[2] = Float.parseFloat(str[3]);
      }
    }
}[/code]


问题:没有移动MPU6050,但是YAW角度一直有变化,请教各位大神 这是什么原因? 如何解决?
视频:


回复

使用道具 举报

 楼主| 发表于 2016-8-18 15:53:00 | 显示全部楼层
Arduinoc程序:
  1. [code]
  2. #include "Wire.h"
  3. #include "I2Cdev.h"
  4. #include "MPU6050_6Axis_MotionApps20.h"

  5. MPU6050 mpu;
  6. uint8_t devStatus;
  7. uint16_t packetSize;
  8. uint16_t fifoCount;
  9. uint8_t fifoBuffer[64];
  10. Quaternion q;
  11. VectorFloat g;
  12. float YawPitchRoll[3];

  13. void setup() {
  14.     Wire.begin();
  15.     Serial.begin(9600);
  16.     mpu.initialize();
  17.     devStatus = mpu.dmpInitialize();
  18.     mpu.setDMPEnabled(true);
  19.     packetSize = mpu.dmpGetFIFOPacketSize();
  20. }


  21. void loop() {
  22.     fifoCount = mpu.getFIFOCount();
  23.     if (fifoCount == 1024){
  24.       mpu.resetFIFO();
  25.     }
  26.     else {
  27.         while (fifoCount < packetSize)
  28.              fifoCount = mpu.getFIFOCount();
  29.         mpu.getFIFOBytes(fifoBuffer,packetSize);
  30.         fifoCount -= packetSize;
  31.         mpu.dmpGetQuaternion(&q , fifoBuffer);
  32.         mpu.dmpGetGravity(&g , &q);
  33.         mpu.dmpGetYawPitchRoll(YawPitchRoll , &q , &g);

  34.         Serial.print('h');
  35.         Serial.print(',');
  36.         Serial.print(YawPitchRoll[0]);
  37.         Serial.print(',');
  38.         Serial.print(YawPitchRoll[1]);
  39.         Serial.print(',');
  40.         Serial.print(YawPitchRoll[2]);
  41.         Serial.println();
  42.         delay(300);
  43.     }
  44.     mpu.resetFIFO();
  45. }

复制代码
[/code]

致谢:
http://www.geek-workshop.com/for ... p;highlight=mpu6050
http://www.geek-workshop.com/for ... =mpu6050&page=1
http://www.geek-workshop.com/thread-1935-1-1.html
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 01:03 , Processed in 0.035581 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表