检测绝对垂直轴的加速度

时间:2017-11-02 10:46:49

标签: swift accelerometer core-motion

我正在编写一个应用程序,我想在绝对垂直轴(即从地面到地板)访问加速度数据,但是使用CoreMotion我只能从设备的相对轴检索加速度。例如,如果设备平放在桌子上,它的垂直轴实际上与绝对垂直轴成90°。

我一直在玩陀螺仪数据试图解决这个问题,但数据没有意义。

// Make sure the accelerometer hardware is available.
    if self.motion.isAccelerometerAvailable && self.motion.isDeviceMotionAvailable {
        self.motion.accelerometerUpdateInterval = 1.0 / Double(slider.value)  // Hz managed by slider
        self.motion.startAccelerometerUpdates()
        self.motion.startDeviceMotionUpdates(using: self.motion.attitudeReferenceFrame)
        self.motion.deviceMotionUpdateInterval = 1.0 / Double(slider.value)



        // Configure a timer to fetch the data.
        self.timer = Timer(fire: Date(), interval: (1.0/Double(slider.value)),
                           repeats: true, block: { (timer) in
                            // Get the gyroscope data.

                            if let data = self.motion.deviceMotion {
                                //Angles: pitch
                                pitch = data.attitude.pitch
                                print("Pitch angle: \(self.degrees(radians: pitch))")

                                    //Angles: yaw
                                yaw = data.attitude.yaw
                                print("Yaw angle: \(self.degrees(radians: yaw))")
                            }

                            //Get the accelerometer data

                            if let data = self.motion.accelerometerData {
                                let x = data.acceleration.x
                                //let y = data.acceleration.y
                                //let z = data.acceleration.z


                                // Use the accelerometer data in your app.
                                self.label.text = "x: \(x)"
                                let xCorrectedWithPitch = cos(self.degrees(radians: pitch)) * x
                                print("x: \(x)\nPitch: \(pitch)\nxCorrectedWithPitch: \(xCorrectedWithPitch)")
                                let xCorrectedWithYaw = cos(self.degrees(radians: yaw)) * xCorrectedWithPitch
                                print("x: \(x)\nYaw: \(yaw)\nxCorrectedWithYaw: \(xCorrectedWithYaw)")

                                // Use the accelerometer data in your app.

                                self.accelArrayY.append(xCorrectedWithYaw*9.81)
                                time = time + timer.timeInterval
                                self.timeArrayY.append(time)


                            }

任何帮助都将非常感谢

1 个答案:

答案 0 :(得分:2)

CMMotionMagager为您提供有关iPhone的轴和重力方向的加速度。通过将这两个相乘,您可以计算从地球到地球的加速度。这是乘法结果的长度。我使用Ray Wenderlich的3D Vector utils。

在你的GameViewController中:

  var motionManager: CMMotionManager!

在GameViewController.viewDidLoad中:

  motionManager = CMMotionManager()
  motionManager.startDeviceMotionUpdates()

在GameScene中:

 override func update(_ currentTime: TimeInterval) {

    if let deviceMotion = motionManager.deviceMotion {

    let gravityVector = Vector3(x: CGFloat(deviceMotion.gravity.x),
                                y: CGFloat(deviceMotion.gravity.y),
                                z: CGFloat(deviceMotion.gravity.z))

    let userAccelerationVector = Vector3(x: CGFloat(deviceMotion.userAcceleration.x),
                                         y: CGFloat(deviceMotion.userAcceleration.y),
                                         z: CGFloat(deviceMotion.userAcceleration.z))

    // Acceleration to/from earth
    let zVector = gravityVector * userAccelerationVector
    let zAcceleration = zVector.length()
  }

了解行动方向:

sign(Double(zVector.x * zVector.y * zVector.z))

如果您不再需要,请不要忘记调用.stopDeviceMotionUpdates()。