Library receiving values from MPU6050

Dependencies:   MPU6050

Dependents:   TI_MPU6050_SAMPLE

  1. include "mbed.h"
  2. include "TI_MPU6050.h"

Example

include the mbed library with this snippet

TI_MPU6050 mpu6050;

DigitalOut led1(LED1);

int main() {
    mpu6050.setSleepEnabled(false);
    
    while(1) {
        if (mpu6050.isHorizontal()) {
            led1 = 1;
        } else {
            led1 = 0;
        }
        
        int verticalAngle = mpu6050.getVerticalAngle();
        printf("verticalAngle is %d\n\r", verticalAngle);
    }
}

Files at this revision

API Documentation at this revision

Comitter:
tichise
Date:
Mon Jun 04 22:41:56 2018 +0000
Parent:
0:81e5563b1ea1
Commit message:
new

Changed in this revision

IS_MPU6050.cpp Show diff for this revision Revisions of this file
IS_MPU6050.h Show diff for this revision Revisions of this file
TI_MPU6050.cpp Show annotated file Show diff for this revision Revisions of this file
TI_MPU6050.h Show annotated file Show diff for this revision Revisions of this file
diff -r 81e5563b1ea1 -r bc1e8a435c95 IS_MPU6050.cpp
--- a/IS_MPU6050.cpp	Mon Jun 04 17:22:09 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,73 +0,0 @@
-#include "IS_MPU6050.h"
-#include "mbed.h"
-
-IS_MPU6050::IS_MPU6050()
-{
-    _mpu.initialize();
-
-    bool result = _mpu.testConnection();
-
-    if (result) {
-        // printf("MPU6050 Test Connection passed\n\r");
-    } else {
-        // printf("MPU6050 Test Connection failed\n\r");
-    }
-}
-
-int IS_MPU6050::getVerticalAngle()
-{
-    int16_t ax, ay, az;
-    int16_t gx, gy, gz;
-
-    int i = 0;
-    long x = 0;
-    long y = 0;
-    long z = 0;
-
-    int repeatCount = 100;
-
-    // 各データを100回読み込んで平均化
-    for (i = 0; i < repeatCount; ++i) {
-        _mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
-
-        // printf("x:%d; y:%d; z:%d;\n\r",ax,ay,az);
-
-        x = x + ax; // X軸を読み込む
-        y = y + ay; // Y軸を読み込む
-        z = z + az; // Y軸を読み込む
-    }
-
-    double avX = x / repeatCount;
-    double avY = y / repeatCount;
-
-    int verticalAngle = atan2(avX - 507, avY - 520) / 3.14159 * 180.0;
-
-    return verticalAngle;
-}
-
-bool IS_MPU6050::isHorizontal()
-{
-    int verticalAngle = getVerticalAngle();
-
-    int baseAngle = 90;
-    bool isHorizontal;
-
-    if (verticalAngle < (baseAngle - 20)) {
-        // printf("vertical Angle : %d;\n\r", verticalAngle);
-
-        isHorizontal = false;
-    } else if ((baseAngle + 20) < verticalAngle) {
-        // printf("vertical Angle : %d;\n\r", verticalAngle);
-
-        isHorizontal = false;
-    } else {
-        isHorizontal = true;
-    }
-
-    return isHorizontal;
-}
-
-void IS_MPU6050::setSleepEnabled(bool enabled)
-{
-    _mpu.setSleepEnabled(enabled);
-}
diff -r 81e5563b1ea1 -r bc1e8a435c95 IS_MPU6050.h
--- a/IS_MPU6050.h	Mon Jun 04 17:22:09 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,19 +0,0 @@
-#ifndef MBED_IS_MPU6050_H
-#define MBED_IS_MPU6050_H
-
-#include "mbed.h"
-#include "MPU6050.h" // https://os.mbed.com/users/garfieldsg/code/MPU6050/
-
-class IS_MPU6050
-{
-public:
-    IS_MPU6050();
-    int getVerticalAngle();
-    bool isHorizontal();
-    void setSleepEnabled(bool enabled);
-
-private:
-    MPU6050 _mpu;
-};
-
-#endif
diff -r 81e5563b1ea1 -r bc1e8a435c95 TI_MPU6050.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TI_MPU6050.cpp	Mon Jun 04 22:41:56 2018 +0000
@@ -0,0 +1,73 @@
+#include "TI_MPU6050.h"
+#include "mbed.h"
+
+TI_MPU6050::TI_MPU6050()
+{
+    _mpu.initialize();
+
+    bool result = _mpu.testConnection();
+
+    if (result) {
+        // printf("MPU6050 Test Connection passed\n\r");
+    } else {
+        // printf("MPU6050 Test Connection failed\n\r");
+    }
+}
+
+int TI_MPU6050::getVerticalAngle()
+{
+    int16_t ax, ay, az;
+    int16_t gx, gy, gz;
+
+    int i = 0;
+    long x = 0;
+    long y = 0;
+    long z = 0;
+
+    int repeatCount = 100;
+
+    // 各データを100回読み込んで平均化
+    for (i = 0; i < repeatCount; ++i) {
+        _mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
+
+        // printf("x:%d; y:%d; z:%d;\n\r",ax,ay,az);
+
+        x = x + ax; // X軸を読み込む
+        y = y + ay; // Y軸を読み込む
+        z = z + az; // Y軸を読み込む
+    }
+
+    double avX = x / repeatCount;
+    double avY = y / repeatCount;
+
+    int verticalAngle = atan2(avX - 507, avY - 520) / 3.14159 * 180.0;
+
+    return verticalAngle;
+}
+
+bool TI_MPU6050::isHorizontal()
+{
+    int verticalAngle = getVerticalAngle();
+
+    int baseAngle = 90;
+    bool isHorizontal;
+
+    if (verticalAngle < (baseAngle - 20)) {
+        // printf("vertical Angle : %d;\n\r", verticalAngle);
+
+        isHorizontal = false;
+    } else if ((baseAngle + 20) < verticalAngle) {
+        // printf("vertical Angle : %d;\n\r", verticalAngle);
+
+        isHorizontal = false;
+    } else {
+        isHorizontal = true;
+    }
+
+    return isHorizontal;
+}
+
+void TI_MPU6050::setSleepEnabled(bool enabled)
+{
+    _mpu.setSleepEnabled(enabled);
+}
diff -r 81e5563b1ea1 -r bc1e8a435c95 TI_MPU6050.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TI_MPU6050.h	Mon Jun 04 22:41:56 2018 +0000
@@ -0,0 +1,19 @@
+#ifndef MBED_TI_MPU6050_H
+#define MBED_TI_MPU6050_H
+
+#include "mbed.h"
+#include "MPU6050.h" // https://os.mbed.com/users/garfieldsg/code/MPU6050/
+
+class TI_MPU6050
+{
+public:
+    TI_MPU6050();
+    int getVerticalAngle();
+    bool isHorizontal();
+    void setSleepEnabled(bool enabled);
+
+private:
+    MPU6050 _mpu;
+};
+
+#endif