Skip to content

Instantly share code, notes, and snippets.

@mizutori
Last active January 6, 2019 13:18
Show Gist options
  • Save mizutori/9a1a584f96cd285caf78dee1b295e227 to your computer and use it in GitHub Desktop.
Save mizutori/9a1a584f96cd285caf78dee1b295e227 to your computer and use it in GitHub Desktop.
/* Kalman Filter */
var Qvalue: Float = 3.0f
val locationTimeInMillis = location.elapsedRealtimeNanos / 1000000
val elapsedTimeInMillis = locationTimeInMillis - runStartTimeInMillis
if (currentSpeed == 0.0f) {
Qvalue = 3.0f //3 meters per second
} else {
Qvalue = currentSpeed // meters per second
}
kalmanFilter.Process(location.latitude, location.longitude, location.accuracy, elapsedTimeInMillis, Qvalue)
val predictedLat = kalmanFilter.get_lat()
val predictedLng = kalmanFilter.get_lng()
val predictedLocation = Location("")//provider name is unecessary
predictedLocation.latitude = predictedLat//your coords of course
predictedLocation.longitude = predictedLng
val predictedDeltaInMeters = predictedLocation.distanceTo(location)
if (predictedDeltaInMeters > 60) {
Log.d(LOG_TAG, "Kalman Filter detects mal GPS, we should probably remove this from track")
kalmanFilter.consecutiveRejectCount += 1
if (kalmanFilter.consecutiveRejectCount > 3) {
kalmanFilter = KalmanLatLong(3f) //reset Kalman Filter if it rejects more than 3 times in raw.
}
kalmanNGLocationList.add(location)
return false
} else {
kalmanFilter.consecutiveRejectCount = 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment