过去一周对此进行了大量研究,但找不到类似的问题。我正在Apple Watch上收集加速度计数据,并查看DeviceMotion类输出的时间戳,这是一个问题。时间戳记并不总是按时间顺序排列。例如,如果我以100Hz的频率收集了几分钟的运动数据,我将从在同一应用中生成的CSV文件中获得以下时间戳(这些时间戳是UTC时间戳):
1531099702.768570
1531099702.778460
1531099702.788380
1531099702.798270
1531099702.738870
1531099702.748780
1531099702.758670
1531099702.768590
如果您注意到,在第5个时间戳记上,时间比第4个时间戳记回跳了很多。
有人知道是什么原因造成的吗? 谢谢!
这是我的代码:
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()
}
}
})
}
}
将accArray中的所有数据插入到CSV文件中,该文件以以下方式生成:
// 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)
}
}