All of the guides to getting compass orientation in Android I've found have a bug: when you hold the phone in portrait mode and "look" above the horizon, the compass arrow turns 180 degrees from the correct direction.
Google Maps orientation indicator doesn't have this problem.
Another nice thing that Google Maps have is that they somehow estimate compass accuracy. Any idea how they do this?
Error you ask is caused because of Euler angles or called Gimbal Lock.
To solve very high angle difference, you should check if your device is flat or not checking inclination after getting inclination using gravity sensor or getting gravity from accelerometer with lowpass filter. If it's not flat(AR application or inclination>25 || inclination < 155) remap coordinate system from x,y to x,z. That will solve the issue.
SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, remappedRM);
SensorManager.getOrientation(remappedRM, orientation);
This solves the Gimbal Lock, however, 3D compasses(normal compasses does not give correct results due to they are only mapped in x,y and occurs Gimbal Lock) i've seen on Play Store and every code here has a diffence when you keep your device laying flat or screen pointing at you. That difference sometimes gets up to 10 degrees. I haven't able to solve it. Mostly difference is between 1-5 degrees but i sometimes see it rise up to 10 degrees which is not acceptable.
Google measures azimuth from your location. There is a code to find bearing.
currentLocation.getBearing();
Accuracy of lat long(accuracy of your current location) is what determines accuracy of bearing.
List of ways doing a compass with the most accurate to least is in order
Use GPS/Wife(Google's fused location) location and get bearing from location
Use Rotation Vector(requires magnetic field sensor, you should check if it's available), it's a fused sensor(magnetic field+gyroscope+accelerometer and software parts) and using Kalman filter to smooth values but check inclination to remap orientation
Gravity/Accelerometer + Magnetic Field Sensor. This will have a terrible noise attached to it, to smooth it you should use moving average or low-pass filter(this not for isolating gravity, it's for using a threshold frequency to prevent high jumps)
bug when looking above the horizon.
There is some tricky parts when you work with compass.
The compass orientation depends on magnetic fields and phone orientation, so, in order to correct deviation you need to do some matrix operations.
Check this article, it provides a simple example -> https://www.journal.deviantdev.com/android-compass-azimuth-calculating/
Paraphrasing the article: "So if the device is not holding flat (∓45° deviation) you have to use remapCoordinateSystem() in a useful way to get correct results."
accuracy.
First of all, accuracy rely on physical components present in the device. Fragmentation is a common problem here. Samsung magnetometers are different from LG's, and Android System is not going to abstract you from this.
On the other hand, devices could rely on different type of sensors: harward or sensor fusion. So you're going to experiment differences between one device and other.
So this is a mess. But it exists some techniques you could apply in order to get accuracy and uniformed data from sensors (not only for compass, probably GPS too).
Some people discard the first few seconds of sensor data. Some time is needed in order to calibrate the signal, so the first data retrieved use to have some deviation. The time you need to discard depends on manufacturer again, buy I'd try with 5 seconds or so.
Use Interpolators and Extrapolators. Many sensors in android provide you a way to retrieve data each certain milliseconds. But this is the abstraction provided by Android. Harward sensors have its own timing and they update the signal when they they think it is necessary. The rest of the time, when Android ask the sensor for signal data the sensor returns the last value or maybe some kind of operation of the last signal data provided by manufacturer (again).
So, it is interesting to have some abstraction layer (interpolator / extrapolator) which receive the data from Android system each 20, 50, 1000... This layer make some operations in order to have some uniformity, and then communicates the data to your app.
The operation here could be some kind of average between current and last value, accumulated average or maybe another kind of normalization.
OK, i figured it out myself. first you need to calculate the bearing from the compass. then the Maps api-2 camera can be rotated.
public void updateCamera(float bearing) {
CameraPosition currentPlace = new CameraPosition.Builder()
.target(new LatLng(centerLatitude, centerLongitude))
.bearing(bearing).tilt(65.5f).zoom(18f).build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(currentPlace));
}
set SensorListener in your code and call this method in onSensorChanged event. i have added a tilt value so the map will rotate in 3D.
[Reference:] [Android Maps v2 rotate mapView with compass1
if you can know about more details http://www.techotopia.com/index.php/Working_with_the_Google_Maps_Android_API_in_Android_Studio
Compasses will give you what you want, but it's so noisy. It is noisy for 2 reasons, one of reason is, it is picking up, real noise, real signal. So, we live in an environment that's magnetically very noisy. So, this compasses, picking up everything that's magnetic. The other reason is that, it's not integrated, so it doesn't have benefit of dropping the frequency component. So, try to combine your compass with gyroscope data. This video will help you so much for using these sensors.
Some more details, you can combine accelerometers also. So in summary, Gyroscopes provide orientation, Accelerometers provide a correction due to gravity, and compasses provide a correction due to magnetic North.
Related
I'm developing an app that uses Android sensors to help vehicles navigate in an indoor location. As part of my evaluation process of different sensors, I wanted to try the "rotation vector" sensors. For various reasons, magnetic field readings are not very useful for my location, so thus I wanted to try the "Game Rotation Vector" sensor (sensor fusion, available from API level 18 and later). The description states that it is identical to the regular Rotation Vector sensor except no magnetic field information is used to correct for gyroscope drift around the vertical axis.
When looking for information about the Rotation Vector sensors, I came across an example from Google, where they show the Rotation Vector sensor using a 3d cube. It works pretty well, except for being very sensitive to local magnetic fields (and me being far north, even worse, since the horizontal component is very small here).
Since long term drift can be compensated by other reference data (map information), I wanted to use the Game Rotation Vector sensor for my app. However, when changing all references from "TYPE_ROTATION_VECTOR" to "TYPE_GAME_ROTATION_VECTOR" in the example code, the cube no longer reacted to rotations around the vertical axis (eg. me spinning my chair, holding the device in front of me). Tilting the device in the other two directions moved the cube. I also noticed the cube was a lot more "laggy" this time around, reacting very slowly to any movement.
Is this the way the Game Rotation Vector sensor is supposed to work (eg. ignoring any Z axis rotations)? It would kind of make sense, since a gamer playing in the back seat shouldn't be affected by the vehicle turning, but at the same time it differs from the description provided by Google (my first link). From the description I was under the impression that it would drift slowly, not ignore rotation all together.
I would be deeply grateful for any input on this issue.
Best Regards,
John
Ok, just in case anyone happens to find this, here are my findings:
The Game Rotation Vector sensor does detect rotation around the vertical axis. It is quite accurate in most situations.
However, it has a couple of issues... First, while lying still it has accelerating horizontal drift (even when a gyroscope-based orientation has linear drift). For my device, Game Rotation Vector started out good, but accelerated and finally drifted more than 400 degrees over the course of an hour.
Secondly, and even more disturbing, it does not seem to ignore magnetic fields, contrary to the official description (linked in the question). I tried driving around the parking lot with my device fixed on the passenger seat, and the Game Rotation Vector fell behind largely (it was more than 180 degrees off after one full rotation over 40 seconds), while integrated gyroscope data was accurate within a few degrees. It also showed changes in rotation when the gyroscope was hovering around zero, suggesting that it was in fact compensating for a change in (what I presume to be) magnetic field.
I still don't know why it acted wierd in the test app I linked to before, but I have since decided to use a complementary filter to combine accelerometer and gyro data instead.
I am trying to use the phone as a gun aiming with the gyroscope. I calibrate with the phone or tablet in a certain orientation. This will shoot straight. Then depending on the direction the phone is turned (left/right/up/down.), the gun shoots in that direction.
I am using the gyroscope. And all this works. Except after shooting for about 30 secs, the gyroscope slowly starts drifting towards left or right. So when I go back to the orientation I calibrated with, it doesn't shoot straight anymore. Does anyone have any experience writing a Complementary or Kalman Filter to fuse gyro and accelerometer data to give better results in Unity 3D?
I've found this online - http://www.x-io.co.uk/open-source-ahrs-with-x-imu/. It seems to do exactly what I want. But I am using it wrong. I sometime get better and sometimes get worse results with. Anybody have any experience with it ?
First place, Gyro/accelerometer fusion will stabilize your pitch/roll angles, since gravity indicates in which direction ground is. However, you cannot correct "left/right" drift because actual heading is unknown. Getting proper heading stabilization cannot be achieved with gyro/accelerometer alone: it requires additional information.
The example you provide (Madgwick’s MARG/IMU filter) is a filter that can integrate magnetometers ("north" reference), but it has two requirements for getting good results:
The magnetometer has been properly calibrated.
There are no magnetic field disturbances. This is generally not true if you are indoors, or if you are moving close to power lines or metallic structures.
An alternative is using a video signal to get optical flow information, or detecting if the phone is resting in a fixed position to compensate gyro biases from time to time.
So, right now I'm grabbing the accelerometer data and converting them to a decently rough estimate of the angle at which the phone is being held. For right now I'm just focused on the yaw axis.
My area of interest is between 0 and 45 degrees on the yaw axis, so I made a limited queue of the past 5 to 10 readings and compared the numbers to determine if it's going up or down, which kind of works, but it is slow and not really as precise or reliable as I'd want it to be.
Is there a way you can kind of just determine which direction your phone is rotating with just the accelerometer and the magnetic field sensor I guess, without keeping a history of past readings, or something like that? I'm really new to sensor manipulation and Android in general. Any help understanding would be great.
It's not clear exactly what you're looking for here, position or velocity. Generally speaking, you don't want to get a position measurement by using integration on the accelerometer data. There's a lot of error associated with that calculation.
If you literally want the "direction your phone is rotating," rather than angular position, you can actually get that directly from the gyroscope sensor, which provides rotational velocities. That would let you get the direction it's rotating from the velocity without storing data. You should be aware that not every phone has a gyroscope sensor, but it does seem like the newer ones do.
If you want the absolute orientation of the phone (position), you can use the Rotation Vector sensor. This is a combined sensor that automatically integrates data from several of the sensors in one go, and provides additional accuracy. From this, you can get roll-pitch-yaw with a single measurement. Basically, you first want to get your data from the Rotation_vector sensor. Then you use the sensor data with getRotationMatrixFromVector. You can use the output from that in getOrientation (see the same page as the previous link), which will spit out roll-pitch-yaw measurements for you. You might need to rotate the axes around a bit to get the angles measured positive in the direction you want.
I am trying to find the angle of rotation of a car while it makes a turn using Gyroscope from an Android device. So imagine a car is travelling in a bearing of angle 168 and makes a right turn on a road. Now I need to calculate the new heading or bearing angle just using Gyroscope. But the values I receive are in radians/sec. I tried integrating these values over the time period dT. But these values are not even close to the actual angles. I thought the rotation are in reference to the device, and I tried to convert the values to the real World coordinates. But I didnt get a good algorithm for that.
Can someone help me or point to the right resources to solve this issue?
EDIT:
I forgot to mention in the question, I am trying to do this without GPS, (in the scenario when GPS fails.) And I am trying to avoid Sensor fusion as I am planning to use only Gyroscope as I am looking for a solution that could run even out of Android platform. I am even talking to the OBD to get the actual speed of the vehicle. So I am just trying to collect Gyroscope data from any client and process it at the back end and determine just the turning of a vehicle
You need rotation vector see description here (API level 9).
This thing is using something called sensor fusion to get good quality information about phone orientation relative to earth and magnetic north.
You can also calculate derivate of GPS position to estimate car turn direction.
I want to know current speed of car and make a passed path. I have an Android phone with accelerometer and gyroscope which sent me data. This is the data in phone system of coordinate that probably wouldn't the same as coordiante system of car.
How I can transform this accelerations and rotations to car system of coordinate?
The generic answer for your generic question is no. The acceleration measures the changes in the speed, so the best you could get from acceleration, is the speed variation.
To get the absolute speed you would have to have the initial speed and add it to the speed change:
v(t) = v0 + a*t
So, if you would have a car moving along a straight line, and your device was fixed to the car, you could get easly the speed changes (although measurements errors will add up and quickly lead to discrepancies)
In practice you will face many issues trying to implement it, namely:
You need the initial speed to be determinate based on the same referential as the acceleration. This would require some measurements and a lot of trignometry, as you would get both values from different sensores at different rates.
The car will not move in a straight line, so your acceleration referential will be constantly moving (a lot more of trignometry and calculus).
If the device is in the user hand, the device movements in relation to the car will increase even more the calculations (and accumulated errors).
Regards.
You need some sort of external reference (e.g. GPS is such a thing): If you just integrate the acceleration, the error will go indefinitely.
Because these sensors are not accurate enough. the error will quickly get out of control. (The linked answer is about position but the same holds for the velocity.)
In case of a car, you are better off with the GPS. If want to do something fancy, you could enforce the environmental constraints deduced from a map, that is, assume that the car goes on a road and not through buildings, etc. You will find more details on this in Chapter 5 of the PhD thesis entitled Pedestrian Localisation for Indoor Environments.
It looks like it's possible to do. I don't have an Android specific example but this forum has quite a lot of chat about it: http://www.edaboard.com/thread119232.html
It would be a lot easier if you used the Android Location class though. Specifically the getSpeed() method should give you what you need: http://developer.android.com/reference/android/location/Location.html
The Location class relies on a location provider though so your app will require appropriate permissions.
Both dont deliver anything if the car travels at the same constant speed for some time. The only way would be GPS which has a calculated speed with every location it provides.