A
A
Armanio2013-12-09 00:05:19
Objective-C
Armanio, 2013-12-09 00:05:19

iPhone accelerometer: move the phone up/down. How to implement?

Gentlemen, I would be very grateful for advice on working with the iPhone accelerometer:
you need to calculate when a person pulls up with a phone in his pocket.
That is, the phone goes up and down in about the same place (+1 pull-up).
This is implemented in a pack of sports-themed applications from runtastic.
Specifically: pull ups .
I understood how to read the data from the sensor, but what to do with them next is not very (not at all):

self.motionManager = [[CMMotionManager alloc]init];
    self.motionManager.deviceMotionUpdateInterval = 10.0/100.0;
    [self.motionManager startDeviceMotionUpdatesToQueue:[[NSOperationQueue alloc]init] withHandler:^(CMDeviceMotion *motion, NSError *error) {
        CMAcceleration acceleration = motion.userAcceleration;
        NSLog(@"%f", acceleration.y);
}];

The data comes in a double of the form -0.270925, -0.211401, etc.
As I understand it, this is the expansion of the acceleration vector into the specified axes (in our case, y), but what to do with it further ... I
feel that the solution is simpler, than it seems.
Fingers in the blood already googled. :(

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Armanio, 2013-12-21
@Armanio

Thanks to all!
Solution found:

-(void) start{
    double __block firstY, mediumY, lastY=0;
    int __block i=0;
    [self addObserver:self forKeyPath:@"count" options:NSKeyValueObservingOptionNew context:nil];
    self.motionManager = [[CMMotionManager alloc]init];
    self.motionManager.deviceMotionUpdateInterval = 0.1;
    [self.motionManager startDeviceMotionUpdatesToQueue:[[NSOperationQueue alloc]init] withHandler:^(CMDeviceMotion *motion, NSError *error) {
        CMAcceleration acceleration = motion.userAcceleration;
        [self changeFilter:[LowpassFilter class]];
        [filter addAcceleration:&acceleration];
        if([UIDevice currentDevice].orientation!=1){
            [self.messageText performSelectorOnMainThread:@selector(setText:) withObject:@"Держите телефон вертикально!" waitUntilDone:NO];
        } else {
            [self.messageText performSelectorOnMainThread:@selector(setText:) withObject:@"" waitUntilDone:NO];
            double y = filter.y>0.5?filter.y:0;
            if (i < 3){
                if(i%3==0){
                    firstY = y;
                }
                if(i%3==1){
                    mediumY = y;
                }
                if(i%3==2){
                    lastY = y;
                }
            }else{
                firstY = mediumY;
                mediumY = lastY;
                lastY = y;
            }
        
        if(mediumY>firstY){
            if(mediumY>lastY){
                self.count++;
                AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

            }
        }
        i++;
        }
    }];
    self.trained = YES;
}

Considers not without clumsiness, of course. But somehow. I would appreciate modifications.

A
Alexey Storozhev, 2013-12-11
@storoj

I suspect that we also need to take data from the gyroscope

T
tre, 2013-12-15
@tre

Pull-up physics: first the body moves vertically with positive acceleration from the ground, then with negative acceleration towards the ground. Accordingly, it is necessary to fix these moments of sign change.
It is important to take into account that the phone will be at the person at random, which means that you first need to find the vertical, and also the acceleration of gravity g always acts along the vertical.

T
tre, 2013-12-15
@tre

https://www.inkling.com/read/learning-ios-programm...
A lot about core motion y iphone

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question