Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. #include "Wire.h"
  2.  
  3. // I2Cdev and MPU9250 must be installed as libraries, or else the .cpp/.h files
  4. // for both classes must be in the include path of your project
  5. #include "I2Cdev.h"
  6. #include "MPU9250.h"
  7. #include "BMP280.h"
  8.  
  9. // class default I2C address is 0x68
  10. // specific I2C addresses may be passed as a parameter here
  11. // AD0 low = 0x68 (default for InvenSense evaluation board)
  12. // AD0 high = 0x69
  13. MPU9250 accelgyro;
  14. I2Cdev I2C_M;
  15.  
  16. uint8_t buffer_m[6];
  17.  
  18.  
  19. int16_t ax, ay, az;
  20. int16_t gx, gy, gz;
  21. int16_t mx, my, mz;
  22.  
  23.  
  24.  
  25. float heading;
  26. float tiltheading;
  27.  
  28. float Axyz[3];
  29. float Gxyz[3];
  30. float Mxyz[3];
  31.  
  32.  
  33. #define sample_num_mdate 5000
  34.  
  35. volatile float mx_sample[3];
  36. volatile float my_sample[3];
  37. volatile float mz_sample[3];
  38.  
  39. static float mx_centre = 0;
  40. static float my_centre = 0;
  41. static float mz_centre = 0;
  42.  
  43. volatile int mx_max = 0;
  44. volatile int my_max = 0;
  45. volatile int mz_max = 0;
  46.  
  47. volatile int mx_min = 0;
  48. volatile int my_min = 0;
  49. volatile int mz_min = 0;
  50.  
  51. float temperature;
  52. float pressure;
  53. float atm;
  54. float altitude;
  55. BMP280 bmp280;
  56.  
  57. #include <Servo.h>
  58.  
  59. Servo servo;
  60. int pos = 0;
  61.  
  62. void setup() {
  63. servo.attach(8);// attaches the servo on pin 9 to the servo object
  64.  
  65. // join I2C bus (I2Cdev library doesn't do this automatically)
  66. Wire.begin();
  67.  
  68. // initialize serial communication
  69. // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
  70. // it's really up to you depending on your project)
  71. Serial.begin(38400);
  72.  
  73. // initialize device
  74. Serial.println("Initializing I2C devices...");
  75. accelgyro.initialize();
  76. bmp280.init();
  77.  
  78. // verify connection
  79. Serial.println("Testing device connections...");
  80. Serial.println(accelgyro.testConnection() ? "MPU9250 connection successful" : "MPU9250 connection failed");
  81.  
  82. delay(1000);
  83. Serial.println(" ");
  84.  
  85. // Mxyz_init_calibrated ();
  86. }
  87.  
  88. void loop() {
  89.  
  90. getAccel_Data();
  91. getGyro_Data();
  92. getCompassDate_calibrated(); // compass data has been calibrated here
  93. getHeading(); //before we use this function we should run 'getCompassDate_calibrated()' frist, so that we can get calibrated data ,then we can get correct angle .
  94. getTiltHeading();
  95.  
  96.  
  97.  
  98. void getHeading(void)
  99. {
  100. heading = 180 * atan2(Mxyz[1], Mxyz[0]) / PI;
  101. if (heading < 0) heading += 360;
  102. }
  103.  
  104. void getTiltHeading(void)
  105. {
  106. float pitch = asin(-Axyz[0]);
  107. float roll = asin(Axyz[1] / cos(pitch));
  108.  
  109. float xh = Mxyz[0] * cos(pitch) + Mxyz[2] * sin(pitch);
  110. float yh = Mxyz[0] * sin(roll) * sin(pitch) + Mxyz[1] * cos(roll) - Mxyz[2] * sin(roll) * cos(pitch);
  111. float zh = -Mxyz[0] * cos(roll) * sin(pitch) + Mxyz[1] * sin(roll) + Mxyz[2] * cos(roll) * cos(pitch);
  112. tiltheading = 180 * atan2(yh, xh) / PI;
  113. if (yh < 0) tiltheading += 360;
  114. }
  115.  
  116.  
  117.  
  118. void Mxyz_init_calibrated ()
  119. {
  120.  
  121. Serial.println(F("Before using 9DOF,we need to calibrate the compass frist,It will takes about 2 minutes."));
  122. Serial.print(" ");
  123. Serial.println(F("During calibratting ,you should rotate and turn the 9DOF all the time within 2 minutes."));
  124. Serial.print(" ");
  125. Serial.println(F("If you are ready ,please sent a command data 'ready' to start sample and calibrate."));
  126. while (!Serial.find("ready"));
  127. Serial.println(" ");
  128. Serial.println("ready");
  129. Serial.println("Sample starting......");
  130. Serial.println("waiting ......");
  131.  
  132. get_calibration_Data ();
  133.  
  134. Serial.println(" ");
  135. Serial.println("compass calibration parameter ");
  136. Serial.print(mx_centre);
  137. Serial.print(" ");
  138. Serial.print(my_centre);
  139. Serial.print(" ");
  140. Serial.println(mz_centre);
  141. Serial.println(" ");
  142. }
  143.  
  144.  
  145. void get_calibration_Data ()
  146. {
  147. for (int i = 0; i < sample_num_mdate; i++)
  148. {
  149. get_one_sample_date_mxyz();
  150. /*
  151. Serial.print(mx_sample[2]);
  152. Serial.print(" ");
  153. Serial.print(my_sample[2]); //you can see the sample data here .
  154. Serial.print(" ");
  155. Serial.println(mz_sample[2]);
  156. */
  157.  
  158.  
  159.  
  160. if (mx_sample[2] >= mx_sample[1])mx_sample[1] = mx_sample[2];
  161. if (my_sample[2] >= my_sample[1])my_sample[1] = my_sample[2]; //find max value
  162. if (mz_sample[2] >= mz_sample[1])mz_sample[1] = mz_sample[2];
  163.  
  164. if (mx_sample[2] <= mx_sample[0])mx_sample[0] = mx_sample[2];
  165. if (my_sample[2] <= my_sample[0])my_sample[0] = my_sample[2]; //find min value
  166. if (mz_sample[2] <= mz_sample[0])mz_sample[0] = mz_sample[2];
  167.  
  168. }
  169.  
  170. mx_max = mx_sample[1];
  171. my_max = my_sample[1];
  172. mz_max = mz_sample[1];
  173.  
  174. mx_min = mx_sample[0];
  175. my_min = my_sample[0];
  176. mz_min = mz_sample[0];
  177.  
  178.  
  179.  
  180. mx_centre = (mx_max + mx_min) / 2;
  181. my_centre = (my_max + my_min) / 2;
  182. mz_centre = (mz_max + mz_min) / 2;
  183.  
  184. }
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191. void get_one_sample_date_mxyz()
  192. {
  193. getCompass_Data();
  194. mx_sample[2] = Mxyz[0];
  195. my_sample[2] = Mxyz[1];
  196. mz_sample[2] = Mxyz[2];
  197. }
  198.  
  199.  
  200. void getAccel_Data(void)
  201. {
  202. accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);
  203. Axyz[0] = (double) ax / 16384;
  204. Axyz[1] = (double) ay / 16384;
  205. Axyz[2] = (double) az / 16384;
  206. }
  207.  
  208. void getGyro_Data(void)
  209. {
  210. accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);
  211. Gxyz[0] = (double) gx * 250 / 32768;
  212. Gxyz[1] = (double) gy * 250 / 32768;
  213. Gxyz[2] = (double) gz * 250 / 32768;
  214. }
  215.  
  216. void getCompass_Data(void)
  217. {
  218. I2C_M.writeByte(MPU9150_RA_MAG_ADDRESS, 0x0A, 0x01); //enable the magnetometer
  219. delay(10);
  220. I2C_M.readBytes(MPU9150_RA_MAG_ADDRESS, MPU9150_RA_MAG_XOUT_L, 6, buffer_m);
  221.  
  222. mx = ((int16_t)(buffer_m[1]) << 8) | buffer_m[0] ;
  223. my = ((int16_t)(buffer_m[3]) << 8) | buffer_m[2] ;
  224. mz = ((int16_t)(buffer_m[5]) << 8) | buffer_m[4] ;
  225.  
  226. Mxyz[0] = (double) mx * 1200 / 4096;
  227. Mxyz[1] = (double) my * 1200 / 4096;
  228. Mxyz[2] = (double) mz * 1200 / 4096;
  229. }
  230.  
  231. void getCompassDate_calibrated ()
  232. {
  233. getCompass_Data();
  234. Mxyz[0] = Mxyz[0] - mx_centre;
  235. Mxyz[1] = Mxyz[1] - my_centre;
  236. Mxyz[2] = Mxyz[2] - mz_centre;
  237. }
  238. delay(150); // waits 15ms for the servo to reach the position
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement