@@ -4,13 +4,15 @@ import com.qualcomm.robotcore.hardware.DcMotor
44import com.qualcomm.robotcore.hardware.DcMotorEx
55import com.qualcomm.robotcore.hardware.DcMotorSimple
66import com.qualcomm.robotcore.hardware.HardwareMap
7- import org.firstinspires.ftc.robotcore.external.Const
87import pioneer.Constants
8+ import pioneer.helpers.Chrono
99import pioneer.helpers.MathUtils
10+ import pioneer.helpers.PIDController
1011import pioneer.helpers.Pose
1112import kotlin.math.PI
1213import kotlin.math.abs
1314import kotlin.math.cos
15+ import kotlin.math.sign
1416import kotlin.math.sin
1517
1618class Turret (
@@ -20,6 +22,13 @@ class Turret(
2022) : HardwareComponent {
2123 private lateinit var turret: DcMotorEx
2224
25+ private val tagTrackPID = PIDController (
26+ Constants .Turret .KP ,
27+ Constants .Turret .KI ,
28+ Constants .Turret .KD ,
29+ )
30+ private val chrono = Chrono ()
31+
2332 private val ticksPerRadian: Double = Constants .Turret .TICKS_PER_REV / (2 * PI )
2433
2534 enum class Mode {
@@ -58,6 +67,7 @@ class Turret(
5867 turret =
5968 hardwareMap.get(DcMotorEx ::class .java, motorName).apply {
6069 mode = DcMotor .RunMode .STOP_AND_RESET_ENCODER
70+ mode = DcMotor .RunMode .RUN_USING_ENCODER
6171 zeroPowerBehavior = DcMotor .ZeroPowerBehavior .FLOAT
6272 direction = DcMotorSimple .Direction .FORWARD
6373 }
@@ -88,14 +98,13 @@ class Turret(
8898
8999 var desiredAngle: Double
90100
91- if (overrideRange){
101+ if (overrideRange) {
92102 desiredAngle = angle
93103 } else {
94104 desiredAngle = MathUtils .normalizeRadians(angle, motorRange)
95105 }
96106
97107 // Logical ticks uses offset
98- val logicalCurrentTicks = currentTicks
99108 val logicalTargetTicks =
100109 (desiredAngle * ticksPerRadian).toInt()
101110 val rawTargetTicks = logicalTargetTicks - offsetTicks
@@ -111,16 +120,34 @@ class Turret(
111120 pose : Pose ,
112121 target : Pose ,
113122 ) {
114- val shootPose = pose + Pose (x = Constants .Turret .OFFSET * sin(- pose.theta), y = Constants .Turret .OFFSET * cos(- pose.theta)) +
123+ val shootPose = pose + Pose (
124+ x = Constants .Turret .OFFSET * sin(- pose.theta),
125+ y = Constants .Turret .OFFSET * cos(- pose.theta)
126+ ) +
115127 Pose (pose.vx * Constants .Turret .LAUNCH_TIME , pose.vy * Constants .Turret .LAUNCH_TIME )
116128 // General Angle (From robot 0 to target):
117129 val targetTheta = (shootPose angleTo target)
118130 val turretTheta = (PI / 2 + targetTheta) - shootPose.theta
119131 gotoAngle(MathUtils .normalizeRadians(turretTheta), 0.85 )
120132 }
121133
122- fun setCustomTarget (pose : Pose , distance : Double ): Pose {
123- val shootPose = pose + Pose (x = Constants .Turret .OFFSET * sin(- pose.theta), y = Constants .Turret .OFFSET * cos(- pose.theta)) +
134+ fun tagTrack (errorDegrees : Double? ) {
135+ if (errorDegrees == null ) {
136+ turret.power = 0.0
137+ return
138+ }
139+ val thetaError = Math .toRadians(errorDegrees)
140+ val power = tagTrackPID.update(thetaError, chrono.dt)
141+ val static = if (abs(power) > 0.001 ) Constants .Turret .KS * sign(power) else 0.0
142+ turret.mode = DcMotor .RunMode .RUN_USING_ENCODER
143+ turret.power = power + static
144+ }
145+
146+ fun setCustomTarget (pose : Pose , distance : Double ): Pose {
147+ val shootPose = pose + Pose (
148+ x = Constants .Turret .OFFSET * sin(- pose.theta),
149+ y = Constants .Turret .OFFSET * cos(- pose.theta)
150+ ) +
124151 Pose (pose.vx * Constants .Turret .LAUNCH_TIME , pose.vy * Constants .Turret .LAUNCH_TIME )
125152 val theta = shootPose.theta + currentAngle
126153 val targetPose = shootPose + Pose (x = distance * sin(theta), y = distance * - cos(theta))
0 commit comments