Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1.  
  2. void getHeading(void)
  3. {
  4. heading = 180 * atan2(Mxyz[1], Mxyz[0]) / PI;
  5. if (heading < 0) heading += 360;
  6. }
  7.  
  8. void getTiltHeading(void)
  9. {
  10. float pitch = asin(-Axyz[0]);
  11. float roll = asin(Axyz[1] / cos(pitch));
  12.  
  13. float xh = Mxyz[0] * cos(pitch) + Mxyz[2] * sin(pitch);
  14. float yh = Mxyz[0] * sin(roll) * sin(pitch) + Mxyz[1] * cos(roll) - Mxyz[2] * sin(roll) * cos(pitch);
  15. float zh = -Mxyz[0] * cos(roll) * sin(pitch) + Mxyz[1] * sin(roll) + Mxyz[2] * cos(roll) * cos(pitch);
  16. tiltheading = 180 * atan2(yh, xh) / PI;
  17. if (yh < 0) tiltheading += 360;
  18. }
  19.  
  20.  
  21.  
  22. void Mxyz_init_calibrated ()
  23. {
  24.  
  25. Serial.println(F("Before using 9DOF,we need to calibrate the compass frist,It will takes about 2 minutes."));
  26. Serial.print(" ");
  27. Serial.println(F("During calibratting ,you should rotate and turn the 9DOF all the time within 2 minutes."));
  28. Serial.print(" ");
  29. Serial.println(F("If you are ready ,please sent a command data 'ready' to start sample and calibrate."));
  30. while (!Serial.find("ready"));
  31. Serial.println(" ");
  32. Serial.println("ready");
  33. Serial.println("Sample starting......");
  34. Serial.println("waiting ......");
  35.  
  36. get_calibration_Data ();
  37.  
  38. Serial.println(" ");
  39. Serial.println("compass calibration parameter ");
  40. Serial.print(mx_centre);
  41. Serial.print(" ");
  42. Serial.print(my_centre);
  43. Serial.print(" ");
  44. Serial.println(mz_centre);
  45. Serial.println(" ");
  46. }
  47.  
  48.  
  49. void get_calibration_Data ()
  50. {
  51. for (int i = 0; i < sample_num_mdate; i++)
  52. {
  53. get_one_sample_date_mxyz();
  54. /*
  55. Serial.print(mx_sample[2]);
  56. Serial.print(" ");
  57. Serial.print(my_sample[2]); //you can see the sample data here .
  58. Serial.print(" ");
  59. Serial.println(mz_sample[2]);
  60. */
  61.  
  62.  
  63.  
  64. if (mx_sample[2] >= mx_sample[1])mx_sample[1] = mx_sample[2];
  65. if (my_sample[2] >= my_sample[1])my_sample[1] = my_sample[2]; //find max value
  66. if (mz_sample[2] >= mz_sample[1])mz_sample[1] = mz_sample[2];
  67.  
  68. if (mx_sample[2] <= mx_sample[0])mx_sample[0] = mx_sample[2];
  69. if (my_sample[2] <= my_sample[0])my_sample[0] = my_sample[2]; //find min value
  70. if (mz_sample[2] <= mz_sample[0])mz_sample[0] = mz_sample[2];
  71.  
  72. }
  73.  
  74. mx_max = mx_sample[1];
  75. my_max = my_sample[1];
  76. mz_max = mz_sample[1];
  77.  
  78. mx_min = mx_sample[0];
  79. my_min = my_sample[0];
  80. mz_min = mz_sample[0];
  81.  
  82.  
  83.  
  84. mx_centre = (mx_max + mx_min) / 2;
  85. my_centre = (my_max + my_min) / 2;
  86. mz_centre = (mz_max + mz_min) / 2;
  87.  
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. void get_one_sample_date_mxyz()
  96. {
  97. getCompass_Data();
  98. mx_sample[2] = Mxyz[0];
  99. my_sample[2] = Mxyz[1];
  100. mz_sample[2] = Mxyz[2];
  101. }
  102.  
  103.  
  104. void getAccel_Data(void)
  105. {
  106. accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);
  107. Axyz[0] = (double) ax / 16384;
  108. Axyz[1] = (double) ay / 16384;
  109. Axyz[2] = (double) az / 16384;
  110. }
  111.  
  112. void getGyro_Data(void)
  113. {
  114. accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);
  115. Gxyz[0] = (double) gx * 250 / 32768;
  116. Gxyz[1] = (double) gy * 250 / 32768;
  117. Gxyz[2] = (double) gz * 250 / 32768;
  118. }
  119.  
  120. void getCompass_Data(void)
  121. {
  122. I2C_M.writeByte(MPU9150_RA_MAG_ADDRESS, 0x0A, 0x01); //enable the magnetometer
  123. delay(10);
  124. I2C_M.readBytes(MPU9150_RA_MAG_ADDRESS, MPU9150_RA_MAG_XOUT_L, 6, buffer_m);
  125.  
  126. mx = ((int16_t)(buffer_m[1]) << 8) | buffer_m[0] ;
  127. my = ((int16_t)(buffer_m[3]) << 8) | buffer_m[2] ;
  128. mz = ((int16_t)(buffer_m[5]) << 8) | buffer_m[4] ;
  129.  
  130. Mxyz[0] = (double) mx * 1200 / 4096;
  131. Mxyz[1] = (double) my * 1200 / 4096;
  132. Mxyz[2] = (double) mz * 1200 / 4096;
  133. }
  134.  
  135. void getCompassDate_calibrated ()
  136. {
  137. getCompass_Data();
  138. Mxyz[0] = Mxyz[0] - mx_centre;
  139. Mxyz[1] = Mxyz[1] - my_centre;
  140. Mxyz[2] = Mxyz[2] - mz_centre;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement