1
0

DCM.ino 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* This file is part of the Razor AHRS Firmware */
  2. // DCM algorithm
  3. /**************************************************/
  4. void Normalize(void)
  5. {
  6. float error=0;
  7. float temporary[3][3];
  8. float renorm=0;
  9. error= -Vector_Dot_Product(&DCM_Matrix[0][0],&DCM_Matrix[1][0])*.5; //eq.19
  10. Vector_Scale(&temporary[0][0], &DCM_Matrix[1][0], error); //eq.19
  11. Vector_Scale(&temporary[1][0], &DCM_Matrix[0][0], error); //eq.19
  12. Vector_Add(&temporary[0][0], &temporary[0][0], &DCM_Matrix[0][0]);//eq.19
  13. Vector_Add(&temporary[1][0], &temporary[1][0], &DCM_Matrix[1][0]);//eq.19
  14. Vector_Cross_Product(&temporary[2][0],&temporary[0][0],&temporary[1][0]); // c= a x b //eq.20
  15. renorm= .5 *(3 - Vector_Dot_Product(&temporary[0][0],&temporary[0][0])); //eq.21
  16. Vector_Scale(&DCM_Matrix[0][0], &temporary[0][0], renorm);
  17. renorm= .5 *(3 - Vector_Dot_Product(&temporary[1][0],&temporary[1][0])); //eq.21
  18. Vector_Scale(&DCM_Matrix[1][0], &temporary[1][0], renorm);
  19. renorm= .5 *(3 - Vector_Dot_Product(&temporary[2][0],&temporary[2][0])); //eq.21
  20. Vector_Scale(&DCM_Matrix[2][0], &temporary[2][0], renorm);
  21. }
  22. /**************************************************/
  23. void Drift_correction(void)
  24. {
  25. float mag_heading_x;
  26. float mag_heading_y;
  27. float errorCourse;
  28. //Compensation the Roll, Pitch and Yaw drift.
  29. static float Scaled_Omega_P[3];
  30. static float Scaled_Omega_I[3];
  31. float Accel_magnitude;
  32. float Accel_weight;
  33. //*****Roll and Pitch***************
  34. // Calculate the magnitude of the accelerometer vector
  35. Accel_magnitude = sqrt(Accel_Vector[0]*Accel_Vector[0] + Accel_Vector[1]*Accel_Vector[1] + Accel_Vector[2]*Accel_Vector[2]);
  36. Accel_magnitude = Accel_magnitude / GRAVITY; // Scale to gravity.
  37. // Dynamic weighting of accelerometer info (reliability filter)
  38. // Weight for accelerometer info (<0.5G = 0.0, 1G = 1.0 , >1.5G = 0.0)
  39. Accel_weight = constrain(1 - 2*abs(1 - Accel_magnitude),0,1); //
  40. Vector_Cross_Product(&errorRollPitch[0],&Accel_Vector[0],&DCM_Matrix[2][0]); //adjust the ground of reference
  41. Vector_Scale(&Omega_P[0],&errorRollPitch[0],Kp_ROLLPITCH*Accel_weight);
  42. Vector_Scale(&Scaled_Omega_I[0],&errorRollPitch[0],Ki_ROLLPITCH*Accel_weight);
  43. Vector_Add(Omega_I,Omega_I,Scaled_Omega_I);
  44. //*****YAW***************
  45. // We make the gyro YAW drift correction based on compass magnetic heading
  46. mag_heading_x = cos(MAG_Heading);
  47. mag_heading_y = sin(MAG_Heading);
  48. errorCourse=(DCM_Matrix[0][0]*mag_heading_y) - (DCM_Matrix[1][0]*mag_heading_x); //Calculating YAW error
  49. Vector_Scale(errorYaw,&DCM_Matrix[2][0],errorCourse); //Applys the yaw correction to the XYZ rotation of the aircraft, depeding the position.
  50. Vector_Scale(&Scaled_Omega_P[0],&errorYaw[0],Kp_YAW);//.01proportional of YAW.
  51. Vector_Add(Omega_P,Omega_P,Scaled_Omega_P);//Adding Proportional.
  52. Vector_Scale(&Scaled_Omega_I[0],&errorYaw[0],Ki_YAW);//.00001Integrator
  53. Vector_Add(Omega_I,Omega_I,Scaled_Omega_I);//adding integrator to the Omega_I
  54. }
  55. void Matrix_update(void)
  56. {
  57. Gyro_Vector[0]=GYRO_SCALED_RAD(gyro[0]); //gyro x roll
  58. Gyro_Vector[1]=GYRO_SCALED_RAD(gyro[1]); //gyro y pitch
  59. Gyro_Vector[2]=GYRO_SCALED_RAD(gyro[2]); //gyro z yaw
  60. Accel_Vector[0]=accel[0];
  61. Accel_Vector[1]=accel[1];
  62. Accel_Vector[2]=accel[2];
  63. Vector_Add(&Omega[0], &Gyro_Vector[0], &Omega_I[0]); //adding proportional term
  64. Vector_Add(&Omega_Vector[0], &Omega[0], &Omega_P[0]); //adding Integrator term
  65. #if DEBUG__NO_DRIFT_CORRECTION == true // Do not use drift correction
  66. Update_Matrix[0][0]=0;
  67. Update_Matrix[0][1]=-G_Dt*Gyro_Vector[2];//-z
  68. Update_Matrix[0][2]=G_Dt*Gyro_Vector[1];//y
  69. Update_Matrix[1][0]=G_Dt*Gyro_Vector[2];//z
  70. Update_Matrix[1][1]=0;
  71. Update_Matrix[1][2]=-G_Dt*Gyro_Vector[0];
  72. Update_Matrix[2][0]=-G_Dt*Gyro_Vector[1];
  73. Update_Matrix[2][1]=G_Dt*Gyro_Vector[0];
  74. Update_Matrix[2][2]=0;
  75. #else // Use drift correction
  76. Update_Matrix[0][0]=0;
  77. Update_Matrix[0][1]=-G_Dt*Omega_Vector[2];//-z
  78. Update_Matrix[0][2]=G_Dt*Omega_Vector[1];//y
  79. Update_Matrix[1][0]=G_Dt*Omega_Vector[2];//z
  80. Update_Matrix[1][1]=0;
  81. Update_Matrix[1][2]=-G_Dt*Omega_Vector[0];//-x
  82. Update_Matrix[2][0]=-G_Dt*Omega_Vector[1];//-y
  83. Update_Matrix[2][1]=G_Dt*Omega_Vector[0];//x
  84. Update_Matrix[2][2]=0;
  85. #endif
  86. Matrix_Multiply(DCM_Matrix,Update_Matrix,Temporary_Matrix); //a*b=c
  87. for(int x=0; x<3; x++) //Matrix Addition (update)
  88. {
  89. for(int y=0; y<3; y++)
  90. {
  91. DCM_Matrix[x][y]+=Temporary_Matrix[x][y];
  92. }
  93. }
  94. }
  95. void Euler_angles(void)
  96. {
  97. pitch = -asin(DCM_Matrix[2][0]);
  98. roll = atan2(DCM_Matrix[2][1],DCM_Matrix[2][2]);
  99. yaw = atan2(DCM_Matrix[1][0],DCM_Matrix[0][0]);
  100. }