• I want to build a small lasercut cube with built in MPU6050.
    each side should also get a small OLED display (behind the tinted acryl glass).

    You can tilt/roll/flip the cube to all the sides and depending on which side it stays
    the OLED display shows up some info (to toggle something)
    and by "yaw" the cube you should be able to modify values (f.e. volume)

    So this would be a kind of remote control...

    My problem is, that i do not know how to calculate the yaw by given values of MPU6050.
    Is there a 9-axis module needed with magnetometer?

    Currently I can get the position of flipping
    left, right, forwards, backwards

    but the yaw angle is not right for now...
    Hope someone can help me to get this working.

    This is my current start source:

    var mpu;
    var alpha = 0.5;
    var fX = 0;
    var fY = 0;
    var fZ = 0;
    var direction = -1;
    var maxAccThreshold = 15000;
    
    // get -1 or 1 for each value depending on the acceleration reach the threshold
    function getDirectionByAccelerationThreshold(xzzA­cc) {
      var xyzDir = [0,0,0];
      for (var i = 0; i < 3; ++i) {
        if (xzzAcc[i] < -maxAccThreshold)
          xyzDir[i] = -1;
        if (xzzAcc[i] > maxAccThreshold)
          xyzDir[i] = 1;
      }
      return xyzDir;
    }
    
    // print cube direction log
    function printDirectionLog(dir) {
      switch(dir) {
        case 0:  console.log("cube side: up");        break;
        case 1:  console.log("cube side: down");      break;
        case 2:  console.log("cube side: left");      break;
        case 3:  console.log("cube side: right");     break;
        case 4:  console.log("cube side: back");      break;
        case 5:  console.log("cube side: front");     break;
        default: console.log("cube side: undefined"); break;
      }
    }
    
    // read accelerations and get direction
    function readMPU6050() {
      var xzzAcceleration = mpu.getAcceleration();
      //low pass filter
      fX = xzzAcceleration[0] * alpha + (fX * (1.0 - alpha));
      fY = xzzAcceleration[1] * alpha + (fY * (1.0 - alpha));
      fZ = xzzAcceleration[2] * alpha + (fZ * (1.0 - alpha));
      // get direction (-1 or 1) for each value (x/y/z)
      var xyzDirection = getDirectionByAccelerationThreshold(xzzA­cceleration);
      var x = xyzDirection[0];
      var y = xyzDirection[1];
      var z = xyzDirection[2];
      // get sum of all acceleration values
      var sum = Math.abs(x) + Math.abs(y) + Math.abs(z);
      if (sum != 1)
        return;
      // get new direction (x/y/z is -1 or 1)
      var newDirection = -1;
      if (z == 1)       newDirection = 0;
      else if (z == -1) newDirection = 1;
      else if (y ==  1) newDirection = 2;
      else if (y == -1) newDirection = 3;
      else if (x ==  1) newDirection = 4;
      else if (x == -1) newDirection = 5;
      // skip if direction did not change
      //if (direction == newDirection)
      //  return;
      // remember new direction and print log
      if (direction != newDirection)
        printDirectionLog(newDirection);
      direction = newDirection;
      // get yaw/pitch/roll depending on direction
      var roll  = Math.atan2(-fY, fZ)*180.0/Math.PI;
      var pitch = Math.atan2(fX, Math.sqrt(fY*fY + fZ*fZ))*180.0/Math.PI;
      var yaw = Math.atan2(fY, fX)*180.0/Math.PI;
      print("roll: "+roll+" pitch: "+pitch+" yaw: "+yaw);
    }
    
    // init the module and start interval
    function onInit() {
      I2C2.setup({scl:B10,sda:B3});
      mpu = require("MPU6050").connect(I2C2);
      setInterval(readMPU6050, 250);
    }
    
    onInit();
    
About

Avatar for Jorgen @Jorgen started