|
|
@@ -0,0 +1,101 @@
|
|
|
+ #include "I2Cdev.h"
|
|
|
+#include "MPU6050.h"
|
|
|
+
|
|
|
+#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
|
|
|
+ #include "Wire.h"
|
|
|
+#endif
|
|
|
+ #include "struct.h"
|
|
|
+
|
|
|
+#include <QMC5883LCompass.h>
|
|
|
+QMC5883LCompass compass;
|
|
|
+
|
|
|
+
|
|
|
+const int MPU=0x68;
|
|
|
+int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
|
|
|
+
|
|
|
+MPU6050 accelgyro(MPU);
|
|
|
+
|
|
|
+ int16_t ax, ay, az;
|
|
|
+ int16_t gx, gy, gz;
|
|
|
+#define OUTPUT_READABLE_ACCELGYRO
|
|
|
+//#define OUTPUT_BINARY_ACCELGYRO
|
|
|
+
|
|
|
+
|
|
|
+#define LED_PIN 13
|
|
|
+bool blinkState = false;
|
|
|
+
|
|
|
+
|
|
|
+void setup()
|
|
|
+{
|
|
|
+
|
|
|
+ #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
|
|
|
+ Wire.begin();
|
|
|
+ #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
|
|
|
+ Fastwire::setup(400, true);
|
|
|
+ #endif
|
|
|
+ Serial.begin(115200);
|
|
|
+ while (!Serial); // Waiting for Serial Monitor
|
|
|
+ Serial.println("\nCompass ang gyro test ");
|
|
|
+ #include "scan.h"
|
|
|
+
|
|
|
+ Serial.println("compass.init()");
|
|
|
+ compass.init();
|
|
|
+ Serial.println("accelgyro.initialize()");
|
|
|
+ accelgyro.initialize();
|
|
|
+ Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
|
|
|
+ accelgyro.setMotionDetectionDuration(80);
|
|
|
+
|
|
|
+
|
|
|
+ pinMode(LED_PIN, OUTPUT);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // setZeroMotionDetectionThreshold(154);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void loop()
|
|
|
+{
|
|
|
+ int x, y, z;
|
|
|
+Serial.println();
|
|
|
+ // Read compass values
|
|
|
+ compass.read();
|
|
|
+
|
|
|
+ x = compass.getX();
|
|
|
+ y = compass.getY();
|
|
|
+ z = compass.getZ();
|
|
|
+
|
|
|
+ Serial.print("Comp X: ");
|
|
|
+ Serial.print(x);
|
|
|
+ Serial.print(" Y: ");
|
|
|
+ Serial.print(y);
|
|
|
+ Serial.print(" Z: ");
|
|
|
+ Serial.print(z);
|
|
|
+
|
|
|
+ accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
|
|
|
+ // these methods (and a few others) are also available
|
|
|
+ //accelgyro.getAcceleration(&ax, &ay, &az);
|
|
|
+ //accelgyro.getRotation(&gx, &gy, &gz);
|
|
|
+#ifdef OUTPUT_READABLE_ACCELGYRO
|
|
|
+ // display tab-separated accel/gyro x/y/z values
|
|
|
+ Serial.print(" a/g:\t");
|
|
|
+ Serial.print(ax); Serial.print("\t");
|
|
|
+ Serial.print(ay); Serial.print("\t");
|
|
|
+ Serial.print(az); Serial.print("\t");
|
|
|
+ Serial.print(gx); Serial.print("\t");
|
|
|
+ Serial.print(gy); Serial.print("\t");
|
|
|
+ Serial.print(gz);
|
|
|
+ #endif
|
|
|
+
|
|
|
+ #ifdef OUTPUT_BINARY_ACCELGYRO
|
|
|
+ Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF));
|
|
|
+ Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF));
|
|
|
+ Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF));
|
|
|
+ Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF));
|
|
|
+ Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF));
|
|
|
+ Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF));
|
|
|
+ #endif
|
|
|
+
|
|
|
+ delay(1500);
|
|
|
+
|
|
|
+}
|