检测iPhone/Apple Watch的物理运动

检测iPhone/Apple Watch的物理运动,iphone,physics,accelerometer,apple-watch,core-motion,Iphone,Physics,Accelerometer,Apple Watch,Core Motion,我试图检测用户执行的移动(向右或向左)。 我们假设用户开始时手臂向前伸展,然后将手臂向右或向左移动(大约偏离中心90度) 我集成了CMMotionManager,希望通过startacceleometerupdatestoqueue和startDeviceMotionUpdatesToQueue方法了解检测方向 有人能建议如何在iPhone和Apple Watch上实现这种逻辑吗?苹果提供了watchOS 3,演示如何使用CMMotionManager()和StartDeviceMotionUp

我试图检测用户执行的移动(向右或向左)。 我们假设用户开始时手臂向前伸展,然后将手臂向右或向左移动(大约偏离中心90度)

我集成了
CMMotionManager
,希望通过
startacceleometerupdatestoqueue
startDeviceMotionUpdatesToQueue
方法了解检测方向

有人能建议如何在iPhone和Apple Watch上实现这种逻辑吗?

苹果提供了watchOS 3,演示如何使用
CMMotionManager()
StartDeviceMotionUpdate(to:)
来计算网拍运动中的摆动次数

他们的代码演示了如何检测一秒钟运动间隔的方向,尽管您可能需要调整阈值以考虑要跟踪的运动的特征

func processDeviceMotion(_ deviceMotion: CMDeviceMotion) {
    let gravity = deviceMotion.gravity
    let rotationRate = deviceMotion.rotationRate

    let rateAlongGravity = rotationRate.x * gravity.x // r⃗ · ĝ
                         + rotationRate.y * gravity.y
                         + rotationRate.z * gravity.z
    rateAlongGravityBuffer.addSample(rateAlongGravity)

    if !rateAlongGravityBuffer.isFull() {
        return
    }

    let accumulatedYawRot = rateAlongGravityBuffer.sum() * sampleInterval
    let peakRate = accumulatedYawRot > 0 ?
        rateAlongGravityBuffer.max() : rateAlongGravityBuffer.min()

    if (accumulatedYawRot < -yawThreshold && peakRate < -rateThreshold) {
        // Counter clockwise swing.
        if (wristLocationIsLeft) {
            incrementBackhandCountAndUpdateDelegate()
        } else {
            incrementForehandCountAndUpdateDelegate()
        }
    } else if (accumulatedYawRot > yawThreshold && peakRate > rateThreshold) {
        // Clockwise swing.
        if (wristLocationIsLeft) {
            incrementForehandCountAndUpdateDelegate()
        } else {
            incrementBackhandCountAndUpdateDelegate()
        }
    }

    // Reset after letting the rate settle to catch the return swing.
    if (recentDetection && abs(rateAlongGravityBuffer.recentMean()) < resetThreshold) {
        recentDetection = false
        rateAlongGravityBuffer.reset()
    }
}
func processDeviceMotion(\deviceMotion:CMDeviceMotion){
让重力=设备运动。重力
让rotationRate=deviceMotion.rotationRate
让rateAlongGravity=旋转速率.x*重力.x//r⃗ · ĝ
+旋转率y*重力y
+旋转率.z*重力.z
rateAlongGravityBuffer.addSample(rateAlongGravity)
if!rateAlongGravityBuffer.isFull(){
返回
}
设累计YAWROT=rateAlongGravityBuffer.sum()*采样间隔
让peakRate=累计的yawrot>0?
rateAlongGravityBuffer.max():rateAlongGravityBuffer.min()
如果(累计的雅沃特<-雅特阈值和峰值<-费率阈值){
//逆时针摆动。
if(左手腕){
递增BackHandCountandUpdateLegate()
}否则{
递增的ForehandCountandUpdateLegate()
}
}否则,如果(累计的yawrot>yawThreshold&peakRate>rateshold){
//顺时针摆动。
if(左手腕){
递增的ForehandCountandUpdateLegate()
}否则{
递增BackHandCountandUpdateLegate()
}
}
//让速率稳定后重置,以捕捉回摆。
if(最近检测和abs(rateAlongGravityBuffer.recentMean())