十大濾波算法程序大全(Arduino精編無錯版)

在極客工坊看到這篇帖子,覺得非常不錯,轉載之留存。 原文鏈接:http://www.geek-workshop.com/thread-7694-1-1.html




作者:shenhaiyu 2013-11-01
最近用Arduino做電子秤,爲了解決數據的跳變研究了不少濾波算法。網上能找到大把的十大濾波算法帖子,每一篇都不太一樣,都號稱精編啊,除錯啊什麼的,可是放到板子裏卻沒一個能正常跑起來的。於是決定自己整理一下這些程序,完美移植到Arduino中。

所以大家看到這個帖子的時候,不要懷疑我重複發帖。我的代碼都是經過反覆試驗,複製到Arduino中就能開跑的成品代碼,移植到自己的程序中非常方便。而且都仔細研究了各個算法,把錯誤都修正了的(別的程序連冒泡算法都是溢出的,不信自己找來細看看),所以也算個小原創吧,在別人基礎上的原創。


1、限幅濾波法(又稱程序判斷濾波法)
2、中位值濾波法
3、算術平均濾波法
4、遞推平均濾波法(又稱滑動平均濾波法)
5、中位值平均濾波法(又稱防脈衝干擾平均濾波法)
6、限幅平均濾波法
7、一階滯後濾波法
8、加權遞推平均濾波法
9、消抖濾波法
10、限幅消抖濾波法
11、新增加 卡爾曼濾波(非擴展卡爾曼),代碼在17樓(點擊這裏)感謝zhangzhe0617分享

程序默認對int類型數據進行濾波,如需要對其他類型進行濾波,只需要把程序中所有int替換成long、float或者double即可。



1、限幅濾波法(又稱程序判斷濾波法)

  1. /*
  2. A、名稱:限幅濾波法(又稱程序判斷濾波法)
  3. B、方法:
  4.     根據經驗判斷,確定兩次採樣允許的最大偏差值(設爲A),
  5.     每次檢測到新值時判斷:
  6.     如果本次值與上次值之差<=A,則本次值有效,
  7.     如果本次值與上次值之差>A,則本次值無效,放棄本次值,用上次值代替本次值。
  8. C、優點:
  9.     能有效克服因偶然因素引起的脈衝干擾。
  10. D、缺點:
  11.     無法抑制那種週期性的干擾。
  12.     平滑度差。
  13. E、整理:shenhaiyu 2013-11-01
  14. */

  15. int Filter_Value;
  16. int Value;

  17. void setup() {
  18.   Serial.begin(9600);       // 初始化串口通信
  19.   randomSeed(analogRead(0)); // 產生隨機種子
  20.   Value = 300;
  21. }

  22. void loop() {
  23.   Filter_Value = Filter();       // 獲得濾波器輸出值
  24.   Value = Filter_Value;          // 最近一次有效採樣的值,該變量爲全局變量
  25.   Serial.println(Filter_Value); // 串口輸出
  26.   delay(50);
  27. }

  28. // 用於隨機產生一個300左右的當前值
  29. int Get_AD() {
  30.   return random(295, 305);
  31. }

  32. // 限幅濾波法(又稱程序判斷濾波法)
  33. #define FILTER_A 1
  34. int Filter() {
  35.   int NewValue;
  36.   NewValue = Get_AD();
  37.   if(((NewValue - Value) > FILTER_A) || ((Value - NewValue) > FILTER_A))
  38.     return Value;
  39.   else
  40.     return NewValue;
  41. }
複製代碼




2、中位值濾波法
  1. /*
  2. A、名稱:中位值濾波法
  3. B、方法:
  4.     連續採樣N次(N取奇數),把N次採樣值按大小排列,
  5.     取中間值爲本次有效值。
  6. C、優點:
  7.     能有效克服因偶然因素引起的波動干擾;
  8.     對溫度、液位的變化緩慢的被測參數有良好的濾波效果。
  9. D、缺點:
  10.     對流量、速度等快速變化的參數不宜。
  11. E、整理:shenhaiyu 2013-11-01
  12. */

  13. int Filter_Value;

  14. void setup() {
  15.   Serial.begin(9600);       // 初始化串口通信
  16.   randomSeed(analogRead(0)); // 產生隨機種子
  17. }

  18. void loop() {
  19.   Filter_Value = Filter();       // 獲得濾波器輸出值
  20.   Serial.println(Filter_Value); // 串口輸出
  21.   delay(50);
  22. }

  23. // 用於隨機產生一個300左右的當前值
  24. int Get_AD() {
  25.   return random(295, 305);
  26. }

  27. // 中位值濾波法
  28. #define FILTER_N 101
  29. int Filter() {
  30.   int filter_buf[FILTER_N];
  31.   int i, j;
  32.   int filter_temp;
  33.   for(i = 0; i < FILTER_N; i++) {
  34.     filter_buf[i] = Get_AD();
  35.     delay(1);
  36.   }
  37.   // 採樣值從小到大排列(冒泡法)
  38.   for(j = 0; j < FILTER_N - 1; j++) {
  39.     for(i = 0; i < FILTER_N - 1 - j; i++) {
  40.       if(filter_buf[i] > filter_buf[i + 1]) {
  41.         filter_temp = filter_buf[i];
  42.         filter_buf[i] = filter_buf[i + 1];
  43.         filter_buf[i + 1] = filter_temp;
  44.       }
  45.     }
  46.   }
  47.   return filter_buf[(FILTER_N - 1) / 2];
  48. }
