极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

楼主: shenhaiyu

十大滤波算法程序大全(Arduino精编无错版)

  [复制链接]
发表于 2013-11-2 17:27:14 | 显示全部楼层
"算法"到最后最是数学问题,整理再多,不会用全白搭!

就是这些源码,直接抄,用在你的代码里,你不明白原理,还是白搭!

所以,算法,其实个人认为可以归为数学类,整理代码出来,没有任何意义........

(楼主莫怪,个人见解而已,对楼主的劳动,表示支持)
回复 支持 反对

使用道具 举报

发表于 2013-11-2 19:18:02 | 显示全部楼层
建议编辑一下这个帖子作为滤波专用的,这样大家查起来也方便。下面是卡尔曼滤波,不是扩展的,但是输出平稳的俯仰和滚转应该够了(凑乎用吧我也不是专业写代码的,欢迎大家拍)
  1. #include <Wire.h> // I2C library, gyroscope

  2. // Accelerometer ADXL345
  3. #define ACC (0x53)    //ADXL345 ACC address
  4. #define A_TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)


  5. // Gyroscope ITG3200
  6. #define GYRO 0x68 // gyro address, binary = 11101000 when AD0 is connected to Vcc (see schematics of your breakout board)
  7. #define G_SMPLRT_DIV 0x15   
  8. #define G_DLPF_FS 0x16   
  9. #define G_INT_CFG 0x17
  10. #define G_PWR_MGM 0x3E

  11. #define G_TO_READ 8 // 2 bytes for each axis x, y, z


  12. // offsets are chip specific.
  13. int a_offx = 0;
  14. int a_offy = 0;
  15. int a_offz = 0;

  16. int g_offx = 0;
  17. int g_offy = 0;
  18. int g_offz = 0;
  19. ////////////////////////

  20. ////////////////////////
  21. char str[512];

  22. void initAcc() {
  23.   //Turning on the ADXL345
  24.   writeTo(ACC, 0x2D, 0);      
  25.   writeTo(ACC, 0x2D, 16);
  26.   writeTo(ACC, 0x2D, 8);
  27.   //by default the device is in +-2g range reading
  28. }

  29. void getAccelerometerData(int* result) {
  30.   int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
  31.   byte buff[A_TO_READ];
  32.   
  33.   readFrom(ACC, regAddress, A_TO_READ, buff); //read the acceleration data from the ADXL345
  34.   
  35.   //each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
  36.   //thus we are converting both bytes in to one int
  37.   result[0] = (((int)buff[1]) << 8) | buff[0] + a_offx;   
  38.   result[1] = (((int)buff[3]) << 8) | buff[2] + a_offy;
  39.   result[2] = (((int)buff[5]) << 8) | buff[4] + a_offz;
  40. }

  41. //initializes the gyroscope
  42. void initGyro()
  43. {
  44.   /*****************************************
  45.   * ITG 3200
  46.   * power management set to:
  47.   * clock select = internal oscillator
  48.   *     no reset, no sleep mode
  49.   *   no standby mode
  50.   * sample rate to = 125Hz
  51.   * parameter to +/- 2000 degrees/sec
  52.   * low pass filter = 5Hz
  53.   * no interrupt
  54.   ******************************************/
  55.   writeTo(GYRO, G_PWR_MGM, 0x00);
  56.   writeTo(GYRO, G_SMPLRT_DIV, 0x07); // EB, 50, 80, 7F, DE, 23, 20, FF
  57.   writeTo(GYRO, G_DLPF_FS, 0x1E); // +/- 2000 dgrs/sec, 1KHz, 1E, 19
  58.   writeTo(GYRO, G_INT_CFG, 0x00);
  59. }


  60. void getGyroscopeData(int * result)
  61. {
  62.   /**************************************
  63.   Gyro ITG-3200 I2C
  64.   registers:
  65.   temp MSB = 1B, temp LSB = 1C
  66.   x axis MSB = 1D, x axis LSB = 1E
  67.   y axis MSB = 1F, y axis LSB = 20
  68.   z axis MSB = 21, z axis LSB = 22
  69.   *************************************/

  70.   int regAddress = 0x1B;
  71.   int temp, x, y, z;
  72.   byte buff[G_TO_READ];
  73.   
  74.   readFrom(GYRO, regAddress, G_TO_READ, buff); //read the gyro data from the ITG3200
  75.   
  76.   result[0] = ((buff[2] << 8) | buff[3]) + g_offx;
  77.   result[1] = ((buff[4] << 8) | buff[5]) + g_offy;
  78.   result[2] = ((buff[6] << 8) | buff[7]) + g_offz;
  79.   result[3] = (buff[0] << 8) | buff[1]; // temperature
  80.   
  81. }


  82. float xz=0,yx=0,yz=0;
  83. float p_xz=1,p_yx=1,p_yz=1;
  84. float q_xz=0.0025,q_yx=0.0025,q_yz=0.0025;
  85. float k_xz=0,k_yx=0,k_yz=0;
  86. float r_xz=0.25,r_yx=0.25,r_yz=0.25;
  87.   //int acc_temp[3];
  88.   //float acc[3];
  89.   int acc[3];
  90.   int gyro[4];
  91.   float Axz;
  92.   float Ayx;
  93.   float Ayz;
  94.   float t=0.025;
  95. void setup()
  96. {
  97.   Serial.begin(9600);
  98.   Wire.begin();
  99.   initAcc();
  100.   initGyro();
  101.   
  102. }

  103. //unsigned long timer = 0;
  104. //float o;
  105. void loop()
  106. {
  107.   
  108.   getAccelerometerData(acc);
  109.   getGyroscopeData(gyro);
  110.   //timer = millis();
  111.   sprintf(str, "%d,%d,%d,%d,%d,%d", acc[0],acc[1],acc[2],gyro[0],gyro[1],gyro[2]);
  112.   
  113.   //acc[0]=acc[0];
  114.   //acc[2]=acc[2];
  115.   //acc[1]=acc[1];
  116.   //r=sqrt(acc[0]*acc[0]+acc[1]*acc[1]+acc[2]*acc[2]);
  117.   gyro[0]=gyro[0]/ 14.375;
  118.   gyro[1]=gyro[1]/ (-14.375);
  119.   gyro[2]=gyro[2]/ 14.375;
  120.   
  121.    
  122.   Axz=(atan2(acc[0],acc[2]))*180/PI;
  123.   Ayx=(atan2(acc[0],acc[1]))*180/PI;
  124.   /*if((acc[0]!=0)&&(acc[1]!=0))
  125.     {
  126.       Ayx=(atan2(acc[0],acc[1]))*180/PI;
  127.     }
  128.     else
  129.     {
  130.       Ayx=t*gyro[2];
  131.     }*/
  132.   Ayz=(atan2(acc[1],acc[2]))*180/PI;
  133.   
  134.   
  135. //kalman filter
  136.   calculate_xz();
  137.   calculate_yx();
  138.   calculate_yz();
  139.   
  140.   //sprintf(str, "%d,%d,%d", xz_1, xy_1, x_1);
  141.   //Serial.print(xz);Serial.print(",");
  142.   //Serial.print(yx);Serial.print(",");
  143.   //Serial.print(yz);Serial.print(",");
  144.   //sprintf(str, "%d,%d,%d,%d,%d,%d", acc[0],acc[1],acc[2],gyro[0],gyro[1],gyro[2]);
  145.   //sprintf(str, "%d,%d,%d",gyro[0],gyro[1],gyro[2]);
  146.     Serial.print(Axz);Serial.print(",");
  147.     //Serial.print(Ayx);Serial.print(",");
  148.     //Serial.print(Ayz);Serial.print(",");
  149.   //Serial.print(str);
  150.   //o=gyro[2];//w=acc[2];
  151.   //Serial.print(o);Serial.print(",");
  152.   //Serial.print(w);Serial.print(",");
  153.   Serial.print("\n");

  154.   
  155.   //delay(50);
  156. }
  157. void calculate_xz()
  158. {

  159. xz=xz+t*gyro[1];
  160. p_xz=p_xz+q_xz;
  161. k_xz=p_xz/(p_xz+r_xz);
  162. xz=xz+k_xz*(Axz-xz);
  163. p_xz=(1-k_xz)*p_xz;
  164. }
  165. void calculate_yx()
  166. {
  167.   
  168.   yx=yx+t*gyro[2];
  169.   p_yx=p_yx+q_yx;
  170.   k_yx=p_yx/(p_yx+r_yx);
  171.   yx=yx+k_yx*(Ayx-yx);
  172.   p_yx=(1-k_yx)*p_yx;

  173. }
  174. void calculate_yz()
  175. {
  176.   yz=yz+t*gyro[0];
  177.   p_yz=p_yz+q_yz;
  178.   k_yz=p_yz/(p_yz+r_yz);
  179.   yz=yz+k_yz*(Ayz-yz);
  180.   p_yz=(1-k_yz)*p_yz;

  181. }


  182. //---------------- Functions
  183. //Writes val to address register on ACC
  184. void writeTo(int DEVICE, byte address, byte val) {
  185.    Wire.beginTransmission(DEVICE); //start transmission to ACC
  186.    Wire.write(address);        // send register address
  187.    Wire.write(val);        // send value to write
  188.    Wire.endTransmission(); //end transmission
  189. }


  190. //reads num bytes starting from address register on ACC in to buff array
  191. void readFrom(int DEVICE, byte address, int num, byte buff[]) {
  192.   Wire.beginTransmission(DEVICE); //start transmission to ACC
  193.   Wire.write(address);        //sends address to read from
  194.   Wire.endTransmission(); //end transmission
  195.   
  196.   Wire.beginTransmission(DEVICE); //start transmission to ACC
  197.   Wire.requestFrom(DEVICE, num);    // request 6 bytes from ACC
  198.   
  199.   int i = 0;
  200.   while(Wire.available())    //ACC may send less than requested (abnormal)
  201.   {
  202.     buff[i] = Wire.read(); // receive a byte
  203.     i++;
  204.   }
  205.   Wire.endTransmission(); //end transmission
  206. }
