# Gyro, accelerometer, magnetometer and other sensors summary (turn)

Source: Internet
Author: User

Transfer from http://blog.csdn.net/a345017062/article/details/6459643

A gyroscope is an internal gyroscope, whose axis is always parallel to the initial direction due to the gyro effect, so that the actual direction can be calculated by deviations from the initial direction. The gyroscope in the cell phone is actually a very sophisticated chip with ultra-tiny tops inside.
Accelerometers are used to detect the size and direction of the acceleration that the phone is subjected to, while the mobile phone is only subjected to the gravitational acceleration (which is learned in high school). So many people call the accelerometer function also known as gravity sensing.
A magnetometer is a test of the strength and direction of a magnetic field.

Gyro measurement is the reference standard is the inner middle in the vertical direction with the ground to rotate the gyro. The result is obtained by the angle between the device and the gyro.
The accelerometer is the result of the internal measurement component in all directions of the force.
The principle of magnetometer is the most simple compass that is involved in middle school physics (that is, remember that the magnetized steel needle?).

They each have their own strengths:
The gyroscope's strength lies in measuring the rotational motion of the device itself. Better at the device's own motion. However, the location of the device cannot be determined.
The accelerometer's strength lies in measuring the force of the device. Better at the movement of the device relative to the external reference (e.g., ground). But it is not accurate to measure the position of the device relative to the ground.
The magnetometer's strength lies in locating the device. It can measure the angle between the current equipment and the four directions.

For a few examples:
Gyroscope detection of the rotation angle of the device is instantaneous and very accurate, to meet the needs of high-resolution and rapid response applications such as FPS game aiming. And the gyroscope with accelerometers can be navigated without satellites and networks, a classic gyroscope application. Accelerometers can be used in applications with fixed gravity reference coordinate systems, linear or oblique motions, but rotational motion is limited to a certain range. When dealing with both linear and rotational motions, it is necessary to combine the acceleration with the gyroscope meter. If you still want the equipment to move without losing direction, add the magnetometer.

For a missile to be launched, the following sets of data are essential to accurately track and adjust the trajectory of the missile:
GPS positioning of its location
Accelerometer Measures current acceleration
The magnetometer determines the direction of the missile's head (only the angle in four directions), and the gyroscope knows the angular velocity of the missile. The combination of the two instruments can determine the precise direction of the missile's stereo motion.

The result of the accelerometer is the XYZ three value, which represents the acceleration in three directions respectively. An introduction to the three values of XYZ can be seen here:
Android Gravity sensing and screen rotation relationships
Http://blog.csdn.net/lzx_bupt/archive/2010/04/20/5507165.aspx

Orientation (azimuth meter) can be calculated with accelerometers and magnetometers, and the orientation involves three concepts:
Roll: Tilt angle, also called Roller corner http://baike.baidu.com/view/1769672.htm
Pitch: Tilt back and forth, also called pitch angle http://baike.baidu.com/view/3832041.htm
Yaw: Sway around, also called yaw angle http://baike.baidu.com/view/1769448.htm

The code below is the source of/development/samples/compass in Android2.3, demonstrating how to use accelerometers and magnetometers to determine the position and orientation of your phone. Because the mobile phone movement of the acceleration is not high, the accuracy is not too large requirements, with the accelerometer alternative gyroscope can also. But if you do some high-precision games, it is best to have a gyroscope.

