我正在尝试计算物理设备的速度
在谷歌我得到了
-
使用通过
CLLocationManager
//我不想使用 -
使用
UIAccelerometer
类:去掉
到目前为止,我已经尝试过这样
func coreMotion() { // CALLING FROM VIEW DID LOAD
if self.motionManager.isDeviceMotionAvailable {
self.motionManager.deviceMotionUpdateInterval = 0.5
self.motionManager.startDeviceMotionUpdates(to: OperationQueue.current!,
withHandler: { [weak self] (deviceMotion, error) -> Void in
if let error = error {
print("ERROR : (error.localizedDescription)")
}
if let deviceMotion = deviceMotion {
self?.handleDeviceMotionUpdate(deviceMotion)
}
})
} else {
print("WHAT THE HELL")
}
}
func handleDeviceMotionUpdate(_ deviceMotion: CMDeviceMotion) {
let attitude = deviceMotion.attitude
let roll = self.degrees(attitude.roll)
let pitch = self.degrees(attitude.pitch)
let yaw = self.degrees(attitude.yaw)
let accl = deviceMotion.userAcceleration
self.calculateSpeed(accl)
self.previousAccl = accl
print("Roll: (roll), Pitch: (pitch), Yaw: (yaw)")
print("ACCELRATION: (accl.x) (accl.y) (accl.z)")
}
func degrees(_ radians: Double) -> Double {
return 180 / Double.pi * radians
}
我也得到了加速对象,即用户加速 我如何从中计算速度?
要计算设备的速度,有两种可能性:
1 - 通过近似位置的导数函数:对于两个位置和这两个位置之间的时间,您可以估计速度。
2 - 或计算加速度的基元。但要考虑到,只有当您知道速度à t_0(测量的开始)时,此方法才会为您提供正确的速度值
但是,如果您坚持使用加速度来执行此操作,则可以以t_i计算速度(其中i是您从加速度计收到的更新数)
速度 (t_i) = 速度 (t_i-1) + 加速度 (t_i) * (t_i -t_i-1)和速度(t_0)应该是已知的
这样,您必须在每次从加速度计更新时执行speed = speed + acceleration * (lastUpdateTime - currentTime)
[编辑]
这确实就像您在评论中提到的那样,如果您希望计算所有三个维度的速度,则必须为每个轴执行此操作三次
speedX = speedX + accelerationX * (lastUpdateTime - currentTime)
speedY = speedY + accelerationY * (lastUpdateTime - currentTime)
speedZ = speedZ + accelerationZ * (lastUpdateTime - currentTime)
您需要了解 t_0处的 speedX/Y/Z 才能以正确的值初始化您的 var。