// onOrientationData() is called whenever a Myo provides its current orientation, // represented as a quaternion. @Override public void onOrientationData(Myo myo, long timestamp, Quaternion rotation) { // Calculate Euler angles (roll, pitch, and yaw) from the quaternion. float roll = (float) Math.toDegrees(Quaternion.roll(rotation)); float pitch = (float) Math.toDegrees(Quaternion.pitch(rotation)); float yaw = (float) Math.toDegrees(Quaternion.yaw(rotation)); // Adjust roll and pitch for the orientation of the Myo on the arm. if (myo.getXDirection() == XDirection.TOWARD_ELBOW) { roll *= -1; pitch *= -1; } // Next, we apply a rotation to the text view using the roll, pitch, and yaw. // mTextView.setRotation(roll); // mTextView.setRotationX(pitch); // mTextView.setRotationY(yaw); }
@Override public void onOrientationData(Myo myo, long timestamp, Quaternion rotation) { Quaternion normalized = rotation.normalized(); double roll = Math.atan2( 2.0f * (normalized.getW() * normalized.getX() + normalized.getY() * normalized.getZ()), 1.0f - 2.0f * (normalized.getX() * normalized.getX() + normalized.getY() * normalized.getY())); double pitch = Math.asin( 2.0f * (normalized.getW() * normalized.getY() - normalized.getZ() * normalized.getX())); double yaw = Math.atan2( 2.0f * (normalized.getW() * normalized.getZ() + normalized.getX() * normalized.getY()), 1.0f - 2.0f * (normalized.getY() * normalized.getY() + normalized.getZ() * normalized.getZ())); rollW = ((roll + Math.PI) / (Math.PI * 2.0) * SCALE); pitchW = ((pitch + Math.PI / 2.0) / Math.PI * SCALE); yawW = ((yaw + Math.PI) / (Math.PI * 2.0) * SCALE); OrientationData orientation = new OrientationData(); orientation.rollW = rollW; orientation.pitchW = pitchW; orientation.yawW = yawW; System.out.println(orientation.rollW); }