@@ -6,12 +6,11 @@ import com.qualcomm.robotcore.hardware.HardwareMap
66import com.qualcomm.robotcore.util.ElapsedTime
77import pioneer.Constants
88import pioneer.decode.Artifact
9- import pioneer.helpers.Chrono
9+ import pioneer.helpers.FileLogger
1010import pioneer.helpers.PIDController
1111import kotlin.math.PI
1212import kotlin.math.abs
1313import kotlin.math.sign
14- import kotlin.time.DurationUnit
1514
1615/*
1716Positive ROT: CW due to under-mounted motor
@@ -79,7 +78,7 @@ class Spindexer(
7978
8079 for (position in MotorPosition .entries) {
8180 val targetTicks = (position.radians * ticksPerRadian).toInt()
82- val error = wrapTicks(targetTicks - currentMotorPosition ).toDouble()
81+ val error = wrapTicks(targetTicks - currentMotorTicks ).toDouble()
8382 if (abs(error) < abs(smallestError)) {
8483 smallestError = error
8584 closestPosition = position
@@ -94,7 +93,7 @@ class Spindexer(
9493 // Check if motor position matches any outtake position
9594 for (position in outtakePositions) {
9695 val targetTicks = (position.radians * ticksPerRadian).toInt()
97- val error = wrapTicks(targetTicks - currentMotorPosition )
96+ val error = wrapTicks(targetTicks - currentMotorTicks )
9897 if (abs(error) < 300.0 ) {
9998 return true
10099 }
@@ -115,7 +114,7 @@ class Spindexer(
115114 val reachedTarget: Boolean
116115 get() {
117116 // Compute circular error
118- val error = wrapTicks(targetMotorPosition - currentMotorPosition )
117+ val error = wrapTicks(targetMotorTicks - currentMotorTicks )
119118 return abs(error) < Constants .Spindexer .POSITION_TOLERANCE_TICKS && (motor.velocity < Constants .Spindexer .VELOCITY_TOLERANCE_TPS )
120119 }
121120
@@ -130,21 +129,28 @@ class Spindexer(
130129 get() = _artifacts .count { it != null }
131130
132131 // Motor position accessors
133- val currentMotorPosition: Int
134- get() = motor.currentPosition - offsetTicks
132+ private val rawMotorTicks: Int
133+ get() {
134+ check(::motor.isInitialized)
135+ return motor.currentPosition
136+ }
137+
138+ val currentMotorTicks: Int
139+ get() = rawMotorTicks + offsetTicks
135140
136- val targetMotorPosition : Int
141+ val targetMotorTicks : Int
137142 get() = (motorState.radians * ticksPerRadian).toInt()
138143
139144 val currentScannedArtifact: Artifact ?
140145 get() = scanArtifact()
141146
142147 var checkingForNewArtifacts = true
143148
149+ var manualMove = false
150+
144151 // Private variables
145152 private var offsetTicks = 0
146153 private var lastPower = 0.0
147- private val chrono = Chrono (autoUpdate = false , units = DurationUnit .MILLISECONDS )
148154 private val ticksPerRadian: Int = (Constants .Spindexer .TICKS_PER_REV / (2 * PI )).toInt()
149155 private val motorPID =
150156 PIDController (
@@ -157,7 +163,6 @@ class Spindexer(
157163 private var artifactSeen = false
158164 private var artifactWasSeenRecently = false
159165 private var lastStoredIndex = 0
160- private var manualMove = false
161166 private val intakePositions =
162167 listOf (MotorPosition .INTAKE_1 , MotorPosition .INTAKE_2 , MotorPosition .INTAKE_3 )
163168 private val outtakePositions =
@@ -199,9 +204,8 @@ class Spindexer(
199204 * Updates the motor position to match the desired motor state.
200205 * Checks for new artifacts if in an intake position.
201206 */
202- override fun update () {
203- chrono.update()
204- runMotorToState()
207+ override fun update (dt : Double ) {
208+ runMotorToState(dt)
205209 if (checkingForNewArtifacts) checkForArtifact()
206210 }
207211
@@ -261,6 +265,7 @@ class Spindexer(
261265 _artifacts [index] = null
262266 // Automatically move to intake if empty
263267 if (isEmpty) moveToNextOpenIntake()
268+ FileLogger .info(" Spindexer" , " Removed $artifact " )
264269 return artifact
265270 }
266271
@@ -314,17 +319,17 @@ class Spindexer(
314319 return lastPower
315320 }
316321
317- private fun runMotorToState () {
322+ private fun runMotorToState (dt : Double ) {
318323 if (manualMove) return
319324
320- val rawError = targetMotorPosition - currentMotorPosition
325+ val rawError = targetMotorTicks - currentMotorTicks
321326 val error = wrapTicks(rawError)
322327
323328 // PID
324- var power = motorPID.update(error.toDouble(), chrono. dt)
329+ var power = motorPID.update(error.toDouble(), dt)
325330
326331 // Ramp power to prevent sudden acceleration
327- power = rampPower(power, chrono. dt)
332+ power = rampPower(power, dt)
328333
329334 // Static constant
330335 val adjustedKS = Constants .Spindexer .KS_START + Constants .Spindexer .KS_STEP * numStoredArtifacts
@@ -418,6 +423,9 @@ class Spindexer(
418423 if (artifactVisibleTimer.milliseconds() >= Constants .Spindexer .CONFIRM_INTAKE_MS ) {
419424 storeArtifact(artifact)
420425
426+ // Log stored artifact
427+ FileLogger .info(" Spindexer" , " Stored $artifact " )
428+
421429 // Reset state
422430 artifactSeen = false
423431 artifactWasSeenRecently = false
0 commit comments