Skip to content

Commit fa82027

Browse files
authored
Reset ProfiledPIDControllers at the same time that control begins (#119)
* reset each PID controller when (and only when) control begins * reset PID controllers upon enabling, use RobotModeTriggers consistently * move algae wrist to coralmode upon first enable
1 parent afe09a5 commit fa82027

File tree

8 files changed

+87
-79
lines changed

8 files changed

+87
-79
lines changed

src/main/java/frc/robot/Robot.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import edu.wpi.first.wpilibj2.command.ConditionalCommand;
2020
import edu.wpi.first.wpilibj2.command.InstantCommand;
2121
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
22+
import edu.wpi.first.wpilibj2.command.button.RobotModeTriggers;
2223
import edu.wpi.first.wpilibj2.command.button.Trigger;
2324
import frc.lib.AllianceSelector;
2425
import frc.lib.AutoOption;
@@ -44,8 +45,6 @@
4445
import frc.robot.elevator.AlgaeRoller;
4546
import frc.robot.elevator.CoralRoller;
4647
import frc.robot.elevator.Elevator;
47-
import frc.robot.elevator.ElevatorConstants.AlgaeWristConstants.AlgaeWristState;
48-
import frc.robot.elevator.ElevatorConstants.CoralWristConstants.CoralWristState;
4948
import frc.robot.elevator.Lifter;
5049
import frc.robot.vision.Vision;
5150
import frc.util.Gamepiece;
@@ -170,9 +169,6 @@ public void disabledPeriodic() {
170169
@Override
171170
public void autonomousInit() {
172171
autoSelector.scheduleAuto();
173-
climber.lockRatchet();
174-
elevator.getCoralWrist().createSetAngleCommand(CoralWristState.Initial).schedule();
175-
elevator.getAlgaeWrist().createSetAngleCommand(AlgaeWristState.Initial).schedule();
176172
lifter.setDefaultCommand(lifter.createRemainAtCurrentHeightCommand());
177173
leds.replaceDefaultCommandImmediately(
178174
leds.createStandardDisplayCommand(algaeModeSupplier, gamepieceSupplier));
@@ -184,18 +180,10 @@ public void autonomousPeriodic() {}
184180
@Override
185181
public void teleopInit() {
186182
autoSelector.cancelAuto();
187-
188-
swerve.resetHeadingOffset();
189-
climber.resetEncoder();
190-
climber.lockRatchet();
191-
elevator.resetPositionControllers();
192183
lifter.setDefaultCommand(lifter.createJoystickControlCommand(operator.getHID()));
193-
// lifter.setDefaultCommand(lifter.createJoystickVoltageCommand(operator.getHID()));
194184
leds.replaceDefaultCommandImmediately(
195185
leds.createStandardDisplayCommand(algaeModeSupplier, gamepieceSupplier));
196186

197-
elevator.getCoralWrist().createSetAngleCommand(CoralWristState.Initial).schedule();
198-
elevator.getAlgaeWrist().createSetAngleCommand(AlgaeWristState.Initial).schedule();
199187
// Test wrist feedforwards
200188
// algaeWrist.setDefaultCommand(algaeWrist.createJoystickControlCommand(operator.getHID()));
201189
// coralWrist.setDefaultCommand(coralWrist.createJoystickControlCommand(operator.getHID()));
@@ -334,14 +322,24 @@ public boolean getAsBoolean() {
334322
}
335323

336324
private void configureEventBindings() {
325+
RobotModeTriggers.autonomous()
326+
.onTrue(elevator.resetPositionControllers()
327+
.andThen(climber.lockRatchet()));
328+
RobotModeTriggers.teleop()
329+
.onTrue(swerve.resetHeadingOffset()
330+
.andThen(elevator.resetPositionControllers())
331+
.andThen(climber.lockRatchet())
332+
.andThen(climber.resetEncoder()));
333+
337334
algaeRoller.hasAlgae.whileTrue(algaeRoller.createHoldAlgaeCommand());
338335
coralRoller.isRolling.whileTrue(createRollerAnimationCommand());
339336
}
340-
// spotless:on
341337

342338
private void configureAutoOptions() {
343-
autoSelector.addAuto(new AutoOption(Alliance.Red, 1, new RedL4Auto(swerve, elevator)));
344-
autoSelector.addAuto(new AutoOption(Alliance.Blue, 1, new BlueL4Auto(swerve, elevator)));
339+
autoSelector.addAuto(
340+
new AutoOption(Alliance.Red, 1, new RedL4Auto(swerve, elevator)));
341+
autoSelector.addAuto(
342+
new AutoOption(Alliance.Blue, 1, new BlueL4Auto(swerve, elevator)));
345343
autoSelector.addAuto(
346344
new AutoOption(Alliance.Red, 2, new RedNoProcess3PieceAuto(swerve, elevator)));
347345
autoSelector.addAuto(
@@ -350,9 +348,12 @@ private void configureAutoOptions() {
350348
new AutoOption(Alliance.Red, 3, new RedProcess3PieceAuto(swerve, elevator)));
351349
autoSelector.addAuto(
352350
new AutoOption(Alliance.Blue, 3, new BlueProcess3PieceAuto(swerve, elevator)));
353-
autoSelector.addAuto(new AutoOption(Alliance.Blue, 4, new BlueMoveAuto(swerve)));
354-
autoSelector.addAuto(new AutoOption(Alliance.Red, 4, new RedMoveAuto(swerve)));
351+
autoSelector.addAuto(
352+
new AutoOption(Alliance.Blue, 4, new BlueMoveAuto(swerve)));
353+
autoSelector.addAuto(
354+
new AutoOption(Alliance.Red, 4, new RedMoveAuto(swerve)));
355355
}
356+
// spotless:on
356357

357358
/**
358359
* Gets the current drawn from the Power Distribution Hub by a CAN motor controller, assuming that

src/main/java/frc/robot/climber/Climber.java

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
1616
import edu.wpi.first.wpilibj2.command.Command;
1717
import edu.wpi.first.wpilibj2.command.FunctionalCommand;
18+
import edu.wpi.first.wpilibj2.command.InstantCommand;
1819
import edu.wpi.first.wpilibj2.command.SubsystemBase;
1920
import frc.robot.Constants.ClimberConstants;
2021
import frc.robot.Constants.MotorConstants.NEOVortexConstants;
@@ -83,16 +84,16 @@ public void periodic() {
8384
// SmartDashboard.putNumber("Climber/Applied Duty Cycle", motor.getAppliedOutput());
8485
}
8586

86-
public void resetEncoder() {
87-
encoder.setPosition(0);
87+
public Command resetEncoder() {
88+
return new InstantCommand(() -> encoder.setPosition(0));
8889
}
8990

90-
public void unlockRatchet() {
91-
servo.set(ClimberConstants.kDisengedPosition);
91+
public Command unlockRatchet() {
92+
return new InstantCommand(() -> servo.set(ClimberConstants.kDisengedPosition));
9293
}
9394

94-
public void lockRatchet() {
95-
servo.set(ClimberConstants.kEngagedPosition);
95+
public Command lockRatchet() {
96+
return new InstantCommand(() -> servo.set(ClimberConstants.kEngagedPosition));
9697
}
9798

9899
private void setPosition(double targetPosition) {
@@ -135,51 +136,43 @@ public Command createDefaultClimberCommand() {
135136
});
136137
}
137138

138-
public Command createUnlockCommand() {
139-
return this.run(() -> unlockRatchet());
140-
}
141-
142-
public Command createLockCommand() {
143-
return this.run(() -> lockRatchet());
144-
}
145-
146139
/**
147140
* @return Command that unlocks and deploys the climber arm
148141
*/
149142
public Command createDeployCommand() {
150143
return new FunctionalCommand(
151-
// initialize
152-
() -> {
153-
unlockRatchet();
154-
setPosition(ClimberConstants.kDeployPosition);
155-
},
156-
// execute
157-
() -> {},
158-
// end
159-
interrupted -> {
160-
lockRatchet();
161-
motor.set(0.0);
162-
},
163-
// isFinished
164-
() -> isDeployed(),
165-
// requirements
166-
this);
144+
// initialize
145+
() -> {
146+
setPosition(ClimberConstants.kDeployPosition);
147+
},
148+
// execute
149+
() -> {},
150+
// end
151+
interrupted -> {
152+
motor.set(0.0);
153+
},
154+
// isFinished
155+
() -> isDeployed(),
156+
// requirements
157+
this)
158+
.beforeStarting(unlockRatchet())
159+
.andThen(lockRatchet());
167160
}
168161

169162
public Command createRetractCommand() {
170163
return new FunctionalCommand(
171-
// initialze
172-
() -> {
173-
lockRatchet();
174-
setPosition(ClimberConstants.kRetractPosition);
175-
},
176-
// execute
177-
() -> {},
178-
// end
179-
interrupted -> {},
180-
// isFinished
181-
() -> isRetracted(),
182-
// requirements
183-
this);
164+
// initialze
165+
() -> {
166+
setPosition(ClimberConstants.kRetractPosition);
167+
},
168+
// execute
169+
() -> {},
170+
// end
171+
interrupted -> {},
172+
// isFinished
173+
() -> isRetracted(),
174+
// requirements
175+
this)
176+
.beforeStarting(lockRatchet());
184177
}
185178
}

src/main/java/frc/robot/drivetrain/Drivetrain.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import edu.wpi.first.units.measure.Dimensionless;
2222
import edu.wpi.first.units.measure.LinearVelocity;
2323
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
24+
import edu.wpi.first.wpilibj2.command.Command;
25+
import edu.wpi.first.wpilibj2.command.InstantCommand;
2426
import edu.wpi.first.wpilibj2.command.SubsystemBase;
2527
import frc.robot.Constants.AutoConstants.RotationControllerGains;
2628
import frc.robot.Constants.AutoConstants.TranslationControllerGains;
@@ -181,8 +183,8 @@ public Rotation2d getHeadingOffset() {
181183
return headingOffset;
182184
}
183185

184-
public void resetHeadingOffset() {
185-
headingOffset = new Rotation2d();
186+
public Command resetHeadingOffset() {
187+
return new InstantCommand(() -> this.headingOffset = new Rotation2d());
186188
}
187189

188190
public void setHeadingOffset() {

src/main/java/frc/robot/elevator/AlgaeWrist.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,12 @@ public Command createRemainAtCurrentAngleCommand() {
146146
return new FunctionalCommand(
147147
// initialize
148148
() -> {
149-
if (targetState == AlgaeWristState.Unknown) {
149+
if (targetState == AlgaeWristState.Initial) {
150150
targetState = AlgaeWristState.CoralMode;
151151
feedback.setGoal(targetState.angle.in(Radians));
152+
// Users should call reset() when they first start running the controller to avoid
153+
// unwanted behavior.
154+
resetController();
152155
}
153156
},
154157
// execute

src/main/java/frc/robot/elevator/CoralWrist.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ public Command createRemainAtCurrentAngleCommand() {
157157
return new FunctionalCommand(
158158
// initialize
159159
() -> {
160-
if (targetState == CoralWristState.Unknown) feedback.setGoal(encoder.getPosition());
160+
if (targetState == CoralWristState.Initial) {
161+
feedback.setGoal(encoder.getPosition());
162+
// Users should call reset() when they first start running the controller to avoid
163+
// unwanted behavior.
164+
resetController();
165+
}
161166
},
162167
// execute
163168
() -> control(),

src/main/java/frc/robot/elevator/Elevator.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import edu.wpi.first.wpilibj2.command.Command;
44
import edu.wpi.first.wpilibj2.command.Commands;
5+
import edu.wpi.first.wpilibj2.command.InstantCommand;
56
import frc.robot.elevator.ElevatorConstants.AlgaeWristConstants.AlgaeWristState;
67
import frc.robot.elevator.ElevatorConstants.CoralWristConstants.CoralWristState;
78
import frc.robot.elevator.ElevatorConstants.LifterConstants.LifterState;
@@ -36,10 +37,13 @@ public AlgaeWrist getAlgaeWrist() {
3637
return algaeWrist;
3738
}
3839

39-
public void resetPositionControllers() {
40-
lifter.resetController();
41-
coralWrist.resetController();
42-
algaeWrist.resetController();
40+
public Command resetPositionControllers() {
41+
return new InstantCommand(
42+
() -> {
43+
lifter.resetController();
44+
coralWrist.resetController();
45+
algaeWrist.resetController();
46+
});
4347
}
4448

4549
public Command coralL4PositionCG() {

src/main/java/frc/robot/elevator/ElevatorConstants.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public final class LifterController {
6262
}
6363

6464
public static enum LifterState {
65-
Unknown(0.0),
65+
Initial(0.0),
6666
Min(0.0),
6767
AlgaeIntakeFloor(1.0),
6868
EncoderReset(0.36),
@@ -149,7 +149,6 @@ public static final class CoralWristConstants {
149149

150150
public static enum CoralWristState {
151151
Initial(90),
152-
Unknown(90),
153152
Min(15),
154153
Max(135),
155154
L1(125),
@@ -234,7 +233,6 @@ public static final class AlgaeWristConstants {
234233

235234
public static enum AlgaeWristState {
236235
Initial(80),
237-
Unknown(90),
238236
Floor(0),
239237
Min(-10),
240238
Max(95),

src/main/java/frc/robot/elevator/Lifter.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class Lifter extends SubsystemBase {
5757
new ElevatorFeedforward(LifterController.kS, LifterController.kG, LifterController.kV);
5858

5959
private final EventLoop loop = new EventLoop();
60-
private LifterState targetState = LifterState.Unknown;
60+
private LifterState targetState = LifterState.Initial;
6161

6262
public Lifter() {
6363
// spotless:off
@@ -110,7 +110,8 @@ public void periodic() {
110110
// SmartDashboard.putNumber("Lifter/Velocity", encoder.getVelocity());
111111

112112
SmartDashboard.putString("Lifter/Target State", getTargetState().name());
113-
SmartDashboard.putNumber("Lifter/Target Height", feedback.getGoal().position);
113+
SmartDashboard.putNumber("Lifter/Height Setpoint", feedback.getSetpoint().position);
114+
SmartDashboard.putNumber("Lifter/Height Goal", feedback.getGoal().position);
114115

115116
// SmartDashboard.putNumber("Lifter/Leader Applied Duty Cycle", leaderMotor.getAppliedOutput());
116117
// SmartDashboard.putNumber(
@@ -126,10 +127,6 @@ public Distance getCurrentHeight() {
126127
return Meters.of(encoder.getPosition());
127128
}
128129

129-
private Distance getSetPointHeight() {
130-
return Meters.of(feedback.getSetpoint().position);
131-
}
132-
133130
public Dimensionless getProportionOfMaxHeight() {
134131
return getCurrentHeight().div(LifterState.Max.height);
135132
}
@@ -185,7 +182,12 @@ public Command createRemainAtCurrentHeightCommand() {
185182
return new FunctionalCommand(
186183
// initialize
187184
() -> {
188-
if (targetState == LifterState.Unknown) feedback.setGoal(encoder.getPosition());
185+
if (targetState == LifterState.Initial) {
186+
feedback.setGoal(encoder.getPosition());
187+
// Users should call reset() when they first start running the controller to avoid
188+
// unwanted behavior.
189+
resetController();
190+
}
189191
},
190192
// execute
191193
() -> control(),

0 commit comments

Comments
 (0)