Created
January 6, 2019 13:15
-
-
Save mizutori/dea10c9e5602411f201bc2f5a0b39296 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun filterAndAddLocation(location: Location): Boolean { | |
val age = getLocationAge(location) | |
if (age > 5 * 1000) { //more than 5 seconds | |
Log.d(LOG_TAG, "Location is old") | |
oldLocationList.add(location) | |
return false | |
} | |
if (location.accuracy <= 0) { | |
Log.d(LOG_TAG, "Latitidue and longitude values are invalid.") | |
noAccuracyLocationList.add(location) | |
return false | |
} | |
//setAccuracy(newLocation.getAccuracy()); | |
val horizontalAccuracy = location.accuracy | |
if (horizontalAccuracy > 1000) { //10meter filter | |
Log.d(LOG_TAG, "Accuracy is too low.") | |
inaccurateLocationList.add(location) | |
return false | |
} | |
/* 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 | |
} | |
/* Notifiy predicted location to UI */ | |
val intent = Intent("PredictLocation") | |
intent.putExtra("location", predictedLocation) | |
LocalBroadcastManager.getInstance(this.application).sendBroadcast(intent) | |
Log.d(LOG_TAG, "Location quality is good enough.") | |
currentSpeed = location.speed | |
locationList.add(location) | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment