打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
卡尔曼滤波算法
建议编辑一下这个帖子作为滤波专用的,这样大家查起来也方便。下面是卡尔曼滤波,不是扩展的,但是输出平稳的俯仰和滚转应该够了(凑乎用吧我也不是专业写代码的,欢迎大家拍)
  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. }
复制代码


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
【   】DIY飞控(STM32+MPU9150),试飞成功 !附PID及互补滤波代码
多元函数的一阶偏导数练习题及答案
braic SurfaAlgeces
多元变量1
I2C通信之加速度计、陀螺仪、磁强计(345+3205+5883)
广东省中考题,已知x y z=50,且xy yz xz=647,求xyz的值?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服