Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1.  
  2. class Ideone
  3. {
  4. main{
  5. double t = time diffrence; //this is the delta t between each imput
  6. double[4] net_rotation = convert(0,0,0); //this is the total rotation quaternion between the start and present
  7. while(true){
  8.  
  9. double x_g = imput x rotation rate; // gyro x axis
  10. double y_g = imput y rotation rate; // gyro y axis
  11. double z_g = imput z rotation rate; // gyro z axis
  12.  
  13. net_rotation = multi( convert( x_g * t , y_g * t , z_g * t ) , net_rotation );
  14.  
  15. double[3] acc_data = { acc_x imput , acc_y imput , acc_z imput } //acceleraterometer imput
  16. multi( acc_data , revert(net_rotation); //this converts the acc data to inertial frame
  17.  
  18. double[3] mag_data = { mag_x imput , mag_y imput , mag_z imput } //magnetometer imput
  19. multi( mag_data , revert(net_rotation); //this converts the mag data to inertial frame
  20. }
  21.  
  22.  
  23. }
  24.  
  25.  
  26.  
  27.  
  28. public static double[4] convert(double x, double y, double z)
  29. {
  30. double x;
  31. double y;
  32. double z;
  33.  
  34. double rot[][] = {
  35. { Math.cos(y) * Math.cos(z) , Math.cos(z) * Math.sin(x) * Math.sin(y) - Math.cos(x) * Math.sin(z) , Math.cos(x) * Math.cos(z) * Math.sin(y) + Math.sin(x) * Math.sin(z) },
  36. { Math.cos(y) * Math.sin(z) , Math.cos(x) * Math.cos(z) + Math.sin(x) * Math.sin(y) * Math.sin(z) , Math.cos(x) * Math.sin(y) * Math.sin(z) - Math.cos(z) * Math.sin(x) },
  37. { -Math.sin(z) , Math.cos(y) * Math.sin(x) , Math.cos(x) * Math.cos(y) }
  38. };
  39.  
  40. double T = rot[0][0]+rot[1][1]+rot[2][2];
  41.  
  42. double q[] = new q[4];
  43.  
  44. if ( T >= T && T >= rot[0][0] && T>= rot[1][1] && T >= rot[2][2]){
  45. q[3] = Math.sqrt((1+T)/4);
  46. q[0] = ( rot[1][2] - rot[2][1] ) / (4 * q[3]);
  47. q[1] = ( rot[2][0] - rot[0][2] ) / (4 * q[3]);
  48. q[2] = ( rot[0][1] - rot[1][0] ) / (4 * q[3]);
  49. }
  50. else{
  51. if ( rot[0][0] >= T && rot[0][0] >= rot[0][0] && rot[0][0] >= rot[1][1] && rot[0][0] >= rot[2][2]){
  52. q[0] = Math.sqrt((2+rot[0][0]-T)/4);
  53. q[1] = ( rot[0][1] + rot[1][0] ) / (4 * q[0]);
  54. q[2] = ( rot[0][2] + rot[2][0] ) / (4 * q[0]);
  55. q[3] = ( rot[1][2] - rot[2][1] ) / (4 * q[0]);
  56. }
  57. else{
  58. if ( rot[1][1] >= T && rot[1][1] >= rot[0][0] && rot[1][1] >= rot[1][1] && rot[1][1] >= rot[2][2]){
  59. q[1] = Math.sqrt((2 + rot[1][1] - T)/4);
  60. q[0] = ( rot[0][1] + rot[1][0] ) / (4 * q[1]);
  61. q[3] = ( rot[2][0] - rot[0][2] ) / (4 * q[1]);
  62. q[2] = ( rot[1][2] + rot[2][1] ) / (4 * q[1]);
  63. }
  64. else{
  65. if ( rot[2][2] >= T && rot[2][2] >= rot[0][0] && rot[2][2] >= rot[1][1] && rot[2][2] >= rot[2][2]){
  66. q[2] = Math.sqrt((2+rot[2][2]-T)/4);
  67. q[3] = ( rot[0][1] - rot[1][0] ) / (4 * q[2]);
  68. q[0] = ( rot[0][2] + rot[2][0] ) / (4 * q[2]);
  69. q[1] = ( rot[1][2] - rot[2][1] ) / (4 * q[2]);
  70. }
  71. }
  72. }
  73. }
  74. return q;
  75. }
  76. public static double[4] multi(double[4] q1, double[4] q2){
  77. //given the one from A-> B and the one from B-> C, the one from A-> C is given by C*B:
  78. //double q1[] = new double [4];//A->B
  79. //double q2[] = new double [4];//B->C
  80. double result[] = new double [4];//final
  81.  
  82. result[0]= q2[3] * q1[0] + q2[2] * q1[1] - q2[1] * q1[2] + q2[0] * q1[3];
  83. result[1]=-q2[2] * q1[0] + q2[3] * q1[1] + q2[0] * q1[2] + q2[1] * q1[3];
  84. result[2]= q2[1] * q1[0] - q2[0] * q1[1] + q2[3] * q1[2] + q2[2] * q1[3];
  85. result[3]=-q2[0] * q1[0] - q2[1] * q1[1] - q2[2] * q1[2] + q2[3] * q1[3];
  86. return result;
  87. }
  88.  
  89. public static double[3][3] revert(double[4] q){
  90. double[3][3] yeah = {
  91. { 2 * q[3] * q[3] + 2 * q[0] * q[0] - 1 , 2 * q[0] * q[1] + 2 * q[3] * q[2] , 2 * q[0] * q[2] - 2 * q[3] * q[1] },
  92. { 2 * q[1] * q[0] - 2 * q[3] * q[2] , 2 * q[3] * q[3] + 2 * q[1] * q[1] - 1 , 2 * q[1] * q[2] + 2 * q[3] * q[0] },
  93. { 2 * q[0] * q[2] + 2 * q[3] * q[1] , 2 * q[1] * q[2] - 2 * q[3] * q[0] , 2 * q[3] * q[3] + 2 * q[2] * q[2] - 1 }
  94. };
  95. return yeah;
  96. }
  97.  
  98. public static void multi(double[3] acc, double[3][3] rot){
  99. acc[0]= acc[0] * rot [0][0] + acc[1] * rot[1][0] + acc[2] * rot[2][0];
  100. acc[1]= acc[0] * rot [0][1] + acc[1] * rot[1][1] + acc[2] * rot[2][1];
  101. acc[2]= acc[0] * rot [0][2] + acc[1] * rot[1][2] + acc[2] * rot[2][2];
  102. }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement