-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b8b9c2
commit 048b401
Showing
4 changed files
with
95 additions
and
1 deletion.
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
52 changes: 52 additions & 0 deletions
52
...c/main/java/com/kylecorry/andromeda/sense/orientation/filter/FilteredOrientationSensor.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,52 @@ | ||
package com.kylecorry.andromeda.sense.orientation.filter | ||
|
||
import com.kylecorry.andromeda.core.sensors.AbstractSensor | ||
import com.kylecorry.andromeda.sense.orientation.IOrientationSensor | ||
import com.kylecorry.sol.math.Quaternion | ||
|
||
class FilteredOrientationSensor( | ||
private val sensor: IOrientationSensor, | ||
private val filter: OrientationSensorFilter | ||
) : AbstractSensor(), IOrientationSensor { | ||
|
||
private val reading = Quaternion.zero.toFloatArray() | ||
private var hasReading = false | ||
|
||
override fun startImpl() { | ||
hasReading = false | ||
sensor.start(this::onSensorUpdate) | ||
} | ||
|
||
override fun stopImpl() { | ||
sensor.stop(this::onSensorUpdate) | ||
} | ||
|
||
override val hasValidReading: Boolean | ||
get() = sensor.hasValidReading | ||
|
||
override val headingAccuracy: Float? | ||
get() = null | ||
|
||
override val orientation: Quaternion | ||
get() = Quaternion.from(rawOrientation) | ||
|
||
override val rawOrientation: FloatArray | ||
get() = reading | ||
|
||
private fun onSensorUpdate(): Boolean { | ||
if (!hasReading) { | ||
sensor.rawOrientation.copyInto(reading) | ||
hasReading = true | ||
filter.reset(reading) | ||
notifyListeners() | ||
return true | ||
} | ||
|
||
val newReading = sensor.rawOrientation.copyOf() | ||
filter.filter(newReading, reading) | ||
|
||
notifyListeners() | ||
return true | ||
} | ||
} | ||
|
36 changes: 36 additions & 0 deletions
36
...n/java/com/kylecorry/andromeda/sense/orientation/filter/LowPassOrientationSensorFilter.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,36 @@ | ||
package com.kylecorry.andromeda.sense.orientation.filter | ||
|
||
import com.kylecorry.sol.math.Quaternion | ||
import com.kylecorry.sol.math.QuaternionMath | ||
|
||
class LowPassOrientationSensorFilter( | ||
var alpha: Float, | ||
var useSlerp: Boolean = true | ||
) : OrientationSensorFilter { | ||
|
||
val value: FloatArray | ||
get() = synchronized(lock) { | ||
_current.copyOf() | ||
} | ||
|
||
private val lock = Any() | ||
private val _current = Quaternion.zero.toFloatArray() | ||
|
||
override fun filter(quaternion: FloatArray, out: FloatArray) { | ||
synchronized(lock) { | ||
if (useSlerp) { | ||
QuaternionMath.slerp(_current, quaternion, alpha, _current, true) | ||
} else { | ||
QuaternionMath.lerp(_current, quaternion, alpha, _current, true) | ||
} | ||
QuaternionMath.normalize(_current, _current) | ||
_current.copyInto(out) | ||
} | ||
} | ||
|
||
override fun reset(value: FloatArray) { | ||
synchronized(lock) { | ||
value.copyInto(_current) | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...src/main/java/com/kylecorry/andromeda/sense/orientation/filter/OrientationSensorFilter.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,6 @@ | ||
package com.kylecorry.andromeda.sense.orientation.filter | ||
|
||
interface OrientationSensorFilter { | ||
fun filter(quaternion: FloatArray, out: FloatArray) | ||
fun reset(value: FloatArray) | ||
} |