Which sensor is needed to make an app where an arrow on the display is always pointing to the ground?
My app is always in landscape. Ive tried to use the Orientation sensor but it only works if Im holding my smartphone in portrait mode. The more I move my device to landscape values become instable und doesnt point to the ground anymore.
To be more specific, in portrait mode I can use y-axis (roll) to find out the angle, but the more Im rotating my device to landscape mode it doesnt work anymore with the y-axis.
Maybe its the wrong sensor or its a question of some trigonometry functions?
Any ideas? Please help me.
Ive found the solution.
The problem described is also known as Gimbbal Lock.
See here and here.
For me the solution can be found here and is the trivial sentence:
Using the camera (Y axis along the camera's axis) for an augmented
reality application where the rotation angles are needed:
remapCoordinateSystem(inR, AXIS_X, AXIS_Z, outR);
This way I can do what I want to do because the Gimbbal Lock is lying in a position where it doesnt argue me.
Related
I'm using TYPE_GAME_ROTATION_VECTOR and changing it to a matrix with getRotationMatrixFromVector.
With this rotation matrix, I multiple my view.
Basically I want to change what is shown on the phone, based on whether I'm looking left, up, down, right, etc. Ie a basic VR type app.
However I'm finding it extremely odd that when I leave my phone on my table, in other words, it is flat and not moving, there is about 15-20 seconds where the "rotation matrix settles down" and stops rotating.
Why does this initial "erroneous rotation readings" occur?
And is there a way around this? If not, what is the proper way to calculate rotation of the phone? Am I suppose to use the GYROSCOPE sensor?
Any guidance will be greatly appreciated. Thanks!
From what I understand, android uses world coordinates for the rotation matrix that I would use to get the orientation of the device. However I'm looking to get the devices orientation relative to the device itself similar to how attitude is represented in iOS.
In other words the axis used for roll would be a line passing through the top and bottom of the device, the pitch axis passing through the sides of the device and the yaw axis passing vertically through the device.
I would like to know if android provides any methods that would allow me to get these orientation values or if there is a way I'd be able to do this manually.
Any help would be greatly appreciated.
I eventually figured out a solution that suited my needs. I simply used the getAngleChange() method from the SensorManager class and used a calibration matrix as the previous matrix.
I would like to develop a personal app for this i need to detect my car's rotation.
In a previous thread i got an answert to which sensors are good for that it's okay.
Now i would like to ask you to please summerize the essential/needed mathematical relationship.
What i would like to see in my app:
- The car rotation in degrees
- The car actual speed (in general this app will be used in slow speed situation like 3-5km/h)
I think the harder part of this is the rotation detect in real time. It will be good to the app could work when i place the phone in a car holder in landscape or portrait mode.
So please summerize me which equations,formulas,realtionships are needed to calculate the car rotation. And please tell me your recomendation to which motion/position sensor are best for this purpuse (gravity,accelerometer,gyro,..)
First i thought that i will use Android 2.2 for better compatibility with my phones but for me 2.3.3 is okay too. In this case i can use TYPE_ROTATION_VECTOR which looks like a good thing but i don't really know that it can be a useful for me or not?
I don't want full source codes i would like to develop it myself but i need to know where can i start, what deep math knowlegde needed and what part of math are needed. And for sensor question: i'am a bit confused there are many sensors which are possible ok for me.
Thanks,
There is no deep math that you need. You should use TYPE_MAGNETIC_FIELD and TYPE_GRAVITY if it is available. Otherwise use TYPE_ACCELEROMETER but you need to filter the accelerometer values using Kalman filter or low pass filter. You should use the direction of the back camera as the reference. This direction is the azimuth returned by calling getOrientation, but before calling getOrientation you need to call remapCoordinateSystem(inR, AXIS_X, AXIS_Z, outR) to get the correct azimuth. Then along as the device is not laying flat, it does not matter what the device orientation is (landscape or portrait). Just make sure that the phone screen is facing the opposite direction of the car direction.
Now declare two class members startDirection and endDirection. In the beginning, startDirection and endDirection have the same values, now if the azimuth change by more than say 3 degrees, there is always a little fluctuation, then change the endDirection to this value and continue to change until say 20 returned azimuth have the same values (you have to experiment with this number). This mean that the car stop turning and then you calculate the difference between startDirection and endDirection, this gives you the degree of rotation. Now set startDirection and endDirection to this new azimuth and wait for next turn.
I have an activity that is showing a compass, and I need to know the orientation of the screen to properly rotate the needle. I need to distinguish between 90° and 270° degree rotation to properly handle this.
When I rotate the screen (on my Nexus S at least) the activity's onCreate gets called, e.g. when I rotate from portrait to landscape mode. When I rotate from one landscape mode to the other with the top edge raised, onCreate() gets called twice (once for portrait orientation and once for the target landscape mode).
However, when I rotate with the bottom edge up, the screen rotates 180° and onCreate is not getting called. Is there an event that gets fired in this case?
You are correct that at a 180 degree rotation you will not get restarted or get a configuration change, because the configuration has not actually changed. (It is still in landscape or portrait, and everything else is the same.)
However if you are showing a compass, that must mean you are updating it from sensor events. Thus you can just call Display.getRotation() each time you get a sensor update to get the current rotation of the screen. In fact that you need is rotation of interpreting sensor events (or actually how your drawing of them will be modified when it gets to the screen), not just the orientation.
I found a relevant blog post that is well worth reading.
Be sure to read the SDK documentation.
Also check out the discussion about using Display.getRotation() to correctly remap sensor coordinates.
The helper class OrientationEventListener makes it very easy to get call backs on rotation. I would suggest you give it a try.
Just take a look at the SensorManager Documentation, there you can get the best examples and explanations of how to use the accelerometer to get the most precise informations. Instead of making the things just by detecting the phone orientation, with that API you can get the exactly rotation, just like used by racing games like Asphalt and Need For Speed.
As Monchote said, in order to make your needle rotate as expected on the compass, it's better to lock your UI to portrait or landscape, and change your needle angle according to the device rotation angle, which can be gained easily from OrientationListener.
In this way, no matter the auto rotation feature is opened or not by user, you can always get rotation angle change notification. Here's a good example for your reference.
Have a look at the OnConfigurationChanged method to track down the orientation change and re-arrange your UI as needed.
See some sample code
I've been playing around with the Android Accelerometer of late using the Android SDK and the Adobe AIR for Android SDK on my Motorola Droid. What I've noticed is that the accelerometer works just fine, but I was wondering if it is possible to compensate in some fashion so that I don't have to use the device in a horizontal position. In other words, I want to use the accelerometer to control my visual display, but initialize it(or modify in some way) so that I don't have to hold it perfectly flat (not much fun having to lean over the phone).
Does anyone know how I can hold the device comfortably in my hand, say 45 degrees, and still utilize the accelerometer to provide forward/backwards readings? Is this possible? Any examples of this this available?
You'll need some simple matrix multiplication math for that. "Calibrate" the rotation by taking the current matrix when you start the app and invert it, then multiply all subsequent matrices with it - that will give you the delta to the starting position.
I had written an application long long ago which dealt with relative rotations. I've forgotten what the code did, but from what I can see, it seems like -
1) I get the initial rotation matrix using - SensorManager.getRotationMatrix(R, I, gravity.clone(), geomagnetic.clone()); (gravity and geomagnetic are the respective accleration and geomagnetic matrices. Dunno why I use clones but there must be some reason.)
2) Now at any stage, get the current rotation matrix and call it R.
3)Calculate the inverse of the initial matrix and call it "initialInverse".
4)Multiply R with initialInverse (or the other way round, you'll have to figure it out).
5) Get your final orientation using SensorManager.getOrientation(delta, values)
I'm sorry but I've totally forgotten what the above code does. I think I remember reading the words Euler Transform somewhere when I wrote this app, so thats what it might be doing. Unfortunately I cannot give you the complete code since I'll probably release this app in the market. However, if you need some more information, please let me know - I'll look into the code and get back to you.
I am working in a project with similar nature where the accelerometer function is not restricted by the position. My way of handling it is very simple, initialize the accelerometer with the current reading as the default. In other words, you have a button that you press once you have the phone in the proper position, upon pressing the button, the current readings of the accelerometer (measures of G) will be your reference (zero values), and make changes when you go above or below those readings... Hope this helps anyone... cheers