1
\$\begingroup\$

I have this code which is rotating my camera object based on Gyro it is working fine

public void CalibrateYAngle()
    {
        calibrationYAngle = appliedGyroYAngle - initialYAngle; // Offsets the y angle in case it wasn't 0 at edit time.
    }

    void ApplyGyroRotation()
    {
        transform.rotation = Input.gyro.attitude; //
        transform.Rotate(0f, 0f, 180f, Space.Self); // Swap "handedness" of quaternion from gyro.
        transform.Rotate(90f, 180f, 0f, Space.World); // Rotate to make sense as a camera pointing out the back of your device.
        appliedGyroYAngle = transform.eulerAngles.y; // Save the angle around y axis for use in calibration.
    }
 public void Update() {

            ApplyGyroRotation();
            ApplyCalibration();

    }

but sometime my camera start to rotate slowly slowly automatically. It slowly rotate left or right but sometime not always. What I am doing wrong? due to inaccurate sensors values?

\$\endgroup\$
4
  • \$\begingroup\$ I know some systems without a magnetometer will slowly center themselves back to a reference yaw value, in an attempt to keep errors from accumulating into large variances in yaw. I don't know if Unity does this. What kind of results are you getting from Input.compass? \$\endgroup\$ – DMGregory Jul 13 '17 at 11:20
  • \$\begingroup\$ @DMGregory Thanks for valuable input. Can you please refer me the source, where did you get that? and sorry i don't have access to device Samsung s5 now so i can't test. \$\endgroup\$ – Muhammad Faizan Khan Jul 13 '17 at 12:16
  • \$\begingroup\$ @DMGregory And You mean that a device that has magnetometer sensor will not make this problem? yaw auto adjustment problem? \$\endgroup\$ – Muhammad Faizan Khan Jul 13 '17 at 12:17
  • \$\begingroup\$ This is just from personal experience implementing motion controls on eg. the Wii remote or SixAxis/DualShock3 & 4. With only a rate gyro and accelerometer, we don't have an absolute yaw reference, so a resting yaw of 90° isn't necessarily meaningful — it could just be accumulated drift as kolenda describes. One strategy to reduce distractions from that spurious yaw is to pull it to zero when at rest. If you have a magnetometer, you can use that for an absolute yaw reference instead, but it's not necessarily automatic. You might need to enable it or fuse it yourself as discussed below. \$\endgroup\$ – DMGregory Jul 13 '17 at 12:59
1
\$\begingroup\$

This is a known problem with low-cost gyroscopes mounted in phones and it's called drift. Try to put your phone on a desk and read gyro values for some time. Even when you don't move your phone the readings will probably change in time.

There's no direct solution for this, you can't make it more precise but one of the workarounds is to use other physical sensors to help to 'fix' the gyro readings. Unfortunately there are many different sensors used in different phones so it may be very hard to find some reliable solution.

For some quick and dirty test I'd try to limit the minimal rotation per time span or frame:

  • Remember orientation from the last frame.
  • If current orientation differs more than some threshold - apply it.
  • If current orientation differs less than some threshold - remember it as a last frame orientation.
  • Let us know if it helped :).
\$\endgroup\$
10
  • \$\begingroup\$ You can also check the tag sensor-fusion for methods to combine gyro and compass readings into a more complete/drift-resistant orientation estimate. I don't know if Unity exposes a fused result on its own or if we need to do this manually. \$\endgroup\$ – DMGregory Jul 13 '17 at 11:48
  • \$\begingroup\$ @kolenda yes you are rigt and i have checked that on S3 and S5 mobile and gyro values are changing continuously. While as workaround you said "use different sensor", i guess there are only two sensors to get rotations from phone, gyro and accelerometer and gyro is lot more better than accelerometer . is there any third sensor available? \$\endgroup\$ – Muhammad Faizan Khan Jul 13 '17 at 12:27
  • \$\begingroup\$ @DMGregory sensor-fusion, sounds amazing, hope that unity didn't think out of the box yet. but i think that any sensor fusion algorithm can easily be applied in C#. what do you think, after all unity allow to get gyro and accelero sensor values. ins't it? \$\endgroup\$ – Muhammad Faizan Khan Jul 13 '17 at 12:30
  • \$\begingroup\$ @kolenda "This is a known problem with low-cost gyroscopes", do you think that Samsung s5 have a low cost gyroscope. ? \$\endgroup\$ – Muhammad Faizan Khan Jul 13 '17 at 12:32
  • \$\begingroup\$ I've certainly implemented sensor fusion in Unity C# in the past, when working with custom sensors that didn't get handled by the Input class automatically. I know Unity does a certain amount of fusion between the rate gyro and accelerometer internally to produce some of the values accessible in the Input class. What I don't know is whether it also uses the compass in that fusion when it's present & enabled. I'd investigate that first, since if they have a 9 dof fusion algorithm internally then it's probably more proven & tested than writing our own. ;) \$\endgroup\$ – DMGregory Jul 13 '17 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.