在Javascript中将移动加速度计X / Y / Z规范化为地球坐标不起作用

我正在使用GryoNormlogging加速计和设备的方向。 我的目标是确保我能够在X / Y / Z上获得加速(不加合并,所以我不想做sqrt (x^2 + y^2 + z^2)而不pipe电话是怎么样的保存,用例是在汽车中跟踪传感器数据(检测不良驾驶模式)

我提到这个线程(和这个线程),我认为我做的是正确的,也就是说,

  • 获取acc数据,
  • 定位,
  • 得到rotationMatrix&颠倒它,
  • 乘以acc。 数据与倒转旋转matrix来获取地球数据

但是,我的结果不是独立于手机的方向

我的设备:iPhone 7,iOS 10.3.3

首先是代码:

 import {mat3} from 'gl-matrix'; let gyroNormArgs = { gravityNormalized:false, // tried true too orientationBase:GyroNorm.WORLD, // tried .GAME too frequency:1000, screenAdjusted:false, } 

当我得到acc时,在callback里面。 数据和方向数据(gdata):

  let alpha = gdata.dm.alpha; let beta = gdata.dm.beta; let gamma = gdata.dm.gamma; let rotatematrix = this.getRotationMatrix(alpha, beta, gamma); let relativeacc = new Array(3); let earthacc = new Array(3); let inv = new Array(9) relativeacc[0] = gdata.dm.gx; relativeacc[1] = gdata.dm.gy; relativeacc[2] = gdata.dm.gy; mat3.invert(inv,rotatematrix); mat3.multiply(earthacc, inv, relativeacc); let accEarthX = earthacc[0]; let accEarthY = earthacc[1]; let accEarthZ = earthacc[2]; console.log (`---RAW DATA --- ` + JSON.stringify(gdata)); console.log (`*** EARTH DATA X=${accEarthX}, Y=${accEarthY} Z=${accEarthZ}`) 

旋转matrix:

 // credit:https://stackoverflow.com/a/36662093/1361529 getRotationMatrix(alpha, beta, gamma) { let out = []; let _z = alpha; let _x = beta; let _y = gamma; let cX = Math.cos( _x ); let cY = Math.cos( _y ); let cZ = Math.cos( _z ); let sX = Math.sin( _x ); let sY = Math.sin( _y ); let sZ = Math.sin( _z ); out[0] = cZ * cY + sZ * sX * sY, // row 1, col 1 out[1] = cX * sZ, // row 2, col 1 out[2] = - cZ * sY + sZ * sX * cY , // row 3, col 1 out[3] = - cY * sZ + cZ * sX * sY, // row 1, col 2 out[4] = cZ * cX, // row 2, col 2 out[5] = sZ * sY + cZ * cY * sX, // row 3, col 2 out[6] = cX * sY, // row 1, col 3 out[7] = - sX, // row 2, col 3 out[8] = cX * cY // row 3, col 3 return out }; 

这里是我的输出(电话正面朝上,桌子上,内部testing):

 2017-09-10 19:48:34.458837-0400 TripData[6642:2247482] ---RAW DATA --- {"do":{"alpha":242.09,"beta":0.4,"gamma":-0.8,"absolute":false},"dm":{"x":0,"y":0,"z":0.13,"gx":-0.13,"gy":-0.07,"gz":-9.67,"alpha":0.02,"beta":0.05,"gamma":0}} 2017-09-10 19:48:34.458906-0400 TripData[6642:2247482] *** EARTH DATA X=-0.13144212418873122, Y=-0.07079655168384706 Z=-0.06641397637870017 2017-09-10 19:48:35.461572-0400 TripData[6642:2247482] ---RAW DATA --- {"do":{"alpha":242.1,"beta":0.4,"gamma":-0.8,"absolute":false},"dm":{"x":0,"y":0,"z":0.12,"gx":-0.13,"gy":-0.07,"gz":-9.69,"alpha":0.02,"beta":-0.07,"gamma":-0.06}} 2017-09-10 19:48:35.461715-0400 TripData[6642:2247482] *** EARTH DATA X=-0.13524640367025567, Y=-0.06279441851157147 Z=-0.06682268550466884 

这里是我的输出(电话面朝下,在桌子上,在家里testing):

 2017-09-10 19:49:32.592124-0400 TripData[6642:2247482] ---RAW DATA --- {"do":{"alpha":65.79,"beta":179.79,"gamma":-1,"absolute":false},"dm":{"x":0,"y":0.01,"z":0.1,"gx":-0.04,"gy":-0.16,"gz":9.9,"alpha":0.06,"beta":0.06,"gamma":-0.01}} 2017-09-10 19:49:32.592262-0400 TripData[6642:2247482] *** EARTH DATA X=-0.05167393815904252, Y=-0.16648286542066582 Z=-0.14971058625381142 2017-09-10 19:49:33.598502-0400 TripData[6642:2247482] ---RAW DATA --- {"do":{"alpha":65.8,"beta":179.79,"gamma":-1,"absolute":false},"dm":{"x":-0.01,"y":0,"z":0.09,"gx":-0.05,"gy":-0.17,"gz":9.9,"alpha":-0.01,"beta":-0.06,"gamma":0.11}} 2017-09-10 19:49:33.598629-0400 TripData[6642:2247482] *** EARTH DATA X=-0.02944164169071515, Y=-0.1595350723662996 Z=-0.18434139638083227 2017-09-10 19:49:34.594939-0400 TripData[6642:2247482] ---RAW DATA --- {"do":{"alpha":65.8,"beta":179.79,"gamma":-1,"absolute":false},"dm":{"x":-0.01,"y":0,"z":0.11,"gx":-0.04,"gy":-0.17,"gz":9.91,"alpha":-0.07,"beta":-0.06,"gamma":0.05}} 2017-09-10 19:49:34.595080-0400 TripData[6642:2247482] *** EARTH DATA X=-0.020228116574781345, Y=-0.16120266970200792 Z=-0.18167146881330154 

这里是我的输出(电话面朝上,有点垂直于桌子,在家里testing):

 2017-09-10 19:50:52.147809-0400 TripData[6642:2247482] ---RAW DATA --- {"do":{"alpha":221.25,"beta":83.19,"gamma":5.78,"absolute":false},"dm":{"x":-0.02,"y":0.02,"z":-0.04,"gx":0.1,"gy":-9.71,"gz":-1.19,"alpha":1.33,"beta":0.21,"gamma":-0.25}} 2017-09-10 19:50:52.147943-0400 TripData[6642:2247482] *** EARTH DATA X=-11.682112584452096, Y=-0.49477805209007863 Z=-7.201502637877455 2017-09-10 19:50:53.147058-0400 TripData[6642:2247482] ---RAW DATA --- {"do":{"alpha":221.15,"beta":83.36,"gamma":5.9,"absolute":false},"dm":{"x":-0.03,"y":0.03,"z":0.02,"gx":0.09,"gy":-9.72,"gz":-1.11,"alpha":-0.69,"beta":0.39,"gamma":-0.11}} 2017-09-10 19:50:53.147192-0400 TripData[6642:2247482] *** EARTH DATA X=7.309035050034833, Y=-10.39162106453064 Z=-5.249487431032232 2017-09-10 19:50:54.147017-0400 TripData[6642:2247482] ---RAW DATA --- {"do":{"alpha":221.87,"beta":83.48,"gamma":5.16,"absolute":false},"dm":{"x":0,"y":0.04,"z":-0.01,"gx":0.1,"gy":-9.7,"gz":-1.12,"alpha":0.47,"beta":-1.02,"gamma":0.42}} 2017-09-10 19:50:54.147151-0400 TripData[6642:2247482] *** EARTH DATA X=4.71089937390668, Y=0.3389442491438679 Z=-12.879539738860931 2017-09-10 19:50:55.147900-0400 TripData[6642:2247482] ---RAW DATA --- {"do":{"alpha":221.78,"beta":83.58,"gamma":5.25,"absolute":false},"dm":{"x":-0.02,"y":0.01,"z":-0.01,"gx":0.08,"gy":-9.73,"gz":-1.1,"alpha":-1.12,"beta":0.39,"gamma":-0.29}} 2017-09-10 19:50:55.148033-0400 TripData[6642:2247482] *** EARTH DATA X=10.120001205152024, Y=-7.904409539460901 Z=-4.945511645947363 2017-09-10 19:50:56.177822-0400 TripData[6642:2247482] ---RAW DATA --- {"do":{"alpha":231.77,"beta":83.98,"gamma":-2.92,"absolute":false},"dm":{"x":-0.02,"y":0.18,"z":0.11,"gx":-0.08,"gy":-9.58,"gz":-0.92,"alpha":-9.31,"beta":0.07,"gamma":0.38}} 

如上所见,读数取决于方向和angular度。 我错过了什么? 谢谢。