找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1052|回复: 0
打印 上一主题 下一主题
收起左侧

尔曼滤波器英文资料与C语言源程序

[复制链接]
跳转到指定楼层
楼主
详细介绍卡尔曼滤波器。
附有程序源码


源程序如下:
  1. /***************************************************************************/
  2. /* kalman.c                                                                */
  3. /* 1-D Kalman filter Algorithm, using an inclinometer and gyro             */
  4. /* Author: Rich Chi Ooi                                                    */
  5. /* Version: 1.0                                                            */
  6. /* Date:30.05.2003                                                         */
  7. /* Adapted from Trammel Hudson(hudson@rotomotion.com)                      */  
  8. /* -------------------------------                                         */
  9. /* Compilation  procedure:                                                 */
  10. /*       Linux                                                             */
  11. /*      gcc68 -c  XXXXXX.c (to create object file)                         */
  12. /*      gcc68 -o  XXXXXX.hex XXXXXX.o ppwa.o                               */
  13. /*Upload data :                                                                   */
  14. /*        ul filename.txt                                                                 */
  15. /***************************************************************************/
  16. /* In this version:                                                        */
  17. /***************************************************************************/
  18. /* This is a free software; you can redistribute it and/or modify          */
  19. /* it under the terms of the GNU General Public License as published       */
  20. /* by the Free Software Foundation; either version 2 of the License,       */
  21. /* or (at your option) any later version.                                  */
  22. /*                                                                         */
  23. /* this code is distributed in the hope that it will be useful,            */
  24. /* but WITHOUT ANY WARRANTY; without even the implied warranty of          */
  25. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           */
  26. /* GNU General Public License for more details.                            */
  27. /*                                                                         */
  28. /* You should have received a copy of the GNU General Public License       */
  29. /* along with Autopilot; if not, write to the Free Software                */
  30. /* Foundation, Inc., 59 Temple Place, Suite 330, Boston,                   */
  31. /* MA  02111-1307  USA                                                     */
  32. /***************************************************************************/

  33. #include <math.h>
  34. #include "eyebot.h"

  35. /*
  36. * The state is updated with gyro rate measurement every 20ms
  37. * change this value if you update at a different rate.
  38. */

  39. static const float dt = 0.02;

  40. /*
  41. * The covariance matrix.This is updated at every time step to
  42. * determine how well the sensors are tracking the actual state.
  43. */
  44. static float P[2][2] = {{ 1, 0 },
  45.                         { 0, 1 }};

  46. /*
  47. * Our two states, the angle and the gyro bias.As a byproduct of computing
  48. * the angle, we also have an unbiased angular rate available.These are
  49. * read-only to the user of the module.
  50. */
  51. float angle;
  52. float q_bias;
  53. float rate;


  54. /*
  55. * The R represents the measurement covariance noise.R=E[vvT]
  56. * In this case,it is a 1x1 matrix that says that we expect
  57. * 0.1 rad jitter from the inclinometer.
  58. * for a 1x1 matrix in this case v = 0.1
  59. */
  60. static const float R_angle = 0.001 ;


  61. /*
  62. * Q is a 2x2 matrix that represents the process covariance noise.
  63. * In this case, it indicates how much we trust the inclinometer
  64. * relative to the gyros.
  65. */
  66. static const float Q_angle = 0.001;
  67. static const float Q_gyro  = 0.0015;


  68. /*
  69. * state_update is called every dt with a biased gyro measurement
  70. * by the user of the module.  It updates the current angle and
  71. * rate estimate.
  72. *
  73. * The pitch gyro measurement should be scaled into real units, but
  74. * does not need any bias removal.  The filter will track the bias.
  75. *
  76. *          A = [ 0 -1 ]
  77. *              [ 0  0 ]
  78. */
  79. void stateUpdate(const float q_m){

  80.         float q;
  81.         float Pdot[4];

  82.         /* Unbias our gyro */
  83.         q = q_m - q_bias;

  84.         /*
  85.          * Compute the derivative of the covariance matrix
  86.          * (equation 22-1)
  87.          *        Pdot = A*P + P*A' + Q
  88.          *
  89.          */
  90.         Pdot[0] = Q_angle - P[0][1] - P[1][0];        /* 0,0 */
  91.         Pdot[1] = - P[1][1];                        /* 0,1 */
  92.         Pdot[2] = - P[1][1];                         /* 1,0 */
  93.         Pdot[3] = Q_gyro;                        /* 1,1 */

  94.         /* Store our unbias gyro estimate */
  95.         rate = q;

  96.         /*
  97.          * Update our angle estimate
  98.          * angle += angle_dot * dt
  99.          *       += (gyro - gyro_bias) * dt
  100.          *       += q * dt
  101.          */
  102.         angle += q * dt;

  103.         /* Update the covariance matrix */
  104.         P[0][0] += Pdot[0] * dt;
  105.         P[0][1] += Pdot[1] * dt;
  106.         P[1][0] += Pdot[2] * dt;
  107.         P[1][1] += Pdot[3] * dt;
  108. }


  109. /*
  110. * kalman_update is called by a user of the module when a new
  111. * inclinoometer measurement is available.
  112. *
  113. * This does not need to be called every time step, but can be if
  114. * the accelerometer data are available at the same rate as the
  115. * rate gyro measurement.
  116. *
  117. *         H  = [ 1 0 ]
  118. *
  119. * because the angle measurement directly corresponds to the angle
  120. * estimate and the angle measurement has no relation to the gyro
  121. * bias.
  122. */
  123. void kalmanUpdate(const float incAngle)
  124. {
  125.         /* Compute our measured angle and the error in our estimate */
  126.         float angle_m = incAngle;
  127.         float angle_err = angle_m - angle;

  128.         /*
  129.          * h_0 shows how the state measurement directly relates to
  130.          * the state estimate.
  131.           *   
  132.          *      H = [h_0 h_1]
  133.          *
  134.          * The h_1 shows that the state measurement does not relate
  135.          * to the gyro bias estimate.  We don't actually use this, so
  136.          * we comment it out.
  137.          */
  138.         float h_0 = 1;
  139.         /* const float                h_1 = 0; */

  140.         /*
  141.          * Precompute PH' as the term is used twice
  142.          * Note that H[0,1] = h_1 is zero, so that term is not not computed
  143.          */
  144.         const float PHt_0 = h_0*P[0][0]; /* + h_1*P[0][1] = 0*/
  145.         const float PHt_1 = h_0*P[1][0]; /* + h_1*P[1][1] = 0*/

  146.         /*
  147.          * Compute the error estimate:
  148.          * (equation 21-1)
  149.          *
  150.          *        E = H P H' + R
  151.          */
  152.         float E = R_angle +(h_0 * PHt_0);

  153.         /*
  154.          * Compute the Kalman filter gains:
  155.          * (equation 21-2)
  156.          *
  157.          *        K = P H' inv(E)
  158.                 */
  159.         float K_0 = PHt_0 / E;
  160.         float K_1 = PHt_1 / E;
  161.                
  162.         /*
  163.          * Update covariance matrix:
  164.          * (equation 21-3)
  165.          *
  166.          *        P = P - K H P
  167.          * Let
  168.          *      Y = H P
  169.          */
  170.         float Y_0 = PHt_0;  /*h_0 * P[0][0]*/
  171.         float Y_1 = h_0 * P[0][1];
  172.          
  173.         P[0][0] -= K_0 * Y_0;
  174.         P[0][1] -= K_0 * Y_1;
  175.         P[1][0] -= K_1 * Y_0;
  176.         P[1][1] -= K_1 * Y_1;
  177.         
  178.         /*
  179.          * Update our state estimate:
  180.          *
  181.          *        Xnew = X + K * error
  182.          *
  183.          * err is a measurement of the difference in the measured state
  184.          * and the estimate state.  In our case, it is just the difference
  185.          * between the inclinometer measured angle and the estimated angle.
  186.          */
  187.         angle        += K_0 * angle_err;
  188.         q_bias        += K_1 * angle_err;
  189. }
复制代码

所有资料51hei提供下载:
卡尔曼.zip (580.13 KB, 下载次数: 13)



评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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