【问题标题】:Calculate distance by using LINEAR_ACCELERATION使用 LINEAR_ACCELERATION 计算距离
【发布时间】:2014-12-13 10:24:53
【问题描述】:

我正在使用 Google Nexus 4 并尝试编写一个在 PC 上控制鼠标光标的应用程序。更特别的是,当我将手机向右移动时,鼠标光标也应该向右移动。

我现在只在 x 轴上工作,并按照this answer 中的the document 的说明进行操作。

但我无法得到我想要的结果。当我将手机向右移动时,光标向右移动,然后又向左移动。这是我的代码的一部分:

@Override
public void onSensorChanged(SensorEvent event) 
{
    // sampling acceleration 
    do 
    {
        acceleration_x[1] = acceleration_x[1] + (int) event.values[0];
        count_x++;

    } while ( count_x < 64);

    acceleration_x[1] = acceleration_x[1] >> 6;

    count_x = 0;

    //Mechanical Filtering
    if( (acceleration_x[1] <= 3) && (acceleration_x[1] >= -3) )
        acceleration_x[1] = 0;


    // first integration
    velocity_x[1] = velocity_x[0] + acceleration_x[0] + 
            ((acceleration_x[1] - acceleration_x[0]) >> 1);

    // second integration
    position_x[1] = position_x[0] + velocity_x[0] +
            ((velocity_x[1] - velocity_x[0]) >> 1);

    acceleration_x[0] = acceleration_x[1];
    velocity_x[0] = velocity_x[1];


    sendData.sendPacket(position_x[1], 0); // sending data(x,y) to PC

    movement_end_check(event);

    position_x[0] = position_x[1];

}

积分公式和movement_end_check方法取自文档。

那么,你能帮我解决这个问题吗?

【问题讨论】:

  • 抱歉回答晚了,我不得不退出。谢谢,我一直在寻找这个主题一段时间,所以我已经看过几次视频了。我同意这个演示文稿确实提供了丰富的信息,但是没有代码示例可供使用。由于我是这个主题的新手,所以一个小例子可能会有用。

标签: android accelerometer android-sensors


【解决方案1】:

您永远不会增加速度/加速度列表中的位置。它总是写入 1。这不是它的工作原理——想想看,如果总是写入 1 就是答案,为什么要有一个数组?

【讨论】:

  • 实际上,我正在尝试查找即时位置的变化。然后,将其发送给 PC 以设置光标的新位置。为此,您还有其他方法可以建议吗? (顺便说一句,由于我的英语不好,我可能会用错误的术语来表达自己。对此真的很抱歉。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多