Advertisement
Guest User

Untitled

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