Ios CMMotionManager中的不规则时间戳
过去一周对此进行了大量研究,但没有发现类似的问题。我正在收集Apple Watch上的加速计数据,并查看DeviceMotion类输出的时间戳,它提出了一个问题。时间戳并不总是按时间顺序排列。例如,如果我以100Hz的频率采集几分钟的运动数据,我会从同一个应用程序中生成的CSV文件中获得以下时间戳(这些是UTC时间戳): 如果您注意到,在第5个时间戳,时间与第4个时间戳相比大幅向后跳 有人知道这是什么原因吗? 谢谢 这是我的代码:Ios CMMotionManager中的不规则时间戳,ios,swift,ios10,accelerometer,core-motion,Ios,Swift,Ios10,Accelerometer,Core Motion,过去一周对此进行了大量研究,但没有发现类似的问题。我正在收集Apple Watch上的加速计数据,并查看DeviceMotion类输出的时间戳,它提出了一个问题。时间戳并不总是按时间顺序排列。例如,如果我以100Hz的频率采集几分钟的运动数据,我会从同一个应用程序中生成的CSV文件中获得以下时间戳(这些是UTC时间戳): 如果您注意到,在第5个时间戳,时间与第4个时间戳相比大幅向后跳 有人知道这是什么原因吗? 谢谢 这是我的代码: func startUpdates() { motion
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)
}
}
您的代码没有打印到控制台,因此不清楚这些数字代表什么。感谢您的回复-这些数字来自
validData.timestamp+self.bootTime.timeintervalnce1970
,只保存到CSV文件中。我可以将其重写以打印到控制台,看看这是否消除了一个错误源。“并简单地保存到CSV文件”好的,但我在您的代码中没有看到,并且连续保存到文件不是“简单的”。-此外,这可能不相关,但如果你想让运动管理器像这样疯狂地经常给你回电话,在我看来,在主线上这样做似乎会增加疯狂。你的回叫率会有丢失帧的风险,即使是目前的情况…感谢你的反馈-我在最初的问题中添加了用于生成CSV结构的函数。你的第二个评论听起来很有关联——你对如何避免掉框有什么建议吗?使用DISPATCH\u QUEUE\u PRIORITY\u HIGH
?@shimmer\u flight您是否找到了解决方案或原因?在我正在做的事情中看到类似的东西。谢谢
// 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)
}
}