Skip to content

Commit 9c6c6dd

Browse files
committed
implemented PID Controller
1 parent 3d80b46 commit 9c6c6dd

File tree

1 file changed

+64
-63
lines changed

1 file changed

+64
-63
lines changed

src/main/java/frc/robot/subsystems/intake/Deploy.java

Lines changed: 64 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import com.revrobotics.spark.SparkMax;
1717
import com.revrobotics.spark.config.SparkBaseConfig.IdleMode;
1818
import com.revrobotics.spark.config.SparkMaxConfig;
19+
20+
import edu.wpi.first.math.controller.ProfiledPIDController;
21+
import edu.wpi.first.math.trajectory.TrapezoidProfile;
1922
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
2023
import edu.wpi.first.wpilibj2.command.Command;
2124
import edu.wpi.first.wpilibj2.command.Commands;
@@ -29,6 +32,7 @@ public class Deploy extends SubsystemBase {
2932

3033
SparkClosedLoopController closedLoopController = deployMotor.getClosedLoopController();
3134
RelativeEncoder deployEncoder = deployMotor.getEncoder();
35+
ProfiledPIDController controller;
3236

3337
SparkMaxConfig config = new SparkMaxConfig();
3438
double p = IntakeConstants.kP;
@@ -42,16 +46,9 @@ public class Deploy extends SubsystemBase {
4246
public Deploy() {
4347
config.smartCurrentLimit(50).idleMode(IdleMode.kBrake);
4448
config.encoder.positionConversionFactor(IntakeConstants.DEPLOY_RATIO);
45-
config
46-
.closedLoop
47-
.feedbackSensor(FeedbackSensor.kPrimaryEncoder)
48-
.p(p)
49-
.i(i)
50-
.d(d)
51-
.feedForward
52-
.kS(s)
53-
.kCos(g)
54-
.kV(v);
49+
50+
controller = new ProfiledPIDController(p, i, d, new TrapezoidProfile.Constraints(1.75, 0.75));
51+
5552
// .kCosRatio(IntakeConstants.DEPLOY_RATIO);
5653
deployMotor.configure(config, ResetMode.kResetSafeParameters, PersistMode.kPersistParameters);
5754
SmartDashboard.putNumber("INTAKE/Deploy Encoder", deployEncoder.getPosition());
@@ -74,53 +71,57 @@ public Deploy() {
7471
// }
7572
// Adds start and stop for deploying
7673
public Command deployCMD() {
77-
// Deploys fuel
78-
return Commands.sequence(
79-
Commands.runOnce(
80-
() -> {
81-
closedLoopController.setSetpoint(
82-
IntakeConstants.DEPLOY_POSITION.in(Radians), ControlType.kPosition);
83-
}, this),
84-
Commands.waitUntil(closedLoopController::isAtSetpoint),
85-
Commands.runOnce(() -> deployMotor.set(rest)))
86-
.withName("Deployed");
87-
// return Commands.deadline(
88-
// Commands.waitUntil(
89-
// () ->
90-
// deployEncoder.getPosition()
91-
// >= IntakeConstants.DEPLOY_POSITION - IntakeConstants.DEPLOY_TOLERANCE),
92-
// runCMD(IntakeConstants.DEPLOY_SPEED))
93-
// .andThen(runCMD(0));
74+
return run(() -> {
75+
deployMotor.set(
76+
controller.calculate(
77+
deployEncoder.getPosition(),
78+
IntakeConstants.DEPLOY_POSITION.in(Radians)));
79+
});
80+
81+
82+
// // Deploys fuel
83+
// return Commands.sequence(
84+
// Commands.runOnce(
85+
// () -> {
86+
// closedLoopController.setSetpoint(
87+
// IntakeConstants.DEPLOY_POSITION.in(Radians), ControlType.kPosition);
88+
// }, this),
89+
// Commands.waitUntil(closedLoopController::isAtSetpoint),
90+
// Commands.runOnce(() -> deployMotor.set(rest)))
91+
// .withName("Deployed");
9492
}
9593

9694
public Command readyCMD() {
97-
return Commands.runOnce(
98-
() -> {
99-
closedLoopController.setSetpoint(
100-
IntakeConstants.READY_POSITION.in(Radians), ControlType.kPosition);
101-
}, this)
102-
.withName("Ready");
103-
// return Commands.deadline(
104-
// Commands.waitUntil(
105-
// () ->
106-
// deployEncoder.getPosition()
107-
// >= IntakeConstants.READY_POSITION - IntakeConstants.DEPLOY_TOLERANCE),
108-
// runCMD(IntakeConstants.DEPLOY_SPEED))
109-
// .andThen(runCMD(0));
95+
return run(() -> {
96+
deployMotor.set(
97+
controller.calculate(
98+
deployEncoder.getPosition(),
99+
IntakeConstants.READY_POSITION.in(Radians)));
100+
});
101+
102+
// return Commands.runOnce(
103+
// () -> {
104+
// closedLoopController.setSetpoint(
105+
// IntakeConstants.READY_POSITION.in(Radians), ControlType.kPosition);
106+
// }, this)
107+
// .withName("Ready");
108+
110109
}
111110

112111
public Command undeployCMD() {
113-
return Commands.run(
114-
() -> {
115-
closedLoopController.setSetpoint(
116-
IntakeConstants.UP_POSITION.in(Radians), ControlType.kPosition);
117-
}, this)
118-
.withName("Retracted");
119-
// return Commands.deadline(
120-
// Commands.waitUntil(
121-
// () -> deployEncoder.getPosition() <= IntakeConstants.DEPLOY_TOLERANCE),
122-
// runCMD(0 - IntakeConstants.DEPLOY_SPEED))
123-
// .andThen(runCMD(0));
112+
return run(() -> {
113+
deployMotor.set(
114+
controller.calculate(
115+
deployEncoder.getPosition(),
116+
IntakeConstants.UP_POSITION.in(Radians)));
117+
});
118+
119+
// return Commands.run(
120+
// () -> {
121+
// closedLoopController.setSetpoint(
122+
// IntakeConstants.UP_POSITION.in(Radians), ControlType.kPosition);
123+
// }, this)
124+
// .withName("Retracted");
124125
}
125126

126127
public Command mannualCMD(DoubleSupplier speed) {
@@ -144,17 +145,17 @@ public void periodic() {
144145
"INTAKE/Deploy State",
145146
getCurrentCommand() == null ? "NONE" : getCurrentCommand().getName());
146147

147-
config
148-
.closedLoop
149-
.feedbackSensor(FeedbackSensor.kPrimaryEncoder)
150-
.p(p)
151-
.i(i)
152-
.d(d)
153-
.feedForward
154-
.kS(s)
155-
.kG(g)
156-
.kV(v);
157-
// .kCosRatio(IntakeConstants.DEPLOY_RATIO);
158-
deployMotor.configure(config, ResetMode.kResetSafeParameters, PersistMode.kNoPersistParameters);
148+
// config
149+
// .closedLoop
150+
// .feedbackSensor(FeedbackSensor.kPrimaryEncoder)
151+
// .p(p)
152+
// .i(i)
153+
// .d(d)
154+
// .feedForward
155+
// .kS(s)
156+
// .kG(g)
157+
// .kV(v);
158+
// // .kCosRatio(IntakeConstants.DEPLOY_RATIO);
159+
// deployMotor.configure(config, ResetMode.kResetSafeParameters, PersistMode.kNoPersistParameters);
159160
}
160161
}

0 commit comments

Comments
 (0)