CoreMotion Gyroscope apple watch

痞子三分冷 提交于 2020-01-04 03:53:20

问题


I'm trying to get access to the gyroscope of the apple watch. From what I read it is available in watchos 3. Unfortunately I cannot get it to work. It keeps coming back with "Gyro not available" so motionManager.isGyroAvailable is always false. Here is my code. Any help would be appreciated.

import WatchKit
import Foundation
import CoreMotion



class InterfaceController: WKInterfaceController {

    let motionManager = CMMotionManager()

    override func awake(withContext context: Any?) {
    super.awake(withContext: context)

    motionManager.gyroUpdateInterval = 0.1

    motionManager.accelerometerUpdateInterval = 0.1
    // Configure interface objects here.
}

override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()
    if (motionManager.isGyroAvailable == true) {
        motionManager.startGyroUpdates(to: OperationQueue.current!, withHandler: { (data, error) -> Void in
            guard let data = data else { return }
            let rotationX = data.rotationRate.x
            let rotationY = data.rotationRate.y
            let rotationZ = data.rotationRate.z
            // do you want to want to do with the data
            print(rotationX)
            print(rotationY)
            print(rotationZ)
        })
    } else {
        print("Gyro not available")
    }

回答1:


From my experience (although I can't find it documented anywhere) raw gyroscope data isn't available on the watch, only the processed data. You can access the processed data using the CMMotionManager method:

startDeviceMotionUpdates(to queue: OperationQueue, withHandler handler: @escaping CMDeviceMotionHandler)

The CMDeviceMotion object in the handler has detailed rotation data, for instance the rotation rate, the documentation for this states that it's processed data from the gyroscope. There is also attitude data.



来源:https://stackoverflow.com/questions/42650961/coremotion-gyroscope-apple-watch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!