We struggle with the errors of the accelerometer, gyroscope, M7, digital compass and other sensors in the iPhone 5S and not only
Many have heard about the problem with uncalibrated sensors in the new iPhone 5S - the “level” tool built into the native compass of iOS 7 shows a deviation of several degrees if the device is placed on a flat surface, such as a table.
In short, to some extent, the sensor orientation problem has always been present and on all iOS devices. Previously, the problem was not observed so often due to the lack of a built-in mobile OS application that allows to measure the level. There is a similar problem on other mobile devices equipped with an accelerometer, since the principles are the same everywhere - every developer who has had to deal with different motion and orientation sensors should be familiar with this firsthand. ')
I develop applications using accelerometer sensors, a gyroscope and a digital compass, exactly since the API became available to developers, almost from the very beginning - being the author of one of the most popular compasses for iOS with accelerometer calibration problems and accuracy of other sensors, I also encountered some years ago.
The way to solve the problem is quite trivial and has already been incorporated into most of both application and gaming applications, which in one way or another use gravity, movement and magnetic field sensors - calibration, which any self-respecting developer should take care of. Depending on how complex the application is and what tasks it solves, from the technical point of view of the developer, the implementation of the solution can be both simple and complex. But the principle is the same for everyone.
I invite developers and application users to figure out how it works, where these errors come from, why you shouldn’t worry too much about accelerometer problems and why you don’t need to run to the store to replace the “defective” device - the new device is unlikely to be much better, and problems with sensor errors are solved in other ways.
How it was
More than four years ago, I was faced with the task of developing not the usual analogue of a two-dimensional compass, which came bundled with iOS, but a compass using augmented reality, operating in three-dimensional space and with high accuracy of combining virtual tags superimposed on real-time video.
To combine a virtual object label with its real position in the picture obtained from the camera, it is necessary to use all motion sensors available in the mobile device.
Accelerometer is needed to determine the gravity vector or, to put it in simple language, to find out which part of the device is looking down. A digital compass sensor or magnetometer is needed for orientation to the cardinal points in order to know which side of the device is directed north. Later a gyroscope appeared that determines the rotation of the device and, accordingly, allows to significantly improve the accuracy of the full orientation in three-dimensional space.
As the development of the application and the possibility of using new sensors almost immediately revealed individual flaws inherent in the sensors.
As it turned out, in all devices, the sensors give out unequal data, which differ within a certain error, somewhere there are more deviations, somewhere less - at the same time, a number of various non-obvious factors influence the sensor readings.
The initial reaction of not having experience in this area at that time was similar to that described in the articles about the incorrectly installed sensor in the iPhone 5S, but further study of the issues made the opinion change and continue development without expecting that the manufacturer can and will correct something, but considering the characteristics of each of the necessary sensors.
As a result, a high-tech toy with problems with accuracy turned out to be a reasonably accurate instrument suitable for real use - the main thing is to know how to use it correctly, which directly follows from the characteristics of each sensor, which I am writing about in detail below.
Accelerometer
Since, unlike the standard compass that works in only one orientation, my application had to work in any of the possible, already at the early stages, before the appearance of the gyroscope, one very strange feature of the accelerometer was discovered.
It turned out that in addition to the fact that in each device the accelerometer has a small deviation, within the same physical device this deviation is different for different orientations of the device - for example, in a normal portrait orientation the deviation from the real axis of gravity can be 1 °, while , when rotated 180 °, in an inverted portrait may be 4 °.
The solution was to add the ability to calibrate the accelerometer separately for each of the six possible orientations, and the appearance of the gyroscope gave new possibilities — calibration of motion sensors, respectively, in one form or another, is already present in each decent application that uses them.
Game developers had a little easier time - in games, it’s enough to support one or two orientations of the device, but it’s still impossible to just ignore the need to allow the user to use calibration, even using a gyroscope sensor.
In the “level” of the iOS 7 compass built into the calibration, it is done simply by tapping the screen — just touch the screen and the current position of the device will be considered the reference or “zero” position.
Compass and GPS / GLONASS (although it would seem)
Before the appearance of the gyroscope, the sensor responsible for orientation in the horizon plane along the cardinal points was the digital compass sensor - the most sensitive to external factors from all sensors and, accordingly, having the greatest problems with accuracy.
The compass is calibrated constantly at the driver level as the device rotates - the more data the device receives, the more accurate the result will be, but there will still be an error.
The absolute solution to the problem of compass accuracy, unfortunately, is practically impossible only through calibration. Although it increases the accuracy. In iOS 7, the built-in compass has an even more severe calibration than in previous versions of the OS. Now the calibration screen closes the entire screen until the user performs the necessary actions. In older versions, there was a small message that did not overlap the screen.
Even compass calibration and constant data filtering do not particularly help in conditions of a non-uniform magnetic field - after all, usually after compass calibration, a person rotates around his own axis, and not around the device axis, which, when rotated 90 °, shifts the device in space by about half a meter, where other magnetic conditions.
Near strong magnetic fields, metal objects, wires under voltage, the readings of the magnetometer are unstable due to the very high sensitivity to electromagnetic radiation - this is especially noticeable in rooms and machines, which are more and more modern, more and more stuffed with various electronic fillings.
Plus, if the compass is required to show the geographical north, then the positioning accuracy comes with GPS and GLONASS, since the coordinates are used to determine the magnetic declination or the difference between the directions to the magnetic and server poles at a particular point on the globe.
The magnetic compass works well and accurately on the street in the field, where there is no magnetic interference - but even so, calibration of the compass is desirable with each measurement of the azimuth.
The direction to the north pole is most accurately determined with good GPS accuracy, also usually outside.
To further improve the accuracy where it is needed, for example, if you need to properly target Wi-Fi or radio antennas at each other, or to make any accurate measurements, deeper support is needed on the application side, which is discussed below.
Gyroscope, gyrocompass and car mode
Indoors, in a car, in a boat or in any other means of transportation, and also when higher accuracy and stability of orientation is required, a conventional magnetic compass does not fit - orientation is needed either along the course of movement or along the gyroscope.
Accordingly, in my application I realized both of these possibilities - for use in various vehicles there is an “automobile” mode and a “gyrocompass” mode for everything else.
With the car mode, everything is simple - the driving course is used, which depends only on the accuracy of GPS and GLONASS, and, accordingly, the direction is determined quite accurately while on foot, on car bikes, boats, airplanes, and so on.
With a gyrocompass, the situation is both easier and more complicated.
In the gyrocompass mode, you can accurately set the initial or correct the current direction using any external reference point - the sun, moon, stars, geographical objects, the tree-covered side of the moss, orient using maps or other methods.
This is done simply for the user. The marker superimposed on the live video or pointing at the object of the arrow on the dial of the compass is combined with the real position of the object or with the direction to it. All complex mathematics based on thousands of lines of formulas remains invisible at the application level.
Approximately the same actions are performed by pilots or personnel serving modern military aircraft, vessels testing and subsequent calibration of inertial navigation systems are performed at the beginning of the voyage and during it, which is also facilitated by a fixed arrangement of sensors, while our mobile devices are almost in constant motion.
It would seem that a gyrocompass is the ideal solution to the problem of compass accuracy and orientation to the cardinal points, but there are also its own pitfalls.
In industrial and military inertial navigation systems, in contrast to what is today in mobile devices, an entire complex, an array of sensors, is used to accurately determine the position in space, which makes it possible to compensate for errors and errors in the readings.
In mobile devices, only one instance of each sensor is usually present, which makes it impossible to compensate for errors and leads to an accumulation of errors.
The more time passes since the gyrocompass has been calibrated, and more precisely, when viewed from a technical point of view, since the determination of the reference "zero" position, the greater the accumulated error, which is expressed in the periodic shift in the orientation of the gyroscope.
The video below illustrates the problem.
The video shot compass mode "gyrocompass" tuned exactly to the server running on the device, which still lies on the table. Although the device is stationary over time, a shift occurs. At 00:09 shifted from 0 ° to 359 °. At 01:21 it decreases to 358 °. At 03:03, we already see azimuth 357 °.
The accumulation of errors occurs due to the discreteness of the sensors, which at some moments can miss events, such as in the video above, the gyroscope readings are affected by the smallest vibrations of the power supply fan in the monitor and computer located nearby on the table. Sensors, of course, progress with time, get higher resolution, but the data resolution remains. Accordingly, such insignificant things as heartbeat and pulse can influence the readings.
The micromechanical systems of such sensors are also influenced by such unobvious factors as ambient temperature - the temperature, although not available to ordinary developers, is taken into account for correcting sensor data at the OS driver level.
At the same time, the orientation on the gyroscope is much more accurate than on the compass sensor - when turning 180 °, the sensor reports that the turn was the same 180 ° and not 150 °, as, for example, the compass can say in terms of interference.
Just keep in mind that a gyroscope has such a feature and take this into account when using the device as a tool or when developing your own applications and games.
But what about the new M7 motion coprocessor?
With the announcement of the M7, I was hoping that mobile devices would become closer to large inertial navigation systems, but unfortunately, this new co-processor solves slightly other tasks.
First of all, the M7 is designed to reduce battery power consumption when using GPS and other sensors. Less time is spent on satellite data due to the fact that this calculation does not start from scratch when the application is started. Additionally, data from other sensors are collected in the background, even when the application is not running, which also reduces battery consumption.
For example, in the video illustrating the situation with the accumulation of errors in the gyroscope given above, the compass in the gyro mode works on the new iPhone 5S already using the M7.
Can you trust mobile devices?
The answer is yes, knowing and considering the features of the sensors used.
Developers will make their own conclusions on their own.
To the users who were interested in reading up to the end, let me give you some tips.
There is no need to change the device. It may not be better. And who said that the surface of the table used is strictly perpendicular to the gravity vector?
In games with tactile control, if the error of the accelerometer or gyroscope is clearly noticeable, look in the settings or in the pause mode of the calibration menu.
In all actual applications implementing the “level” tool, there should be a calibration setting the “zero” position - naturally, it is also in the embedded application.
Magnetic compass works well only on hikes in nature. You should not expect the device to do the impossible by trying to absolutely accurately determine the direction next to the computer, speakers, radiator or in any means of transportation. Use those specially designed for this application and modes that best fit the task.
When using a magnetic compass, remember that the readings are relevant only immediately after calibration, until the device has been moved to any significant distance - a 90 ° rotation along the axis of the spine may require re-calibration.
When using applications such as “level” or “gyrocompass”, remember that the sensor readings are relevant for about one to two minutes, which is quite enough to make a measurement — in order to avoid accumulating errors, repeat the calibration before each measurement to improve measurement accuracy.