检测绝对垂直轴上的加速度 [英] Detect acceleration in absolute vertical axis

查看:142
本文介绍了检测绝对垂直轴上的加速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing an app in which I want to access the acceleration data in an absolute vertical axis (i.e., from the ground to the floor), but with CoreMotion I'm only able to retrieve acceleration from the device's relative axis. For example if the device is laying flat on the table, it's vertical axis is actually at 90º from the absolute vertical axis.

我一直在使用陀螺仪数据尝试解决此问题,但数据没有意义。

I've been playing with the gyro data to try to fix this, but data don't make sense.

// 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)


                            }

任何人的帮助将不胜感激

Anyone help would be much appreciated

推荐答案

CMMotionMagager可使您绕iPhone的轴以及重力方向加速。通过将这两者相乘,您可以计算出从地球到地球的加速度。这是相乘结果的长度。我正在使用Ray Wenderlich的3D Vector工具。

CMMotionMagager gives you acceleration about the iPhone's axes and also the direction of gravity. By multiplying these two you can calculate the acceleration from/to the earth. It's the length of the result of this multiplication. I'm using Ray Wenderlich's 3D Vector utils.

在您的GameViewController中:

In your GameViewController:

  var motionManager: CMMotionManager!

在GameViewController.viewDidLoad中:

In GameViewController.viewDidLoad:

  motionManager = CMMotionManager()
  motionManager.startDeviceMotionUpdates()

在GameScene中:

In 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()。

Don't forget to call .stopDeviceMotionUpdates() when you don't need it anymore.

这篇关于检测绝对垂直轴上的加速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