复制代码
回复 支持 3 反对 0

使用道具 举报

发表于 2013-11-3 01:07:07 | 显示全部楼层
这个好,收藏了,滤波用得上
回复 支持 反对

使用道具 举报

发表于 2013-11-3 10:26:27 | 显示全部楼层
拾瑞 发表于 2013-11-2 17:27
"算法"到最后最是数学问题,整理再多,不会用全白搭!

就是这些源码,直接抄,用在你的代码里,你不明白原理,还 ...

Arduino追根究底也是电子电路一种,模块什么,直接抄,组合起来,不明白原理也是白搭。稍有问题,就是抓瞎。“所以,Arduino,其实个人认为可以归为电子类,整理模块,库出来,没有任何意义?”

哈哈,开个玩笑。

进入正题:这些源码,没人整理,也就在哪里扔着,整理起来,后人学习起来方便更多。没有网上那些错误百出的代码,后进者可以少走很多弯路。
回复 支持 反对

使用道具 举报

发表于 2013-11-3 11:50:27 | 显示全部楼层
感谢楼主分享,收藏了以后肯定有用
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-4 11:32:19 | 显示全部楼层
zhangzhe0617 发表于 2013-11-2 19:18
建议编辑一下这个帖子作为滤波专用的,这样大家查起来也方便。下面是卡尔曼滤波,不是扩展的,但是输出平稳 ...