複製代碼






3、算術平均濾波法

  1. /*
  2. A、名稱:算術平均濾波法
  3. B、方法:
  4.     連續取N個採樣值進行算術平均運算:
  5.     N值較大時:信號平滑度較高,但靈敏度較低;
  6.     N值較小時:信號平滑度較低,但靈敏度較高;
  7.     N值的選取:一般流量,N=12;壓力:N=4。
  8. C、優點:
  9.     適用於對一般具有隨機干擾的信號進行濾波;
  10.     這種信號的特點是有一個平均值,信號在某一數值範圍附近上下波動。
  11. D、缺點:
  12.     對於測量速度較慢或要求數據計算速度較快的實時控制不適用;
  13.     比較浪費RAM。
  14. E、整理:shenhaiyu 2013-11-01
  15. */

  16. int Filter_Value;

  17. void setup() {
  18.   Serial.begin(9600);       // 初始化串口通信
  19.   randomSeed(analogRead(0)); // 產生隨機種子
  20. }

  21. void loop() {
  22.   Filter_Value = Filter();       // 獲得濾波器輸出值
  23.   Serial.println(Filter_Value); // 串口輸出
  24.   delay(50);
  25. }

  26. // 用於隨機產生一個300左右的當前值
  27. int Get_AD() {
  28.   return random(295, 305);
  29. }

  30. // 算術平均濾波法
  31. #define FILTER_N 12
  32. int Filter() {
  33.   int i;
  34.   int filter_sum = 0;
  35.   for(i = 0; i < FILTER_N; i++) {
  36.     filter_sum += Get_AD();
  37.     delay(1);
  38.   }
  39.   return (int)(filter_sum / FILTER_N);
  40. }
複製代碼


4、遞推平均濾波法(又稱滑動平均濾波法)


  1. /*
  2. A、名稱:遞推平均濾波法(又稱滑動平均濾波法)
  3. B、方法:
  4.     把連續取得的N個採樣值看成一個隊列,隊列的長度固定爲N,
  5.     每次採樣到一個新數據放入隊尾,並扔掉原來隊首的一次數據(先進先出原則),
  6.     把隊列中的N個數據進行算術平均運算,獲得新的濾波結果。
  7.     N值的選取:流量,N=12;壓力,N=4;液麪,N=4-12;溫度,N=1-4。
  8. C、優點:
  9.     對週期性干擾有良好的抑制作用,平滑度高;
  10.     適用於高頻振盪的系統。
  11. D、缺點:
  12.     靈敏度低,對偶然出現的脈衝性干擾的抑制作用較差;
  13.     不易消除由於脈衝干擾所引起的採樣值偏差;
  14.     不適用於脈衝干擾比較嚴重的場合;
  15.     比較浪費RAM。
  16. E、整理:shenhaiyu 2013-11-01
  17. */

  18. int Filter_Value;

  19. void setup() {
  20.   Serial.begin(9600);       // 初始化串口通信
  21.   randomSeed(analogRead(0)); // 產生隨機種子
  22. }

  23. void loop() {
  24.   Filter_Value = Filter();       // 獲得濾波器輸出值
  25.   Serial.println(Filter_Value); // 串口輸出
  26.   delay(50);
  27. }

  28. // 用於隨機產生一個300左右的當前值
  29. int Get_AD() {
  30.   return random(295, 305);
  31. }

  32. // 遞推平均濾波法(又稱滑動平均濾波法)
  33. #define FILTER_N 12
  34. int filter_buf[FILTER_N + 1];
  35. int Filter() {
  36.   int i;
  37.   int filter_sum = 0;
  38.   filter_buf[FILTER_N] = Get_AD();
  39.   for(i = 0; i < FILTER_N; i++) {
  40.     filter_buf[i] = filter_buf[i + 1]; // 所有數據左移,低位仍掉
  41.     filter_sum += filter_buf[i];
  42.   }
  43.   return (int)(filter_sum / FILTER_N);
  44. }
