Tuesday 22 February 2022

How to get the Z,Y,X Rotation of Android/iOS Device in React Native? (Pitch,Yaw,Roll)?

I tried using: "react-native-sensors" library

https://github.com/react-native-sensors/react-native-sensors/blob/master/docs/API.md#orientation-observableqx-number-qy-number-qz-number-qw-number-pitch-number-roll-number-yaw-number-timestamp-string

import { orientation } from "react-native-sensors";

const subscription = orientation.subscribe(({ qx, qy, qz, qw, pitch, roll, yaw, timestamp }) =>
    console.log({ qx, qy, qz, qw, pitch, roll, yaw, timestamp })
);

I tried using their given pitch,roll,yaw and converted from radian_to_degress still not the same values as the emulator.

They also provide a quaternion to Angles function

function quaternionToAngles(q) {
    let data = q;

    let ysqr = data.y * data.y;
    let t0 = -2.0 * (ysqr + data.z * data.z) + 1.0;
    let t1 = +2.0 * (data.x * data.y + data.w * data.z);
    let t2 = -2.0 * (data.x * data.z - data.w * data.y);
    let t3 = +2.0 * (data.y * data.z + data.w * data.x);
    let t4 = -2.0 * (data.x * data.x + ysqr) + 1.0;

    t2 = t2 > 1.0 ? 1.0 : t2;
    t2 = t2 < -1.0 ? -1.0 : t2;

    const toDeg = 180 / Math.PI;

    const euler = {};
    euler.pitch = Math.asin(t2) * toDeg;
    euler.roll = Math.atan2(t3, t4) * toDeg;
    euler.yaw = Math.atan2(t1, t0) * toDeg;

    return euler;
}

The function gives wrong results for roll,yaw and pitch. I dont want to dive into the Math of this. google searching gives 100x different equations.

enter image description here

Im searching for simple equations to get the Z,Y,X Rotations of my Device just like the Android Emulator provides. If Z-Rot Value is "90"(on emulator) my program should also receive the same value. I have access to gyrometer,barometer,accelometer etc.

PS: I wasted way to much time on google for a problem that should be solvable with a copy paste after one google search so I hope this question will help.



from How to get the Z,Y,X Rotation of Android/iOS Device in React Native? (Pitch,Yaw,Roll)?

No comments:

Post a Comment