简体   繁体   中英

How to calculate back and forth motion of phone using coremotion

I would like to know if there is a way or how one would calculate the back and forth motion of a device.

For instance, if you lay a phone on a flat surface and push it back and forth on the y axis it give readings for the y axis.. if you rotate the phone or turn it it might then start giving reading on the x axis etc.

I am trying to make a stroke calculator for a rower where they just attach their device to any part of the boat, what I want to know is how to handle the fact the device could be put at any angle or rotation...

You do it by using core motion's device motion.

@property (strong,nonatomic,readonly) CMMotionManager *motionManager;



// Motion Manager setup
    if(!_motionManager)
    {
        _motionManager = [[CMMotionManager alloc] init];
        _motionManager.deviceMotionUpdateInterval = 1.0/70.0;

        [_motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical];

        NSLog(@"AppDelegate: Motion Manager created and started.");
    }

Notice how i set a reference frame.

Then you can get the readings by doing

_motionManager.deviceMotion.userAcceleration

I dont remember if these readings were in device coordinates or global coordinates, but if they are in global coordinates you can use the attitute (thats the device orientation at a given point in reference to the true magnetic north specified earlier that matches whatever acceleration there was at that same point) to translate device coordinates to world coordinates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM