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++ 2.01 KB | None | 0 0
  1. /* This file is part of the Razor AHRS Firmware */
  2.  
  3. // Computes the dot product of two vectors
  4. float Vector_Dot_Product(float vector1[3], float vector2[3])
  5. {
  6.   float op=0;
  7.  
  8.   for(int c=0; c<3; c++)
  9.   {
  10.     op+=vector1[c]*vector2[c];
  11.   }
  12.  
  13.   return op;
  14. }
  15.  
  16. // Computes the cross product of two vectors
  17. void Vector_Cross_Product(float vectorOut[3], float v1[3], float v2[3])
  18. {
  19.   vectorOut[0]= (v1[1]*v2[2]) - (v1[2]*v2[1]);
  20.   vectorOut[1]= (v1[2]*v2[0]) - (v1[0]*v2[2]);
  21.   vectorOut[2]= (v1[0]*v2[1]) - (v1[1]*v2[0]);
  22. }
  23.  
  24. // Multiply the vector by a scalar.
  25. void Vector_Scale(float vectorOut[3], float vectorIn[3], float scale2)
  26. {
  27.   for(int c=0; c<3; c++)
  28.   {
  29.     vectorOut[c]=vectorIn[c]*scale2;
  30.   }
  31. }
  32.  
  33. // Adds two vectors
  34. void Vector_Add(float vectorOut[3], float vectorIn1[3], float vectorIn2[3])
  35. {
  36.   for(int c=0; c<3; c++)
  37.   {
  38.     vectorOut[c]=vectorIn1[c]+vectorIn2[c];
  39.   }
  40. }
  41.  
  42. //Multiply two 3x3 matrixs. This function developed by Jordi can be easily adapted to multiple n*n matrix's. (Pero me da flojera!).
  43. void Matrix_Multiply(float a[3][3], float b[3][3],float mat[3][3])
  44. {
  45.   float op[3];
  46.   for(int x=0; x<3; x++)
  47.   {
  48.     for(int y=0; y<3; y++)
  49.     {
  50.       for(int w=0; w<3; w++)
  51.       {
  52.        op[w]=a[x][w]*b[w][y];
  53.       }
  54.       mat[x][y]=0;
  55.       mat[x][y]=op[0]+op[1]+op[2];
  56.      
  57.       float test=mat[x][y];
  58.     }
  59.   }
  60. }
  61.  
  62. // Init rotation matrix using euler angles
  63. void init_rotation_matrix(float m[3][3], float yaw, float pitch, float roll)
  64. {
  65.   float c1 = cos(roll);
  66.   float s1 = sin(roll);
  67.   float c2 = cos(pitch);
  68.   float s2 = sin(pitch);
  69.   float c3 = cos(yaw);
  70.   float s3 = sin(yaw);
  71.  
  72.   // Euler angles, right-handed, intrinsic, XYZ convention
  73.   // (which means: rotate around body axes Z, Y', X'')
  74.   m[0][0] = c2 * c3;
  75.   m[0][1] = c3 * s1 * s2 - c1 * s3;
  76.   m[0][2] = s1 * s3 + c1 * c3 * s2;
  77.  
  78.   m[1][0] = c2 * s3;
  79.   m[1][1] = c1 * c3 + s1 * s2 * s3;
  80.   m[1][2] = c1 * s2 * s3 - c3 * s1;
  81.  
  82.   m[2][0] = -s2;
  83.   m[2][1] = c2 * s1;
  84.   m[2][2] = c1 * c2;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement