Android Sensor Simulator SDK - Accelerometer, Gyroscope, Magnetometer calculation based on official Android formulas
npm install @vmosedge/sensor-simulatorAndroid 传感器模拟器 SDK,用于在浏览器或 Node 环境中根据旋转/位移输入计算传感器数据。
``bash`
npm install @vmosedge/sensor-simulatoror
pnpm add @vmosedge/sensor-simulator
`ts
import { SensorSimulator, DeviceOrientation } from '@vmosedge/sensor-simulator'
const simulator = new SensorSimulator()
// 监听数据变化
simulator.on('change', (sensors) => {
console.log('accelerometer', sensors.accelerometer)
console.log('gyroscope', sensors.gyroscope)
console.log('magnetometer', sensors.magnetometer)
})
// 设置旋转角度 (度)
simulator.setRotation({ x: 10, y: 20, z: 30 })
// 设置设备方向 (0/90/180/270)
simulator.setDeviceOrientation(DeviceOrientation.LANDSCAPE_LEFT)
`
`ts`
// X: Pitch, Y: Roll, Z: Yaw (单位: 度)
simulator.setRotation({
x: 15,
y: -10,
z: 45
})
`ts
// 位移 (单位: m)
simulator.setPosition({
x: 1.5,
y: 0.5,
z: -0.3
})
// 需要在动画循环中调用 update 处理线性加速度衰减
let last = performance.now()
const animate = () => {
const now = performance.now()
const deltaSeconds = (now - last) / 1000
last = now
simulator.update(deltaSeconds)
requestAnimationFrame(animate)
}
animate()
`
`ts`
simulator.setDeviceOrientation(DeviceOrientation.PORTRAIT)
simulator.setDeviceOrientation(DeviceOrientation.LANDSCAPE_LEFT)
simulator.setDeviceOrientation(DeviceOrientation.PORTRAIT_REVERSE)
simulator.setDeviceOrientation(DeviceOrientation.LANDSCAPE_RIGHT)
- rotation: 度 (deg)
- position: 米 (m)
- accelerometer: m/s^2
- gyroscope: rad/s
- magnetometer: uT
- rotationVector: 四元数
`ts``
const simulator = new SensorSimulator({
gravity: 9.80665,
magneticField: { x: 0, y: 5.9, z: -48.4 },
linearDecaySeconds: 0.08,
maxLinearAccel: 34.3,
linearEpsilon: 0.01
})
SDK 仅负责传感器数值计算,不包含网络请求与 UI 渲染。