複製代碼



5、中位值平均濾波法(又稱防脈衝干擾平均濾波法)

  1. /*
  2. A、名稱:中位值平均濾波法(又稱防脈衝干擾平均濾波法)
  3. B、方法:
  4.     採一組隊列去掉最大值和最小值後取平均值,
  5.     相當於“中位值濾波法”+“算術平均濾波法”。
  6.     連續採樣N個數據,去掉一個最大值和一個最小值,
  7.     然後計算N-2個數據的算術平均值。
  8.     N值的選取:3-14。
  9. C、優點:
  10.     融合了“中位值濾波法”+“算術平均濾波法”兩種濾波法的優點。
  11.     對於偶然出現的脈衝性干擾,可消除由其所引起的採樣值偏差。
  12.     對週期干擾有良好的抑制作用。
  13.     平滑度高,適於高頻振盪的系統。
  14. D、缺點:
  15.     計算速度較慢,和算術平均濾波法一樣。
  16.     比較浪費RAM。
  17. E、整理:shenhaiyu 2013-11-01
  18. */

  19. int Filter_Value;

  20. void setup() {
  21.   Serial.begin(9600);       // 初始化串口通信
  22.   randomSeed(analogRead(0)); // 產生隨機種子
  23. }

  24. void loop() {
  25.   Filter_Value = Filter();       // 獲得濾波器輸出值
  26.   Serial.println(Filter_Value); // 串口輸出
  27.   delay(50);
  28. }

  29. // 用於隨機產生一個300左右的當前值
  30. int Get_AD() {
  31.   return random(295, 305);
  32. }

  33. // 中位值平均濾波法(又稱防脈衝干擾平均濾波法)(算法1)
  34. #define FILTER_N 100
  35. int Filter() {
  36.   int i, j;
  37.   int filter_temp, filter_sum = 0;
  38.   int filter_buf[FILTER_N];
  39.   for(i = 0; i < FILTER_N; i++) {
  40.     filter_buf[i] = Get_AD();
  41.     delay(1);
  42.   }
  43.   // 採樣值從小到大排列(冒泡法)
  44.   for(j = 0; j < FILTER_N - 1; j++) {
  45.     for(i = 0; i < FILTER_N - 1 - j; i++) {
  46.       if(filter_buf[i] > filter_buf[i + 1]) {
  47.         filter_temp = filter_buf[i];
  48.         filter_buf[i] = filter_buf[i + 1];
  49.         filter_buf[i + 1] = filter_temp;
  50.       }
  51.     }
  52.   }
  53.   // 去除最大最小極值後求平均
  54.   for(i = 1; i < FILTER_N - 1; i++) filter_sum += filter_buf[i];
  55.   return filter_sum / (FILTER_N - 2);
  56. }


  57. //  中位值平均濾波法(又稱防脈衝干擾平均濾波法)(算法2)
  58. /*
  59. #define FILTER_N 100
  60. int Filter() {
  61.   int i;
  62.   int filter_sum = 0;
  63.   int filter_max, filter_min;
  64.   int filter_buf[FILTER_N];
  65.   for(i = 0; i < FILTER_N; i++) {
  66.     filter_buf[i] = Get_AD();
  67.     delay(1);
  68.   }
  69.   filter_max = filter_buf[0];
  70.   filter_min = filter_buf[0];
  71.   filter_sum = filter_buf[0];
  72.   for(i = FILTER_N - 1; i > 0; i--) {
  73.     if(filter_buf[i] > filter_max)
  74.       filter_max=filter_buf[i];
  75.     else if(filter_buf[i] < filter_min)
  76.       filter_min=filter_buf[i];
  77.     filter_sum = filter_sum + filter_buf[i];
  78.     filter_buf[i] = filter_buf[i - 1];
  79.   }
  80.   i = FILTER_N - 2;
  81.   filter_sum = filter_sum - filter_max - filter_min + i / 2; // +i/2 的目的是爲了四捨五入
  82.   filter_sum = filter_sum / i;
  83.   return filter_sum;
  84. }*/
複製代碼






