ios - 使用 Core Motion 检测道路颠簸

标签 ios

我正在尝试创建一个在驾驶时检测道路颠簸或坑洼的应用程序,我在谷歌上搜索了可能的解决方案,我找到了一个使用加速度计值来检测道路颠簸的解决方案。

我使用了此链接中的代码,当您用手掌轻敲手机时这很好,但它在开车时没有检测到颠簸。 iOS: Accurately determining energy of a bump from accelerometer output

我还发现了一种可用于查找道路颠簸的算法 Z-Diff,该算法的解释如下:http://open-sci.net/wiki/sealappexamplepotholes

这是我使用的代码:

类型定义结构{ 双 x、y、z; }vec_d3;

#define RECENT_COUNT 10
#define SMOOTH_IP(x,x_new,fac) x = fac * x + (1. -fac) * x_new
#define DOUBLE_EMPTY DBL_MAX

-(void)startTakingMotionValues{



[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {


    static vec_d3 smooth = {DOUBLE_EMPTY,0,0};
    {
        if(smooth.x == DOUBLE_EMPTY){
            smooth.x=accelerometerData.acceleration.x;
            smooth.y=accelerometerData.acceleration.y;
            smooth.z=accelerometerData.acceleration.z;

            return;
        }
        SMOOTH_IP(smooth.x, accelerometerData.acceleration.x, 0.9);
        SMOOTH_IP(smooth.y, accelerometerData.acceleration.y, 0.9);
        SMOOTH_IP(smooth.z, accelerometerData.acceleration.z, 0.9);
    }

    // keep track of last k smoother acceleration values
    static vec_d3 recent[RECENT_COUNT];
    {
        static int ptr=0;
        static BOOL gotEnoughData = NO;

        recent[ptr]= smooth;
        ptr++;
        if(ptr==RECENT_COUNT){
            ptr=0;
            gotEnoughData=YES;
        }
        if (!gotEnoughData) {
            return;
        }
    }
    //get the resultant variation in acceleration over the whole array
    double variation;
    {
        vec_d3 min = smooth,max=smooth;
        for (int i=0; i< RECENT_COUNT; i++) {
            min.x = MIN(min.x, recent[i].x);
            min.y = MIN(min.y, recent[i].y);
            min.z = MIN(min.z, recent[i].z);

            max.x = MAX(max.x, recent[i].x);
            max.y = MAX(max.y, recent[i].y);
            max.z = MAX(max.z, recent[i].z);
        }
        vec_d3 V = (vec_d3)
        {
            .x = max.x - min.x,
            .y = max.y - min.y,
            .z = max.z - min.z
        };
        variation =sqrt(
                        V.x * V.x +
                        V.y * V.y +
                        V.z * V.z
                        );
    }
    //smooth it
    static double var_smoothed = DOUBLE_EMPTY;
    {
        if (var_smoothed == DOUBLE_EMPTY) {
            var_smoothed = variation;
            return;
        }
        SMOOTH_IP(var_smoothed, variation, 0.9);
    }

    // see if it's just passed a peak
    {
        static double varSmoothed_last = DOUBLE_EMPTY;
        if (varSmoothed_last == DOUBLE_EMPTY) {
            varSmoothed_last=var_smoothed;
            return;
        }
        static double varSmoother_preLast = DOUBLE_EMPTY;
        if (varSmoother_preLast == DOUBLE_EMPTY) {
            varSmoother_preLast = varSmoothed_last;
            varSmoothed_last = var_smoothed;
            return;
        }

#define THRESHOLD_IMPLUSE .40

        if (varSmoothed_last > varSmoother_preLast &&
            varSmoothed_last > var_smoothed &&
            varSmoothed_last > THRESHOLD_IMPLUSE) {
            didFindLocation=NO;
            if (newLocation.speed < 5) {

            }else{
            NSLog(@"varSmoothed_last: @ %f",varSmoothed_last);
            NSLog(@"varSmoother_preLast : %f",varSmoother_preLast);
            NSLog(@"var_smoothed : %f",var_smoothed);

            [self.bumpsData addObject:[NSString stringWithFormat:@"%f",varSmoothed_last]];
            [self.bumpsTableView reloadData];
            }

            //[self peakedWithImpulse: varSmoothed_last ];

        }
        varSmoother_preLast = varSmoothed_last;
        varSmoothed_last = var_smoothed;
    }

    }];
}

最佳答案

您需要从调整参数开始,以了解如何将算法调整到不同的频率灵敏度:

#define THRESHOLD_IMPLUSE .40
#define RECENT_COUNT 10

此外,您的平滑系数为 0.9,您可以尝试调整:

SMOOTH_IP(smooth.x, accelerometerData.acceleration.x, 0.9);

您正在尝试隔离指示道路颠簸的振幅和波长。调整 RECENT_COUNT 或平滑系数应该可以降低对高频颠簸的敏感度。调整 THRESHOLD_IMPULSE 可能会影响幅度截止。

要衡量这些因素如何影响结果,您还应该设计一些测试和测量方法。 记录道路颠簸的一些数据将有助于您了解您希望检测的行为。

一旦你理解了算法并调整了它,如果它仍然不能隔离你所追求的运动,你应该看看第一个链接中的其他算法,分别理解它们,然后尝试添加他们混合。

关于ios - 使用 Core Motion 检测道路颠簸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28186543/

相关文章:

ios - 在 Swift 4 中 touchesBegan 时重复功能

ios - GPX 文件不加载 iOS Xcode

ios - 由于发布版本中的 "Cannot connect to iTunes Store",我的应用程序内购买失败的比例非常高

ios - 防止 iOS 在几分钟后杀死 App

javascript - 解析对象数组并填充表格 View 单元格 Swift

iphone - 调用具有多个参数且来自不同类的栏按钮的选择器

ios - 在 iOS 中后台上传照片超过 10 分钟

ios - 不同类型的 Json 属性

iphone - 无法编辑 UITableView 框架.y

ios - 新iPad : Low Memory Warnings Not Appearing?