Irregular timestamp in CMMotionManager

2019-07-19 05:17发布

Researched this a lot for the past week but could not find a similar issue. I am collecting accelerometer data on the Apple Watch and looking at the timestamp outputted by the the DeviceMotion class which presents an issue. The timestamps do not always go in chronoligcal order. For example, if I collect a few minutes of motion data at 100Hz, I get the following timestamps from a CSV file I generate in the same app (these are UTC timestamps):

1531099702.768570
1531099702.778460
1531099702.788380
1531099702.798270
1531099702.738870
1531099702.748780
1531099702.758670
1531099702.768590

If you notice, at the 5th timestamp, the time has jumped back by a significant amount compared to the 4th one.

Does anyone know what might cause this? Thanks!

This is my code:

func startUpdates() {
    motionManager = CMMotionManager()
    if motionManager.isDeviceMotionAvailable {
        self.motionManager.deviceMotionUpdateInterval = updateInterval
        self.motionManager.showsDeviceMovementDisplay = true
        self.motionManager.startDeviceMotionUpdates(using: .xArbitraryZVertical, to: .main, withHandler: { (data, error) in
            // Make sure the data is valid before accessing it.
            if let validData = data {
                // find boot time of device to normalize timestamps
                if (self.accArray.isEmpty) {
                    self.bootTime = Date(timeIntervalSinceNow:-validData.timestamp)
                }

                self.accArray.append([validData.timestamp + self.bootTime.timeIntervalSince1970,
                                      validData.attitude.pitch,
                                      validData.attitude.roll,
                                      validData.attitude.yaw,
                                      validData.userAcceleration.x,
                                      validData.userAcceleration.y,
                                      validData.userAcceleration.z,
                                      validData.rotationRate.x,
                                      validData.rotationRate.y,
                                      validData.rotationRate.z,
                                      validData.gravity.x,
                                      validData.gravity.y,
                                      validData.gravity.z])

                if (self.accArray.count == 100) {
                    let accChunk = NSKeyedArchiver.archivedData(withRootObject: self.accArray)
                    watchSessionManager.sharedManager.sendMessageData(accChunk: accChunk)
                    self.reset()
                }
            }
        })
    }
}

All data from accArray is inserted into a CSV file which is generated in the following manner:

// Create CSV with collected array
    func createCSV() -> String {
        if(self.accArray.count == 0) {
            return("No data captured")
        }
        else {
            // Create CSV structure
            var csvText = "Number,Pitch,Roll,Yaw,RotX,RotY,RotZ,GravX,GravY,GravZ,AccX,AccY,AccZ,Time\n"
            for index in 0...accArray.count-1 {
                let newLine = [String(index),
                               String(accArray[index][1]),
                               String(accArray[index][2]),
                               String(accArray[index][3]),
                               String(accArray[index][4]),
                               String(accArray[index][5]),
                               String(accArray[index][6]),
                               String(accArray[index][7]),
                               String(accArray[index][8]),
                               String(accArray[index][9]),
                               String(accArray[index][10]),
                               String(accArray[index][11]),
                               String(accArray[index][12]),
                               String(accArray[index][0])].joined(separator: ",")
                // Insert data into CSV file
                csvText.append(newLine + "\n")
            }
            self.reset()
            return(csvText)
        }
    }

0条回答
登录 后发表回答