Android Sensor --- Motion Sensor (3)

Source: Internet
Author: User

Use gravity Sensor

The gravity sensor provides three dimensional vectors to indicate the direction and weight of gravity. The following code shows how to obtain an instance of a default gravity sensor:

Private SensorManager mSensorManager;

Private Sensor mSensor;

...

MSensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE );

MSensor = mSensorManager. getdefasensensor (Sensor. TYPE_GRAVITY );

The Unit is the same as the unit (m/s2) used by the accelerometer, and the coordinate system is the same as that used by the accelerometer.

Note: when the device is at rest, the output of the gravity sensor should be the same as that of the acceleration sensor.

Use Gyroscope

The gyroscope uses rad/s (radians/second) as the unit to measure the speed or rotation angle around the X, Y, and Z axes of the device. The following code shows how to obtain a default gyroscope instance:

Private SensorManager mSensorManager;

Private Sensor mSensor;

...

MSensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE );

MSensor = mSensorManager. getdefasensensor (Sensor. TYPE_GYROSCOPE );

The coordinate system of the sensor is the same as that used by the acceleration sensor. The clockwise rotation is a positive value. That is to say, if the device is clockwise, the observer will see some positive X, Y, and Z axes with the device origin as the center. This is a standard mathematical definition of forward rotation, and is different from the definition used for rolling by a direction sensor.

Generally, the output of a gyroscope is integrated into time to calculate the rotation angle changes over a certain period of time. For example:

// Create a constant to convert nanoseconds to seconds. required NS2S = 1.0f/commandid 0.0f; privatefinalfloat [] deltaRotationVector = newfloat [4] (); privatefloat timestamp; publicvoid onSensorChanged (SensorEventevent) {// This timestep's delta rotation to be multiplied by the current rotation // after computing it from the gyro sample data. if (timestamp! = 0) {finalfloat dT = (event. timestamp-timestamp) * NS2S; // Axis of the rotation sample, not normalized yet. float axisX = event. values [0]; float axisY = event. values [1]; float axisZ = event. values [2]; // Calculate the angular speed of the sample float omegaMagnitude = sqrt (axisX * axisX + axisY * axisY + axisZ * axisZ ); // Normalize the rotation vector if it's big enough to get the axis // (that is, EPSILO N shoshould represent your maximum allowable margin of error) if (omegamagn.pdf> EPSILON) {axisX/= omegamagn.pdf; axisY/= omegamagn.pdf; axisZ/= omegamagn.pdf ;} // Integrate around this axis with the angular speed by the timestep // in order to get a delta rotation from this sample over the timestep // We will convert this axis-angle representation of delta rotation // into a quaternion B Efore turning it into the rotation matrix. float thetaOverTwo = omegamagn?* dT/2.0f; float sinThetaOverTwo = sin (thetaOverTwo); float =cos (thetaOverTwo); deltaRotationVector [0] = %* axisX; deltaRotationVector [1] = sinThetaOverTwo * axisY; deltaRotationVector [2] = sinThetaOverTwo * axisZ; deltaRotationVector [3] = cosThetaOverTwo;} timestamp = event. timestamp; float [] DeltaRotationMatrix = newfloat [9]; SensorManager. getRotationMatrixFromVector (deltaRotationMatrix, deltaRotationVector); // User code shocould concatenate the delta rotation we computed with the current rotation // in order to get the updated rotation. // rotationCurrent = rotationCurrent * deltaRotationMatrix;} The standard gyroscope provides raw rotation data without any filtering or noise and drift (eccentric) correction. In practice, the noise and drift of the gyroscope introduce errors, so it is necessary to offset this. It is usually determined by monitoring other sensors, such as gravity sensors or accelerometer, to determine the drift (eccentric) and noise.

 

Related Article

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.