检测iPhone/Apple Watch的身体运动 [英] Detect physical movement of iPhone/Apple Watch

查看:199
本文介绍了检测iPhone/Apple Watch的身体运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测用户执行的移动(向右或向左). 我们假设用户从他的手臂在他的前方伸出来开始,然后将他的手臂向右或向左移动(偏离中心大约90度).

I'm trying to detect the movement (to the right or left) performed by users. We assume that the user starts with his arm extended in front of him and then moves his arm to the right or to the left (about 90 degrees off center).

我已经集成了CMMotionManager,并且想了解通过startAccelerometerUpdatesToQueuestartDeviceMotionUpdatesToQueue方法检测方向的方法.

I've integrated CMMotionManager and want to understand detecting direction via startAccelerometerUpdatesToQueue and startDeviceMotionUpdatesToQueue methods.

有人可以建议如何在iPhone上然后在Apple Watch上实现此逻辑吗?

Can anyone suggest how to implement this logic on an iPhone and then on an Apple Watch?

推荐答案

Apple提供watchOS 3

Apple provides watchOS 3 SwingWatch sample code demonstrating how to use CMMotionManager() and startDeviceMotionUpdates(to:) to count swings in a racquet sport.

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

Their code demonstrates how to detect the direction of a one-second interval of motion, although you may have to tweak the thresholds to account for the characteristics of the motion you want to track.

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()
    }
}

这篇关于检测iPhone/Apple Watch的身体运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