feat(watch): improve lap detection with CMDeviceMotion and low-pass filtering
This commit is contained in:
@@ -393,32 +393,44 @@ class WorkoutManager: NSObject, ObservableObject, HKLiveWorkoutBuilderDelegate {
|
|||||||
healthStore.execute(query)
|
healthStore.execute(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Lap Detection (Accelerometer)
|
// MARK: - Lap Detection (CMDeviceMotion)
|
||||||
|
|
||||||
|
private var filteredUserAccel: Double = 0
|
||||||
|
private var filteredRotationX: Double = 0
|
||||||
|
private let filterAlpha: Double = 0.3
|
||||||
|
|
||||||
private func startLapDetection() {
|
private func startLapDetection() {
|
||||||
guard motionManager.isAccelerometerAvailable else { return }
|
guard motionManager.isDeviceMotionAvailable else { return }
|
||||||
motionManager.accelerometerUpdateInterval = 0.1 // 10 Hz
|
motionManager.deviceMotionUpdateInterval = 0.1 // 10 Hz
|
||||||
motionManager.startAccelerometerUpdates()
|
motionManager.startDeviceMotionUpdates(using: .xMagneticNorthZVertical)
|
||||||
|
|
||||||
lapDetectionTimer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true) { [weak self] _ in
|
lapDetectionTimer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true) { [weak self] _ in
|
||||||
self?.processAccelerometerData()
|
self?.processDeviceMotion()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func stopLapDetection() {
|
private func stopLapDetection() {
|
||||||
lapDetectionTimer?.invalidate()
|
lapDetectionTimer?.invalidate()
|
||||||
lapDetectionTimer = nil
|
lapDetectionTimer = nil
|
||||||
motionManager.stopAccelerometerUpdates()
|
motionManager.stopDeviceMotionUpdates()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func processAccelerometerData() {
|
private func processDeviceMotion() {
|
||||||
guard let data = motionManager.accelerometerData else { return }
|
guard let motion = motionManager.deviceMotion else { return }
|
||||||
|
|
||||||
let magnitude = sqrt(data.acceleration.x * data.acceleration.x +
|
// userAcceleration has gravity removed — clean movement signal
|
||||||
data.acceleration.y * data.acceleration.y +
|
let rawUserAccel = sqrt(motion.userAcceleration.x * motion.userAcceleration.x +
|
||||||
data.acceleration.z * data.acceleration.z)
|
motion.userAcceleration.y * motion.userAcceleration.y +
|
||||||
|
motion.userAcceleration.z * motion.userAcceleration.z)
|
||||||
|
|
||||||
let isStationary = magnitude < 1.1 // Near rest (~1g from gravity)
|
// Low-pass filter for smoothing
|
||||||
|
filteredUserAccel = filterAlpha * rawUserAccel + (1 - filterAlpha) * filteredUserAccel
|
||||||
|
|
||||||
|
// Rotation rate on x-axis detects the flip/turn at the wall
|
||||||
|
let rawRotationX = abs(motion.rotationRate.x)
|
||||||
|
filteredRotationX = filterAlpha * rawRotationX + (1 - filterAlpha) * filteredRotationX
|
||||||
|
|
||||||
|
let isStationary = filteredUserAccel < 0.05
|
||||||
let now = Date()
|
let now = Date()
|
||||||
|
|
||||||
if isStationary {
|
if isStationary {
|
||||||
@@ -439,7 +451,6 @@ class WorkoutManager: NSObject, ObservableObject, HKLiveWorkoutBuilderDelegate {
|
|||||||
self.lapDetected = true
|
self.lapDetected = true
|
||||||
self.lastLapDetectionTime = now
|
self.lastLapDetectionTime = now
|
||||||
}
|
}
|
||||||
// Reset after detection
|
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
|
||||||
self.lapDetected = false
|
self.lapDetected = false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user