Coder Social home page Coder Social logo

Comments (6)

Gerharbo avatar Gerharbo commented on June 7, 2024 2

We've had the same problem. It seems that the problem is the value of timeInterval. It's 0 when calculating velocityXComponent, velocityYComponent and velocityZComponent.

See lines 197, 198 and 199:
https://github.com/Hypercubesoft/HCKalmanFilter/blob/master/HCKalmanFilter/HCKalmanAlgorithm.swift#L197

After calculating the velocity, the outcome is:

velocityXComponent = (Double) -Inf
velocityYComponent = (Double) NaN
velocityZComponent = (Double) NaN

What's the best approach? Should we skip the location? Or should the KalmanFilter be altered to devide by 1 instead of zero?

from hckalmanfilter.

yashbedi avatar yashbedi commented on June 7, 2024

Hey Thomas thanks for bringing up this issue,
Can you suggest any best way to overcome this issue,? I mean instead using bunch of if let's or guard statements.

from hckalmanfilter.

tdimeco avatar tdimeco commented on June 7, 2024

Hey Yash,

I am not using the library anymore, but in the past, I temporary "solved" the issue by resetting the Kalman filter when it was dirty. Here is some parts of the code:

class LocationDataProvider: CLLocationManagerDelegate {
    
    // ...
    
    private var locationManager: CLLocationManager?
    private var kalmanAlgorithm: HCKalmanAlgorithm?
    
    // ...
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        
        let location = locations.last!
        var correctedLocation: CLLocation!
        
        // Use the kalman algorithm to smooth location data
        if self.kalmanAlgorithm == nil {
            self.kalmanAlgorithm = HCKalmanAlgorithm(initialLocation: location)
            correctedLocation = location
        } else {
            correctedLocation = self.kalmanAlgorithm!.processState(currentLocation: location)
        }
        
        correctedLocation = self.tempFixForNaNLocations(location: location, correctedLocation: correctedLocation)
        
        // ... <Use correctedLocation>
    }
    
    // FIX: Temporary fix to avoid NaN locations: https://github.com/Hypercubesoft/HCKalmanFilter/issues/19
    private func tempFixForNaNLocations(location: CLLocation, correctedLocation: CLLocation) -> CLLocation {
        if correctedLocation.coordinate.latitude.isNaN || correctedLocation.coordinate.longitude.isNaN || correctedLocation.altitude.isNaN {
            self.kalmanAlgorithm = HCKalmanAlgorithm(initialLocation: location)
            return location
        } else {
            return correctedLocation
        }
    }
}

Hope this can help you.
Cheers.

from hckalmanfilter.

yashbedi avatar yashbedi commented on June 7, 2024

Hey, Thomas

Yeah, I too figured out a way something like that.
Though Really appreciate you taking out time to write the snippet. Thank you.

from hckalmanfilter.

Mike1707 avatar Mike1707 commented on June 7, 2024

Hey guys, I found this library right now and didn't test it yet but looked into this issue.

For me it sounds as if you are using cached locations which you receive in your didUpdateLocations method because the timestamp of the new and previous location must be the same if these three values are 0.

In my own app, I skip cached locations in my didUpdateLocations method when they are older than 10 seconds (however, you should test how many seconds fit to your case). I also skip locations which are invalid (horizontalAccuracy < 0) and those with an horizontalAccuracy over 30 meters.

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    
        guard let mostRecentLocation = locations.last else {
            return
        }
        
        let age = -mostRecentLocation.timestamp.timeIntervalSinceNow
        
        // Filter cached locations, invalid ones and ones with accuracy over 30 meters
        if age > 10 || mostRecentLocation.horizontalAccuracy < 0 || mostRecentLocation.horizontalAccuracy > 30 {
            return
        }

        // ... Use your location 
}

This will not fix the library code which doesn't test for division by zero, but maybe this will help anyway.

from hckalmanfilter.

PguOrange avatar PguOrange commented on June 7, 2024

Hello,

Just if some one else got this error.
I have done some investigation and i find that i have a NaN error when my location (previous and current one) have the same timestamp.

If they have the same timestamps some we do a division by 0 and we got a NaN.
To prevent it you can just check your locations timestamps before use Kalman or update KalmanAlgo to set value to 0 if the interval between locations is 0

from hckalmanfilter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.