iphone - 如何使用陀螺仪计算步数?

标签 iphone ios objective-c sensors gyroscope

正在开发一个应用程序来计算人类行走时的步数, 我使用加速度计做到了这一点,但我发现它不准确。 因此,在谷歌搜索后,我发现使用陀螺仪传感器比加速度计更准确。 任何有关可能执行此操作的控件的帮助,因为我从未使用 GyroScope 进行开发,或者阅读有关 GyroScope 传感器的更多信息的文档。 预先感谢您的帮助。

-(void)addAcceleration:(UIAcceleration*)accel
{
    x = accel.x;
    y = accel.y;
    z = accel.z;
}

-(NSString*)name
{
    return @"You should not see this";
}

@end

#define kAccelerometerMinStep               5.02
#define kAccelerometerNoiseAttenuation      1.0

double Norm(double x, double y, double z)
{
    return sqrt(x * x + y * y + z * z);
}

double Clamp(double v, double min, double max)
{
    if(v > max)
        return max;
    else if(v < min)
        return min;
    else
        return v;
}

// See http://en.wikipedia.org/wiki/Low-pass_filter for details low pass filtering
@implementation LowpassFilter

-(id)initWithSampleRate:(double)rate cutoffFrequency:(double)freq
{
    self = [super init];
    if(self != nil)
    {
        double dt = 1.0 / rate;
        double RC = 1.0 / freq;
        filterConstant = dt / (dt + RC);
    }
    return self;
}

-(void)addAcceleration:(UIAcceleration*)accel
{
    double alpha = filterConstant;

    if(adaptive)
    {
        double d = Clamp(fabs(Norm(x, y, z) - Norm(accel.x, accel.y, accel.z)) / kAccelerometerMinStep - 1.0, 0.0, 1.0);
        alpha = (1.0 - d) * filterConstant / kAccelerometerNoiseAttenuation + d * filterConstant;
    }

    x = accel.x * alpha + x * (1.0 - alpha);
    y = accel.y * alpha + y * (1.0 - alpha);
    z = accel.z * alpha + z * (1.0 - alpha);
}

-(NSString*)name
{
    return adaptive ? @"Adaptive Lowpass Filter" : @"Lowpass Filter";
}

@end

// See http://en.wikipedia.org/wiki/High-pass_filter for details on high pass filtering
@implementation HighpassFilter

-(id)initWithSampleRate:(double)rate cutoffFrequency:(double)freq
{
    self = [super init];
    if(self != nil)
    {
        double dt = 1.0 / rate;
        double RC = 1.0 / freq;
        filterConstant = RC / (dt + RC);
    }
    return self;
}

-(void)addAcceleration:(UIAcceleration*)accel
{
    double alpha = filterConstant;

    if(adaptive)
    {
        double d = Clamp(fabs(Norm(x, y, z) - Norm(accel.x, accel.y, accel.z)) / kAccelerometerMinStep - 1.0, 0.0, 1.0);
        alpha = d * filterConstant / kAccelerometerNoiseAttenuation + (1.0 - d) * filterConstant;
    }

    x = alpha * (x + accel.x - lastX);
    y = alpha * (y + accel.y - lastY);
    z = alpha * (z + accel.z - lastZ);

    lastX = accel.x;
    lastY = accel.y;
    lastZ = accel.z;
}

-(NSString*)name
{
    return adaptive ? @"Adaptive Highpass Filter" : @"Highpass Filter";
}

最佳答案

使用CoreMotion FrameWork。

#import <CoreMotion/CoreMotion.h>

    self.motionManager = [[CMMotionManager alloc] init];


    //Gyroscope
    if([self.motionManager isGyroAvailable])
    {
        /* Start the gyroscope if it is not active already */ 
        if([self.motionManager isGyroActive] == NO)
        {
            /* Update us 2 times a second */
            [self.motionManager setGyroUpdateInterval:1.0f / 2.0f];

            /* Add on a handler block object */

            /* Receive the gyroscope data on this block */
            [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue]
             withHandler:^(CMGyroData *gyroData, NSError *error)
            {
                NSString *x = [[NSString alloc] initWithFormat:@"%.02f",gyroData.rotationRate.x];
                self.gyro_xaxis.text = x;

                NSString *y = [[NSString alloc] initWithFormat:@"%.02f",gyroData.rotationRate.y];
                self.gyro_yaxis.text = y;

                NSString *z = [[NSString alloc] initWithFormat:@"%.02f",gyroData.rotationRate.z];
                self.gyro_zaxis.text = z;
            }];
        }
    }
    else
    {
        NSLog(@"Gyroscope not Available!");
    }

阅读此内容:http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Introduction/Introduction.html

需要使用两个类:CMGyrodata和CMMotionManager

与您的问题相关:How to count steps using an Accelerometer?

阅读该内容,将是一个很好的起点......

希望这有帮助...

关于iphone - 如何使用陀螺仪计算步数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16760584/

相关文章:

javascript - 如何在日期选择器中的 JQUERY 中设置从当前日期开始的 -3 年和从当前日期开始的 +9 个月

ios - 是否可以在 dismissmodelviewcontroller 时返回一个值

ios - swift 如何让动画在两个 UIView 之间翻转

iphone - 如何在我的 iPhone 应用程序中启动 YouTube 视频?

ios - 如何在 UIPickerView 中为两个不同的组件显示两种不同的对象类型?

ios - 在 iOS 中向 vimeo 视频添加评论

ios - 如何测量 iOS 中的延迟

iphone - IO6 不调用 -(BOOL)shouldAutorotate

ios - 媒体查询在 iOS 版 Chrome 中无法正常工作

ios - 如何在 UIViewController 消失时停止位置管理器