How to calculate correct velocity values from accerometer

后端 未结 1 404
有刺的猬
有刺的猬 2021-02-01 11:11

I need to calculate iphone velocity in space, punch speed for example.
My first question is: Are the acceleration values accelerometer:didAcceleratewith this f

相关标签:
1条回答
  • 2021-02-01 11:59

    Here's the example code I managed to hash out. I'll just put it here for now:

    //
    //  ViewController.m
    //  Acce
    //
    //  Created by Diziet on 14/05/2012.
    //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController () {
        UIAccelerationValue gravX;
        UIAccelerationValue gravY;
        UIAccelerationValue gravZ;
        UIAccelerationValue prevVelocity;
        UIAccelerationValue prevAcce;
    }
    
    @property (strong) UIAccelerometer *sharedAcc;
    
    @end
    
    @implementation ViewController
    
    @synthesize sharedAcc = _sharedAcc;
    
    #define kAccelerometerFrequency        50.0 //Hz
    #define kFilteringFactor 0.1
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.sharedAcc = [UIAccelerometer sharedAccelerometer];
        self.sharedAcc.delegate = self;
        self.sharedAcc.updateInterval = 1 / kAccelerometerFrequency; 
    
        gravX = gravY = gravZ = prevVelocity = prevAcce = 0.f;
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    
    - (UIAccelerationValue)tendToZero:(UIAccelerationValue)value {
        if (value < 0) {
            return ceil(value);
        } else {
            return floor(value);
        }
    }
    
    #define kAccelerometerFrequency        50.0 //Hz
    #define kFilteringFactor 0.1
    - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
        gravX = (acceleration.x * kFilteringFactor) + (gravX * (1.0 - kFilteringFactor));
        gravY = (acceleration.y * kFilteringFactor) + (gravY * (1.0 - kFilteringFactor));
        gravZ = (acceleration.z * kFilteringFactor) + (gravZ * (1.0 - kFilteringFactor));
    
        UIAccelerationValue accelX = acceleration.x - ( (acceleration.x * kFilteringFactor) + (gravX * (1.0 - kFilteringFactor)) );
    
        UIAccelerationValue accelY = acceleration.y - ( (acceleration.y * kFilteringFactor) + (gravY * (1.0 - kFilteringFactor)) );
        UIAccelerationValue accelZ = acceleration.z - ( (acceleration.z * kFilteringFactor) + (gravZ * (1.0 - kFilteringFactor)) );
        accelX *= 9.81f;
        accelY *= 9.81f;
        accelZ *= 9.81f;
        accelX = [self tendToZero:accelX];
        accelY = [self tendToZero:accelY];
        accelZ = [self tendToZero:accelZ];
    
        UIAccelerationValue vector = sqrt(pow(accelX,2)+pow(accelY,2)+pow(accelZ, 2));
        UIAccelerationValue acce = vector - prevVelocity;
        UIAccelerationValue velocity = (((acce - prevAcce)/2) * (1/kAccelerometerFrequency)) + prevVelocity;
    
        NSLog(@"X %g Y %g Z %g, Vector %g, Velocity %g",accelX,accelY,accelZ,vector,velocity);
    
        prevAcce = acce;
        prevVelocity = velocity;
    }
    
    @end
    

    It'll need modifying for your needs, especially as I'm just throwing the values away afterwards. The resultant value 'velocity' logged as 'Velocity' tends back towards extremely small negative values after acceleration events have ceased, e.g. *10^-17. So yeah, that's practically zero in my book. You'll want to do some rounding in there and probably even scale the values up. I don't think I could get it higher than 0.2 or 0.3 but then again I don't want to hurl my phone across the room (yet).

    0 讨论(0)
提交回复
热议问题