2013-12-19 141 views
0

目前我完全陷入大学锻炼。过去几天我一直在努力尝试,也做了大量的研究,但是我想要做一些不可能的事情,或者我在推理中遇到了一个可怕的错误。通过加速度计和磁场传感器检测Android设备的倾斜和抖动

我的目标是什么? - 我想实现一个Android应用程序(android:minSdkVersion =“8”),它可以通过OSC发送反馈消息(正面或负面)。发送反馈信息不仅可以通过点击某些按钮(这很容易;),而且还可以通过摇动和倾斜设备来实现。

摇动意味着将智能手机从右向左或以其他方式旋转 - 就像摇头。倾斜意味着上下旋转设备 - 就像点头一样。

由于我的设备不是市场上最新鲜的,我只能使用加速度计和磁场传感器(我没有陀螺仪或其他东西)。

我的想法基于google搜索很多,是听加速度计和磁场事件,并使用旋转矩阵来计算角度之间的增量。 x轴上的某个三角形将被解释为倾斜(点头),并且y上的某个三角形将被抖动。由于到目前为止我还没有取得好成绩,我在问自己这是否是正确的做法?!

目前我SensorEventListener看起来是这样的:

/** 
* TYPE_ACCELEROMETER 
* <ul> 
* <li>SensorEvent.values[0] Acceleration force along the x axis (including 
* gravity) in m/s2</li> 
* <li>SensorEvent.values[1] Acceleration force along the y axis (including 
* gravity) in m/s2</li> 
* <li>SensorEvent.values[2] Acceleration force along the z axis (including 
* gravity) in m/s2</li> 
* </ul> 
* 
* TYPE_MAGNETIC_FIELD 
* <ul> 
* <li>SensorEvent.values[0] Geomagnetic field strength along the x axis in 
* µT</li> 
* <li>SensorEvent.values[1] Geomagnetic field strength along the y axis in 
* µT</li> 
* <li>SensorEvent.values[2] Geomagnetic field strength along the z axis in 
* µT</li> 
* </ul> 
*/ 
@Override 
public void onSensorChanged(SensorEvent event) { 
    now = event.timestamp; 

    // Handle the events for which we registered 
    switch (event.sensor.getType()) { 
    case Sensor.TYPE_ACCELEROMETER: 
     System.arraycopy(event.values, 0, valuesAccelerometer, 0, 3); 

     // no magnetic field data 
     if (isArrayZeroFilled(valuesMagneticField)) { 
      return; 
     } 

     // if rotation matrix cannot be retrieved 
     if (!SensorManager.getRotationMatrix(null, rotationMatrix, 
       valuesAccelerometer, valuesMagneticField)) 
      return; 

     SensorManager.getOrientation(rotationMatrix, valuesOrientation); 

     // valuesOrientation 
     // values[0]: azimuth, rotation around the Z axis. 
     // values[1]: pitch, rotation around the X axis. 
     // values[2]: roll, rotation around the Y axis. 

     zRotation = valuesOrientation[0]; 
     xRotation = valuesOrientation[1]; 
     yRotation = valuesOrientation[2]; 

     float xRotationDelta = Math.abs(xRotation - lastXRotation); 
     System.out.println("x rotation delta " + xRotationDelta); 

     float yRotationDelta = Math.abs(yRotation - lastYRotation); 
     System.out.println("y rotation delta " + yRotationDelta); 

     float zRotationDelta = Math.abs(zRotation - lastZRotation); 
     System.out.println("z rotation delta " + zRotationDelta); 

     break; 

    case Sensor.TYPE_MAGNETIC_FIELD: 
     System.arraycopy(event.values, 0, valuesMagneticField, 0, 3); 
     break; 
    } 
} 

奇怪的是,Y和Z三角洲始终0.0,无论我如何移动或摇晃我的电话。

我希望有人能给我提示我的代码或我的想法有什么问题。

在此先感谢!

回答

-1

此代码将帮助您检测抖动:1