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 17:22:09 2018 +0000
Child:
1:bc1e8a435c95
Commit message:
new

Changed in this revision

IS_MPU6050.cpp Show annotated file Show diff for this revision Revisions of this file
IS_MPU6050.h Show annotated file Show diff for this revision Revisions of this file
MPU6050.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IS_MPU6050.cpp	Mon Jun 04 17:22:09 2018 +0000
@@ -0,0 +1,73 @@
+#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);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IS_MPU6050.h	Mon Jun 04 17:22:09 2018 +0000
@@ -0,0 +1,19 @@
+#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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MPU6050.lib	Mon Jun 04 17:22:09 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/garfieldsg/code/MPU6050/#1e0baaf91e96