Skip to content

Commit

Permalink
Fused altimeter improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Nov 25, 2023
1 parent 4e9f3f9 commit fb0502e
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kylecorry.trail_sense.shared.sensors.altimeter

import android.content.Context
import android.util.Log
import com.kylecorry.andromeda.core.coroutines.onDefault
import com.kylecorry.andromeda.core.sensors.AbstractSensor
import com.kylecorry.andromeda.core.sensors.IAltimeter
Expand Down Expand Up @@ -55,7 +56,7 @@ class FusedAltimeter2(
filteredPressure = 0f
gpsAltimeter.start(this::onGPSUpdate)
barometer.start(this::onBarometerUpdate)
updateTimer.interval(Duration.ofMillis(20))
updateTimer.interval(Duration.ofMillis(200))
}

override fun stopImpl() {
Expand All @@ -74,7 +75,7 @@ class FusedAltimeter2(
}

private fun updatePressure(pressure: Float) {
val filter = pressureFilter ?: LowPassFilter(0.8f, barometer.pressure)
val filter = pressureFilter ?: LowPassFilter(0.1f, barometer.pressure)
pressureFilter = filter
filteredPressure = filter.filter(pressure)
}
Expand Down Expand Up @@ -127,6 +128,8 @@ class FusedAltimeter2(
Distance.meters(filter.value)
)
)

// Log.d("FusedAltimeter", "Altitude: ${filter.value}m, GPS: ${gpsAltimeter.altitude}m, Barometer: ${barometricAltitude}m, Sea level: ${seaLevel.pressure}hPa")
return true
}

Expand All @@ -145,15 +148,21 @@ class FusedAltimeter2(

private fun setLastSeaLevelPressure(pressure: Pressure) {
cache.putFloat(LAST_SEA_LEVEL_PRESSURE_KEY, pressure.pressure)
cache.putInstant(LAST_SEA_LEVEL_PRESSURE_TIME_KEY, Instant.now())

// Only update the time if the GPS was used - this prevents only using the barometer over the long term
if (gpsAltimeter.hasValidReading) {
cache.putInstant(LAST_SEA_LEVEL_PRESSURE_TIME_KEY, Instant.now())
}
}

companion object {
private const val LAST_SEA_LEVEL_PRESSURE_KEY =
"cache_fused_altimeter_last_sea_level_pressure"
private const val LAST_SEA_LEVEL_PRESSURE_TIME_KEY =
"cache_fused_altimeter_last_sea_level_pressure_time"
private val SEA_LEVEL_EXPIRATION = Duration.ofHours(1)

// The amount of time before the sea level pressure expires if not updated using the GPS
private val SEA_LEVEL_EXPIRATION = Duration.ofMinutes(1)

// 2% GPS, 98% barometer
private const val ALPHA = 0.02f
Expand Down

0 comments on commit fb0502e

Please sign in to comment.