Display data from the gyroscope on Apple Watch

Hello,

I'm new to Apple Watch development.

I would like to display live data from the gyroscope on the watch.

I tried with CoreMotion (CMMotionManager) but I cannot recover the data.

Someone would have any idea ?

Thanks

Replies

    theManager = [[CMMotionManager alloc] init];
    [theManager startAccelerometerUpdates];
    theManager.deviceMotionUpdateInterval=0.1;
    [theManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical 
                toQueue:[NSOperationQueue mainQueue] 
                withHandler:^(CMDeviceMotion *motion,NSError*error){
                   
                    //  code here that involves things like
                   //       theManager.accelerometerData.acceleration.x
                   //    or
                   //          [self someOtherMethod];
                
                
                }
      ];

Hi, thanks for your time but i don't understand yout code...

I have this :


@IBOutlet weak var dataX: WKInterfaceLabel!
@IBOutlet weak var dataY: WKInterfaceLabel!
@IBOutlet weak var dataZ: WKInterfaceLabel!

@IBAction func buttonStart() {
        motionManager.gyroUpdateInterval = 0.2
        motionManager.startGyroUpdates()
   
        if let data = motionManager.gyroData {
            self.dataX.setText("\(data.rotationRate.x)")
            self.dataY.setText("\(data.rotationRate.y)")
            self.dataZ.setText("\(data.rotationRate.z)")
        }
    }

can you modify this for it work ?

I want write the data of gyroscope in the 3 label when i tap on a button on watch.


Thanks

I program in Objective C not Swift. Blame Apple for dividing the 'help' community into 2 camps as did God by the tower of Babel.


Your code is examining the gyroscope data only once. You need to do that repeatedly. You need to do one of two things:

1) convert to the start gyroscope with completion handler :

https://developer.apple.com/documentation/coremotion/cmmotionmanager/1616104-startgyroupdatestoqueue?language=objc

That will result in the completionhandler being called over and over again each time the gyroscope has new data.


or


2) create a timer that continually monitors the value of the gyroscope. You can do that with NSTimer or with

perform(_:with:afterDelay:)

https://developer.apple.com/documentation/objectivec/nsobject/1416176-perform