[Java]View Plaincopy
1. Turn on the accelerometer and magnetometer two sensors. Sensor gsensor = msensormanager.getdefaultsensor (Sensor.type_accelerometer); Sensor msensor = msensormanager.getdefaultsensor (Sensor.type_magnetic_field); Msensormanager.registerlistener (This, gsensor, sensormanager.sensor_delay_game); Msensormanager.registerlistener (this, msensor, sensormanager.sensor_delay_game);//Accelerometer data. private float[] mgdata = new float[3];//magnetometer data. private float[] mmdata = new float[3];//rotation matrix (Rotation matrix), Used to calculate the rotation angle of the phone itself relative to the ground coordinate system. For example, the cell phone is now sideways or upright. It reflects the location of the mobile phone's three-dimensional placement. This data is best measured with gyroscopes, without gyroscopes, with the aid of accelerometers and the acceleration of gravity, but not enough precision. private float[] mr = new float[16];//Tilt Matrix (Inclination matrix) for calculating the position of the phone. For example, the head of the cell phone is now facing south or north. is for a plane on the four directions of the cardinal direction. private float[] mi = new float[16];//Store the final rotational angle data private float[]  MORIENTATION&NBSP;=&NBSP;NEW&NBSP;FLOAT[3];p ublic void onsensorchanged (SensorEvent event)   {&NBsp;       int type = event.sensor.gettype ();         float[] data;        if  (Type == sensor.type_accelerometer)  {             data = mgdata;        } else if   (Type == sensor.type_magnetic_field)  {             data = mmdata;        } else  {            // we should not  be here.            return;         }        for  (int i  = 0; i &lt; 3; i++)             data[i]  = event.values[i];//This step is to obtain the MR and Mi two matrices.         sensormanager.getrotationmatrix (MR, mI, mGData,  mmdata);//Use the matrix Mr to get the rotation angle of the phone.         sensormanager.getorientation (mR, mOrientation);// Use matrix mi to get the angle of the mobile phone in four directions.         float incl = sensormanager.getinclination (MI);         final float rad2deg =  (float) (180.0f  /&NBSP;MATH.PI)         log.d ("Compass",  "yaw: "  +   (int) (MORIENTATION[0]&NBSP;*&NBSP;RAD2DEG)  +    pitch:   +  (int) ( MORIENTATION[1]&NBSP;*&NBSP;RAD2DEG)  +  "  roll: "  +  (int) (morientation[2] &NBSP;*&NBSP;RAD2DEG)  +  "  incl:  " +  (int) (INCL&NBSP;*&NBSP;RAD2DEG));}

The gsensor is relatively easy to complete, usually to allow the machine to be placed at a fixed angle (such as horizontal), and then successive n values (such as 20) for a series of calculations. In Android and iphone, the screen will change depending on the direction of the device's rotation, but only the gsensor is applied.
The msensor is divided into horizontal positive and inclined compensation. The horizontal plane is more positive, 8 characters are positive, and ten faces are more positive than the other methods. After the level is positive, the msensor can be used when the horizontal position, but the equipment and the water level has a certain tilt angle, the tilt angle will affect the accuracy of the msensor. Therefore, it is necessary to compensate for the inclination angle more positively. The tilt angle is obtained by the gsensor, and then the skew angle (pitch,roll) is used to compensate for the more positive calculation.
Orientation sensor can be implemented by a single chip, and can be computed by gsensor and Msensor (I think the compass in android2.3 's sample has done so). Orientation sensor output Three values: Roll,pitch,yaw. Among them, roll and pitch represent the x-axis and the y-axis of the mobile phone and the angle of the horizontal plane respectively, so these two values can be calculated by Gsensor. Yaw represents the angle between the projection on the horizontal plane of the mobile phone and the north direction, calculated by Msensor.
Gyroscopes can capture very small motion trajectory changes, so high resolution and fast response rotation detection can be done. However, the current direction of operation cannot be measured like msensor or orientation sensor. The plane uses it to record movement angle changes, thus forming the motion trajectory. The disadvantage is that as time increases there is an accumulation of errors,

The gyroscope's XYZ represents the angular velocity of the device rotating around the XYZ three axes, respectively: Radians/second. As for XYZ, the coordinate system used is the same as gsensor. When rotated counterclockwise, the value of XYZ is positive. The following is the demo code when developing with a gyroscope:

[Java]View Plaincopy
1. Private static final float ns2s = 1.0f/ 1000000000.0f;
2. private float timestamp;
3. Public void Onsensorchanged (Sensorevent event)
4. {
5. if (timestamp! = 0) {
6. final Float DT = (event.timestamp-timestamp) * NS2S;
7. angle[0] + = event.data[0] * DT;
8. angle[1] + = event.data[1] * DT;
9. angle[2] + = event.data[2] * DT;
10. }
11. timestamp = Event.timestamp;
12. }

In addition, after a certain period of operation, the noise and offset can lead to data deviations and need to be more positive with other sensors.

With the accelerometer (gsensor), the magnetometer (msensor) simulates the azimuth sensor (orientation) to

The latest version of Android has not recommended that we use azimuth sensors as follows when developing:
Sensor ori = msensormanager.getdefaultsensor (sensor.type_orientation);
Msensormanager.registerlistener (This, Ori, sensormanager.sensor_delay_game);
Instead, it is recommended to use the Sensormanager.getorientation (MR, morientation) API to calculate the azimuth sensor itself in the application's onsensorchanged. But when we were working on the HAL layer, we had to consider some of the APK or the direct use of the azimuth sensor as in the code above, so we had to simulate a azimuth sensor in the SENSOR.C to:
When the azimuth sensor is activated, the accelerometer and magnetometer are activated at the same time, and the accelerometer and magnetometer are turned off while the azimuth sensor is turned off, as well as if the user has registered an accelerometer or magnetometer and is in use.

[CPP]View Plaincopy
1. The default state, 0 for shutdown.
2. static int accstatus = 0;
3. static int magstatus = 0;
4. static int switchsensor (int fd, int handle, int enabled) {
5. int status = 0;
6. switch (handle) {
7. Case Id_acceleration:
8. if (enabled) {
9. if (!accstatus) {//=0 indicates that the current state is closed, perform an open operation.  Otherwise, the description has been opened.
10. Status = IOCTL (FD, Acc_flag, 1);
11. LOGW ("Open gsensor");
12. }
13. accstatus++;
14. } Else {
15. if (Accstatus > 0) {//>0 indicates that the current state is open, this time it will attempt to close gsensor.
16. accstatus--;
17. if (!accstatus) {//minus 1 if ==0, indicating orientation is not using gsensor, you can close Gsensor
18. Status = IOCTL (FD, Acc_flag, 0);
19. LOGW ("Close gsensor");
20. }
21. }
22. }
23. Break ;
24. Case id_magnetic:
25. if (enabled) {
26. if (!magstatus) {
27. Status = IOCTL (FD, Mag_flag, 1);
28. LOGW ("Open msensor");
29. }
30. magstatus++;
31. } Else {
32. if (Magstatus > 0) {
33. magstatus--;
34. if (!magstatus) {
35. Status = IOCTL (FD, Mag_mflag, 0);
36. LOGW ("Close msensor");
37. }
38. }
39. }
40. Break ;
41. Case Id_orientation:
42. if (enabled) {
43. if (!accstatus) {//=0 description gsensor not opened, open directly.
44. Status = IOCTL (FD, Acc_aflag, 1);
45. LOGW ("Open msensor");
46. }
47. if (!magstatus) {
48. Status = IOCTL (FD, Mag_flag, 1);
49. LOGW ("Open msensor");
50. }
51. accstatus++;
52. magstatus++;
53. } Else {
54. if (Accstatus > 0) {//>0 description was opened, try to close gsensor.
55. accstatus--;
56. if (!accstatus) {//minus 1 = 0 indicates that the user is not using gsensor directly, but orientation is used, the gsensor can be turned off.
57. Status = IOCTL (FD, Acc_aflag, 0);
58. LOGW ("Close gsensor");
59. }
60. }
61. if (Magstatus > 0) {
62. magstatus--;
63. if (!magstatus) {
64. Status = IOCTL (FD, Mag_flag, 0);
65. LOGW ("Close msensor");
66. }
67. }
68. }
69. Break ;
70. Default:
71. LOGW ("Unsupported sensor type");
72. }
73. return status;
74. }

Reference article:

The difference between accelerometers and gyroscopes

Http://www.cnblogs.com/liuq0s/archive/2010/09/02/1816394.html

How a three-axis gyroscope and accelerometer can assist with iphone positioning

http://www.weizhiquan.com/archives/1072

ST Integrated Sensor solution for electronic compass function

Http://www.eeworld.com.cn/gykz/2011/0408/article_5352.html

Implement Gyroscope (Orientation Sensor) in Android devices

http://blog.csdn.net/beeboobeeboo/article/details/6536941

Easy-to-understand text with the iphone 4 gyroscope you know

Http://mobile.zol.com.cn/187/1876651.html

Android OS 11 sensor introduction

Http://hi.baidu.com/aokikyon/blog/item/f9a539d74a4f5fc4a044df40.html

Gyro, accelerometer, magnetometer and other sensors summary (turn)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.