Android Tri-axis acceleration sensor "turn"

Source: Internet
Author: User
Tags cos

First, the sensor commonly used in mobile phones

In the Android2.3 gingerbread system, Google offers 11 layers of sensor supply, as follows: (Sensor Class)

#define Sensor_type_accelerometer 1//acceleration
#define Sensor_type_magnetic_field 2//Magnetic
#define Sensor_type_orientation 3//direction
#define SENSOR_TYPE_GYROSCOPE 4//Gyroscope
#define Sensor_type_light 5//Light Sensing
#define SENSOR_TYPE_PRESSURE 6//pressure
#define Sensor_type_temperature 7//temperature
#define Sensor_type_proximity 8//Close
#define Sensor_type_gravity 9//Gravity
#define Sensor_type_linear_acceleration 10//Linear acceleration
#define Sensor_type_rotation_vector 11//Rotation vector

1-1 Acceleration Sensor

The accelerometer is also called G-sensor, which returns the acceleration values for the X, y, and Z axes.
This value contains the effect of gravity, in units of m/s^2.
Put your phone flat on the desktop, the X axis defaults to the 0,y axis default 0,z axis default 9.81.
Place your phone face down on the desktop with a Z axis of-9.81.
Tilt the phone to the left and the x-axis as positive.
Tilt the phone to the right and negative x-axis.
Tilt the phone upward and the y-axis is negative.
Tilt the phone downward and the y-axis is positive.
The accelerometer is probably the most sophisticated MEMS product, with a wide range of accelerometer sensors on the market.
The commonly used accelerometer in mobile phones is Bosch's BMA series, AMK 897X series, St's LIS3X series, etc.
These sensors typically provide an acceleration measurement range from ±2g to ±16g with an I²C or SPI interface connected to the MCU with a data accuracy of less than 16bit.

1-2 Magnetic Sensor

The magnetic sensor is referred to as m-sensor and returns the environmental magnetic field data of the X, y, and Z axes.
The unit of the value is micro-Tesla (Micro-tesla), expressed in UT.
The unit can also be Gauss (Gauss), 1tesla=10000gauss.
There is generally no independent magnetic sensor on the hardware, and the magnetic data is provided by the electronic Compass Sensor (E-COMPASS).
The Electronic Compass sensor also provides direction sensor data for the following.

1-3 Direction Sensor

The direction sensor is abbreviated as O-SENSOR, which returns the angle data of the three axes, and the unit of the direction data is the angle.
To get accurate angular data, e-compass needs to get g-sensor data,
The production o-sensor data is calculated, otherwise only the horizontal angle can be obtained.
The direction sensor provides three data, azimuth, pitch, and roll respectively.
Azimuth: Azimuth, returns the angle of the magnetic north and Y axes at horizontal time, ranging from 0° to 360°.
0°= North, 90°= East, 180°= South, 270°= West.
The angle between the pitch:x axis and the horizontal plane, ranging from -180° to 180°.
When the z axis rotates toward the y axis, the angle is positive.
The angle between the roll:y axis and the horizontal plane, for historical reasons, ranges from -90° to 90°.
When the x-axis moves toward the z-axis, the angle is positive.
The electronic compass needs to be calibrated before it can get the correct data, usually with a 8-word calibration method.
8-word calibration requires the user to do 8-word shaking in the air using a device that needs to be calibrated,
In principle, as much as possible, the device normal direction points to all 8 quadrants of space.
The electronic compass chip used in the mobile phone has the AKM company's 897X series, St company's LSM series as well as Yamaha company and so on.
Because of the need to read g-sensor data and calculate m-sensor and o-sensor data,
So vendors will generally provide a background daemon to complete the work, electronic compass algorithm is generally the company's private property rights.

1-4 Gyro Sensor

The gyroscope sensor is called gyro-sensor and returns the angular acceleration data for the X, y, and Z axes.
The unit of angular acceleration is radians/second.
Measured by Nexus S mobile:
Rotate horizontally counterclockwise and the z axis is positive.
The horizontal counterclockwise rotation, the z axis is negative.
Rotate to the left and negative on the Y axis.
Rotates to the right and the y-axis is positive.
Rotate up and the x axis is negative.
Rotate downward and the x axis is positive.
ST's L3G series of gyroscope sensors are more popular, iphone4 and Google's Nexus S use this kind of sensor.

