gimbal lock, can't solve by quaternion - android

When device is rotated by some amount, a simple cube has to rotate by the same amount but in the opposite direction. For example, the cube has to rotate to 45 degrees to the left if the device is rotated 45 degrees to the right. Or when pitch is 30 degrees, the cube has to rotate -30 degrees around X axis. When the yaw is 10 degrees, the cube has to rotate -10 degrees around Z axis. I've used .getRotationMatrixFromVector followed by getOrientation like so:
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
SensorManager.getRotationMatrixFromVector(
mRotationMatrix , event.values);
SensorManager.getOrientation(mRotationMatrix, orientationVals);
azimuthVal = (Math.round((Math.toDegrees(orientationVals[0]))*100.0)/100.0);
pitchVal= (Math.round((Math.toDegrees(orientationVals[1]))*100.0)/100.0);
rollVal = (Math.round((Math.toDegrees(orientationVals[2]))*100.0)/100.0);}
But the problem with it change in pitch affects roll and vice versa and as a result when device is rotated around X axis, the pinch value changes->roll changes -> the cube rotates not only around X but also around Y, when I don't need that.
I've looked around the internet and many refer to Quaternions as a solution but how can I apply quaternions to my specific application, as I need to know amount of degrees device is rotated by along an axes.

Gimbal lock happens when you want to extract (Euler) rotation angles from the rotation matrix, basically at some specific rotation we loose a degree of freedom in the equation between rotation matrix components and rotation angles and the actual rotation angles are not recoverable,
So in your code it may happen at :
SensorManager.getOrientation(mRotationMatrix, orientationVals);
You should somehow solve the issue before extracting the rotation angles,
this could be done by modifying the quaternion's components as is explained here:
http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/

Related

Rotate a rotation matrix about what would be the pitch Euler angle

I've got my 3D scene's camera rotating as desired, but to do it I converted to Euler angles using SensorManager.getOrientation(rotationMatrixIn, eulerAnglesOut); so I could add 90 to the pitch before recalculating a rotation matrix from the modified Euler angles. I'm adding 90 to the pitch so my rotation can be used for a cubemap where Y is up when the phone is held perpendicular to the ground.
I'm using Euler angles because this made it intuitive to rotate the pitch by 90 degrees. But I'm suffering from gimbal lock now when the phone is held near vertical and the pitch approaches -90 or 90 degrees. The view rapidly rotates 180 degrees (in the roll and azimuth axes) when it gets close to these angles. This does not occur when I use the unmodified rotation matrix, but then I don't have the orientation corrections I need.
I'm having trouble wrapping my head around the math needed to skip the Euler angles conversion and directly rotate the rotation matrix by 90 degrees about what would be the X axis in Euler angles and invert the roll. I have access to various math classes from a library (Matrix and Quaternion) which can be multiplied by each other and convert to and from Eulers.
I want to rotate about what would be the X axis. Is there a way to do this without introducing gimbal lock?
To clarify: the Euler angles I'm talking about are in -Z, X, Y order, where X is the pitch.
As #meowgoesthedog stated in a deleted comment, the rotation matrix should be pre-multiplied by a matrix that holds a 90 degree rotation about the X axis.
The rotation matrix from Android's sensors converts device orientation to world orientation, so pre-multiplying it by some other matrix is like applying a rotation to the device first before converting the coordinate system.
I was also tripped up by Android's row-major matrix ordering. The matrix must be inverted to get it into OpenGL's column-major ordering. My Euler angles worked OK because they were hiding this inconsistency.

Android device orientation , pitch calculation not consistent

I have developing an android application , which requires device inclination for real time processing. the device is inclined on a surface
i wanted to calculate the angle, for this i have used the project in github to calculate the pitch value. but the pitch values returned by this method is not accurate over multiple tests.. in the pitch value there is some margin of error most of the times .
And the same program tested over another phone it shows different pitch value in same position (laying the phones on the table) .
is there any way i can get the accurate pitch values across multiple devices.
i had used s6 and one plus 2 devices.
The Rotation Matrix is defined by applying roll first, then the pitch, and finally the yaw rotation. You can get the phone in the same position if you apply pitch first, then roll and again finally yaw. This is why you expect a certain pitch, but you get inaccurate values.
To prove to yourself this, play with the phone by bringing it in a certain random position by applying angle rotations in an certain order and then try to get to the same position by different order of rotations (a good position to try is phone in vertical position like keeping it in front of your face and tilted a bit to the side).
Most of the times you would use code like this
int rotation = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
if(rotation == 0) // Default display rotation is portrait
SensorManager.remapCoordinateSystem(Rmat, SensorManager.AXIS_MINUS_X, SensorManager.AXIS_Y, R2);
else // Default display rotation is landscape
SensorManager.remapCoordinateSystem(Rmat, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, R2);
to make it more intuitive. This is how by you virtually would change the order, since you still have the Rotation Matrix defined by applying roll first, then the pitch, and finally the yaw rotation, but however this rotations are defined against an new XYZ coordinate system.
public abstract void onSensorChanged (int sensor, float[] values)
Added in API level 1
Called when sensor values have changed. The length and contents of the values array vary depending on which sensor is being monitored. See SensorManager for details on possible sensor types.
Definition of the coordinate system used below.
The X axis refers to the screen's horizontal axis (the small edge in portrait mode, the long edge in landscape mode) and points to the right.
The Y axis refers to the screen's vertical axis and points towards the top of the screen (the origin is in the lower-left corner).
The Z axis points toward the sky when the device is lying on its back on a table.
IMPORTANT NOTE: The axis are swapped when the device's screen orientation changes. To access the unswapped values, use indices 3, 4 and 5 in values[].
SENSOR_ORIENTATION, SENSOR_ORIENTATION_RAW:
All values are angles in degrees.
values[0]: Azimuth, rotation around the Z axis (0<=azimuth<360). 0 = North, 90 = East, 180 = South, 270 = West
values[1]: Pitch, rotation around X axis (-180<=pitch<=180), with positive values when the z-axis moves toward the y-axis.
values[2]: Roll, rotation around Y axis (-90<=roll<=90), with positive values when the z-axis moves toward the x-axis.
Note that this definition of yaw, pitch and roll is different from the traditional definition used in aviation where the X axis is along the long side of the plane (tail to nose).
And the difference between phones it is expected.

