-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow use of non-integer and custom encoders #7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,11 @@ import kotlin.math.min | |
import kotlin.math.round | ||
|
||
class PositionVelocityPair( | ||
@JvmField val position: Int, @JvmField val velocity: Int, | ||
@JvmField val rawPosition: Int, @JvmField val rawVelocity: Int | ||
@JvmField val position: Double, @JvmField val velocity: Double, | ||
@JvmField val rawPosition: Double, @JvmField val rawVelocity: Double | ||
) | ||
|
||
sealed interface Encoder { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are the odometry sensors modeled as a different kind of encoder? What alternate implementations are you pondering and why shouldn't they be included in this module? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently in my road-runner-ftc fork I am representing the OTOS position and the Pinpoint raw encoder data as encoder implementations only for tuning. https://github.com/jdhs-ftc/road-runner-ftc-otos/blob/master/RoadRunner/src/main/java/com/acmerobotics/roadrunner/ftc/OTOS.kt and https://github.com/jdhs-ftc/road-runner-ftc-otos/blob/pinpoint/RoadRunner/src/main/java/com/acmerobotics/roadrunner/ftc/Pinpoint.kt (note: for the pinpoint I am currently using the raw odometry encoder passthrough for tuning, not the calculated pose, and the comments reflect that) |
||
interface Encoder { | ||
fun getPositionAndVelocity(): PositionVelocityPair | ||
|
||
val controller: DcMotorController | ||
|
@@ -23,7 +23,7 @@ sealed interface Encoder { | |
class RawEncoder(private val m: DcMotorEx) : Encoder { | ||
override var direction: DcMotorSimple.Direction = DcMotorSimple.Direction.FORWARD | ||
|
||
private fun applyDirection(x: Int): Int { | ||
private fun applyDirection(x: Double): Double { | ||
rbrott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var x = x | ||
if (m.direction == DcMotorSimple.Direction.REVERSE) { | ||
x = -x | ||
|
@@ -37,8 +37,8 @@ class RawEncoder(private val m: DcMotorEx) : Encoder { | |
} | ||
|
||
override fun getPositionAndVelocity(): PositionVelocityPair { | ||
val rawPosition = m.currentPosition | ||
val rawVelocity = m.velocity.toInt() | ||
val rawPosition = m.currentPosition.toDouble() | ||
val rawVelocity = m.velocity | ||
return PositionVelocityPair( | ||
applyDirection(rawPosition), | ||
applyDirection(rawVelocity), | ||
|
@@ -70,19 +70,19 @@ class RollingThreeMedian { | |
// by the time they reach here, they are widened into an int and possibly negated | ||
private const val CPS_STEP = 0x10000 | ||
|
||
private fun inverseOverflow(input: Int, estimate: Double): Int { | ||
private fun inverseOverflow(input: Int, estimate: Double): Double { | ||
// convert to uint16 | ||
var real = input and 0xFFFF | ||
// initial, modulo-based correction: it can recover the remainder of 5 of the upper 16 bits | ||
// because the velocity is always a multiple of 20 cps due to Expansion Hub's 50ms measurement window | ||
real += real % 20 / 4 * CPS_STEP | ||
// estimate-based correction: it finds the nearest multiple of 5 to correct the upper bits by | ||
real += round((estimate - real) / (5 * CPS_STEP)).toInt() * 5 * CPS_STEP | ||
return real | ||
return real.toDouble() | ||
} | ||
|
||
class OverflowEncoder(@JvmField val encoder: RawEncoder) : Encoder { | ||
private var lastPosition: Int = encoder.getPositionAndVelocity().position | ||
private var lastPosition: Double = encoder.getPositionAndVelocity().position | ||
private val lastUpdate = ElapsedTime() | ||
|
||
private val velEstimate = RollingThreeMedian() | ||
|
@@ -97,7 +97,7 @@ class OverflowEncoder(@JvmField val encoder: RawEncoder) : Encoder { | |
|
||
return PositionVelocityPair( | ||
p.position, | ||
inverseOverflow(p.velocity, v), | ||
inverseOverflow(p.velocity.toInt(), v), | ||
p.rawPosition, | ||
p.rawVelocity, | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import Plotly from 'plotly.js-dist-min'; | ||
import regression, { DataPoint } from 'regression'; | ||
import regression, {DataPoint} from 'regression'; | ||
|
||
// TODO: time-interpolate data | ||
|
||
|
@@ -78,6 +78,12 @@ function fixVels(ts: number[], xs: number[], vs: number[]) { | |
if (ts.length !== xs.length || ts.length !== vs.length) { | ||
throw new Error(`${ts.length} !== ${xs.length} !== ${vs.length}`); | ||
} | ||
// check if the second to last number in the data is non-integer | ||
if (xs[-2] !== Math.round(xs[-2])) { | ||
return numDerivOffline(ts, xs).map((est, i) => { | ||
return (vs[i + 1], est) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you intend for this to do? I'm assuming you don't want the comma operator here (or maybe I'm missing something). And doesn't this conflate "double positions" with "missing velocities"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My only goal with this change is to not run inverseOverflow on the data if it isn't from an integer source. I am honestly uncertain that this is a great way of detecting and doing that, and very open to feedback on better ways. |
||
}); | ||
} | ||
|
||
return numDerivOffline(ts, xs).map((est, i) => inverseOverflow(vs[i + 1], est)); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I intend for this class to represent encoder measurements in tick units (and really measurements made through a Rev hub given the raw fields). I would rather create new types and plumbing for sensors that don't fit this model.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. It may still be worth having some way of expanding it for other ways of reading, such as Octoquad or the pinpoint's raw encoder passthrough system.