CoreMotionActivityManager返回汽车或汽车+固定true [英] CoreMotionActivityManager returning either Automotive or Automotive + Stationary true

查看:87
本文介绍了CoreMotionActivityManager返回汽车或汽车+固定true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测 汽车 ActivtyType ,但是问题是如果我先开车然后停下车然后呆在车里,然后简单地检查coreMotion日志:我将继续获得大量的混合回调,或者

I'm trying to detect Automotive ActivtyType, however the problem is if "I go on a drive and then stop the car and stay in the car" and simply check the coreMotion logs: I would continue to get numerous mixed callbacks of either

high Confidence: Automotive: True, Stationary: True 

high confidence: Automotive: True, Stationary: False

low confidence: Automotive: True, Stationary: True




我没有得到固定的:正确,汽车
也总是正确的

I do not get an only Stationary: True, it always comes with automotive being True as well

,或者至少我找不到模式。

There's not much of a pattern to how they come, or at least I haven't been able to find a pattern.

问:有人找到了吗?一种可靠的检测汽车何时真正成为汽车的方法?

我尝试计算得到的回调次数,然后进行一些计算,但这没有似乎不可靠。

I've tried counting the number of callbacks I get and then doing some calculation but that doesn't seem reliable.

当用户下车时,我会走路或静止(没有汽车...这很不错)回调,并使用这些回调来将标志设置为true ...因此之后,如果我得到任何 automotive 回调...那么我知道这是真正的汽车...

FWIW the moment the user gets out of the car then I get either a walking or stationary (with no automotive...which is good) callback and use those callbacks to set a flag to true...so after that if I get any automotive callback...then I know it’s a real automotive...

我的代码:

func beginMotionTracking(){

    let motionLog = OSLog(subsystem: "Spike", category: "Motion")

    shouldUseTimer = false
    motionActivityManager = CMMotionActivityManager()
    var totalWalking = 0
    var totalAutomotive = 0
    var totalStationary = 0
    var totalFalseAutomotive = 0

    motionActivityManager?.startActivityUpdates(to: OperationQueue.main){
        [weak self] activity in

        os_log("Motion is Tracking | desiredAccuracy is %{public}f | RemainingTime : %{public}f ",log: motionLog, type: .default, (self?.locationManager.desiredAccuracy)! , UIApplication.shared.remainingTime())

        if activity?.walking == true && (activity?.confidence == .medium || activity?.confidence == .high) && activity?.automotive == false && activity?.stationary == false && activity?.unknown == false {
            totalWalking += 1

            os_log("medium and high conf: walking %{public}d time", log: motionLog, type: .error, totalWalking)

        }else if activity?.stationary == true && (activity?.confidence == .medium || activity?.confidence == .high) && activity?.automotive == false && activity?.walking == false && activity?.unknown == false {
            totalStationary += 1

            os_log("medium and high conf: stationary %{public}d time", log: motionLog, type: .error, totalStationary)

            // false automotive
        }else if activity?.automotive == true && activity?.stationary == true && (activity?.confidence == .high) && activity?.walking == false  && activity?.unknown == false {
            totalFalseAutomotive += 1
            os_log("high conf: FALSE Automotive %{public}d time", log: motionLog, type: .error, totalFalseAutomotive)


            if totalFalseAutomotive > 2{
                totalFalseAutomotive = 0
                totalAutomotive = 0
                totalStationary = 0
                totalWalking = 0

                os_log("Too many FALSE automotives, REST all counts back to 0", log: motionLog, type: .fault)
            }
        }
        else if activity?.automotive == true && (activity?.confidence == .high) && activity?.walking == false && activity?.stationary == false && activity?.unknown == false {
            totalAutomotive += 1
            os_log("high conf: Automotive %{public}d time", log: motionLog, type: .error, totalAutomotive)

            if ((totalWalking > 3 && totalAutomotive > 2) || (totalStationary > 3 && totalAutomotive > 2) || (totalAutomotive > 7)){
                self?.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters

                os_log("Motion is Automotive and is about to be stopped: desired AccuracyChanged to HundredMeters | RemainingTime : %{public}f ", log: motionLog, type: .fault, UIApplication.shared.remainingTime())

                self?.shouldUseTimer = true
                self?.motionActivityManager?.stopActivityUpdates()
            }
        }
    }
}






我是经历了所有这些麻烦,因为我正在尝试降低我的核心位置每当用户不开车超过3分钟然后在以后使用核心运动来检测汽车 Motion并将其放回原处时的准确性定位精度至百米。


I'm going through all this hassle, because I'm trying to degrade my core-location Accuracy whenever the user isn't driving for more than 3 minutes and then later using core-motion to detect automotive Motion and use that to put back location Accuracy to hundredMeter.

推荐答案

这适用于我的类似用例,在这种情况下,我想在设置.automotive后积极限制交互,但是如果可以,可以轻松恢复交互。

This is what works for my similar use case, where I want to aggressively restrict interaction when .automotive has become set, but where I'm ok allowing an easy restoration of interaction if it becomes stationary. In logging the messages, it seems to hold onto the .automotive until another mode is detected (e.g. walking)

自从我在交通繁忙的WiFi总线上通勤以来,我一直记录着.automotive直到发现另一种模式(例如,步行)。看到以下重复消息:

Since I commute on a bus with WiFi in heavy traffic, I saw repeated messages of:

confidence low, stationary true, automotive true
confidence high, stationary false, automotive true
....

它们反复切换(我从未见过任何媒介置信活动),并且只有在静止足够长的时间(10-12秒)时,置信度才会达到很高,置信度为真实,汽车为真实,因此我将其取消。

They toggle as such over and over (and I never saw any medium confidence activities) and only when stationary for a sufficient time (10-12s) does the confidence to go high with stationary true, automotive true so I key off that.

这里我要工作的代码:

func handleMotionActivityUpdates(activity: CMMotionActivity?) {
    if let a = activity {
        if a.automotive && !a.stationary && (a.confidence == .medium || a.confidence == .high) {
            //restrict interaction
        } else
        if (!a.automotive || a.stationary) && (a.confidence == .medium || a.confidence == .high) {
            //re-enable interaction
        }
    }
}

这篇关于CoreMotionActivityManager返回汽车或汽车+固定true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