如何计算从accerometer正确的速度值(How to calculate correct ve

2019-07-29 08:53发布

我需要计算iphone速度在太空中,冲压速度为例子。
我的第一个问题是:加速度值accelerometer:didAccelerate这个过滤操作:

gravity_x = acceleration.x * kFilteringFactor + gravity_x * (1.0 - kFilteringFactor);
gravity_y = acceleration.y * kFilteringFactor + gravity_y * (1.0 - kFilteringFactor);
gravity_z = acceleration.z * kFilteringFactor + gravity_z * (1.0 - kFilteringFactor);
float gravityNorm = sqrt(gravity_x * gravity_x + gravity_y * gravity_y + gravity_z * gravity_z);
accelX = acceleration.x - gravity_x / gravityNorm;
accelY = acceleration.y - gravity_y / gravityNorm;
accelZ = acceleration.z - gravity_z / gravityNorm;

与使用CoreMotion的
motionManager.deviceMotion.userAcceleration.x; motionManager.deviceMotion.userAcceleration.y; motionManager.deviceMotion.userAcceleration.z;

接下来,按照同样的问题,我这样做

    const float accuracy=10;
    float accx = (float) ((int)  (accelX * accuracy))/ accuracy;
    float accy=  (float) ((int)  (accelY * accuracy))/ accuracy;
    float accz=  (float) ((int)  (accelZ * accuracy))/ accuracy;

舍入值,那么,我认为,我获得当前速度

float dx = accx*9.81+prevX*9.81;
float dy = accy*9.81+prevY*9.81;
float dz = accz*9.81+prevZ*9.81;

speed_after_x = dx/2*myAccelerometer.updateInterval+speed_before_x;
speed_after_y = dy/2*myAccelerometer.updateInterval+speed_before_y;
speed_after_z = dz/2*myAccelerometer.updateInterval+speed_before_z;

根据iphone加速度计的速度和距离
然后

    prevX=accx;
    prevY=accy;
    prevZ=accz;

    speed_before_x=speed_after_x;
    speed_before_y=speed_after_y;
    speed_before_z=speed_after_z;

最后计算的速度矢量的长度

    float speed = sqrt(speed_after_x*speed_after_x+speed_after_y*speed_after_y+speed_after_z*speed_after_z);
    if (max_speed<speed)  max_speed = speed;

但是速度值,即包含标签,总是不断增加。 我的意思是,如果我移动设备,然后停了下来,速度值不为0不加速在0补偿本身?

Answer 1:

下面是示例代码,我设法敲定。 我只是把它放在这里,现在:

//
//  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

这将需要修改您的需求,尤其是我只是扔值远之后。 将所得的值“速度”记录为“速度”趋于向后朝向后加速度事件已经停止极小负值,例如,* 10 ^ -17。 所以啊,这是几乎为零在我的书。 你会希望做一些四舍五入在那里,甚至可能缩放值了。 我不认为我能得到它高于0.2或0.3,但后来我又不想投我的手机在房间里(还)。



文章来源: How to calculate correct velocity values from accerometer