Android open-source project Sensors
The android open-source project (aosp) provides three software-based mobile sensors: gravity sensor, linear acceleration sensor, and rotation vector sensor. These three sensors are updated in android4.0 and both use Gyroscope (except for other sensors) to improve stability and performance. If you want to try these sensors, you can identify them by using the getvendor () method and getversion () method (vendor: Google Inc; version: 3 ). It is necessary to identify these sensors through suppliers and versions, because Android considers these sensors as the second sensor. For example, if device manufacturers provide their own gravity sensors, aosp's gravity sensor will act as a second gravity sensor. These three Sensors depend on gyroscope: If there is no gyroscope on the device, these sensors will not be displayed and cannot be used.
Acceleration Sensor
The acceleration sensor measures the acceleration acting on the device, including gravity. The following code shows how to obtain an instance of the default accelerometer:
Private sensormanager msensormanager;
Private sensor msensor;
...
Msensormanager = (sensormanager) getsystemservice (context. sensor_service );
Msensor = msensormanager. getdefasensensor (sensor. type_accelerometer );
Conceptually, the accelerometer tests the force acting on the sensor itself (FS) and uses the following formula to determine the (AD) Acceleration acting on the device:
Ad = - ∑Fs / mass
However, gravity always affects the measurement results of the following formula:
Ad = -g - ∑F / mass
For this reason, when the device is on the desktop (there is no acceleration), The read result of the accelerometer g = 9.81 m/s2. Similarly, when the device moves freely, because the rapid downward acceleration is: 9.81
M/s2, so the result read by the accelerometer is: g = 0 m/s2. Therefore, to measure the actual acceleration of the device, the impact of gravity must be deleted from the acceleration sensor data. This problem can be achieved through Qualcomm filtering. On the contrary, low-pass filtering can be used to isolate gravity. The following example demonstrates how to do this:
Public void onsensorchanged (sensorevent event ){
// In this example, Alpha is calculated as t/(t + dt ),
// Where T is the low-pass filter's time-constant and
// DT is the event delivery rate.
Final float alpha = 0.8;
// Isolate the force of gravity with the low-pass filter.
Gravity [0] = Alpha * gravity [0] + (1-alpha) * event. Values [0];
Gravity [1] = Alpha * gravity [1] + (1-alpha) * event. Values [1];
Gravity [2] = Alpha * gravity [2] + (1-alpha) * event. Values [2];
// Remove the gravity contribution with the high-pass filter.
Linear_acceleration [0] = event. Values [0]-gravity [0];
Linear_acceleration [1] = event. Values [1]-gravity [1];
Linear_acceleration [2] = event. Values [2]-gravity [2];
}
Note:You can use different technologies to filter sensor data. The code example above uses a simple filter constant (alpha) to create a low-pass filter. The filter constant is derived from the time constant (T). It roughly expresses the latency of the filter added to the sensor event and the send frequency (DT) of the sensor event ). For demonstration, the sample code uses a 0.8 Alpha value. If you want to use this filtering method, you need to select different Alpha values based on the actual situation.
The accelerometer uses a standard sensor coordinate system. In practice, the following conditions apply when a device is squashed on the desktop in its natural direction:
1. If you push the device to the left (move it to the right), the acceleration value of the X axis is a positive value;
2. If you push the device at the bottom (let it move up), the acceleration value of the Y axis is positive;
3. Use
M/s2 acceleration to promote the device, then the Z axis acceleration value is a + 9.81, it corresponds to the device's acceleration (+
M/s2) minus gravity (-9.81 m/s2 ).
4. A static device has an acceleration value of + 9.81, which corresponds to the acceleration of the device (0 m/s2 minus gravity, that is,-9.81 m/s2 ).
Generally, an acceleration sensor is a good sensor used to monitor the movement of devices. Almost every android handheld device and tablet has an accelerometer, and its energy consumption is 10 times lower than that of other mobile sensors. The only drawback is that you need to implement low-pass and high-pass filtering to eliminate gravity and reduce noise effects.
The android SDK provides an example program for how to use an acceleration sensor (accelerometer play)