MPU6050.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "MPU6050.h"
  2. MPU6050::MPU6050() {
  3. }
  4. void MPU6050::getAngularVelocity() {
  5. gyroX = gyroXPresent / 131.0;
  6. gyroY = gyroYPresent / 131.0;
  7. gyroZ = gyroZPresent / 131.0;
  8. }
  9. void MPU6050::calculateAngle() {
  10. winkelX = winkelX + ((timePresent - timePast)*(gyroXPresent + gyroXPast - 2*gyroXCalli)) * 0.00000382;
  11. winkelY = winkelY + ((timePresent - timePast)*(gyroYPresent + gyroYPast - 2*gyroYCalli)) * 0.00000382;
  12. winkelZ = winkelZ + ((timePresent - timePast)*(gyroZPresent + gyroZPast - 2*gyroZCalli)) * 0.00000382;
  13. }
  14. void MPU6050::printData() {
  15. Serial.print("3-Achsen-Gyroskop ");
  16. Serial.print("\tX=");
  17. Serial.print(gyroX);
  18. Serial.print("°/s");
  19. Serial.print("\tY=");
  20. Serial.print(gyroY);
  21. Serial.print("°/s");
  22. Serial.print("\tZ=");
  23. Serial.print(gyroZ);
  24. Serial.print("°\r\n");
  25. Serial.print("Winkel der Achsen ");
  26. Serial.print("\tX=");
  27. Serial.print(winkelX);
  28. Serial.print("° ");
  29. Serial.print("\tY=");
  30. Serial.print(winkelY);
  31. Serial.print("° ");
  32. Serial.print("\tZ=");
  33. Serial.print(winkelZ);
  34. Serial.print("°\r\n");
  35. Serial.print("3-Achsen-Beschleunigungsmesser ");
  36. Serial.print("\tX=");
  37. Serial.print(beschleunigungX);
  38. Serial.print("m/s² ");
  39. Serial.print("\tY=");
  40. Serial.print(beschleunigungY);
  41. Serial.print("m/s² ");
  42. Serial.print("\tZ=");
  43. Serial.print(beschleunigungZ);
  44. Serial.print("m/s²\r\n");
  45. }
  46. void MPU6050::setUpMPU() {
  47. // power management
  48. Wire.beginTransmission(0b1101000);
  49. Wire.write(0x6B);
  50. Wire.write(0b00000000);
  51. Wire.endTransmission();
  52. // configure gyro
  53. Wire.beginTransmission(0b1101000);
  54. Wire.write(0x1B);
  55. Wire.write(0b00000000);
  56. Wire.endTransmission();
  57. // configure accelerometer
  58. Wire.beginTransmission(0b1101000);
  59. Wire.write(0x1C);
  60. Wire.write(0b00000000);
  61. Wire.endTransmission();
  62. }
  63. void MPU6050::callibrateGyroValues() {
  64. for (int i=0; i<5000; i++) {
  65. getGyroValues();
  66. gyroXCalli = gyroXCalli + gyroXPresent;
  67. gyroYCalli = gyroYCalli + gyroYPresent;
  68. gyroZCalli = gyroZCalli + gyroZPresent;
  69. }
  70. gyroXCalli = gyroXCalli/5000;
  71. gyroYCalli = gyroYCalli/5000;
  72. gyroZCalli = gyroZCalli/5000;
  73. }
  74. void MPU6050::readAndProcessAccelData() {
  75. Wire.beginTransmission(0b1101000);
  76. Wire.write(0x3B);
  77. Wire.endTransmission();
  78. Wire.requestFrom(0b1101000,6);
  79. while(Wire.available() < 6);
  80. accelX = Wire.read()<<8|Wire.read();
  81. accelY = Wire.read()<<8|Wire.read();
  82. accelZ = Wire.read()<<8|Wire.read();
  83. MPU6050::processAccelData();
  84. }
  85. void MPU6050::processAccelData() {
  86. beschleunigungX = (accelX/16384.0)*9.81;
  87. beschleunigungY = (accelY/16384.0)*9.81;
  88. beschleunigungZ = (accelZ/16384.0)*9.81;
  89. }
  90. void MPU6050::readAndProcessGyroData() {
  91. gyroXPast = gyroXPresent;
  92. gyroYPast = gyroYPresent;
  93. gyroZPast = gyroZPresent;
  94. timePast = timePresent;
  95. timePresent = millis();
  96. MPU6050::getGyroValues();
  97. MPU6050::getAngularVelocity();
  98. MPU6050::calculateAngle();
  99. }
  100. void MPU6050::getGyroValues() {
  101. Wire.beginTransmission(0b1101000);
  102. Wire.write(0x43);
  103. Wire.endTransmission();
  104. Wire.requestFrom(0b1101000,6);
  105. while(Wire.available() < 6);
  106. gyroXPresent = Wire.read()<<8|Wire.read();
  107. gyroYPresent = Wire.read()<<8|Wire.read();
  108. gyroZPresent = Wire.read()<<8|Wire.read();
  109. }