Skip to content

Commit 70722d5

Browse files
committed
Fix dt issues; each hardware component now keeps track of its own dt.
1 parent 08acd99 commit 70722d5

File tree

22 files changed

+78
-68
lines changed

22 files changed

+78
-68
lines changed

TeamCode/src/main/kotlin/pioneer/Bot.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class Bot private constructor(
6060
)
6161
}
6262

63-
fun updateAll(dt: Double) {
64-
hardwareComponents.values.forEach { it.update(dt) }
63+
fun updateAll() {
64+
hardwareComponents.values.forEach { it.update() }
6565
}
6666

6767
// Companion for builder and fromType

TeamCode/src/main/kotlin/pioneer/Constants.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ object Constants {
194194

195195
@JvmField var MAX_POWER_RATE = 50.0
196196

197-
const val POSITION_TOLERANCE_TICKS = 40
197+
const val POSITION_TOLERANCE_TICKS = 50
198+
const val PID_TOLERANCE_TICKS = 20
198199
const val VELOCITY_TOLERANCE_TPS = 10
199200
const val TICKS_PER_REV = 8192
200201

@@ -215,8 +216,8 @@ object Constants {
215216
}
216217

217218
object ServoPositions {
218-
const val LAUNCHER_REST = 0.065
219-
const val LAUNCHER_TRIGGERED = 0.315
219+
const val LAUNCHER_REST = 0.3
220+
const val LAUNCHER_TRIGGERED = 0.7
220221
}
221222

222223
object TransferData {

TeamCode/src/main/kotlin/pioneer/hardware/Flywheel.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.qualcomm.robotcore.hardware.DcMotorSimple
66
import com.qualcomm.robotcore.hardware.HardwareMap
77
import org.firstinspires.ftc.robotcore.external.navigation.CurrentUnit
88
import pioneer.Constants
9+
import pioneer.helpers.Chrono
910
import pioneer.helpers.PIDController
1011
import pioneer.helpers.Pose
1112
import kotlin.math.cos
@@ -24,6 +25,8 @@ class Flywheel(
2425
Constants.Flywheel.KD,
2526
)
2627

28+
private val chrono = Chrono()
29+
2730
val motor: DcMotorEx
2831
get() = flywheel
2932

@@ -48,12 +51,12 @@ class Flywheel(
4851
}
4952
}
5053

51-
override fun update(dt: Double) {
54+
override fun update() {
5255
if (targetVelocity == 0.0) {
5356
flywheel.power = 0.0
5457
return
5558
}
56-
val correction = motorPID.update(targetVelocity - velocity, dt)
59+
val correction = motorPID.update(targetVelocity - velocity, chrono.dt)
5760
flywheel.power = Constants.Flywheel.KF * targetVelocity + correction
5861
// FileLogger.debug("Flywheel","Set Power: ${Constants.Flywheel.KF * targetVelocity + correction}")
5962
}

TeamCode/src/main/kotlin/pioneer/hardware/HardwareComponent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ interface HardwareComponent {
66

77
fun init()
88

9-
fun update(dt: Double) {}
9+
fun update() {}
1010
}

TeamCode/src/main/kotlin/pioneer/hardware/Intake.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.qualcomm.robotcore.hardware.DcMotorEx
55
import com.qualcomm.robotcore.hardware.DcMotorSimple
66
import com.qualcomm.robotcore.hardware.HardwareMap
77
import pioneer.Constants
8+
import pioneer.helpers.FileLogger
89

910
class Intake(
1011
private val hardwareMap: HardwareMap,

TeamCode/src/main/kotlin/pioneer/hardware/LaunchServos.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class LaunchServos(
3636
timer.reset()
3737
}
3838

39-
override fun update(dt: Double) {
39+
override fun update() {
4040
if (timer.milliseconds() < 250) {
4141
if (launch) {
4242
setPower(1.0)

TeamCode/src/main/kotlin/pioneer/hardware/Spindexer.kt

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.qualcomm.robotcore.hardware.HardwareMap
66
import com.qualcomm.robotcore.util.ElapsedTime
77
import pioneer.Constants
88
import pioneer.decode.Artifact
9+
import pioneer.helpers.Chrono
910
import pioneer.helpers.FileLogger
1011
import pioneer.helpers.PIDController
1112
import kotlin.math.PI
@@ -114,8 +115,7 @@ class Spindexer(
114115
val reachedTarget: Boolean
115116
get() {
116117
// Compute circular error
117-
val error = wrapTicks(targetMotorTicks - currentMotorTicks)
118-
return abs(error) < Constants.Spindexer.POSITION_TOLERANCE_TICKS && (motor.velocity < Constants.Spindexer.VELOCITY_TOLERANCE_TPS)
118+
return abs(motorTicksError) < Constants.Spindexer.POSITION_TOLERANCE_TICKS && (motor.velocity < Constants.Spindexer.VELOCITY_TOLERANCE_TPS)
119119
}
120120

121121
// Getters for artifact storage status
@@ -144,6 +144,9 @@ class Spindexer(
144144
val targetMotorTicks: Int
145145
get() = (motorState.radians * ticksPerRadian).toInt()
146146

147+
val motorTicksError: Int
148+
get() = wrapTicks(targetMotorTicks - currentMotorTicks)
149+
147150
val currentScannedArtifact: Artifact?
148151
get() = scanArtifact()
149152

@@ -172,6 +175,7 @@ class Spindexer(
172175
listOf(MotorPosition.OUTTAKE_1, MotorPosition.OUTTAKE_2, MotorPosition.OUTTAKE_3)
173176
private lateinit var motor: DcMotorEx
174177
private lateinit var intakeSensor: RevColorSensor
178+
private val chrono = Chrono(false)
175179

176180
/**
177181
* Returns the index of the current motor position in the intake/outtake lists.
@@ -207,8 +211,8 @@ class Spindexer(
207211
* Updates the motor position to match the desired motor state.
208212
* Checks for new artifacts if in an intake position.
209213
*/
210-
override fun update(dt: Double) {
211-
runMotorToState(dt)
214+
override fun update() {
215+
runMotorToState()
212216
if (checkingForNewArtifacts) checkForArtifact()
213217
}
214218

@@ -322,21 +326,21 @@ class Spindexer(
322326
return lastPower
323327
}
324328

325-
private fun runMotorToState(dt: Double) {
329+
private fun runMotorToState() {
326330
if (manualMove) return
327331

328-
val rawError = targetMotorTicks - currentMotorTicks
329-
val error = wrapTicks(rawError)
330-
331332
// PID
332-
var power = motorPID.update(error.toDouble(), dt)
333+
chrono.update()
334+
var power = motorPID.update(motorTicksError.toDouble(), chrono.dt)
333335

334336
// Ramp power to prevent sudden acceleration
335-
power = rampPower(power, dt)
337+
power = rampPower(power, chrono.dt)
336338

337339
// Static constant
338340
val adjustedKS = Constants.Spindexer.KS_START + Constants.Spindexer.KS_STEP * numStoredArtifacts
339-
power += if (abs(error) > 100) adjustedKS * sign(error.toDouble()) else 0.0
341+
power += if (abs(motorTicksError) > 100) adjustedKS * sign(motorTicksError.toDouble()) else 0.0
342+
343+
if (abs(motorTicksError) < Constants.Spindexer.PID_TOLERANCE_TICKS) power = 0.0
340344

341345
// Apply power
342346
val maxPower = 1.0

TeamCode/src/main/kotlin/pioneer/localization/Localizer.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ interface Localizer : HardwareComponent {
1515

1616
/**
1717
* Updates the pose of the robot based on sensor data
18-
* @param dt The time since the last update in seconds
1918
*/
20-
override fun update(dt: Double)
19+
override fun update()
2120

2221
/**
2322
* Resets the localizer to a specific pose

TeamCode/src/main/kotlin/pioneer/localization/localizers/Pinpoint.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit
77
import org.firstinspires.ftc.robotcore.external.navigation.Pose2D
88
import org.firstinspires.ftc.robotcore.external.navigation.UnnormalizedAngleUnit
99
import pioneer.Constants
10+
import pioneer.helpers.Chrono
1011
import pioneer.helpers.Pose
1112
import pioneer.localization.Localizer
1213

@@ -21,6 +22,8 @@ class Pinpoint(
2122
) : Localizer {
2223
override val name = "PinpointLocalizer"
2324

25+
private val chrono = Chrono(false)
26+
2427
override var pose: Pose = startPose
2528
override var prevPose: Pose = startPose.copy()
2629

@@ -40,7 +43,7 @@ class Pinpoint(
4043
pinpoint.update()
4144
}
4245

43-
override fun update(dt: Double) {
46+
override fun update() {
4447
pinpoint.update()
4548

4649
// Coordinate conversion: robot_x = -pinpoint_y, robot_y = pinpoint_x
@@ -49,14 +52,16 @@ class Pinpoint(
4952
val vx = -pinpoint.getVelY(DistanceUnit.CM)
5053
val vy = pinpoint.getVelX(DistanceUnit.CM)
5154

55+
chrono.update() // Update before using dt
56+
5257
// Numerical differentiation for acceleration
53-
val ax = (vx - prevPose.vx) / dt
54-
val ay = (vy - prevPose.vy) / dt
58+
val ax = (vx - prevPose.vx) / chrono.dt
59+
val ay = (vy - prevPose.vy) / chrono.dt
5560

5661
// Angular motion with coordinate conversion
5762
val theta = pinpoint.getHeading(AngleUnit.RADIANS)
5863
val omega = pinpoint.getHeadingVelocity(UnnormalizedAngleUnit.RADIANS)
59-
val alpha = (omega - prevPose.omega) / dt
64+
val alpha = (omega - prevPose.omega) / chrono.dt
6065

6166
prevPose = pose
6267
pose = Pose(x, y, vx, vy, ax, ay, theta, omega, alpha)

TeamCode/src/main/kotlin/pioneer/localization/localizers/ThreeWheelOdometry.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ThreeWheelOdometry(
4949
override val encoderYTicks: Int
5050
get() = prevCenterTicks
5151

52-
override fun update(dt: Double) {
52+
override fun update() {
5353
// Get current encoder values
5454
val dLeftCM = odoLeft.toCentimeters()
5555
val dRightCM = odoRight.toCentimeters()

0 commit comments

Comments
 (0)