1-5 Light Sensing Sensor

The light sensor detects real-time light intensity, the unit of light is lux, and its physical meaning is luminous flux on the unit area.
The light sensor is mainly used for the LCD auto-brightness function of Android system.
The brightness of the LCD can be adjusted in real time according to the light intensity sampled.

1-6 Pressure sensor

The pressure sensor returns the current pressure in the unit of Hectopascal (HPa).

1-7 Temperature Sensor

The temperature sensor returns the current temperature.

1-8 Proximity Sensor

The proximity sensor detects the distance between the object and the phone, in centimeters.
Some proximity sensors can only return far and near two states,
Therefore, the proximity sensor returns the maximum distance to the far state, which is less than the maximum distance to return to the near state.
Proximity sensors can be used to automatically turn off the LCD screen when answering a phone to conserve power.
Some chips have integrated proximity sensors and light sensors for both functions.
The following three sensors are the new sensor types proposed by ANDROID2, and it is not yet clear which applications are available.

1-9 Gravity sensor

The gravity sensor is referred to as gv-sensor and outputs gravity data.
On Earth, the value of gravity is 9.8, the unit is m/s^2.
The coordinate system is the same as the accelerometer sensor.
When the device is reset, the output of the gravity sensor is the same as the accelerometer sensor.

1-10 Linear acceleration Sensor

Linear accelerometer is referred to as la-sensor.
The linear accelerometer is the data obtained by the acceleration sensor minus the gravity effect.
The unit is m/s^2 and the coordinate system is the same as the accelerometer sensor.
The formula for acceleration sensors, gravity sensors and linear accelerometer is as follows:
Acceleration = gravity + linear acceleration

1-11 Rotation vector sensor

Rotary vector sensor for short rv-sensor.
The rotation vector represents the direction of the device, which is a mix of axes and angles to calculate the data.
Rv-sensor output of three data:
X*sin (THETA/2)
Y*sin (THETA/2)
Z*sin (THETA/2)
Sin (THETA/2) is an order of magnitude for RV.
The RV is in the same direction as the Axis rotation.
The three values of the RV, together with the Cos (THETA/2), constitute a four-tuple.
RV data has no units, and the coordinate system used is the same as acceleration.
Example:
Sensors_event_t.data[0] = X*sin (THETA/2)
SENSORS_EVENT_T.DATA[1] = Y*sin (THETA/2)
SENSORS_EVENT_T.DATA[2] = Z*sin (THETA/2)
SENSORS_EVENT_T.DATA[3] = cos (THETA/2)
The values for GV, LA, and RV are not available in the physical sensor directly,
G-sensor, O-sensor, and gyro-sensor are required to be calculated by the algorithm.
The algorithm is usually the private property of the sensor company.

Second, Android induction detection Management---sensormanager

1. Get Sensormanager


Using inductive detection sensor first to obtain the detection signal of the sensing device, you can call the Context.getsysteservice (Senser_service) method to obtain the induction detection service

2, to achieve the sensing detection sensor status monitoring function


The following two Sensoreventlistener methods are implemented to monitor and obtain the sensor status of induction detection:

Called when sensing detects a change in the precision of the sensor.
public void onaccuracychanged (Senso sensor,int accuracy);
The sensor is called when it detects that the value of the sensor has changed.
public void Onsensorchanged (Sensorevent event);

3. Achieve the value of sensor target of induction detection

The following getsensorlist () method is implemented to obtain the value of sensing sensor;
list<sensor> sensors = sm.getsensorlist (sensor.type_temperature);

4, Registered Sensorlistener

Sm.regesterlistener (Sensoreventlistener Listener, sensor sensor, int rate);

The first parameter: The sensor event is monitored, the second parameter is the value of the sensor target type, and the third parameter is the precision density of the delay time. The precision parameters of the delay time are as follows:

Parameters

Delay Time

Sensormanager.sensor_delay_fastest

0ms

Sensormanager.sensor_delay_game

20ms

sensormanager.sensor_delay_ui

60ms

