I need to find a way to detect if the phone has rotated 360 degrees, by using (preferably) the accelerometer, gyroscope and/or compass. But there is another catch: When the phone is in portrait mode, I need to detect the rotation along the Y axis, and when in landscape mode the rotation along the X axis. But what if the phone is in between those two modes?
Is this possible to achieve and if so, what is the best way to achieve this?
Related
Usually, there's a compass and an accelerometer on a typical smartphone these days. So, using information from these sensors can we recreate the whole orientation in which the photo was taken?
I mean like if you open the compass app on your phone, it firstly states the direction you are looking (link), how lifted or tilted is your phone in front direction ( I don't know how to state it in a better way) (link) and how much is it titled in sideways (link). Does it cover 3 degrees of freedom (i guess)?
Is it enough information to recreate that orientation of the phone?
Also if you think it's not the right place to ask such a question, can you comment where should I ask this question?
Recording sensor values, it would be possible to restore phone direction in which photo was taken. Some fundamental XR apps(e.g. Google Street View) are actually doing such.
Device rotation
There is a handy helper function, SensorManager.getRotationMatrix exactly for that purpose. Giving magnetic field and gravity sensor-values, you can obtain the device rotation matrix(=complete set of orthogonal 3d basis vectors) which is enough for you to reproduce phone direction afterwards.
Camera orientation
In some cases camera's up direction may be different from device's up direction due to screen orientation changes(portrait or landscape, locked or auto-rotate). So if you handle raw camera inputs, you may need to record screen orientation too. See Display.getRotation.
I need to get exact angle of my android device from each axis.
First I should say I searched through web and also stackoverflow and I saw pages like these:
How to measure the tilt of the phone in XY plane using accelerometer in Android
Get Android rotation angle in x-axis
But they were not helpful. I used some of them but they give wrong values.
If the axis in device will be like this:
I need to get these rotation angle:
I don't want to use any hardware sensor except accelerometer and I work with API 17.
I think this is possible based on racing games, but I don't know how to do that.
I will appreciate if you help me.
Thanks
There is no way you can get all the rotations using TYPE_ACCELEROMETER alone. You also need TYPE_MAGNETIC_FIELD. If you do not care about how sensitive the values changed with respect to quick movement then you need TYPE_GRAVITY or TYPE_ACCELEROMETER together with TYPE_MAGNETIC_FIELD.
For rotation with respect to the z-axis you can follow the first link in your question.
For rotation with respect to the x and y axes you can use pitch and roll and screen facing.
The Problem: Certain Android devices (listed at the bottom of the question) exhibit unexpected behavior when utilizing the setOrientationHint(int degrees) function for videos taken with the front facing camera. The expected behavior is for the video to be rotated clockwise, but these devices rotate the video counterclockwise.
My Goal: To identify a variable within either the camera settings or hardware orientations that allows me to predictably know when this will occur. Specifically, I would like to avoid special casing these phones in my code!
Further Explanation: I am recording video using the standard MediaRecorder object, and in preparing for recording, I set the orientation of the video using setOrientationHint(). In the documentation for setOrientationHint(), the following is specified for the degrees parameter:
degrees –– the angle to be rotated clockwise in degrees. The supported angles are
0, 90, 180, and 270.
The function is intended to add a composition matrix containing the rotation angle so that a video player can display the video as intended. So, what I do is that I get the camera hardware's orientation using the CameraInfo class and use that as the degrees parameter in the setOrientationHint fn. (I have tried variation on this code using the AOSP as a guide, but I had the exact same result.)
A Real Example: The Samsung Galaxy S3 front-facing camera (and most others, in fact) will have a hardware orientation of 270, so I use this when recording, and the resulting video is displayed correctly. An HTC Vivid will similarly return a hardware orientation of 270 for the same camera, but will only be displayed correctly if I use 90 as the degrees parameter in the setOrientationHint fn. If I use the 270 of the hardware orientation like any other phone, the video will be upside down.
NB: The setOrientationHint() function includes a warning:
Note that some video players may choose to ignore the compostion
matrix in a video during playback.
However, this is not what is happening, because I can easily fix this issue on these phones if I fake it and put in 90 instead of 270.
Phones that I have seen specifically exhibit this issue: HTC Vivid (PH39100) running Android 4.0.3, HTC EVO 4G (PG86100) running Android 4.0.3, HTC Thunderbolt (ADR6400L) running Android 2.3.4. Notice that they are all HTC phones. Perhaps someone at HTC mistook clockwise for counterclockwise.
Yes, the HTC phones rotate in the wrong direction for the front facing cameras. Instead of trying to guess, I ended up adding a settings screen that would take two pictures with the second one rotating 90 degrees. Then the user could keep hitting next as I cycled through the different rotation direction and angle combinations until both pictures appeared oriented the same way.
I'm trying to create an app which requires me to find the compass bearing.
The users will hold their device so that the screen is always facing them but will be able to rotate it 360 degrees.
I can successfully determine the bearing for one orientation - portrait or landscape - but I need to find a method which will determine the bearing regardless of rotation.
Can I do this?
Thanks.
You should use data from gyroscope that can fully determine angles of device.
You can find this link helpful:
http://www.touchqode.com/misc/20101025_jsug/20101025_touchqode_sensors.pdf
I'm trying to understand the gyroscope sensor output from the Nexus S. When you rotate about the z-axis (the axis perpendicular to the screen), the z-axis correctly records the angular velocity as a result of the turn. However, the y-axis also reports spikes in angular velocity, despite no change in the orientation of the device relative to the y-axis. So if I turn around when holding the phone at a particular orientation, it appears that I have not only turned around, but also tilted the phone left/right (which I haven't).
Why is this? Any ideas how to compensate or correct for this?
Please note I'm talking about the raw sensor output here.
First of all raw sensor data always contains some bias that has to be filtered out. Furtheron it is almost impossible to really suppress other rotational directions manually. If you suspect your Nexus to have a sensor bug or you want to know exactly what's going on I suggest to build your own apparatus. As inspiration a picture of mine :-)