Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreMotion Gyroscope apple watch

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")
    }
like image 895
user1602074 Avatar asked Oct 28 '25 10:10

user1602074


1 Answers

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.

like image 185
James Snook Avatar answered Oct 30 '25 14:10

James Snook