Google Dataset Search
Kaggle
Define activities to record using Motion API
enum ActivityType : Int {
case none, driveIt, shakeIt, chopIt
}
Accessing sensor data from Core Motion
Declare Core Motion properties
let motionManager = CMMotionManager ( )
let queue = OperationQueue ( )
var activityData : [ String ] = [ ]
Define sampling interval for sensors
enum Config {
static let samplesPerSecond = 25.0
}
Write sensor data to a file
private func confirmSavingActivityData( _ action: UIAlertAction ) {
let dataURL = FileManager . documentDirectoryURL
. appendingPathComponent ( " u \( self . userIdField. text!) - \( self . selectedActivityName) -data " )
. appendingPathExtension ( " csv " )
do {
try self . activityData. appendLinesToURL ( fileURL: dataURL)
print ( " Data appended to \( dataURL) " )
} catch {
print ( " Error appending data: \( error) " )
}
}
func enableMotionUpdates( ) {
// 1
motionManager. deviceMotionUpdateInterval = 1 / Config. samplesPerSecond
// 2
activityData = [ ]
// 3
motionManager. startDeviceMotionUpdates (
using: . xArbitraryZVertical,
to: queue,
withHandler: { [ weak self] motionData, error in
// 4
guard let self = self , let motionData = motionData else
{
let errorText = error? . localizedDescription ?? " Unknown "
print ( " Device motion update error: \( errorText) " )
return
}
// 5
self . process ( data: motionData)
}
)
}
func process( data motionData: CMDeviceMotion ) {
let activity = isRecording ? currendActivity : . none
let sample = """
\( sessionId!) - \( numberOfActionsRecorded) , \
\( activity. rawValue) , \
\( motionData. attitude. roll) , \
\( motionData. attitude. pitch) , \
\( motionData. attitude. yaw) , \
\( motionData. rotationRate. x) , \
\( motionData. rotationRate. y) , \
\( motionData. rotationRate. z) , \
\( motionData. gravity. x) , \
\( motionData. gravity. y) , \
\( motionData. gravity. z) , \
\( motionData. userAcceleration. x) , \
\( motionData. userAcceleration. y) , \
\( motionData. userAcceleration. z)
"""
activityData. append ( sample)
}
Start & Stop data collection session
case Utterances. sessionStart:
// TODO: enable Core Motion
enableMotionUpdates ( )
queueNextActivity ( )
case Utterances. sessionComplete:
disableMotionUpdates ( )
func disableMotionUpdates( ) {
motionManager. stopDeviceMotionUpdates ( )
}