Answer the question
In order to leave comments, you need to log in
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);
}];
Answer the question
In order to leave comments, you need to log in
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;
}
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question