Skip to content

Commit 8a47321

Browse files
committed
State Machine Diffy
Cleaner + allows live updating finally
1 parent 4de6f18 commit 8a47321

8 files changed

Lines changed: 72 additions & 70 deletions

File tree

TeamCode/src/main/java/org/firstinspires/ftc/teamcode/common/subsystems/drive/AutoPickup.kt

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,6 @@ class AutoPickup(
6060

6161
lastTarget = targetPose to (actualAngle / 360)
6262

63-
// robot.intake.diffy.roll = Diffy.hoverRoll + actualAngle / 360
64-
// println("${Diffy.hoverRoll + actualAngle / 360} $actualAngle ${target.angle}")
65-
// if (target.angle > 1) {
66-
// robot.intake.diffy.roll += rollSpeed * robot.deltaTime.deltaTime * target.angle
67-
// } else if (target.angle < -1) {
68-
// robot.intake.diffy.roll += rollSpeed * robot.deltaTime.deltaTime * target.angle
69-
// }
70-
7163
robot.telemetry.addData("target roll", Diffy.hoverRoll + actualAngle / 360)
7264
robot.telemetry.addData("actual angle", actualAngle)
7365
robot.telemetry.addData("target angle", target.angle)
@@ -81,17 +73,9 @@ class AutoPickup(
8173
}
8274
}
8375

84-
fun startScanning() =
85-
InstantCommand({
86-
scanning = true
87-
// robot.intake.diffy.isManual = true
88-
})
76+
fun startScanning() = InstantCommand({ scanning = true })
8977

90-
fun stopScanning() =
91-
InstantCommand({
92-
scanning = false
93-
// robot.intake.diffy.isManual = false
94-
})
78+
fun stopScanning() = InstantCommand({ scanning = false })
9579

9680
fun rumble() {
9781
if (lastTarget == null) return
@@ -114,7 +98,8 @@ class AutoPickup(
11498
robot.drive.pidManager.isOn = true
11599
robot.drive.pidManager.isSamplePickup = true
116100
robot.drive.pidManager.target = target.first
117-
robot.intake.diffy.pitch = Diffy.pickupPitch
101+
102+
robot.intake.diffy.state = Diffy.State.PICKUP
118103
robot.intake.diffy.roll = Diffy.hoverRoll + target.second
119104
}),
120105
WaitUntilCommand(robot.drive.pidManager::atTarget),