Sensormanager.sensor_delay_normal

200ms

Because induction detection sensor service is frequent and slow and the cost of battery parameters, but also affect the efficiency of processing, so taking into account the balance of the consumption of battery and processing efficiency, set induction detection sensor delay time is an important knowledge, need to be based on the needs of the application system to do the appropriate settings.
The hardware detection components of sensor sensing sensors are provided by different vendors. You can use the sensor's Getvendor (), sensor () GetName () and sensor's getveesrion () method to obtain the manufacturer's name, product, and version.

5. Cancellation of registration

Sm.unregisterlistener (Sensoreventlistener Listener)

6. Inductive detection

Acceleration Sensing Detection--accelerometer

Accelerometer sensor measures the negative value (including the gravitational acceleration) of the acceleration generated by all forces exerted on the device. The unit used for acceleration is m/sec^2, and the value is the negative value of the acceleration.

SENSOREVENT.VALUES[0]: Negative value of acceleration on X-axis
SENSOREVENT.VALUES[1]: Negative value of acceleration in Y-axis
SENSOREVENT.VALUES[2]: Negative value of acceleration in z-axis

For example:

The acceleration on the x-axis is positive when the phone's z-axis is flat on the desktop and the phone is pushed from left to right.

When the phone's z-axis is stationary on the desktop, the acceleration of the z-axis is +9.81m/sec^2.

When the phone is falling free from the air, the acceleration is 0.

When the cell phone upward with am/sec^2 acceleration to the air, this time the acceleration is a+9.81m/sec^2

Gravitational acceleration Sensing detection--gravity

The gravitational acceleration, whose units are m/sec^2, is consistent with the accelerometer use. When the phone is stationary, the value of the gravity is consistent with the value of the accelerometer.

Linear Acceleration Sensing detection--linear-acceleration

The relationship between Accelerometer, Gravity and Linear-acceleration is as follows:

Accelerometer = Gravity + linear-acceleration

Ground magnetic field sensing detection--magnetic-field

The unit of the geomagnetic field is Micro-tesla (UT), which detects an absolute magnetic field on the X, Y, Z axes.

Gyro Sensing Detection--gyroscope

The gyroscope is in radians per second, measuring the angular velocity of the object rotating around the x, Y, Z axes. Its coordinate system is the same as the acceleration sensor's coordinate system. The angle of rotation counterclockwise is positive. That is, if the device is rotated counterclockwise, the observer will look at the square of the x, Y, Z axis and report that the device is turning. Note that this is the standard mathematical definition of positive rotation.

Light Sensing Detection--light

Values[0]: Indicates the level of ambient illumination, in SI lux units.

Position approximation Sensing detection--proximity

Values[0]: Approximate distance, in cm (cm) units. Some sensors can only support near and far two states, in which case the sensor must report its Maximum_range value in the far state and the small value in the near state.

Rotational vector sensing detects--rotation vectors

The rotation vector is used to represent the direction of the device, which is made up of angles and axes, or the device rotates the θ angle around one of the x, Y, Z axes. The three elements of the rotation vector are so that the size of the rotation vector equals sin (Θ/2), and the direction of the rotation vector equals the direction of the axis of rotation.

Values[0]: X*sin (Θ/2)
VALUES[1]: Y*sin (Θ/2)
VALUES[2]: Z*sin (Θ/2)
VALUES[3]: cos (Θ/2) (optional:only if value.length = 4)

Direction Sensing Detection--orientation

Its unit is angle

Values[0]: Azimuth (azimuth), the angle of the geomagnetic North direction and the Y axis, rotating around the Z axis (0 to 359). 0=north, 90=east, 180=south, 270=west
VALUES[1]: Pitch (pitch), rotating around x-axis ( -180 to 180), positive when z-axis is moving to y-axis
VALUES[2]: Roll (rolling), rotating around the y-axis ( -90 to 90), positive when the x-axis moves to the z-axis

Three, example of the-gsensor1, illustrated three-axis direction

The coordinate system of the Android Gravity sensor is centered at the bottom left of the screen ("note" 2d is programmed with the upper left of the screen as the origin) and the arrow is pointing in a positive direction. From 10 to 10, use floating-point numbers as a unit of magnitude to visualize the following scenarios:

