-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Smoothen speed values by using data from two consecutive locations
- Loading branch information
Showing
5 changed files
with
74 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
40 changes: 40 additions & 0 deletions
40
app/src/main/java/xyz/malkki/neostumbler/scanner/source/SmoothenedGpsSpeedSource.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package xyz.malkki.neostumbler.scanner.source | ||
|
||
import android.location.Location | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import xyz.malkki.neostumbler.extensions.elapsedRealtimeMillisCompat | ||
import xyz.malkki.neostumbler.extensions.pairwise | ||
import xyz.malkki.neostumbler.scanner.speed.SpeedSource | ||
import kotlin.math.abs | ||
import kotlin.time.Duration.Companion.milliseconds | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
//Maximum age difference between two consecutive locations | ||
private val LOCATION_MAX_AGE_DIFF = 5.seconds | ||
|
||
//Smoothening factor, i.e. how much to weight previous speed vs. current speed | ||
private const val A = 0.15 | ||
private const val B = 1 - A | ||
|
||
/** | ||
* Calculates smoothened speed by using data from two consecutive locations | ||
*/ | ||
class SmoothenedGpsSpeedSource(private val locationFlow: Flow<Location>) : SpeedSource { | ||
override fun getSpeedFlow(): Flow<Double> { | ||
return locationFlow | ||
.pairwise() | ||
.map { (prevLocation, currentLocation) -> | ||
if (currentLocation.hasSpeed()) { | ||
//Smoothen the speed value by using data from two consecutive locations | ||
if (prevLocation.hasSpeed() && abs(prevLocation.elapsedRealtimeMillisCompat - currentLocation.elapsedRealtimeMillisCompat).milliseconds <= LOCATION_MAX_AGE_DIFF) { | ||
prevLocation.speed * A + currentLocation.speed * B | ||
} else { | ||
currentLocation.speed.toDouble() | ||
} | ||
} else { | ||
0.0 | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/xyz/malkki/neostumbler/scanner/speed/SpeedSource.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package xyz.malkki.neostumbler.scanner.speed | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
|
||
fun interface SpeedSource { | ||
/** | ||
* @return Flow emitting the speed in meters per second | ||
*/ | ||
fun getSpeedFlow(): Flow<Double> | ||
} |
This file contains 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