-1

I'm building an app that needs to take level pictures. I want to use the gyroscope to make sure the phone is level when taking a picture.

I've gotten the gyroscope updates(which gradually increase even though the device isn't moving) but I can't seem to find out how to get the initial gyroscope cordinates.

How can I get the initial values of the gyroscope?

Here's a condensed version of my code

import UIKit
import CoreMotion

class SizingFlowFrontPhotoViewController: UIViewController, AVCapturePhotoCaptureDelegate {


    var gyroX:Double = 0.0
    var gyroY:Double = 0.0
    var gyroZ:Double = 0.0

    var motionManager = CMMotionManager()

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)


        motionManager.gyroUpdateInterval = 0.2
        motionManager.startGyroUpdates(to: OperationQueue.current!) { (data, error) in
            if let myData = data {
                // x = rotate it on sim card (front flip/back flip)
                // y = rotate it on charger (twister spin)
                // z = rotate it on tilt (landscape vs portrait)
                self.gyroX += myData.rotationRate.x
                self.gyroY += myData.rotationRate.y
                self.gyroZ += myData.rotationRate.z
                print("gyroX: \(self.gyroX) - gyroY: \(self.gyroY) - gyroZ: \(self.gyroZ)")
            }
        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        motionManager.stopGyroUpdates()
    }


}
Trevor Wood
  • 2,347
  • 5
  • 31
  • 56

1 Answers1

0

I found out I was using the wrong device.

I should have been using deviceMotion. Something like this.

motionManager.deviceMotionUpdateInterval = 0.05
        motionManager.startDeviceMotionUpdates(to: OperationQueue.current!) { (data, error) in
            if let myData = data{
                self.mPitch = myData.attitude.pitch * 180 / Double.pi
                self.mRoll = myData.attitude.roll * 180 / Double.pi
                self.mYaw = myData.attitude.yaw * 180 / Double.pi
                self.updateGyroBarPosition()
            }
        }
Trevor Wood
  • 2,347
  • 5
  • 31
  • 56