📄

AirPods Proのモーションデータをcsvファイルに記録する

2020/10/03に公開

iOS14から取得できるようになったモーションデータをcsvファイルに保存するアプリを作成した。

ソースコードのリポジトリはこちら。
CMHeadphoneMotionManager-Sampler

csvに記録するclass

import Foundation
import CoreMotion

class MotionWriter {

    var file: FileHandle?
    var sample: Int = 0

    func open(_ filePath: URL) {
        do {
            FileManager.default.createFile(atPath: filePath.path, contents: nil, attributes: nil)
            let file = try FileHandle(forWritingTo: filePath)
            var header = ""
            header += "acceleration_x,"
            header += "acceleration_y,"
            header += "acceleration_z,"
            header += "attitude_pitch,"
            header += "attitude_roll,"
            header += "attitude_yaw,"
            header += "gravity_x,"
            header += "gravity_y,"
            header += "gravity_z,"
            header += "quaternion_x,"
            header += "quaternion_y,"
            header += "quaternion_z,"
            header += "quaternion_w,"
            header += "rotation_x,"
            header += "rotation_y,"
            header += "rotation_z"
            header += "\n"
            file.write(header.data(using: .utf8)!)
            self.file = file
        } catch let error {
            print(error)
        }
    }

    func write(_ motion: CMDeviceMotion) {
        guard let file = self.file else { return }
        var text = ""
        text += "\(motion.userAcceleration.x),"
        text += "\(motion.userAcceleration.y),"
        text += "\(motion.userAcceleration.z),"
        text += "\(motion.attitude.pitch),"
        text += "\(motion.attitude.roll),"
        text += "\(motion.attitude.yaw),"
        text += "\(motion.gravity.x),"
        text += "\(motion.gravity.y),"
        text += "\(motion.gravity.z),"
        text += "\(motion.attitude.quaternion.x),"
        text += "\(motion.attitude.quaternion.y),"
        text += "\(motion.attitude.quaternion.z),"
        text += "\(motion.attitude.quaternion.w),"
        text += "\(motion.rotationRate.x),"
        text += "\(motion.rotationRate.y),"
        text += "\(motion.rotationRate.z)"
        text += "\n"
        file.write(text.data(using: .utf8)!)
        sample += 1
    }

    func close() {
        guard let file = self.file else { return }
        file.closeFile()
        print("\(sample) sample")
        self.file = nil
    }
}

以下のような内容がcsvに保存される

収集したcsvをCreate MLで機械学習などに利用する。

MotionWriter.swift の利用の流れ。


// 生成
let writer = MotionWriter()
writer.open(path)

...

// モーションイベントの書き込み
CMHeadphoneMotionManager.startDeviceMotionUpdates(to: .main) { (motion, error) in
    if let motion = motion {
	writer.write(motion)
    }
    if let error = error {
	print(error)
    }
}

...

// 終了
writer.close()

Discussion