简体   繁体   English

使用加速度计获取设备运动方向

[英]Get device motion direction using accelerometer

I'm trying to detect the direction of motion of an android device using the accelerometer. 我正在尝试使用加速度计检测android设备的运动方向。 I've removed the gravity components from the accelerometer output values and deducted the angles. 我已经从加速度计的输出值中删除了重力分量,并减去了角度。

So I am able to get the direction of motion but I also get arbitrary angles momentarily. 因此,我既可以获取运动方向,又可以瞬间获取任意角度。 Like when I stop moving the device. 就像我停止移动设备时一样。

Any suggestions how I could filter those angles out? 有什么建议可以过滤掉这些角度吗?

EDIT: I've somewhat been able to solve this by taking the mean of the current and past values. 编辑:我已经能够通过采用当前和过去值的平均值来解决此问题。 Another problem that persists is that initially for a few moments, the accelerometer reports values in the opposite direction of motion. 仍然存在的另一个问题是,在最初的一会儿,加速度计会报告运动方向相反的值。

That's a typical trouble with accelerometers... Initially there's no any solution because of the inertia, etc... But You can try using some kind of "integer controller". 这是加速度计的典型问题。最初,由于惯性等原因,没有任何解决方案。但是,您可以尝试使用某种“整数控制器”。

Another possible solution is detecting abrupt changes on the acceleration and interpret them as changes of direction, I mean, for example if you have the acceleration on the X edge (Ax). 另一个可能的解决方案是检测加速度的突然变化,并将其解释为方向变化,例如,如果您在X边缘(Ax)上具有加速度,则表示这种变化。

while(1){
Ax = readAx();
if(changeSign(Ax)){  //From + to - or - to +.
      //Do what ever you need, for example if sign is changed and keep on it then is that the mobilephone is been moved in the other direction. Else if it's acceleration is close to 0 it means that the device has stopped
}else{
     //The device keep moving on the same direction.
}
}

Feel free to be creative. 随时发挥创造力。 There are many ways to manage a solution depends on your target. 管理解决方案的方法有很多,具体取决于您的目标。

I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM