Possible gimbal lock with Maui .Net on Android

80 Views Asked by At

I am using orientation sensor with Maui .NET on an Android Device (Samsung S8+). When I rotate the device around Z axis in vertical position, I get different values than I expect.

I implemented the following code:

private void ToggleOrientation()
{
    if (OrientationSensor.Default.IsSupported)
    {
        if (!OrientationSensor.Default.IsMonitoring)
        {
            // Turn on orientation
            OrientationSensor.Default.ReadingChanged += Orientation_ReadingChanged;
            OrientationSensor.Default.Start(SensorSpeed.UI);
        }
        else
        {
            // Turn off orientation
            OrientationSensor.Default.Stop();
            OrientationSensor.Default.ReadingChanged -= Orientation_ReadingChanged;
        }
    }
}

private void Orientation_ReadingChanged(object sender, OrientationSensorChangedEventArgs e)
{
    // Update UI Label with orientation state
    OrientationLabel.TextColor = Colors.Green;
    OrientationLabel.Text = $"Orientation: {e.Reading}";
}

Source: https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/device/sensors?view=net-maui-8.0&tabs=windows#orientation

In the device, I am doing the following steps:

  1. Set device in identity position (device lies flat on a table with its screen facing up, with the top of the device (in portrait mode) pointing north - Quaternion: (0,0,0,1) - all good here, data shows as expected.

  2. Rotate device around X axis 90 degrees (device is held upright so that the top (in portrait mode) points towards the sky, and the back of the device faces north) - Quaternion: (0.707, 0, 0, 0.707) - all good, data shows as expected.

  3. Rotate the device again around the Z axis 180 degrees (device is held upright so that the top (in portrait mode) points towards the sky, and the back of the device faces south) - Reading Quaternion: (0, 0.707, 0.707, 0) and I was expecting (0.707, 0, 0.707, 0).

Where the components of the Quaternion are (X,Y,Z,W)

X - is tangent to to the surface of the Earth and points east

Y - is tangent to to the surface of the Earth and point north

Z - is perpendicular to the surface of the Earth and points up

steps 2 and 3

So my question is: In 3rd step, should Y=0 value in reading quaternion? I am not rotating Y at all.

I am new with Quaternion, Maui and Mobile Development. Any hint or correction will be more than welcome.

Thanks.

0

There are 0 best solutions below