Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1.  
  2. #include<Wire.h>
  3.  
  4. const int MPU_addr=0x68;
  5. uint32_t timer;
  6. double AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
  7. double compRoll, compPitch;
  8.  
  9.  
  10. //radians to degree
  11. #define deg 57.2957795
  12.  
  13. int MPU6050_write_reg(int reg, uint8_t data)
  14. {
  15. int e;
  16. e = MPU6050_write(reg, &data, 1);
  17.  
  18. return (e);
  19. }
  20.  
  21. int MPU6050_write(int start, const uint8_t *pData, int size)
  22. {
  23. int n, k;
  24. Wire.beginTransmission(0x68);
  25. n = Wire.write(start); // write the start address
  26. if (n != 1)
  27. return (-20);
  28.  
  29. n = Wire.write(pData, size); // write data bytes
  30. if (n != size)
  31. return (-21);
  32.  
  33. k = Wire.endTransmission(true); // release the I2C-bus
  34. if (k != 0)
  35. return (k);
  36.  
  37. return (0); // return : no error
  38. }
  39.  
  40.  
  41.  
  42. void setup()
  43. {
  44.  
  45. // Set I2C frequency to 400kHz
  46. Wire.begin();
  47. #if ARDUINO >= 157
  48. Wire.setClock(400000UL);
  49. #else
  50. TWBR = ((F_CPU / 400000UL) - 16) / 2;
  51. #endif
  52.  
  53. //Initial readings from MPU + DLPF activation
  54. Wire.beginTransmission(MPU_addr);
  55. Wire.write(0x6B); // PWR_MGMT_1 register
  56. Wire.write(0); // set to zero (wakes up the MPU-6050)
  57. Wire.endTransmission(true);
  58. Serial.begin(115200);
  59. delay(3000);
  60.  
  61. Wire.beginTransmission(MPU_addr);
  62. Wire.write(0x3B);
  63. Wire.endTransmission(false);
  64. Wire.requestFrom(MPU_addr,14,true);
  65. AcX=Wire.read()<<8|Wire.read();
  66. AcY=Wire.read()<<8|Wire.read();
  67. AcZ=Wire.read()<<8|Wire.read();
  68. Tmp=Wire.read()<<8|Wire.read();
  69. GyX=Wire.read()<<8|Wire.read();
  70. GyY=Wire.read()<<8|Wire.read();
  71. GyZ=Wire.read()<<8|Wire.read();
  72. Wire.beginTransmission(MPU_addr);
  73. Wire.write(0x6B);
  74. Wire.write(0);
  75. Wire.endTransmission(true);
  76. Serial.begin(115200);
  77. Wire.beginTransmission(MPU_addr);
  78.  
  79. MPU6050_write_reg(0x1A, 0x06);
  80. Wire.write(0x3B);
  81. Wire.endTransmission(false);
  82. Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
  83. AcX=Wire.read()<<8|Wire.read();
  84. AcY=Wire.read()<<8|Wire.read();
  85. AcZ=Wire.read()<<8|Wire.read();
  86. Tmp=Wire.read()<<8|Wire.read();
  87. GyX=Wire.read()<<8|Wire.read();
  88. GyY=Wire.read()<<8|Wire.read();
  89. GyZ=Wire.read()<<8|Wire.read();
  90.  
  91. double roll = atan2(AcY, AcZ)*deg;
  92. double pitch = atan2(-AcX, AcZ)*deg;
  93. double compRoll = roll;
  94. double compPitch = pitch;
  95.  
  96. timer = micros();
  97. }
  98.  
  99. void loop() {
  100.  
  101. Wire.beginTransmission(MPU_addr);
  102. Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
  103.  
  104. Wire.endTransmission(false);
  105. Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
  106. AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  107. AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  108. AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  109. Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  110. GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  111. GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  112. GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  113.  
  114. double dt1 = (double)(micros() - timer) / 1000000;
  115. timer = micros();
  116.  
  117. double roll = atan2(AcY, AcZ)*deg;
  118. double pitch = atan2(-AcX, AcZ)*deg;
  119.  
  120. //1 deg=131 raw value
  121. double gyroXrate = GyX/131.0;
  122. double gyroYrate = GyY/131.0;
  123. double gyroZrate= GyZ/131.0;
  124.  
  125. //Complementary filter
  126. compRoll = 0.99 * (compRoll + gyroXrate * dt1) + 0.01 * roll;
  127. compPitch = 0.99 * (compPitch + gyroYrate * dt1) + 0.01 * pitch;
  128. double InputP=compRoll-8.52;
  129. double InputR=compPitch-5.83;
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement