 
 class MScannerMotion
 {
+    private enum MotionOrientation
+    {
+        case portrait
+        case landscapeRight
+        case landscapeLeft
+    }
+    
     private weak var controller:CScanner!
+    private var motionOrientation:MotionOrientation
     private let manager:CMMotionManager!
-    private let kUpdatesInterval:TimeInterval = 0.09
+    private let kRotationThreshold:Double = 2.7
+    private let k180Deg:Float = 180
+    private let kUpdatesInterval:TimeInterval = 0.07
     
     init(controller:CScanner)
     {
         self.controller = controller
+        motionOrientation = MotionOrientation.portrait
         
         let manager:CMMotionManager = CMMotionManager()
         manager.deviceMotionUpdateInterval = kUpdatesInterval

        
        if zRotationFloat >= 0
        {
            normalizedZRotation = -(180 - zRotationFloat)
        }
        else
        {
            normalizedZRotation = 180 + zRotationFloat
        }
        
        if rawRotation >= 0
        {
            if rawRotation < 2.7
            {
                controller.modelGPS?.changeOrientationLandscapeRight()
            }
            else
            {
                controller.modelGPS?.changeOrientationPortrait()
            }
        }
        else
        {
            if rawRotation > -2.7
            {
                controller.modelGPS?.changeOrientationLandscapeLeft()
            }
            else
            {
                controller.modelGPS?.changeOrientationPortrait()
            }
        }
        

        if manager.isDeviceMotionAvailable
        {
            manager.startDeviceMotionUpdates(
                using:CMAttitudeReferenceFrame.xArbitraryZVertical,
                to:OperationQueue())
            { (data:CMDeviceMotion?, error:Error?) in
                
                guard
                    
                    let dataMotion:CMDeviceMotion = data
                    
                else
                {
                    return
                }
                
                let acceleration:CMAcceleration = dataMotion.gravity
                
                DispatchQueue.global(qos:DispatchQoS.QoSClass.background).async
                { [weak self] in
                    
                    self?.gravityCompute(acceleration:acceleration)
                }
            }
        }
    }
    
    //MARK: private
    
    private func orientationPortrait()
    {
        if motionOrientation != MotionOrientation.portrait
        {
            motionOrientation = MotionOrientation.portrait
            controller.modelGPS?.changeOrientationPortrait()
        }
    }
    
    private func orientationLandscapeLeft()
    {
        if motionOrientation != MotionOrientation.landscapeLeft
        {
            motionOrientation = MotionOrientation.landscapeLeft
            controller.modelGPS?.changeOrientationLandscapeLeft()
        }
    }
    
    private func orientationLandscapeRight()
    {
        if motionOrientation != MotionOrientation.landscapeRight
        {
            motionOrientation = MotionOrientation.landscapeRight
            controller.modelGPS?.changeOrientationLandscapeRight()
        }
    }
    
    private func gravityCompute(acceleration:CMAcceleration)
    {
