Guest User

Untitled

a guest
Jul 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <math.h>
  3.  
  4. void setup() {
  5. Serial.begin(115200);
  6. Serial.println("Start");
  7.  
  8. Wire.beginTransmission(0x68);
  9. Wire.write(0x1B);//速度のフルスケール設定があるレジスタ
  10. Wire.write(0b00000000);//4,3bitを01に指定することで500degree/secまで測れる
  11. Wire.endTransmission();
  12.  
  13. Wire.beginTransmission(0x68);
  14. Wire.write(0x1C);//加速度のスケールレンジ設定があるレジスタ
  15. Wire.write(0b00000000);//4,3bitを00に指定することで+-2gまで測れる。gが重力加速度
  16. Wire.endTransmission();
  17.  
  18. Wire.beginTransmission(0x68);
  19. Wire.write(0x6B);//クロック設定があるレジスタ
  20. Wire.write(0b00000000);//2,1,0bitを000に指定することで、20MHz振動に設定
  21. Wire.endTransmission();
  22. delay(1000);
  23. }
  24.  
  25. uint8_t Data[14] = {0};
  26. int16_t DData[7] = {0};
  27. float AData[7] = {0};
  28. float angl[3] = {0};
  29. float grad[3] = {0};
  30.  
  31.  
  32. void loop() {
  33. int static timeA = 0;
  34. int static timeB = 0;
  35. double static timeDelta = 0;
  36. Wire.beginTransmission(0x68);
  37. Wire.write(0x3B);
  38. Wire.endTransmission();
  39. Wire.requestFrom(0x68, 14);
  40. uint8_t index = 0;
  41.  
  42. while(Wire.available()){
  43. Data[index++] = Wire.read();
  44. }
  45.  
  46. DData[0] = (int16_t)(Data[0]<<8 | Data[1]);
  47. DData[1] = (int16_t)(Data[2]<<8 | Data[3]);
  48. DData[2] = (int16_t)(Data[4]<<8 | Data[5]);
  49. DData[3] = (int16_t)(Data[6]<<8 | Data[7]);
  50. DData[4] = (int16_t)(Data[8]<<8 | Data[9]);
  51. DData[5] = (int16_t)(Data[10]<<8 | Data[11]);
  52. DData[6] = (int16_t)(Data[12]<<8 | Data[13]);
  53.  
  54. AData[0] = ((float)DData[0]/16383)*9.81;//単位は重力加速度[g]。例、1[g] = 9.81[m/s^2]
  55. AData[1] = ((float)DData[1]/16383)*9.81;
  56. AData[2] = ((float)DData[2]/16383)*9.81;
  57.  
  58. AData[4] = (float)DData[4]/131;//角速度x
  59. AData[5] = (float)DData[5]/131;//角速度y
  60. AData[6] = (float)DData[6]/131;//角速度z
  61.  
  62. timeA = micros();
  63. timeDelta = (double)(timeA - timeB)/pow(10,6);
  64. //Δ角度 = 角速度 × Δ時間
  65. //角度 = 今までのΔ角度を足し合わせたもの
  66. angl[0] += AData[4] * timeDelta;//角度x = Σ (Δ角度x)
  67. angl[1] += AData[5] * timeDelta;
  68. angl[2] += AData[6] * timeDelta;
  69. timeB = timeA;
  70.  
  71. //Serial.print("GYRO[deg/s]: ");
  72. //Serial.print(AData[4]); Serial.print("\t");
  73. //Serial.print(AData[5]); Serial.print("\t");
  74. //Serial.print(AData[6]); Serial.print("\t");
  75.  
  76. Serial.print("angl[deg]: ");
  77. Serial.print(angl[0]); Serial.print("\t");
  78. Serial.print(angl[1]); Serial.print("\t");
  79. Serial.print(angl[2]); Serial.print("\t");
  80.  
  81. Serial.print("\n");
  82. delay(10);
  83.  
  84.  
  85. }
Add Comment
Please, Sign In to add comment