太棒了,感谢分享,我把你的楼层加到帖子的索引里~~~
回复 支持 反对

使用道具 举报

发表于 2013-11-4 11:51:57 | 显示全部楼层
灰常好。这才是本网站存在的意义之一。
回复 支持 反对

使用道具 举报

发表于 2013-11-4 14:36:07 | 显示全部楼层
shenhaiyu 发表于 2013-11-4 11:32
太棒了,感谢分享,我把你的楼层加到帖子的索引里~~~

希望有问题大家一起进步解决,而不是提问了没人理。。。。。。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-4 16:36:47 | 显示全部楼层
zhangzhe0617 发表于 2013-11-4 14:36
希望有问题大家一起进步解决,而不是提问了没人理。。。。。。

是啊~~         
回复 支持 反对

使用道具 举报

发表于 2013-11-6 23:31:47 | 显示全部楼层
卧槽,连卡尔曼滤波都有……
回复 支持 反对

使用道具 举报

发表于 2013-11-7 00:12:10 | 显示全部楼层
原本看标题真的以为是 “重复发旧帖”, 看到内容真是感激啊 !!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-7 10:46:02 | 显示全部楼层
tangmao48 发表于 2013-11-6 23:31
卧槽,连卡尔曼滤波都有……

吼吼,这个要感谢zhagnzhe0617
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-7 10:46:43 | 显示全部楼层
hmjack2008 发表于 2013-11-7 00:12
原本看标题真的以为是 “重复发旧帖”, 看到内容真是感激啊 !!

这个。。。。我也没那么无聊吧,不会发重复贴的
回复 支持 反对

使用道具 举报

发表于 2013-11-7 17:11:34 | 显示全部楼层
好贴必须顶!!
回复 支持 反对

使用道具 举报

发表于 2013-11-7 19:18:29 | 显示全部楼层
好帖!留名
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-16 19:19 , Processed in 0.052932 second(s), 26 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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