1. 程式人生 > >濾波演算法總結

濾波演算法總結

  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.   readFrom(ACC, regAddress, A_TO_READ, buff); //read the acceleration data from the ADXL345
  33.   //each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
  34.   //thus we are converting both bytes in to one int
  35.   result[0] = (((int)buff[1]) << 8) | buff[0] + a_offx;   
  36.   result[1] = (((int)buff[3]) << 8) | buff[2] + a_offy;
  37.   result[2] = (((int)buff[5]) << 8) | buff[4] + a_offz;
  38. }
  39. //initializes the gyroscope
  40. void initGyro()
  41. {
  42.   /*****************************************
  43.   * ITG 3200
  44.   * power management set to:
  45.   * clock select = internal oscillator
  46.   *     no reset, no sleep mode
  47.   *   no standby mode
  48.   * sample rate to = 125Hz
  49.   * parameter to +/- 2000 degrees/sec
  50.   * low pass filter = 5Hz
  51.   * no interrupt
  52.   ******************************************/
  53.   writeTo(GYRO, G_PWR_MGM, 0x00);
  54.   writeTo(GYRO, G_SMPLRT_DIV, 0x07); // EB, 50, 80, 7F, DE, 23, 20, FF
  55.   writeTo(GYRO, G_DLPF_FS, 0x1E); // +/- 2000 dgrs/sec, 1KHz, 1E, 19
  56.   writeTo(GYRO, G_INT_CFG, 0x00);
  57. }
  58. void getGyroscopeData(int * result)
  59. {
  60.   /**************************************
  61.   Gyro ITG-3200 I2C
  62.   registers:
  63.   temp MSB = 1B, temp LSB = 1C
  64.   x axis MSB = 1D, x axis LSB = 1E
  65.   y axis MSB = 1F, y axis LSB = 20
  66.   z axis MSB = 21, z axis LSB = 22
  67.   *************************************/
  68.   int regAddress = 0x1B;
  69.   int temp, x, y, z;
  70.   byte buff[G_TO_READ];
  71.   readFrom(GYRO, regAddress, G_TO_READ, buff); //read the gyro data from the ITG3200
  72.   result[0] = ((buff[2] << 8) | buff[3]) + g_offx;
  73.   result[1] = ((buff[4] << 8) | buff[5]) + g_offy;
  74.   result[2] = ((buff[6] << 8) | buff[7]) + g_offz;
  75.   result[3] = (buff[0] << 8) | buff[1]; // temperature
  76. }
  77. float xz=0,yx=0,yz=0;
  78. float p_xz=1,p_yx=1,p_yz=1;
  79. float q_xz=0.0025,q_yx=0.0025,q_yz=0.0025;
  80. float k_xz=0,k_yx=0,k_yz=0;
  81. float r_xz=0.25,r_yx=0.25,r_yz=0.25;
  82.   //int acc_temp[3];
  83.   //float acc[3];
  84.   int acc[3];
  85.   int gyro[4];
  86.   float Axz;
  87.   float Ayx;
  88.   float Ayz;
  89.   float t=0.025;
  90. void setup()
  91. {
  92.   Serial.begin(9600);
  93.   Wire.begin();
  94.   initAcc();
  95.   initGyro();
  96. }
  97. //unsigned long timer = 0;
  98. //float o;
  99. void loop()
  100. {
  101.   getAccelerometerData(acc);
  102.   getGyroscopeData(gyro);
  103.   //timer = millis();
  104.   sprintf(str, "%d,%d,%d,%d,%d,%d", acc[0],acc[1],acc[2],gyro[0],gyro[1],gyro[2]);
  105.   //acc[0]=acc[0];
  106.   //acc[2]=acc[2];
  107.   //acc[1]=acc[1];
  108.   //r=sqrt(acc[0]*acc[0]+acc[1]*acc[1]+acc[2]*acc[2]);
  109.   gyro[0]=gyro[0]/ 14.375;
  110.   gyro[1]=gyro[1]/ (-14.375);
  111.   gyro[2]=gyro[2]/ 14.375;
  112.   Axz=(atan2(acc[0],acc[2]))*180/PI;
  113.   Ayx=(atan2(acc[0],acc[1]))*180/PI;
  114.   /*if((acc[0]!=0)&&(acc[1]!=0))
  115.     {
  116.       Ayx=(atan2(acc[0],acc[1]))*180/PI;
  117.     }
  118.     else
  119.     {
  120.       Ayx=t*gyro[2];
  121.     }*/
  122.   Ayz=(atan2(acc[1],acc[2]))*180/PI;
  123. //kalman filter
  124.   calculate_xz();
  125.   calculate_yx();
  126.   calculate_yz();
  127.   //sprintf(str, "%d,%d,%d", xz_1, xy_1, x_1);
  128.   //Serial.print(xz);Serial.print(",");
  129.   //Serial.print(yx);Serial.print(",");
  130.   //Serial.print(yz);Serial.print(",");
  131.   //sprintf(str, "%d,%d,%d,%d,%d,%d", acc[0],acc[1],acc[2],gyro[0],gyro[1],gyro[2]);
  132.   //sprintf(str, "%d,%d,%d",gyro[0],gyro[1],gyro[2]);
  133.     Serial.print(Axz);Serial.print(",");
  134.     //Serial.print(Ayx);Serial.print(",");
  135.     //Serial.print(Ayz);Serial.print(",");
  136.   //Serial.print(str);
  137.   //o=gyro[2];//w=acc[2];
  138.   //Serial.print(o);Serial.print(",");
  139.   //Serial.print(w);Serial.print(",");
  140.   Serial.print("\n");
  141.   //delay(50);
  142. }
  143. void calculate_xz()
  144. {
  145. xz=xz+t*gyro[1];
  146. p_xz=p_xz+q_xz;
  147. k_xz=p_xz/(p_xz+r_xz);
  148. xz=xz+k_xz*(Axz-xz);
  149. p_xz=(1-k_xz)*p_xz;
  150. }
  151. void calculate_yx()
  152. {
  153.   yx=yx+t*gyro[2];
  154.   p_yx=p_yx+q_yx;
  155.   k_yx=p_yx/(p_yx+r_yx);
  156.   yx=yx+k_yx*(Ayx-yx);
  157.   p_yx=(1-k_yx)*p_yx;
  158. }
  159. void calculate_yz()
  160. {
  161.   yz=yz+t*gyro[0];
  162.   p_yz=p_yz+q_yz;
  163.   k_yz=p_yz/(p_yz+r_yz);
  164.   yz=yz+k_yz*(Ayz-yz);
  165.   p_yz=(1-k_yz)*p_yz;
  166. }
  167. //---------------- Functions
  168. //Writes val to address register on ACC
  169. void writeTo(int DEVICE, byte address, byte val) {
  170.    Wire.beginTransmission(DEVICE); //start transmission to ACC 
  171.    Wire.write(address);        // send register address
  172.    Wire.write(val);        // send value to write
  173.    Wire.endTransmission(); //end transmission
  174. }
  175. //reads num bytes starting from address register on ACC in to buff array
  176. void readFrom(int DEVICE, byte address, int num, byte buff[]) {
  177.   Wire.beginTransmission(DEVICE); //start transmission to ACC 
  178.   Wire.write(address);        //sends address to read from
  179.   Wire.endTransmission(); //end transmission
  180.   Wire.beginTransmission(DEVICE); //start transmission to ACC
  181.   Wire.requestFrom(DEVICE, num);    // request 6 bytes from ACC
  182.   int i = 0;
  183.   while(Wire.available())    //ACC may send less than requested (abnormal)
  184.   { 
  185.     buff[i] = Wire.read(); // receive a byte
  186.     i++;
  187.   }
  188.   Wire.endTransmission(); //end transmission
  189. }