6、限幅平均濾波法

  1. /*
  2. A、名稱:限幅平均濾波法
  3. B、方法:
  4.     相當於“限幅濾波法”+“遞推平均濾波法”;
  5.     每次採樣到的新數據先進行限幅處理,
  6.     再送入隊列進行遞推平均濾波處理。
  7. C、優點:
  8.     融合了兩種濾波法的優點;
  9.     對於偶然出現的脈衝性干擾,可消除由於脈衝干擾所引起的採樣值偏差。
  10. D、缺點:
  11.     比較浪費RAM。
  12. E、整理:shenhaiyu 2013-11-01
  13. */

  14. #define FILTER_N 12
  15. int Filter_Value;
  16. int filter_buf[FILTER_N];

  17. void setup() {
  18.   Serial.begin(9600);       // 初始化串口通信
  19.   randomSeed(analogRead(0)); // 產生隨機種子
  20.   filter_buf[FILTER_N - 2] = 300;
  21. }

  22. void loop() {
  23.   Filter_Value = Filter();       // 獲得濾波器輸出值
  24.   Serial.println(Filter_Value); // 串口輸出
  25.   delay(50);
  26. }

  27. // 用於隨機產生一個300左右的當前值
  28. int Get_AD() {
  29.   return random(295, 305);
  30. }

  31. // 限幅平均濾波法
  32. #define FILTER_A 1
  33. int Filter() {
  34.   int i;
  35.   int filter_sum = 0;
  36.   filter_buf[FILTER_N - 1] = Get_AD();
  37.   if(((filter_buf[FILTER_N - 1] - filter_buf[FILTER_N - 2]) > FILTER_A) || ((filter_buf[FILTER_N - 2] - filter_buf[FILTER_N - 1]) > FILTER_A))
  38.     filter_buf[FILTER_N - 1] = filter_buf[FILTER_N - 2];
  39.   for(i = 0; i < FILTER_N - 1; i++) {
  40.     filter_buf[i] = filter_buf[i + 1];
  41.     filter_sum += filter_buf[i];
  42.   }
  43.   return (int)filter_sum / (FILTER_N - 1);
  44. }
複製代碼





7、一階滯後濾波法



    1. /*
    2. A、名稱:一階滯後濾波法
    3. B、方法:
    4.     取a=0-1,本次濾波結果=(1-a)*本次採樣值+a*上次濾波結果。
    5. C、優點:
    6.     對週期性干擾具有良好的抑制作用;
    7.     適用於波動頻率較高的場合。
    8. D、缺點:
    9.     相位滯後,靈敏度低;
    10.     滯後程度取決於a值大小;
    11.     不能消除濾波頻率高於採樣頻率1/2的干擾信號。
    12. E、整理:shenhaiyu 2013-11-01
    13. */

    14. int Filter_Value;
    15. int Value;

    16. void setup() {
    17.   Serial.begin(9600);       // 初始化串口通信
    18.   randomSeed(analogRead(0)); // 產生隨機種子
    19.   Value = 300;
    20. }

    21. void loop() {
    22.   Filter_Value = Filter();       // 獲得濾波器輸出值
    23.   Serial.println(Filter_Value); // 串口輸出
    24.   delay(50);
    25. }

    26. // 用於隨機產生一個300左右的當前值
    27. int Get_AD() {
    28.   return random(295, 305);
    29. }

    30. // 一階滯後濾波法
    31. #define FILTER_A 0.01
    32. int Filter() {
    33.   int NewValue;
    34.   NewValue = Get_AD();
    35.   Value = (int)((float)NewValue * FILTER_A + (1.0 - FILTER_A) * (float)Value);
    36.   return Value;
    37. }
    複製代碼







8、加權遞推平均濾波法


  1. /*
  2. A、名稱:加權遞推平均濾波法
  3. B、方法:
  4.     是對遞推平均濾波法的改進,即不同時刻的數據加以不同的權;
  5.     通常是,越接近現時刻的數據,權取得越大。
  6.     給予新採樣值的權係數越大,則靈敏度越高,但信號平滑度越低。
  7. C、優點:
  8.     適用於有較大純滯後時間常數的對象,和採樣週期較短的系統。
  9. D、缺點:
  10.     對於純滯後時間常數較小、採樣週期較長、變化緩慢的信號;
  11.     不能迅速反應系統當前所受干擾的嚴重程度,濾波效果差。
  12. E、整理:shenhaiyu 2013-11-01
  13. */

  14. int Filter_Value;

  15. void setup() {
  16.   Serial.begin(9600);       // 初始化串口通信
  17.   randomSeed(analogRead(0)); // 產生隨機種子
  18. }

  19. void loop() {
  20.   Filter_Value = Filter();       // 獲得濾波器輸出值
  21.   Serial.println(Filter_Value); // 串口輸出
  22.   delay(50);
  23. }

  24. // 用於隨機產生一個300左右的當前值
  25. int Get_AD() {
  26.   return random(295, 305);
  27. }

  28. // 加權遞推平均濾波法
  29. #define FILTER_N 12
  30. int coe[FILTER_N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};    // 加權係數表
  31. int sum_coe = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12; // 加權係數和
  32. int filter_buf[FILTER_N + 1];
  33. int Filter() {
  34.   int i;
  35.   int filter_sum = 0;
  36.   filter_buf[FILTER_N] = Get_AD();
  37.   for(i = 0; i < FILTER_N; i++) {
  38.     filter_buf[i] = filter_buf[i + 1]; // 所有數據左移,低位仍掉
  39.     filter_sum += filter_buf[i] * coe[i];
  40.   }
  41.   filter_sum /= sum_coe;
  42.   return filter_sum;
  43. }