Get wearable device rotation in degrees from 0-360 around X axis

I am using the Rotation Vector sensor to try to track how many degrees rotation 0-360 around the X axis (aka wrist movement) the user moves.
I am using the SensorManagers getOrientation to the the yaw, pitch and roll this way
float[] rotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(rotationMatrix, sensorData.getValues());
float[] orientation = new float[3];
SensorManager.getOrientation(rotationMatrix, orientation);
with the watch face pointing up it gives 0 degrees like I want but when I rotate I only get +/- 90 degree increments. rotating left gives me +90 degrees and rotating right gives me -90 degrees. I i continue rotating lets say from the +90 degrees I start getting negative degrees so when the watch is face down (180 degrees) is shows 0 again. using 90 degree increments is going to make it difficult to accurately get the actual rotation angle.
Is there a way to go from +/- 90 degree increments to 0-360 degrees?
You can use the z rotation value to find out whether the watch is face up or down. This determines the perspective you are viewing from and gives you 4 quadrants, instead of the 2 that you started with.
If z is positive: watch is face up. If z is negative, it is face down.
From here, you can create a coordinate system for conversion.
If the watch is facing up, coordinate is 90+x_rotation. Since rotating right gives [-90,0), and rotating left yields [0,90], this gives degrees of [-90+90,90+90] = [0, 180] Which is what we would expect of a watch facing up on the wrist.
If the watch is facing down, coordinate is 270+x_rotation. Quadrant 3 gives negative values of 90, making the values range from [270+-90,270] = [180,270]. Likewise, Quadrant 4 has positive values of 90, creating a range of [270,90+270]. So a watch facing down gives [180, 360].
Combined, we get the complete [0,360] device rotation range.
Hope this helps!

Set sprite rotation based on heading

I want to set a 2D sprite's rotation so it faces the direction it's moving in. Currently I hooked the accelerometer to the sprite's linear velocity and when I tilt my device it doesn't rotate, only moves. I am running AndEngine on Android.
I want to calculate x+/x-/y+/y- to receive a value in rotation degrees.
atan2(y,x) should do the trick.
So if angle=0 is in positive x direction,
angle = Math.atan2(y_velocity, x_velocity);
gives you the angle you have to rotate.
Figured it out eventually, to achieve this I did the following:
float radians=(float)Math.atan2(-acceleration.x, acceleration.y); //No Idea why I had to invert x axiz but it wouldn't work without it being done
float degrees=(float)Math.toDegrees(radians)+90; //Had to rotate my sprite by 90 degrees
radians=(float)Math.toRadians(degrees);
sprite.setTransform(sprite.getWorldCenter(), radians);

Cube rotation issue in opengl android

I want to rotate the cube when the user is touching it. To rotate it,is quite simple but actual problem is if the user is tilting it then always it should stop in particuler face(cube has 6 faces and i mean that only one face should be visible at one time). Please give your suggetion if anyone worked on that.
In the case of a cube this is simple: Face normals are the cartesian axes. So one looks straigt on a face if you constrain the rotations around the cartesian axes (X, Y, Z) and the rotation angles are multiple of pi/2 = 90°.
So in your code when the user stops interacting, set the rotation angles to the next multiple of 90°
fmod(round(angle/90) * 90, 360); // degrees
fmod(round(angle/(pi/2)) * pi/2, 2*pi); // radians
Either do it hard, or animate it.
If your object is not a cube, but arbitrary, you need to find the additional rotation for the face to get perpendicular to the view axis. This angle is determined by acos( scalar_product(normalize(face_normal), normalize(view_axis)) ) the axis of rotation is given by cross_product(normalize(face_normal), normalize(view_axis))

Categories

Resources