SENSOR_TYPE_ACCELEROMETER 加速度 SENSOR_TYPE_MAGNETIC_FIELD 磁力 SENSOR_TYPE_ORIENTATION 方向 SENSOR_TYPE_GYROSCOPE 陀螺仪 SENSOR_TYPE_LIGHT 光线感应 SENSOR_TYPE_PRESSURE 压力 SENSOR_TYPE_TEMPERATURE 温度 SENSOR_TYPE_PROXIMITY 接近 SENSOR_TYPE_GRAVITY 重力 SENSOR_TYPE_LINEAR_ACCELERATION 线性加速度 SENSOR_TYPE_ROTATION_VECTOR 旋转矢量
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor == null) {
return;
}
if (sensorEvent.sensor.getType() == accelerometerSensorType) {
float accelerationZ = sensorEvent.values[2];
if (accelerationZ > 0) {
recognitionKnockRatio = 20;
recognitionUniqueRatio = 10;
smoothSectionMaxRatio = 5f;
} else {
recognitionKnockRatio = 7.5f;
recognitionUniqueRatio = 6;
smoothSectionMaxRatio = 2.5f;
}
gravityZ = alpha * gravityZ + (1 - alpha) * accelerationZ;
linearAccelerationZ = accelerationZ - gravityZ;
if (calibrateLinearAcceleration) {
calibrateLinearAccelerationIndex++;
if (calibrateLinearAccelerationIndex <= calibrateLinearAccelerationSectionNumber) {
return;
}
calibrateLinearAcceleration = false;
}
if (sensorDataShowIndex >= sensorDataShowNumber) {
sensorDataShowIndex = sensorDataShowNumber - sensorDataShowDurationNumber;
Iterator<?> it = linearAccelerationZShowList.listIterator(0);
for (int i = 0; i < sensorDataShowDurationNumber; i++) {
it.next();
it.remove();
}
MainActivity.UpdateSensorData(linearAccelerationZShowList);
}
linearAccelerationZShowList.add(linearAccelerationZ);
sensorDataShowIndex++;
if (!stable) {
linearAccelerationZList.add(linearAccelerationZ);
if (linearAccelerationZList.size() >= stableSectionNumber) {
stableRecognition();
linearAccelerationZList.clear();
}
return;
}
knockRecognition(linearAccelerationZ);
}
}
* <p>
* It should be apparent that in order to measure the real acceleration of
* the device, the contribution of the force of gravity must be eliminated.
* This can be achieved by applying a <i>high-pass</i> filter. Conversely, a
* <i>low-pass</i> filter can be used to isolate the force of gravity.
* </p>
*
* <pre class="prettyprint">
*
* public void onSensorChanged(SensorEvent event)
* {
* // alpha is calculated as t / (t + dT)
* // with t, the low-pass filter's time-constant
* // and dT, the event delivery rate
*
* final float alpha = 0.8;
*
* 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];
*
* linear_acceleration[0] = event.values[0] - gravity[0];
* linear_acceleration[1] = event.values[1] - gravity[1];
* linear_acceleration[2] = event.values[2] - gravity[2];
* }
* </pre>
private void stableRecognition() {
int exceptionNumber = 0;
float accelerationZValue;
float minAccelerationZValue = Integer.MAX_VALUE;
float maxAccelerationZValue = Integer.MIN_VALUE;
for (int i = stableSectionNumber - 1; i >= 0; i--) {
accelerationZValue = linearAccelerationZList.get(i);
if (Math.abs(accelerationZValue) > maxStableOffset) {
exceptionNumber++;
} else {
if (accelerationZValue > maxAccelerationZValue) {
maxAccelerationZValue = accelerationZValue;
} else {
if (accelerationZValue < minAccelerationZValue) {
minAccelerationZValue = accelerationZValue;
}
}
}
}
stable = exceptionNumber <= maxExceptionNumber;
if (stable) {
if (linearAccelerationZStableSection == 0) {
linearAccelerationZStableSection =
(maxAccelerationZValue - minAccelerationZValue) / 2;
}
if (linearAccelerationZStableSection > maxStableOffset) {
linearAccelerationZStableSection = maxStableOffset;
}
}
MainActivity.UpdateStable(stable);
LogFunction.log("stable", "" + stable);
LogFunction.log("exceptionNumber", "" + exceptionNumber);
LogFunction.log("linearAccelerationZStableSection", "" + linearAccelerationZStableSection);
}
private void knockRecognition(float linearAccelerationZ) {
float linearAccelerationZAbsolute = Math.abs(linearAccelerationZ);
float linearAccelerationZAbsoluteRadio =
linearAccelerationZAbsolute / linearAccelerationZStableSection;
if (linearAccelerationZAbsoluteRadio > recognitionUniqueRatio) {
uniqueLinearAccelerationZList.add(linearAccelerationZ);
currentForecastNumber = forecastNumber;
} else {
if (uniqueLinearAccelerationZList.size() > 0) {
if (currentForecastNumber > 0) {
currentForecastNumber--;
} else {
handleUniqueLinearAccelerationZ();
}
}
}
if (linearAccelerationZAbsoluteRadio < smoothSectionMaxRatio) {
float offsetWeight = 0.001f;
linearAccelerationZStableSection =
weightedMean(offsetWeight, linearAccelerationZAbsolute,
linearAccelerationZStableSection);
}
}
private void handleUniqueLinearAccelerationZ() {
LogFunction.log("linearAccelerationZStableSection", "" + linearAccelerationZStableSection);
int recognitionKnockNumber = 1;
int uniqueLinearAccelerationZListLength = uniqueLinearAccelerationZList.size();
float accelerationZOffsetAbsolute;
float maxAccelerationZOffsetAbsolute = 0;
for (int i = 0; i < uniqueLinearAccelerationZListLength; i++) {
accelerationZOffsetAbsolute = Math.abs(uniqueLinearAccelerationZList.get(i));
if (maxAccelerationZOffsetAbsolute < accelerationZOffsetAbsolute) {
maxAccelerationZOffsetAbsolute = accelerationZOffsetAbsolute;
}
LogFunction.log("uniqueLinearAccelerationZList index" + i,
"" + uniqueLinearAccelerationZList.get(i));
}
uniqueLinearAccelerationZList.clear();
LogFunction.log("uniqueLinearAccelerationZListLength",
"" + uniqueLinearAccelerationZListLength);
if (uniqueLinearAccelerationZListLength > unstableListLength) {
stable = false;
MainActivity.UpdateStable(stable);
return;
}
LogFunction.log("maxAccelerationZOffsetAbsolute / linearAccelerationZStableSection",
"" + (maxAccelerationZOffsetAbsolute / linearAccelerationZStableSection));
if (maxAccelerationZOffsetAbsolute >
linearAccelerationZStableSection * recognitionKnockRatio) {
LogFunction.log("recognitionKnockRatio", "" + recognitionKnockRatio);
LogFunction.log("recognitionUniqueRatio", "" + recognitionUniqueRatio);
knockRecognitionSuccess(recognitionKnockNumber);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有