旋转的黑莓加速度计数据

时间:2012-11-30 21:57:10

标签: blackberry accelerometer

我理解x / y / z G力值和“方向”数据available for Blackberry programs

我的问题是如何提取旋转/方向RAW-DATA? (例如程序员用于the "Blackberry-Spirit-Level" app

“Orientation”API似乎会返回ORIENTATION_TOP_UPORIENTATION_RIGHT_UP之类的常量,而不是某种程度的浮动。

1 个答案:

答案 0 :(得分:1)

我认为这不是直接获取设备方向角度的方法,但您可以使用您提到的AccelerometerSensor类提供的原始数据来计算它们。

查看example they show in the API docs,您可以获得原始的X,Y和Z加速度数据。然后使用三角学来确定角度。例如,Z轴垂直向下(并且不移动......或相对于地球加速)的设备将使Z加速度值等于 G ,{{3} }。

如果设备保持在某个其他角度,沿着该轴的加速度值将通过轴之间的角度的正弦/余弦减小,并且垂直线朝向地球。

此示例代码显示了如何获得某些角度:

public void run()
{
    // open channel
    Channel rawDataChannel = AccelerometerSensor.openRawDataChannel( Application.getApplication() );
    // create raw sample vector with three components - X, Y, Z
    short[] xyz = new short[ 3 ];
    while( isRunning() ) {
        // read acceleration
        rawDataChannel.getLastAccelerationData( xyz );
        // process the acceleration
        process( xyz );
        // sleep for maintaining query rate
        try {
            Thread.sleep( 500 );
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    // close the channel to stop sensor query routine
    rawDataChannel.close();
}

private void process(short[] xyz) {

    short X = xyz[0];
    short Y = xyz[1];
    short Z = xyz[2];

    final double roll = MathUtilities.atan2(X, Z) * 180.0 / Math.PI;
    final double pitch = MathUtilities.atan2(Y, Math.sqrt(X*X + Z*Z)) * 180.0 / Math.PI;
    final double tilt = MathUtilities.acos(Z / Math.sqrt(X*X + Y*Y + Z*Z)) * 180.0 / Math.PI;

    UiApplication.getUiApplication().invokeLater(new Runnable() {
        public void run() {
            String output = "Angles are {" + roll + ", " + pitch + ", " + tilt + "}";
            textField.setText(output);
        }
    });
}

有关 roll 音高倾斜的含义的参考,请参阅the acceleration due to Gravity。那是数学的来源。

其他问题

这不是来自我编写的制作应用,因此代码尚未经过全面测试。角度的符号可能是错误的,方程中可能存在不稳定性,或者其他类似的东西。

此外,如果您使用原始加速度计数据,则需要实施校准功能。如果你运行任何类型的智能手机级应用程序,你会看到一个如何做的例子。因此,上面计算的结果需要通过校准常数进行调整。

最后,原始加速度计数据可能会产生噪音,因此您可能需要过滤/平滑数据,具体取决于您希望如何使用它。

这只是一个起点。祝你好运!

相关问题