Android:使用加速度计的速度计算不正确
Posted
技术标签:
【中文标题】Android:使用加速度计的速度计算不正确【英文标题】:Android: incorrect velocity calculation using accelerometer 【发布时间】:2017-04-14 09:52:41 【问题描述】:我希望这不是重复的,因为我在 android 中看到了一些关于加速度计的相关帖子,但它们都对我没有帮助。
我需要获取移动设备的 X 轴速度(仅)。
问题是,当我沿直线移动设备时(初始速度 = 0),首先将其加速然后减速(直到完全停止),我得到的计算速度与实际速度不符。
问题的主要症状是设备停止后,我计算的速度不是0。
我可以在结果中看到加速度和减速度(首先是正加速度值,然后是负加速度值),但我认为它们不够准确。
无论我向哪个方向(左/右)移动设备,都会发生这种情况。
我想我要的是一种可能以更智能的方式进行计算的方法,这可以提高结果的准确性..
这是我的 onSensorChanged 代码:
public void onSensorChanged(SensorEvent sensorEvent)
if((!isCancelled()) && (this.isConnected))
if(mode.equals("ac"))
// Getting X acceleration
currentAccelX = sensorEvent.values[0];
// Getting current sample timestamp
currentTimeStamp = sensorEvent.timestamp;
// if first sample then interval = 0
if(prevTimeStamp == 0) prevTimeStamp = currentTimeStamp;
// calculating interval (in seconds)
interval = (currentTimeStamp - prevTimeStamp) * NS2S;
// updating prevTimeStamp for next sample..
prevTimeStamp = currentTimeStamp;
// My device shows acceleration of about 0.22 when isn't moving at all
// so for ignoring this offset:
if(Math.abs(currentAccelX) < 0.3) currentAccelX = 0;
// Updating the X-axis velocity
currentVelocityX += currentAccelX * interval;
Log.d(TAG, "ACCEL X IS: " + currentAccelX + " INTERVAL IS: " + interval + " VELOCITY X IS: " + currentVelocityX);
以下是结果(来自日志):
ACCEL X IS: 0.0 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.17563461
ACCEL X IS: 0.5072479 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.17137761
ACCEL X IS: 1.8605042 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.15576364
ACCEL X IS: 2.970047 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.130838
ACCEL X IS: 3.1661987 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.10426621
ACCEL X IS: 2.5031128 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.083259255
ACCEL X IS: 1.4252472 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.07129811
ACCEL X IS: 0.66192627 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.065743
ACCEL X IS: 0.8834839 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.05832851
ACCEL X IS: 1.476593 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.045936447
ACCEL X IS: 2.1182098 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.028159723
ACCEL X IS: 2.5206451 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.0070056263
ACCEL X IS: 2.5039368 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.014008248
ACCEL X IS: 2.0747833 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.03142052
ACCEL X IS: 1.6359253 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.04514975
ACCEL X IS: 1.9437103 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.061462015
ACCEL X IS: 2.5153046 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.08257129
ACCEL X IS: 2.7262726 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.10545108
ACCEL X IS: 2.6504364 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.12769443
ACCEL X IS: 2.7267609 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.15057832
ACCEL X IS: 3.2504425 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.17785712
ACCEL X IS: 2.66333 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.20020866
ACCEL X IS: 1.2032013 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.21030633
ACCEL X IS: 0.0 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.21030633
ACCEL X IS: -0.82492065 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.20338333
ACCEL X IS: -1.1881866 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.19341166
ACCEL X IS: -1.234375 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.18305238
ACCEL X IS: -1.1171875 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.17367657
ACCEL X IS: -0.66615295 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.16808599
ACCEL X IS: 0.6031494 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.17314783
ACCEL X IS: 1.6951294 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.18737392
ACCEL X IS: 2.1244202 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.20520276
ACCEL X IS: 2.0012054 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.22199754
ACCEL X IS: 1.7078094 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.23633005
ACCEL X IS: 1.6888428 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.2505034
ACCEL X IS: 1.7953186 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.2655703
ACCEL X IS: 2.3388367 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.2851986
ACCEL X IS: 3.2739716 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.31267485
ACCEL X IS: 4.067398 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.3468098
ACCEL X IS: 4.221344 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.38223672
ACCEL X IS: 4.281769 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.41817075
ACCEL X IS: 3.0664673 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.44390556
ACCEL X IS: 1.0762634 INTERVAL IS: 0.008392333 VELOCITY X IS: 0.45293793
ACCEL X IS: -0.5091095 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.44866532
ACCEL X IS: -0.82073975 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.4417774
ACCEL X IS: 0.5644531 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.4465145
ACCEL X IS: 3.284546 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.4740795
ACCEL X IS: 4.3601074 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.51067096
ACCEL X IS: 4.3199615 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.54692554
ACCEL X IS: 3.0349426 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.5723958
ACCEL X IS: 1.763977 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.5871997
ACCEL X IS: 1.4712982 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.5995473
ACCEL X IS: 2.1172028 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.6173156
ACCEL X IS: 2.1423798 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.63529515
ACCEL X IS: 1.6801605 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.64939564
ACCEL X IS: 1.8257599 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.66471803
ACCEL X IS: 1.8712311 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.680422
ACCEL X IS: 2.1062775 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.6980986
ACCEL X IS: 2.054016 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.71533656
ACCEL X IS: 0.0 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.71533656
ACCEL X IS: -2.2228088 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.696682
ACCEL X IS: -2.936676 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.6720364
ACCEL X IS: -3.1568298 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.6455432
ACCEL X IS: -2.484604 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.6246916
ACCEL X IS: -2.213089 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.6061186
ACCEL X IS: -1.923996 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.5899718
ACCEL X IS: -2.902832 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.56561023
ACCEL X IS: -5.460327 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.51978534
ACCEL X IS: -7.1407623 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.45985767
ACCEL X IS: -8.03479 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.39242703
ACCEL X IS: -8.921448 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.31755525
ACCEL X IS: -10.293076 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.23117232
ACCEL X IS: -11.660858 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.1333105
ACCEL X IS: -12.821564 INTERVAL IS: 0.008392334 VELOCITY X IS: 0.025707655
ACCEL X IS: -12.108749 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.07591301
ACCEL X IS: -9.302032 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.15397877
ACCEL X IS: -5.889694 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.20340705
ACCEL X IS: -3.0645447 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.22912574
ACCEL X IS: -1.2965088 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.24000648
ACCEL X IS: -0.36557007 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.24307446
ACCEL X IS: 0.0 INTERVAL IS: 0.008392334 VELOCITY X IS: -0.24307446
*请注意,日志中的初始速度不是 0(而是 -0.17563461),因为某些传感器错误会在手机不移动时导致“假”加速度值。
非常感谢。
【问题讨论】:
@creativecreatorormaybenot 问题:计算速度!=真实速度。 【参考方案1】:我认为你应该使用Android API提供的线性加速度传感器:
https://developer.android.com/reference/android/hardware/Sensor.html#TYPE_LINEAR_ACCELERATION
它似乎完全符合您的需求
编辑:要处理精度问题,您可以尝试执行手动校准(手机必须平放在桌子上并且测量必须稳定)以消除偏移。看这里了解更多细节(尤其是结尾):https://electronics.stackexchange.com/a/33387
但您还应该记住,传感器并不完美,尤其是在手机中,这就是为什么使用某些手机的传感器可能无法获得您想要的精度。解决方案是使您的算法处理适应传感器提供的精度, 要么执行传感器融合(例如使用陀螺仪)。
【讨论】:
你是对的,我切换到TYPE_LINEAR_ACCELERATION
,但结果仍然不够准确。我认为,如果我可以修复手机根本不移动时存在的偏移量(大约 0.2 - 0.3),那么它可能会解决我的问题......有什么办法吗?谢谢
我只是针对您的准确性问题进行编辑,希望对您有所帮助。
谢谢,我试试这个校准方法。无论如何,你能详细说明一下这个传感器融合的事情吗?以上是关于Android:使用加速度计的速度计算不正确的主要内容,如果未能解决你的问题,请参考以下文章