Sensor aggregation, including gyroscope, accelerator, and magnetic Meter

Source: Internet
Author: User

A gyroscope is an internal gyroscope. Its axis is always parallel to the initial direction because of the gyro effect, so that the actual direction can be calculated by deviation from the initial direction. The gyroscope in the mobile phone is actually a very precise chip with an ultra-small gyroscope.
The accelerator is used to detect the size and direction of the mobile phone's acceleration, while the mobile phone's static location is only subject to the acceleration of gravity (this high school has passed. therefore, many people call the accelerator function gravity sensing.
Magnetic meters are used to test the magnetic intensity and direction.

Gyroscope measurement is a reference standard for a gyroscope that rotates in the center vertical to the ground. The angle between the device and the gyro is obtained.
The accelerator is based on the forces of the internal measurement component in all directions.
The principle of the magnetic meter is the simplest compass involved in the middle school physics (remember the magnetized steel needle ).

They have their own strengths:
A gyroscope is designed to measure the rotating motion of a device. Better at the movements of devices. However, the location of the device cannot be determined.
The advantage of a accelerometer is to measure the force of a device. Better at the movement of devices than external reference objects (such as the ground. However, it is not accurate to measure the placement posture of the device relative to the ground.
The strength of a magnetic meter lies in positioning the device's location. The angle between the current device and the southeast and northwest directions can be measured.

For example:
The gyroscope detects the Rotation Angle of the device instantly and accurately. It can meet the needs of applications that require high resolution and rapid response, such as FPS game targeting. In addition, the gyroscope can be used with the accelerator to navigate without Satellites and Networks. This is a classic application of the gyroscope. The accelerometer can be used for applications with fixed gravity reference coordinate system, linear or skewed motion, but the rotation motion is restricted within a certain range. When processing linear motion and rotating motion at the same time, acceleration and gyroscope shall be combined for use. If you still want to keep the device from getting lost while moving, add a magnetic field.

For a missile to be launched, to precisely track and adjust its orbit, the following sets of data are required:
GPS location
The accelerometer measures the current acceleration.
The magnetic meter determines the direction of the missile (only the angle between the four directions in the Southeast and northwest can be known), and the gyroscope knows the missile's velocity. These two instruments can be combined to determine the precise three-dimensional movement direction of missiles.

The result of the accelerator is three XYZ values, representing the acceleration in three directions respectively. For details about the three values of XYZ, refer to the following:
Android gravity sensing and screen Rotation
Http://blog.csdn.net/lzx_bupt/archive/2010/04/20/5507165.aspx

Orientation (azimuth) can be calculated using an accelerometer and a magnetic field. Orientation involves three concepts:
Roll: left and right tilt angle, also called Rolling Angle http://baike.baidu.com/view/1769672.htm
Pitch: front and back tilt, also called the pitch angle http://baike.baidu.com/view/3832041.htm
Yaw: Left and Right swing, also called yaw angle http://baike.baidu.com/view/1769448.htm

The following code is the source code of/development/samples/compass in android2.3. It demonstrates how to determine the placement posture and orientation of the mobile phone using the accelerator and magnetic meter. Because mobile phones do not require high acceleration and accuracy, you can use a accelerometer to replace gyroscope. However, if you are playing games with relatively high precision, it is best to have a gyroscope.

 

// Enable the two sensors, accelerator and mobile. Sensor gsensor = msensormanager. getdefasensensor (sensor. type_accelerometer); sensor msensor = msensormanager. getdefasensensor (sensor. type_magnetic_field); msensormanager. registerlistener (this, gsensor, sensormanager. sensor_delay_game); msensormanager. registerlistener (this, msensor, sensormanager. sensor_delay_game); // accelerates the counting of data. Private float [] mgdata = new float [3]; // magnetic meter data. Private float [] mmdata = new float [3]; // rotation matrix, used to calculate the rotation angle between the mobile phone and the ground coordinate system. For example, whether the mobile phone is horizontal or vertical. It reflects the three-dimensional placement of mobile phones. It is best to use Gyroscope for measurement of this data. If there is no gyroscope, it can also be achieved by means of the accelerator and gravity acceleration, but the accuracy is not strong enough. Private float [] MR = new float [16]; // inclination matrix, used to calculate the mobile phone location. For example, the head of a mobile phone is facing south or north. It is for the Southeast and northwest directions on a plane. Private float [] MI = new float [16]; // stores the final rotation angle data private float [] morientation = new float [3]; Public void onsensorchanged (sensorevent event) {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 shoshould not be here. return;} For (INT I = 0; I <3; I ++) data [I] = event. VA Lues [I]; // This step aims to obtain the MR and Mi matrices. Sensormanager. getrotationmatrix (MR, mi, mgdata, mmdata); // use the matrix Mr to obtain the Rotation Angle of the mobile phone. Sensormanager. getorientation (MR, morientation); // use matrix mi to obtain the angle between the mobile phone in the four directions of the southeast and northwest. Float incl = sensormanager. getinclination (MI); Final float rad2deg = (float) (180.0f/math. pi); log. D ("Compass", "Yaw:" + (INT) (morientation [0] * rad2deg) + "pitch:" + (INT) (morientation [1] * rad2deg) + "Roll:" + (INT) (morientation [2] * rad2deg) + "incl:" + (INT) (incl * rad2deg ));} 

Supplemented

Gsensor is easier to complete. Generally, the machine is placed at a fixed angle (such as horizontal), and N values (such as 20) are obtained consecutively for a series of calculations. In Android and iPhone, the screen changes with the orientation of the device. In fact, only gsensor is used.
Msensor is divided into horizontal comparison and tilt compensation. Horizontal comparison has several methods, such as positive plane, positive 8-character, and positive 10-side. After the level is positive, msensor can be used for horizontal placement. However, when the equipment and the horizontal plane have a certain tilt angle, this tilt angle will affect the precision of msensor. Therefore, you also need to make a positive tilt angle compensation. First, the tilt angle is obtained through a more accurate gsensor, and then the pitch angle (pitch, roll) is used to make a positive compensation.
Orientation sensor can be implemented by a single chip, or jointly calculated by gsensor and msensor (I think compass in android2.3's sample has already done this ). Orientation sensor outputs three values: Roll, pitch, and yaw. Roll and pitch represent the angle between the x-axis and Y-axis of the mobile phone and the horizontal plane respectively, so these two values can be calculated by gsensor. Yaw represents the angle between the projection on the Y axis of the mobile phone and the normal north direction. It is calculated by msensor.
Gyroscope can capture very small changes in motion tracks, so it can perform high-resolution and fast-response rotation detection. However, the current operation direction cannot be measured like msensor or orientation sensor. The plane uses it to record the movement angle changes to form a motion track. The disadvantage is that there will be errors over time,

Supplemented by 2011.7.19

The XYZ of the gyroscope represents the angular velocity of the device rotating around the three axes of XYZ: radians/second. The coordinate system used by XYZ is the same as that used by gsensor. The value of XYZ is positive when it is rotated in the clockwise direction. The following is the Demo code for developing with gyroscope:

private static final float NS2S = 1.0f / 1000000000.0f;private float timestamp;public void onSensorChanged(SensorEvent event){   if (timestamp != 0) {       final float dT = (event.timestamp - timestamp) * NS2S;       angle[0] += event.data[0] * dT;       angle[1] += event.data[1] * dT;       angle[2] += event.data[2] * dT;   }   timestamp = event.timestamp;}

In addition, after the gyroscope runs for a period of time, noise and offset will lead to data deviation, and other sensors must be used for correction.

Supplemented by 2011.7.20

Using the accelerometer and the msensor to simulate the orientation

In the latest version of Android, we do not recommend that you directly use the orientation sensor as follows during development:
Sensor Ori = msensormanager. getdefasensensor (sensor. type_orientation );
Msensormanager. registerlistener (this, Ori, sensormanager. sensor_delay_game );
Instead, we recommend that you use the sensormanager. getorientation (MR, morientation) API to calculate the azimuth sensor in onsensorchanged of the application. However, when we were working on the Hal layer, we had to consider the fact that some APK still directly uses the azimuth sensor as in the code above. Therefore, we had to simulate an azimuth sensor in sensor. C:
When activating the azimuth sensor, both the accelerometer and the magnetic meter are activated. When turning off the azimuth sensor, the accelerator and the magnetic meter are also disabled. Also, if the user registers an accelerometer or magnetic meter, and is in use.

// The default status. 0 indicates disabled. Static int accstatus = 0; static int magstatus = 0; static int switchsensor (int fd, int handle, int enabled) {int status = 0; Switch (handle) {Case id_acceleration: if (Enabled) {If (! Accstatus) {// = 0 indicates that when the current status is disabled, the open operation is performed. Otherwise, it indicates that it has been enabled. Status = IOCTL (FD, acc_flag, 1); logw ("Open gsensor");} accstatus ++;} else {If (accstatus> 0) {//> 0 indicates that the current status is open, and gsensor will be disabled. Accstatus --; If (! Accstatus) {// if it is equal to or greater than 1, it indicates that orientation does not use gsensor. You can disable gsensor's status = IOCTL (FD, acc_flag, 0 ); logw ("Close gsensor") ;}} break; Case id_magnetic: If (Enabled) {If (! Magstatus) {status = IOCTL (FD, mag_flag, 1); logw ("Open msensor") ;}magstatus ++ ;}else {If (magstatus> 0) {magstatus --; if (! Magstatus) {status = IOCTL (FD, mag_mflag, 0); logw ("Close msensor") ;}} break; Case id_orientation: If (Enabled) {If (! Accstatus) {// = 0 indicates that gsensor has not been enabled and is enabled directly. Status = IOCTL (FD, acc_aflag, 1); logw ("Open msensor");} If (! Magstatus) {status = IOCTL (FD, mag_flag, 1); logw ("Open msensor");} accstatus ++; magstatus ++;} else {If (accstatus> 0) {//> 0 indicates that it has been enabled. Try to disable gsensor. Accstatus --; If (! Accstatus) {// minus 1 = 0 indicates that the user does not directly use gsensor, but orientation is used. You can disable gsensor. Status = IOCTL (FD, acc_aflag, 0); logw ("Close gsensor") ;}} if (magstatus> 0) {magstatus --; If (! Magstatus) {status = IOCTL (FD, mag_flag, 0); logw ("Close msensor") ;}} break; default: logw ("unsupported sensor type ");} return status ;}

References:

Difference between accelerometer and Gyroscope

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

How does the Three-Axis Gyroscope and accelerometer help the iPhone locate

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

The St integrated sensor solution realizes the Electronic Compass Function

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

Orientation sensor in Android devices)

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

Easy-to-understand text to show you about iPhone 4 Gyroscope

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

11 sensors in Android operating system

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

Contact Us

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.