Search code examples
iphonexcodeaccelerometer

Saving user acceleration from iPhone devieMotion to a filetext


I'm using deviceMotion for getting useracceleration(x, y, z). My aim is to create a filetext where in each iteration my application writes the 3 components in a row.

I'm using MotionGraphs code sample.

How is it possible - directly, or is necessary to create an array first? This array; is it NSMutableArray or NSMutableNumber?

I've been looking for this question and I'm lost. :-(

I'm not an Objective-C expert but I remember Pascal code where I opened a file, and then I was writing in each iteration, but I checked: that programming has changed.

At the beginning we don't take into account different filters or discrimination window. For them, I've implemented freescale procedure. I'm just looking to save accelerometer data / to store data from accelerometer using deviceMotion userAcceleration.

    float minX = 1.0f;

    float minY = 1.0f;

    float minZ = 1.0f;

    NSMutableArray *container = [[NSMutableArray alloc] init];

    -(void)startUpdatesWithSliderValue:(int)sliderValue
    {

        NSTimeInterval delta = 0.005;

        NSTimeInterval updateInterval = deviceMotionMin + delta * sliderValue;

        CMMotionManager *mManager = [(APLAppDelegate *)[[UIApplication sharedApplication] delegate] sharedManager];

        APLDeviceMotionGraphViewController * __weak weakSelf = self;

        [container addObject:[NSNumber numberWithFloat:deviceMotion.userAcceleration.x]];

        [container addObject:[NSNumber numberWithFloat:deviceMotion.userAcceleration.y]];

        [container addObject:[NSNumber numberWithFloat:deviceMotion.userAcceleration.z]];
    }

//Finally we have to dump data to filetext, this is I don´t know correctly.

Solution

  • 1 Create NSMutableArray *container = [[NSMutableArray alloc] init]; to be your container.

    2 Within the Accelerometer delegate method for did detect motion be sure to set a min for each of the 3 axis. e.g. float min_X = 1.0f; float min_y =1.0f; float min_Z = 1.0f

    -(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    
    }
    

    3 Use Simple Filter Logic as in: (keep in mind the Acceleration is maxed at +- 2.3g so both positive and negative thresholds need consideration.

    if ((acceleration.x > min_X || acceleration.x < -min_X) && (Y's..) && (Z's...) ) {
    
    [container addObject:[NSNumber numberWithFloat:acceleration.x]];
    [container addObject:[NSNumber numberWithFloat:acceleration.y]];
    [container addObject:[NSNumber numberWithFloat:acceleration.z]];
    
    }
    

    4 The Array should be full of NSNumbers in groups of three (x,y,z).

    5 The filter is needed, otherwise the accelerometers can pick small vibrations just sitting on the table.


    WARNING: The Array will fill up fast, so Set the Sample Rate to an acceptable range based on how long you want to record data.