Skip to content

Commit

Permalink
Extract fused altimeter refresh interval to prefs
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Dec 1, 2023
1 parent 4789ca3 commit b30282c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ package com.kylecorry.trail_sense.settings.infrastructure

import android.content.Context
import com.kylecorry.andromeda.preferences.BooleanPreference
import com.kylecorry.andromeda.preferences.DurationPreference
import com.kylecorry.trail_sense.R
import java.time.Duration

class AltimeterPreferences(context: Context): PreferenceRepo(context) {
class AltimeterPreferences(context: Context) : PreferenceRepo(context) {

val useContinuousCalibration by BooleanPreference(
val useFusedAltimeterContinuousCalibration by BooleanPreference(
cache,
context.getString(R.string.pref_altimeter_continuous_calibration),
false
)

val fusedAltimeterForcedRecalibrationInterval by DurationPreference(
cache,
context.getString(R.string.pref_altimeter_forced_recalibration_interval),
Duration.ofHours(1)
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class SensorService(ctx: Context) {
context,
gps,
Barometer(context, ENVIRONMENT_SENSOR_DELAY),
useContinuousCalibration = userPrefs.altimeter.useContinuousCalibration
useContinuousCalibration = userPrefs.altimeter.useFusedAltimeterContinuousCalibration
)
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,23 @@ class FusedAltimeter(
}
val gpsWeight = if (useContinuousCalibration && hasPendingGPSUpdate) {
hasPendingGPSUpdate = false

// The weight is higher for more accurate GPS readings
val errorWeight = 1 - SolMath.map(gpsError, 0f, MAX_GPS_ERROR, MIN_ALPHA, MAX_ALPHA)
.coerceIn(MIN_ALPHA, MAX_ALPHA)

// The weight increases over time (up to a maximum at MAX_TIME_FOR_WEIGHT hours)
val timeSinceLastGPSUsed = getTimeSinceLastGPSUsed()?.seconds?.toFloat() ?: 0f
val timeWeight = SolMath.map(
timeSinceLastGPSUsed,
0f,
SEA_LEVEL_EXPIRATION.seconds.toFloat(),
MAX_TIME_FOR_WEIGHT.seconds.toFloat(),
0f,
MAX_ALPHA_TIME
).coerceIn(0f, MAX_ALPHA_TIME)
errorWeight + timeWeight

// Simply add the weights together and restrict to the range
(errorWeight + timeWeight).coerceIn(0f, 1f)
} else {
0f
}
Expand Down Expand Up @@ -223,7 +229,7 @@ class FusedAltimeter(
val time = cache.getInstant(LAST_SEA_LEVEL_PRESSURE_TIME_KEY) ?: return null

val timeSinceReading = Duration.between(time, Instant.now())
if (timeSinceReading > SEA_LEVEL_EXPIRATION || timeSinceReading.isNegative) {
if (timeSinceReading > prefs.altimeter.fusedAltimeterForcedRecalibrationInterval || timeSinceReading.isNegative) {
// Sea level pressure has expired
return null
}
Expand Down Expand Up @@ -260,12 +266,10 @@ class FusedAltimeter(
"cache_fused_altimeter_last_sea_level_pressure_time"
private const val LAST_GPS_USED_TIME_KEY = "cache_fused_altimeter_last_gps_used_time"

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

private const val MIN_ALPHA = 0.96f
private const val MAX_ALPHA = 0.99f
private const val MAX_ALPHA_TIME = 0.1f
private const val MAX_ALPHA_TIME = 0.4f
private val MAX_TIME_FOR_WEIGHT = Duration.ofHours(2)
private const val MAX_GPS_ERROR = 5f
private const val BAROMETER_SMOOTHING = 0.9f
private val UPDATE_FREQUENCY = Duration.ofMillis(200)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,7 @@
<string name="service_restart_channel_description">A notification to restart services</string>
<string name="restart_services_title">Restart services</string>
<string name="restart_services_message">Background services could not be restarted. Tap to restart.</string>
<string name="pref_altimeter_forced_recalibration_interval" translatable="false">pref_altimeter_forced_recalibration_interval</string>
<plurals name="map_group_summary">
<item quantity="one">%d map</item>
<item quantity="other">%d maps</item>
Expand Down

0 comments on commit b30282c

Please sign in to comment.