The value of (x, Y, z) is (0,0,10) when the mobile screen is horizontally placed upward (Z axis);
The value of (x, Y, z) is (0,0,-10) when the mobile screen is horizontally placed downward (z-axis);
The value of (x, Y, z) is (10,0,0) when the screen of the phone is placed to the left.
When the cell phone is upright (y-axis) upward, the (x, y, Z) values are (0,10,0) respectively;
The other analogy, the law is: The upside is positive, the ground is negative. Using x, Y, z three values to calculate the trigonometric functions, you can accurately detect the mobile phone's state of motion.

2, by monitoring the gsensor to determine the mobile phone in a stationary/mobile state
 Public classMainactivityextendsActivityImplementsSensoreventlistener {Private Static FinalString TAG = mainactivity.class. Getsimplename (); PrivateSensormanager Msensormanager; PrivateSensor Msensor; PrivateTextView textviewx; PrivateTextView Textviewy; PrivateTextView Textviewz; PrivateTextView TEXTVIEWF; Private intMX, MY, MZ; Private LongLasttimestamp = 0;    Calendar Mcalendar; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Textviewx=(TextView) Findviewbyid (R.ID.TEXTVIEW1); Textviewy=(TextView) Findviewbyid (R.ID.TEXTVIEW3); Textviewz=(TextView) Findviewbyid (R.ID.TEXTVIEW4); TEXTVIEWF=(TextView) Findviewbyid (R.ID.TEXTVIEW2); Msensormanager=(Sensormanager) Getsystemservice (Sensor_service); Msensor= Msensormanager.getdefaultsensor (Sensor.type_accelerometer);//type_gravity        if(NULL==Msensormanager) {LOG.D (TAG,"Deveice not support Sensormanager"); }        //parameter three, the accuracy of the detectionMsensormanager.registerlistener ( This, Msensor, sensormanager.sensor_delay_normal);//Sensor_delay_game} @Override Public voidOnaccuracychanged (sensor sensor,intaccuracy) {} @Override Public voidonsensorchanged (Sensorevent event) {if(Event.sensor = =NULL) {            return; }        if(Event.sensor.getType () = =sensor.type_accelerometer) {            intx = (int) event.values[0]; inty = (int) event.values[1]; intz = (int) event.values[2]; Mcalendar=calendar.getinstance (); LongStamp = Mcalendar.gettimeinmillis ()/1000l;//1393844912Textviewx.settext (string.valueof (x));            Textviewy.settext (string.valueof (y));            Textviewz.settext (String.valueof (z)); intSecond = Mcalendar.get (Calendar.second);// -            intpx = Math.Abs (MX-x); intPY = Math.Abs (MY-y); intPZ = Math.Abs (MZ-z); LOG.D (TAG,"PX:" + px + "py:" + py + "PZ:" + PZ + "stamp:" + Stamp + "Second:" +second); intMaxValue =getmaxvalue (px, py, PZ); if(MaxValue > 2 && (stamp-lasttimestamp) > 30) {Lasttimestamp=Stamp; LOG.D (TAG,"Sensor ismoveorchanged ..."); Textviewf.settext ("Detect mobile phone in mobile ..."); } MX=x; MY=y; MZ=Z; }    }    /*** Get a maximum value * *@parampx *@paramPY *@paramPZ *@return     */     Public intGetmaxvalue (intPxintPyintPZ) {        intMax = 0; if(px > py && px >PZ) {Max=px; } Else if(Py > px && py >PZ) {Max=py; } Else if(Pz > px && pz >py) {Max=PZ; }        returnMax; }}

The principle is that the value of the x, Y, z axes of each x, and the next time the value of the largest of the absolute value of each difference if more than a certain threshold (defined by itself), and this state lasts for the second, we see the mobile phone is in (bumpy) moving state, of course, this judgment is certainly unscientific, Sometimes, the ideal scenario is to take a mobile phone on a bus or drive a car.

Other resources available for reference:

http://blog.csdn.net/zhandoushi1982/article/details/8591878

http://blog.csdn.net/ZhengZhiRen/article/details/5930451

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

Android Tri-axis acceleration sensor "turn"

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.