目前,我完全被大学练习所困扰。过去几天我一直在努力,也做了很多研究,但要么我正在尝试做一些不可能的事情,要么我在推理上遇到了可怕的错误。
我的目标是什么?- 我想实现一个 Android 应用程序 (android:minSdkVersion="8"),它可以通过 OSC 发送反馈消息(正面或负面)。反馈发送不仅可以通过单击一些按钮(那个很简单;-),还可以通过摇晃和倾斜设备来实现。
摇动意味着将智能手机从右向左旋转或反过来——就像摇头一样。倾斜意味着上下旋转设备 - 就像点头一样。
由于我的设备不是市场上最新鲜的,我只能使用加速度计和磁场传感器(我没有陀螺仪或其他东西)。
我基于谷歌搜索的想法是听加速度计和磁场事件,并使用旋转矩阵来计算角度之间的增量。x 轴上的某个 delta 将被解释为倾斜(点头),而 y 轴上的某个 delta 将被解释为摇晃。由于到目前为止我没有取得好的结果,我问自己这是否是正确的方法?!
目前我的 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。
我希望有人可以提示我的代码或我的想法有什么问题。
提前致谢!