Currently I'm working on an app which needs to detect if the iPhone starts and stops moving vertically. I need to be able to detect a pretty short (50-100 cm) vertical distance traveled, i.e. if a person performs a squat.
Is there a way to calculate that the Core Motion framework?
let motion = CMMotionManager()
if motion.isDeviceMotionAvailable {
self.motion.deviceMotionUpdateInterval = 1.0 / 60.0
self.motion.showsDeviceMovementDisplay = true
self.motion.startDeviceMotionUpdates(using: .xMagneticNorthZVertical, to: .main, withHandler: { (data, error) in
if let validData = data {
// Just a random minimum acceleration threshold
if validData.userAcceleration.y > 3 {
print(validData.userAcceleration.y)
}
}
})
}
Is there a way to calculate vertical distance traveled with the Core Motion framework?
Core Motion can detect attitude (how the phone is oriented) and acceleration (how the phone starts or stops moving, speeds up or slows down). A smooth vertical movement would not register at all. You might know that the movement started and ended, but not how far apart those events occurred. You might guess something about that based on the initial acceleration (which could let you calculate how fast we got going) and the time before deceleration. But it wouldn’t be very much more than a guess.
So you're saying it's not technically possible to detect if a person makes a full squat?
There is no “squat” in your question. I say nothing about that. I'm saying what I’m saying. There is nothing inherent in the phone sensors that tells you the phone is now located 2 feet closer to the ground. If the squat is fast and sudden enough you can sense the start and stop as jerks and guess that it is a squat. You could certainly count those gestures. But you cannot be certain it’s a squat or know exactly how deep it is.
I edited my question. Thank you for taking your time.
Your edit doesn't change anything about my answer. You ask what the CoreMotion framework is capable of detecting and I've answered. You will need to collect the data into a buffer and analyze it for acceleration and deceleration and use the amounts and the intervening time to guess that this is a squat and what the distance travelled over that time might be, as I said in my answer. If I were you I would start by doing some squats yourself, collecting the data, so that you can see what the data looks like.