HarzSR

Untitled

Feb 6th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. #include "I2Cdev.h"
  2. #include "MPU6050_6Axis_MotionApps20.h"
  3. #include "Wire.h"
  4. MPU6050 mpu;
  5.  
  6. #define DEBUG
  7. #ifdef DEBUG
  8. //#define DPRINT(args...) Serial.print(args) //OR use the following syntax:
  9. #define DPRINTSTIMER(t) for (static unsigned long SpamTimer; (unsigned long)(millis() - SpamTimer) >= (t); SpamTimer = millis())
  10. #define DPRINTSFN(StrSize,Name,...) {char S[StrSize];Serial.print("\t");Serial.print(Name);Serial.print(" "); Serial.print(dtostrf((float)__VA_ARGS__ ,S));}//StringSize,Name,Variable,Spaces,Percision
  11. #define DPRINTLN(...) Serial.println(__VA_ARGS__)
  12. #else
  13. #define DPRINTSTIMER(t) if(false)
  14. #define DPRINTSFN(...) //blank line
  15. #define DPRINTLN(...) //blank line
  16. #endif
  17.  
  18.  
  19.  
  20. #define LED_PIN 13 //
  21.  
  22. // supply your own gyro offsets here, scaled for min sensitivity use MPU6050_calibration.ino
  23. // -4232 -706 1729 173 -94 37
  24. // XA YA ZA XG YG ZG
  25. //int MPUOffsets[6] = { -4232, -706, 1729, 173, -94, 37};
  26. int MPUOffsets[6] = { 2471, -563, 1628, 8, -23, 53};
  27.  
  28.  
  29. // ================================================================
  30. // === i2c SETUP Items ===
  31. // ================================================================
  32. void i2cSetup() {
  33. // join I2C bus (I2Cdev library doesn't do this automatically)
  34. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  35. Wire.begin();
  36. TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
  37. #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  38. Fastwire::setup(400, true);
  39. #endif
  40. }
  41.  
  42. // ================================================================
  43. // === INTERRUPT DETECTION ROUTINE ===
  44. // ================================================================
  45. volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
  46. void dmpDataReady() {
  47. mpuInterrupt = true;
  48. }
  49.  
  50. // ================================================================
  51. // === MPU DMP SETUP ===
  52. // ================================================================
  53. int FifoAlive = 0; // tests if the interrupt is triggering
  54. int IsAlive = -20; // counts interrupt start at -20 to get 20+ good values before assuming connected
  55. // MPU control/status vars
  56. uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
  57. uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
  58. uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
  59. uint16_t fifoCount; // count of all bytes currently in FIFO
  60. uint8_t fifoBuffer[64]; // FIFO storage buffer
  61.  
  62. // orientation/motion vars
  63. Quaternion q; // [w, x, y, z] quaternion container
  64. VectorInt16 aa; // [x, y, z] accel sensor measurements
  65. VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements
  66. VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements
  67. VectorFloat gravity; // [x, y, z] gravity vector
  68. float euler[3]; // [psi, theta, phi] Euler angle container
  69. float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
  70. byte StartUP = 100; // lets get 100 readings from the MPU before we start trusting them (Bot is not trying to balance at this point it is just starting up.)
  71.  
  72. void MPU6050Connect() {
  73. static int MPUInitCntr = 0;
  74. // initialize device
  75. mpu.initialize(); // same
  76. // load and configure the DMP
  77. devStatus = mpu.dmpInitialize();// same
  78.  
  79. if (devStatus != 0) {
  80. // ERROR!
  81. // 1 = initial memory load failed
  82. // 2 = DMP configuration updates failed
  83. // (if it's going to break, usually the code will be 1)
  84.  
  85. char * StatStr[5] { "No Error", "initial memory load failed", "DMP configuration updates failed", "3", "4"};
  86.  
  87. MPUInitCntr++;
  88.  
  89. Serial.print(F("MPU connection Try #"));
  90. Serial.println(MPUInitCntr);
  91. Serial.print(F("DMP Initialization failed (code "));
  92. Serial.print(StatStr[devStatus]);
  93. Serial.println(F(")"));
  94.  
  95. if (MPUInitCntr >= 10) return; //only try 10 times
  96. delay(1000);
  97. MPU6050Connect(); // Lets try again
  98. return;
  99. }
  100. mpu.setXAccelOffset(MPUOffsets[0]);
  101. mpu.setYAccelOffset(MPUOffsets[1]);
  102. mpu.setZAccelOffset(MPUOffsets[2]);
  103. mpu.setXGyroOffset(MPUOffsets[3]);
  104. mpu.setYGyroOffset(MPUOffsets[4]);
  105. mpu.setZGyroOffset(MPUOffsets[5]);
  106.  
  107. Serial.println(F("Enabling DMP..."));
  108. mpu.setDMPEnabled(true);
  109. // enable Arduino interrupt detection
  110. Serial.println(F("Enabling interrupt detection (Arduino external interrupt pin 2 on the Uno)..."));
  111. Serial.print("mpu.getInterruptDrive= "); Serial.println(mpu.getInterruptDrive());
  112. attachInterrupt(0, dmpDataReady, RISING); //pin 2 on the Uno
  113. mpuIntStatus = mpu.getIntStatus(); // Same
  114. // get expected DMP packet size for later comparison
  115. packetSize = mpu.dmpGetFIFOPacketSize();
  116. delay(1000); // Let it Stabalize
  117. mpu.resetFIFO(); // Clear fifo buffer
  118. mpu.getIntStatus();
  119. mpuInterrupt = false; // wait for next interrupt
  120. }
  121.  
  122. // ================================================================
  123. // === MPU DMP Get Data ===
  124. // ================================================================
  125. void GetDMP() { // Best version I have made so far
  126. // Serial.println(F("FIFO interrupt at:"));
  127. // Serial.println(micros());
  128. static unsigned long LastGoodPacketTime;
  129. mpuInterrupt = false;
  130. FifoAlive = 1;
  131. fifoCount = mpu.getFIFOCount();
  132. if ((!fifoCount) || (fifoCount % packetSize)) { // we have failed Reset and wait till next time!
  133. digitalWrite(LED_PIN, LOW); // lets turn off the blinking light so we can see we are failing.
  134. mpu.resetFIFO();// clear the buffer and start over
  135. } else {
  136. while (fifoCount >= packetSize) { // Get the packets until we have the latest!
  137. mpu.getFIFOBytes(fifoBuffer, packetSize); // lets do the magic and get the data
  138. fifoCount -= packetSize;
  139. }
  140. LastGoodPacketTime = millis();
  141. MPUMath(); // <<<<<<<<<<<<<<<<<<<<<<<<<<<< On success MPUMath() <<<<<<<<<<<<<<<<<<<
  142. digitalWrite(LED_PIN, !digitalRead(LED_PIN)); // Blink the Light
  143. }
  144. }
  145.  
  146.  
  147. // ================================================================
  148. // === MPU Math ===
  149. // ================================================================
  150. float Yaw, Pitch, Roll;
  151. void MPUMath() {
  152. mpu.dmpGetQuaternion(&q, fifoBuffer);
  153. mpu.dmpGetGravity(&gravity, &q);
  154. mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  155. Yaw = (ypr[0] * 180.0 / M_PI);
  156. Pitch = (ypr[1] * 180.0 / M_PI);
  157. Roll = (ypr[2] * 180.0 / M_PI);
  158. DPRINTSTIMER(100) {
  159. DPRINTSFN(15, "\tYaw:", Yaw, 6, 1);
  160. DPRINTSFN(15, "\tPitch:", Pitch, 6, 1);
  161. DPRINTSFN(15, "\tRoll:", Roll, 6, 1);
  162. DPRINTLN();
  163. }
  164. }
  165. // ================================================================
  166. // === Setup ===
  167. // ================================================================
  168. void setup() {
  169. Serial.begin(115200); //115200
  170. while (!Serial);
  171. Serial.println("i2cSetup");
  172. i2cSetup();
  173. Serial.println("MPU6050Connect");
  174. MPU6050Connect();
  175. Serial.println("Setup complete");
  176. pinMode(LED_PIN, OUTPUT);
  177. }
  178. // ================================================================
  179. // === Loop ===
  180. // ================================================================
  181. void loop() {
  182. if (mpuInterrupt ) { // wait for MPU interrupt or extra packet(s) available
  183. GetDMP();
  184. }
  185. }
Add Comment
Please, Sign In to add comment