Guest User

Untitled

a guest
Dec 10th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #define LEDPIN 2
  2.  
  3. #include <Wire.h>
  4. #include "rgb_lcd.h"
  5.  
  6. #define MPU6050 0x68 //Device address (standard)
  7. #define ACCEL_CONFIG 0x1C //Accelerometer configuration address
  8. #define GYRO_CONFIG 0x1B //Gyro configuration address
  9.  
  10. //Registers: Accelerometer, Temp, Gyroscope
  11. #define ACCEL_XOUT_H 0x3B
  12. #define ACCEL_XOUT_L 0x3C
  13. #define ACCEL_YOUT_H 0x3D
  14. #define ACCEL_YOUT_L 0x3E
  15. #define ACCEL_ZOUT_H 0x3F
  16. #define ACCEL_ZOUT_L 0x40
  17. #define TEMP_OUT_H 0x41
  18. #define TEMP_OUT_L 0x42
  19. #define GYRO_XOUT_H 0x43
  20. #define GYRO_XOUT_L 0x44
  21. #define GYRO_YOUT_H 0x45
  22. #define GYRO_YOUT_L 0x46
  23. #define GYRO_ZOUT_H 0x47
  24. #define GYRO_ZOUT_L 0x48
  25.  
  26. #define PWR_MGMT_1 0x6B
  27. #define PWR_MGMT_2 0x6C
  28.  
  29.  
  30. //Sensor output scaling
  31. #define accSens 3 // 0 = 2g, 1 = 4g, 2 = 8g, 3 = 16g
  32.  
  33.  
  34.  
  35. int16_t AcX, AcY, GyZ; // acceleration en x et y, angle en z
  36.  
  37. int nbpas=0;
  38. boolean detectpas = true;
  39. rgb_lcd lcd;
  40.  
  41.  
  42. void setup() {
  43.  
  44. pinMode (LEDPIN, OUTPUT);
  45. Serial.begin(57600);
  46.  
  47. lcd.begin(16, 2);
  48. lcd.setRGB(255, 0, 0);
  49.  
  50.  
  51. setup();
  52. }
  53.  
  54. void loop() {
  55.  
  56.  
  57. angle_calc();
  58. Serial.println("--------------- Nouvelle Mesure ---------------");
  59.  
  60.  
  61. }
  62.  
  63. //------------------------------------------------------------------
  64. //Robot angle calculations------------------------------------------
  65. //------------------------------------------------------------------
  66.  
  67. void writeTo(byte device, byte address, byte value) {
  68. Wire.beginTransmission(device);
  69. Wire.write(address);
  70. Wire.write(value);
  71. Wire.endTransmission(true);
  72. }
  73.  
  74.  
  75.  
  76. //setup MPU6050
  77. void setup()
  78. {
  79. Wire.begin();
  80. delay (100);
  81. writeTo(MPU6050, PWR_MGMT_1, 0);
  82. writeTo(MPU6050, ACCEL_CONFIG, accSens << 3); // Specifying output scaling of accelerometer
  83. delay (100);
  84. Serial.println("setup done !");
  85. }
  86.  
  87.  
  88. //calculate robot tilt angle
  89. void angle_calc()
  90. {
  91. // read raw accel/gyro measurements from device
  92. Wire.beginTransmission(MPU6050);
  93. Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
  94. Wire.endTransmission(false);
  95. Wire.requestFrom(MPU6050, 4, true); // request a total of 4 registers
  96. AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  97. AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  98. Wire.beginTransmission(MPU6050);
  99. Wire.write(0x47);
  100. Wire.endTransmission(false);
  101. Wire.requestFrom(MPU6050, 2, true); // request a total of 2 registers
  102. GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  103.  
  104.  
  105. float testy = AcY/1024.0;
  106. float testx = AcX/1024.0;
  107.  
  108.  
  109. Serial.print("AcX = "); Serial.print(testx); Serial.print("t");
  110.  
  111.  
  112. }
Add Comment
Please, Sign In to add comment