Dhilip Kumar
Dhilip Kumar

Reputation: 69

React Native: How to compute x, y and z axis rotation angles based on gyroscope sensor outputs?

I was able to achieve a 2D rotation on X-Axis using x and y values but would like to compute Y-Axis and Z-Axis rotation angles.

Below is the formula that I have used for computing xAngle rotaion.

var xAngle = Math.atan2(x, y)/(Math.PI/180)

where x and y are the gravitational values on x and y-axis provided by the gyroscope sensor respectively.

Can someone help me by sharing a similar set of formulae for yAngle and zAngle computations?

Thanks in Advance!

Upvotes: 1

Views: 1838

Answers (1)

SEoF
SEoF

Reputation: 1114

Homework

Having just done the maths on this myself, there are some caveats you might want to be aware of.

Lets start by taking a quick look at the axis and using the correct names. Pitch, roll, and yaw describe the rotation around each of the axis. See https://sidvind.com/wiki/Yaw,_pitch,_roll_camera for more information.

Also, everything is relative. The sensors might be relative to one way of thinking, whilst the desired way of thinking might be relative to another.

My solution

In my case, the relative aspect is the tablet in landscape (landscape-left: rotated 90° counter-clockwise) then laid flat on its back (upright has a pitch of 90°).

The accelerometer readings (that I use) are relative to portrait upright.

Relative to gravity, the rotation around the y-axis can't be calculated from accelerometer information. This means:

  • Pitch can't be calculated when portrait and upright
  • Roll can't be calculated when flat on a desk
  • Yaw can't be calculated when landscape and upright
pitch = Math.atan2(-x, -z) * 180 / Math.PI;// In degrees
roll = Math.atan2(-y, -x) * 180 / Math.PI;// In degrees
yaw = Math.atan2(y, -z) * 180 / Math.PI;// In degrees

Assuming the sensors and the desired way of thinking match

  • Pitch can't be calculated when landscape and upright
  • Roll can't be calculated when flat on a desk
  • Yaw can't be calculated when portrait and upright
pitch = Math.atan2(z, -y) * 180 / Math.PI;// In degrees
roll = Math.atan2(x, -y) * 180 / Math.PI;// In degrees
yaw = Math.atan2(-x, -z) * 180 / Math.PI;// In degrees

Your solution

Your x-axis rotation matches the pitch on neither of these (nor gets close, negating the symbols), which suggests you're not calculating the rotation around the x-axis, but rotation of the x-axis around another, most likely the z-axis rotation (roll).

Upvotes: 1

Related Questions