我尝试使用:“react本机-传感器”库。
import { orientation } from "react-native-sensors";
const subscription = orientation.subscribe(({ qx, qy, qz, qw, pitch, roll, yaw, timestamp }) =>
console.log({ qx, qy, qz, qw, pitch, roll, yaw, timestamp })
);我试着用他们给出的音高、滚动、偏航和从radian_to_degress转换而来的值,仍然与仿真器不同。
它们还为Angles函数提供了一个四元数。
function quaternionToAngles(q) {
let data = q;
let ysqr = data.y * data.y;
let t0 = -2.0 * (ysqr + data.z * data.z) + 1.0;
let t1 = +2.0 * (data.x * data.y + data.w * data.z);
let t2 = -2.0 * (data.x * data.z - data.w * data.y);
let t3 = +2.0 * (data.y * data.z + data.w * data.x);
let t4 = -2.0 * (data.x * data.x + ysqr) + 1.0;
t2 = t2 > 1.0 ? 1.0 : t2;
t2 = t2 < -1.0 ? -1.0 : t2;
const toDeg = 180 / Math.PI;
const euler = {};
euler.pitch = Math.asin(t2) * toDeg;
euler.roll = Math.atan2(t3, t4) * toDeg;
euler.yaw = Math.atan2(t1, t0) * toDeg;
return euler;
}该函数给出了错误的结果,滚动,偏航和俯仰。我不想钻研这方面的数学问题。谷歌搜索给出了100倍不同的方程式。

我正在寻找简单的方程来得到我的设备的Z,Y,X旋转,就像Android模拟器提供的那样。如果Z值是“90”(在模拟器上),我的程序也应该接收相同的值。我可以使用陀螺仪,气压计,接力计等。
PS:我在谷歌上浪费了很多时间,因为一个问题应该在一个google搜索之后通过复制粘贴来解决,所以我希望这个问题能有所帮助。
发布于 2022-02-28 06:07:29
你可以使用‘博览会-传感器’包。
要回答您的问题,可以使用DeviceMotion API或陀螺仪 API。陀螺仪可以直接从它们的文档中使用,因此我提供了一个DeviceMotion示例。
import { DeviceMotion } from 'expo-sensors';
....
....
componentDidMount(){
DeviceMotion.addListener((devicemotionData) => {
console.log(devicemotionData);
// call to setState reflecting the deviceMotion changes.
....
....
});
// set update delay in ms
DeviceMotion.setUpdateInterval(100);
}
componentWillUnmount(){
DeviceMotion.removeAllListeners();
}
....
....侦听器接收的对象将采用以下格式:
{
"acceleration": {
"z": -0.0005327463150024414,
"y": 0.0034074783325195312,
"x": 0.0005932972417213023
},
"accelerationIncludingGravity": {
"z": -0.8134145140647888,
"y": -9.769495010375977,
"x": 0.0011865944834426045
},
"orientation": 0,
"rotation": {
"gamma": -0.000039085680327843875,
"alpha": 0.00004289219214115292,
"beta": 1.4878977537155151
},
"rotationRate": { "gamma": 0, "alpha": 0, "beta": 0 }
}有关使用的更多示例,可以参考这篇文章
我的android设备显示了一种不同的测量方法。也许这能帮你得到扣减:
alpha range : 0 to 360
beta range : -180 to 180
gamma range : in anticlockwise motion,
0 to 90 for 1st quadrant,
90 to 0 for 2nd quadrant,
0 to -90 for 3rd quadrant,
-90 to 0 for 4th quadranthttps://stackoverflow.com/questions/71187397
复制相似问题