极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 23297|回复: 6

根据malc《【翻译】加速度计和陀螺仪指南 》写的mpu6050程序·

[复制链接]
发表于 2013-8-24 19:09:03 | 显示全部楼层 |阅读模式
本帖最后由 cyq456 于 2013-8-24 21:02 编辑

看了这贴http://www.geek-workshop.com/thread-1695-1-1.html都有几天了,有所领悟,所以根据http://starlino.com/imu_kalman_arduino.html这贴写了一个mpu6050可以用的程序。
————————————————分割线—————————————————————————————————
其中RwEst[0] 为处理后x轴的加速度, RwEst[1] 为处理后y轴的加速度RwEst[z] 为处理后z轴的加速度。
处理后的加速度可以直接用于求角度。
想求角度可以参考这贴http://www.geek-workshop.com/for ... 328&page=1#pid27876[/url]  
代码:
  1. #include "Wire.h"
  2. #include "I2Cdev.h"
  3. #include "MPU6050.h"
  4. MPU6050 accelgyro;
  5. #define PI 3.14159265358979f
  6. #define INPUT_COUNT 5
  7. #define Gz_offset 0.30
  8. #define Gy_offset 1.09
  9. #define Gx_offset -1.94
  10. int16_t ax, ay, az;
  11. int16_t gx, gy, gz;
  12. int an[INPUT_COUNT];
  13. char firstSample;
  14. float wGyro;
  15. float RwEst[3];
  16. unsigned long lastMicros;
  17. unsigned long interval;
  18. float RwAcc[3];
  19. float RwGyro[3];
  20. float Awz[2];
  21. bool blinkState = false;
  22. void setup()
  23. {
  24.   Serial.begin(38400);
  25.   Wire.begin();
  26.   accelgyro.initialize();
  27.   wGyro=10;
  28.   firstSample=1;
  29. }
  30. void loop()
  31. {
  32.   getEstimatedInclination();
  33.   Serial.print(interval);
  34.   Serial.print(",");   
  35.   Serial.print(RwAcc[0]);  //Inclination X axis (as measured by accelerometer)
  36.   Serial.print(",");
  37.   Serial.print(RwEst[0]);  //Inclination X axis (estimated / filtered)
  38.   Serial.print(",");   
  39.   Serial.print(RwAcc[1]);  //Inclination Y axis (as measured by accelerometer)
  40.   Serial.print(",");
  41.   Serial.print(RwEst[1]);  //Inclination Y axis (estimated / filtered)
  42.   Serial.print(",");   
  43.   Serial.print(RwAcc[2]);  //Inclination Z axis (as measured by accelerometer)
  44.   Serial.print(",");
  45.   Serial.print(RwEst[2]);  //Inclination Z axis (estimated / filtered)  
  46.   Serial.println("");
  47.   blinkState = !blinkState;
  48. }

  49. void getEstimatedInclination()
  50. {
  51.   static int i,w;
  52.   static float tmpf,tmpf2;
  53.   static unsigned long newMicros;
  54.   static char signRzGyro;
  55.   newMicros=millis();
  56.   Get_an();
  57.   interval=newMicros-lastMicros;   
  58.   lastMicros = newMicros;
  59.   for(w=0;w<=2;w++) RwAcc[w]=getInput(w);
  60.   normalize3DVector(RwAcc);
  61.   if (firstSample)
  62.   {
  63.     for(w=0;w<=2;w++) RwEst[w]=RwAcc[w];    //initialize with accelerometer readings
  64.   }
  65.   else
  66.   {
  67.     if(abs(RwEst[2])<0.1)
  68.     {
  69.       for(w=0;w<=2;w++) RwGyro[w]=RwEst[w];
  70.     }
  71.     else
  72.     {
  73.       for(w=0;w<=1;w++)
  74.       {
  75.         tmpf=getInput(3+w);
  76.         tmpf *= interval / 1,000.0f;
  77.         Awz[w] = atan2(RwEst[w],RwEst[2]) * 180 /PI;
  78.         Awz[w] += tmpf;
  79.       }
  80.       signRzGyro=(cos(Awz[0]*PI/180)>=0)?1:-1;
  81.       for(w=0;w<=1;w++)
  82.       {
  83.         RwGyro[w] = sin(Awz[w]*PI/180);
  84.         RwGyro[w]/=sqrt(1+squared(cos(Awz[w]*PI/180))*squared(tan(Awz[1-w]*PI/180)) );
  85.       }
  86.        RwGyro[2]=signRzGyro*sqrt(1-squared(RwGyro[0])-squared(RwGyro[1]));
  87.     }
  88.     for(w=0;w<=2;w++) RwEst[w] = (RwAcc[w] + wGyro* RwGyro[w])/(1 + wGyro);
  89.     normalize3DVector(RwEst);
  90.   }
  91.   firstSample = 0;
  92. }


  93. float squared(float x)
  94. {
  95.   return x*x;
  96. }


  97. float getInput(char i)
  98. {
  99.   static float tmpf;
  100.   if(i<=2) tmpf=an[i]/16384.0;
  101.   else if(i==3) tmpf=an[i]/131.0-Gx_offset;
  102.   else if(i==4) tmpf=an[i]/131.0-Gy_offset;
  103.   else tmpf=an[i]/131.0-Gz_offset;
  104.   return tmpf;
  105. }


  106. void normalize3DVector(float* vector)
  107. {
  108.   static float R;  
  109.   R = sqrt(vector[0]*vector[0]+vector[1]*vector[1]+vector[2]*vector[2]);
  110.   vector[0] /= R;
  111.   vector[1] /= R;  
  112.   vector[2] /= R;  
  113. }


  114. void Get_an()
  115. {
  116.   accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  117.   an[0]=ax;
  118.   an[1]=ay;
  119.   an[2]=az;
  120.   an[3]=gx;
  121.   an[4]=gy;
  122.   an[5]=gz;
  123. }
复制代码





回复

使用道具 举报

发表于 2013-8-25 09:59:11 | 显示全部楼层
调试效果怎样???
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-8-25 23:12:19 来自手机 | 显示全部楼层
学慧放弃 发表于 2013-8-25 09:59
调试效果怎样???

比没处理的数据稳定多了
回复 支持 反对

使用道具 举报

发表于 2013-8-26 16:59:48 | 显示全部楼层
cyq456 发表于 2013-8-25 23:12
比没处理的数据稳定多了

滤波效果怎样?
回复 支持 反对

使用道具 举报

发表于 2013-8-26 17:00:23 | 显示全部楼层
cyq456 发表于 2013-8-25 23:12
比没处理的数据稳定多了

滤波效果怎样?
回复 支持 反对

使用道具 举报

发表于 2015-6-19 09:56:58 | 显示全部楼层
请问这个可以用在51单片机上吗?
回复 支持 反对

使用道具 举报

发表于 2017-7-8 14:44:56 | 显示全部楼层
想問#define Gz_offset 0.30
#define Gy_offset 1.09
#define Gx_offset -1.94
GX GY GZ這三個值是如何得出來的
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-13 17:57 , Processed in 0.045554 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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