複製代碼




9、消抖濾波法

  1. /*
  2. A、名稱:消抖濾波法
  3. B、方法:
  4.     設置一個濾波計數器,將每次採樣值與當前有效值比較:
  5.     如果採樣值=當前有效值,則計數器清零;
  6.     如果採樣值<>當前有效值,則計數器+1,並判斷計數器是否>=上限N(溢出);
  7.     如果計數器溢出,則將本次值替換當前有效值,並清計數器。
  8. C、優點:
  9.     對於變化緩慢的被測參數有較好的濾波效果;
  10.     可避免在臨界值附近控制器的反覆開/關跳動或顯示器上數值抖動。
  11. D、缺點:
  12.     對於快速變化的參數不宜;
  13.     如果在計數器溢出的那一次採樣到的值恰好是干擾值,則會將干擾值當作有效值導入系統。
  14. E、整理:shenhaiyu 2013-11-01
  15. */

  16. int Filter_Value;
  17. int Value;

  18. void setup() {
  19.   Serial.begin(9600);       // 初始化串口通信
  20.   randomSeed(analogRead(0)); // 產生隨機種子
  21.   Value = 300;
  22. }

  23. void loop() {
  24.   Filter_Value = Filter();       // 獲得濾波器輸出值
  25.   Serial.println(Filter_Value); // 串口輸出
  26.   delay(50);
  27. }

  28. // 用於隨機產生一個300左右的當前值
  29. int Get_AD() {
  30.   return random(295, 305);
  31. }

  32. // 消抖濾波法
  33. #define FILTER_N 12
  34. int i = 0;
  35. int Filter() {
  36.   int new_value;
  37.   new_value = Get_AD();
  38.   if(Value != new_value) {
  39.     i++;
  40.     if(i > FILTER_N) {
  41.       i = 0;
  42.       Value = new_value;
  43.     }
  44.   }
  45.   else
  46.     i = 0;
  47.   return Value;
  48. }
複製代碼




10、限幅消抖濾波法
  1. /*
  2. A、名稱:限幅消抖濾波法
  3. B、方法:
  4.     相當於“限幅濾波法”+“消抖濾波法”;
  5.     先限幅,後消抖。
  6. C、優點:
  7.     繼承了“限幅”和“消抖”的優點;
  8.     改進了“消抖濾波法”中的某些缺陷,避免將干擾值導入系統。
  9. D、缺點:
  10.     對於快速變化的參數不宜。
  11. E、整理:shenhaiyu 2013-11-01
  12. */

  13. int Filter_Value;
  14. int Value;

  15. void setup() {
  16.   Serial.begin(9600);       // 初始化串口通信
  17.   randomSeed(analogRead(0)); // 產生隨機種子
  18.   Value = 300;
  19. }

  20. void loop() {
  21.   Filter_Value = Filter();       // 獲得濾波器輸出值
  22.   Serial.println(Filter_Value); // 串口輸出
  23.   delay(50);
  24. }

  25. // 用於隨機產生一個300左右的當前值
  26. int Get_AD() {
  27.   return random(295, 305);
  28. }

  29. // 限幅消抖濾波法
  30. #define FILTER_A 1
  31. #define FILTER_N 5
  32. int i = 0;
  33. int Filter() {
  34.   int NewValue;
  35.   int new_value;
  36.   NewValue = Get_AD();
  37.   if(((NewValue - Value) > FILTER_A) || ((Value - NewValue) > FILTER_A))
  38.     new_value = Value;
  39.   else
  40.     new_value = NewValue;
  41.   if(Value != new_value) {
  42.     i++;
  43.     if(i > FILTER_N) {
  44.       i = 0;
  45.       Value = new_value;
  46.     }
  47.   }
  48.   else
  49.     i = 0;
  50.   return Value;
  51. }
複製代碼

11. 卡爾曼濾波(非擴展卡爾曼)
  1. <font size="3">#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. }</font>
複製代碼

發佈了14 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章