Hello,
I am trying to access the motion rotation rate on an Apple Watch and store up to 50 consecutive readings in an array. Currently, the readings stop suddenly after 32 readings. I have pretty much the exact same code for acceleration and roll, and the code for those sensors work just fine. Any insight into why this is happening?
struct ModelConstants {
static let predictionWindowSize = 50
static let sensorsUpdateInterval = 1.0 / 50.0
}
let motionManager = CMMotionManager()
func MyGyro(){
guard motionManager.isDeviceMotionAvailable else {
print("Device Motion is not available on the device")
return }
motionManager.deviceMotionUpdateInterval = TimeInterval(ModelConstants.sensorsUpdateInterval)
motionManager.startDeviceMotionUpdates(using: .xMagneticNorthZVertical, to: OperationQueue.current!){ [self] (deviceData, error) in
if let gyroDeviceData = deviceData {
self.gyroX.setText("\(gyroDeviceData.rotationRate.x)")
self.gyroY.setText("\(gyroDeviceData.rotationRate.y)")
self.gyroZ.setText("\(gyroDeviceData.rotationRate.z)")
// Add the current data sample to the data array
self.addGyroSampleToDataArray(deviceMotionSample: gyroDeviceData)
}
}
}
func addGyroSampleToDataArray (deviceMotionSample: CMDeviceMotion) {
// Add the current gyroscope reading to the data array
gyroDataX.append(deviceMotionSample.rotationRate.x)
gyroDataY.append(deviceMotionSample.rotationRate.y)
gyroDataZ.append(deviceMotionSample.rotationRate.z)
if (gyroDataZ.count == ModelConstants.predictionWindowSize) {
gyroDataX_mean = Double(gyroDataX.mean)
gyroDataY_mean = Double(gyroDataY.mean)
gyroDataZ_mean = Double(gyroDataZ.mean)
gyroDataX_std = Double(gyroDataX.sd)
gyroDataY_std = Double(gyroDataY.sd)
gyroDataZ_std = Double(gyroDataZ.sd)
gyroDataX_min = Double(gyroDataX.min() ?? 0)
gyroDataY_min = Double(gyroDataY.min() ?? 0)
gyroDataZ_min = Double(gyroDataZ.min() ?? 0)
gyroDataX_max = Double(gyroDataX.max() ?? 0)
gyroDataY_max = Double(gyroDataY.max() ?? 0)
gyroDataZ_max = Double(gyroDataZ.max() ?? 0)
gyroDataX.removeAll()
gyroDataY.removeAll()
gyroDataZ.removeAll()
}
}
func MyAccel(){
guard motionManager.isAccelerometerAvailable else { return }
motionManager.accelerometerUpdateInterval = TimeInterval(ModelConstants.sensorsUpdateInterval)
motionManager.startAccelerometerUpdates(to: OperationQueue.current!){ (accelerometerData, error) in
if let accelerometerData = accelerometerData {
self.accelX.setText("\(accelerometerData.acceleration.x)")
self.accelY.setText("\(accelerometerData.acceleration.y)")
self.accelZ.setText("\(accelerometerData.acceleration.z)")
// Add the current data sample to the data array
self.addAccelSampleToDataArray(accelSample: accelerometerData)
}
}
}
func addAccelSampleToDataArray (accelSample: CMAccelerometerData) {
// Add the current accelerometer reading to the data array
accelDataX.append(accelSample.acceleration.x)
accelDataY.append(accelSample.acceleration.y)
accelDataZ.append(accelSample.acceleration.z)
if (accelDataZ.count == ModelConstants.predictionWindowSize) {
accelDataX_mean = Double(accelDataX.mean)
accelDataY_mean = Double(accelDataY.mean)
accelDataZ_mean = Double(accelDataZ.mean)
accelDataX_std = Double(accelDataX.sd)
accelDataY_std = Double(accelDataY.sd)
accelDataZ_std = Double(accelDataZ.sd)
accelDataX_min = Double(accelDataX.min() ?? 0)
accelDataY_min = Double(accelDataY.min() ?? 0)
accelDataZ_min = Double(accelDataZ.min() ?? 0)
accelDataX_max = Double(accelDataX.max() ?? 0)
accelDataY_max = Double(accelDataY.max() ?? 0)
accelDataZ_max = Double(accelDataZ.max() ?? 0)
accelDataX.removeAll()
accelDataY.removeAll()
accelDataZ.removeAll()
}
}
override func awake(withContext context: Any?) {
// Configure interface objects here.
UNUserNotificationCenter.current().delegate = self
MyGyro()
MyAccel()
MyRoll()
self.scheduleNotifications(content_body: self.pred_action)
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
UNUserNotificationCenter.current().delegate = self
MyGyro()
MyAccel()
MyRoll()
self.scheduleNotifications(content_body: self.pred_action)
}
}