TeamCode/src/main/java/org/firstinspires/ftc/teamcode/common/subsystems/intake/Diffy.kt

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,67 @@ import org.firstinspires.ftc.teamcode.common.utils.hardware.AxonCR
1111
class Diffy(
1212
val robot: Robot,
1313
) : Subsystem() {
14-
val leftServo = AxonCR(robot.hardware, "diffyLeft", "analog2", false)
15-
val rightServo = AxonCR(robot.hardware, "diffyRight", "analog1")
14+
enum class State {
15+
TRANSFER,
16+
HOVER,
17+
PICKUP,
18+
SWEEP,
19+
MANUAL,
20+
DIRECT,
21+
}
22+
23+
val left = AxonCR(robot.hardware, "diffyLeft", "analog2", false)
24+
val right = AxonCR(robot.hardware, "diffyRight", "analog1")
1625

17-
var isManual: Boolean = false
26+
var state: State = State.TRANSFER
1827

19-
// each only has an effective range of +/- 0.25 aka 0-0.5 or half
20-
// as you need to ensure that pitch + roll never exceeds the bounds of 0 to 1
21-
var pitch: Double = transferPitch // up and down, -1 to 1
22-
var roll: Double = transferRoll // rotate, -1 to 1
28+
var pitch: Double = transferPitch // up and down, ideally -1 to 1
29+
var roll: Double = transferRoll // rotate, ideally -1 to 1
2330

24-
override fun init() {
25-
// periodic()
31+
override fun init() {}
32+
33+
// run in a loo during init
34+
fun initLoopable() {
35+
periodic()
2636
}
2737

2838
val pidLeft = PIDController(kP, kI, kD)
2939
val pidRight = PIDController(kP, kI, kD)
3040

3141
override fun periodic() {
32-
if (isManual) {
33-
return
34-
}
42+
if (state == State.DIRECT) return // direct control
3543

3644
pidLeft.setPID(kP, kI, kD)
3745
pidRight.setPID(kP, kI, kD)
3846

39-
val pitchPosition = pitch
40-
val leftPosition = pitchPosition - roll
41-
val rightPosition = pitchPosition + roll
42-
43-
leftServo.power = pidLeft.calculate(leftServo.position, leftPosition)
44-
rightServo.power = pidLeft.calculate(rightServo.position, rightPosition)
47+
val (pitch, roll) =
48+
when (state) {
49+
State.TRANSFER -> transferPitch to transferRoll
50+
State.HOVER -> hoverPitch to hoverPitch // use pickup for when you're autorotating
51+
State.PICKUP -> pickupPitch to this.roll
52+
State.SWEEP -> sweepPitch to sweepRoll
53+
else -> return
54+
}
55+
56+
// make sure relative offsets work
57+
this.pitch = pitch
58+
this.roll = roll
59+
60+
val leftTarget = pitch - roll
61+
val rightTarget = pitch + roll
62+
left.power = pidLeft.calculate(left.position, leftTarget)
63+
right.power = pidLeft.calculate(right.position, rightTarget)
4564
}
4665

47-
fun transfer() =
48-
InstantCommand({
49-
pitch = transferPitch
50-
roll = transferRoll
51-
})
52-
53-
fun hover() =
54-
InstantCommand({
55-
pitch = hoverPitch
56-
roll = hoverRoll
57-
})
58-
59-
fun pickup() =
60-
InstantCommand({
61-
pitch = pickupPitch
62-
})
63-
64-
fun sweep() =
65-
InstantCommand({
66-
pitch = sweepPitch
67-
roll = sweepRoll
68-
})
66+
fun transfer() = InstantCommand({ state = State.TRANSFER })
67+
68+
fun hover() = InstantCommand({ state = State.HOVER })
69+
70+
fun pickup() = InstantCommand({ state = State.PICKUP })
71+
72+
fun sweep() = InstantCommand({ state = State.SWEEP })
73+
74+
fun manual() = InstantCommand({ state = State.MANUAL })
6975

7076
companion object {
7177
@JvmField

TeamCode/src/main/java/org/firstinspires/ftc/teamcode/opmodes/auton/FasterSampleAuton.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class FasterSampleAuton : OpMode() {
3434
robot.outtake.claw.close()
3535
robot.outtake.claw.periodic()
3636
robot.intake.arm.periodic()
37-
robot.intake.diffy.periodic()
3837
robot.intake.claw.periodic()
3938

4039
val grab = {
@@ -148,6 +147,10 @@ class FasterSampleAuton : OpMode() {
148147
)
149148

150149
schedule(SequentialCommandGroup(*commands.toTypedArray()))
150+
151+
while (!isStarted()) {
152+
robot.intake.diffy.initLoopable()
153+
}
151154
}
152155

153156
companion object {

TeamCode/src/main/java/org/firstinspires/ftc/teamcode/opmodes/auton/SampleAuton.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class SampleAuton : OpMode() {
3434
robot.outtake.claw.close()
3535
robot.outtake.claw.periodic()
3636
robot.intake.arm.periodic()
37-
robot.intake.diffy.periodic()
3837
robot.intake.claw.periodic()
3938

4039
val grab = {
@@ -144,6 +143,10 @@ class SampleAuton : OpMode() {
144143
)
145144

146145
schedule(SequentialCommandGroup(*commands.toTypedArray()))
146+
147+
while (!isStarted()) {
148+
robot.intake.diffy.initLoopable()
149+
}
147150
}
148151

149152
companion object {

TeamCode/src/main/java/org/firstinspires/ftc/teamcode/opmodes/auton/SpecimenAuton.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ class SpecimenAuton : OpMode() {
5656
outtake.claw.close()
5757
outtake.claw.periodic()
5858
intake.arm.periodic()
59-
// intake.diffy.periodic()
6059
intake.claw.periodic()
6160

6261
fun readySpecimen() =
@@ -155,6 +154,10 @@ class SpecimenAuton : OpMode() {
155154
park(),
156155
),
157156
)
157+
158+
while (!isStarted()) {
159+
intake.diffy.initLoopable()
160+
}
158161
}
159162
}
160163

TeamCode/src/main/java/org/firstinspires/ftc/teamcode/opmodes/tuning/CameraTuner.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,6 @@ class SampleDectectionTuner : OpMode() {
212212
}
213213
if (targetPose != null) {
214214
if (rotationEnabled) {
215-
// if (targetAngle <= angleThres) {
216-
// robot.intake.diffy.roll = targetAngle
217-
// }
218215
val newAngle = -Math.toRadians(robot.intake.diffy.roll * 90)
219216
rotationalOffset = Vec2d(cos(newAngle), sin(newAngle) - 1.0).rotate(-90.0) * clawRadius
220217
robot.drive.pidManager.target = targetPose!! - rotationalOffset
@@ -227,9 +224,6 @@ class SampleDectectionTuner : OpMode() {
227224
roll = -centered.angle / 90.0
228225
robot.telemetry.addData("roll", roll)
229226
targetAngle = roll
230-
// if (rotationEnabled) {
231-
// robot.intake.diffy.roll = (robot.intake.diffy.roll + roll).clamp(-1.0, 1.0)
232-
// }
233227
}
234228
}
235229

TeamCode/src/main/java/org/firstinspires/ftc/teamcode/opmodes/tuning/DiffyTuner.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ class DiffyTuner : OpMode() {
2020
override val robot by lazy { DiffyOnly(this) }
2121

2222
override fun exec() {
23+
robot.diffy.state = Diffy.State.MANUAL
2324
robot.diffy.pitch = pitch
2425
robot.diffy.roll = roll
2526

26-
robot.telemetry.addData("left", robot.diffy.leftServo.position)
27-
robot.telemetry.addData("right", robot.diffy.rightServo.position)
27+
robot.telemetry.addData("left", robot.diffy.left.position)
28+
robot.telemetry.addData("right", robot.diffy.right.position)
2829
}
2930

3031
companion object {

TeamCode/src/main/java/org/firstinspires/ftc/teamcode/opmodes/tuning/IntakeArmTuner.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class IntakeArmTuner : OpMode() {
4141
IntakeArm.extendedPosition = (IntakeArm.extendedPosition + step).coerceIn(0.0, 1.0)
4242
} else if (state == 2) {
4343
IntakeArm.floorPosition = (IntakeArm.floorPosition + step).coerceIn(0.0, 1.0)
44+
} else if (state == 3) {
45+
IntakeArm.sweepPosition = (IntakeArm.sweepPosition + step).coerceIn(0.0, 1.0)
4446
}
4547
}
4648

@@ -52,6 +54,8 @@ class IntakeArmTuner : OpMode() {
5254
IntakeArm.extendedPosition = (IntakeArm.extendedPosition - step).coerceIn(0.0, 1.0)
5355
} else if (state == 2) {
5456
IntakeArm.floorPosition = (IntakeArm.floorPosition - step).coerceIn(0.0, 1.0)
57+
} else if (state == 3) {
58+
IntakeArm.sweepPosition = (IntakeArm.sweepPosition - step).coerceIn(0.0, 1.0)
5559
}
5660
}
5761
}
@@ -69,6 +73,9 @@ class IntakeArmTuner : OpMode() {
6973
} else if (state == 2) {
7074
robot.intakeArm.state = IntakeArmPosition.FLOOR
7175
robot.telemetry.addData("state", "floor")
76+
} else if (state == 3) {
77+
robot.intakeArm.state = IntakeArmPosition.SWEEP
78+
robot.telemetry.addData("state", "sweep")
7279
}
7380

7481
robot.telemetry.addData("position", robot.intakeArm.leftServo.position)

0 commit comments

Comments
 (0)