Skip to content

Commit d8c859c

Browse files
mechanism code
1 parent 1061486 commit d8c859c

File tree

11 files changed

+119
-68
lines changed

11 files changed

+119
-68
lines changed

src/main/java/frc/robot/RobotContainer.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.pathplanner.lib.auto.AutoBuilder;
66
import com.pathplanner.lib.auto.NamedCommands;
7+
import edu.wpi.first.math.MathUtil;
78
import edu.wpi.first.math.geometry.Pose2d;
89
import edu.wpi.first.math.geometry.Rotation2d;
910
import edu.wpi.first.math.geometry.Transform2d;
@@ -68,6 +69,7 @@
6869
import frc.robot.utility.commands.CustomCommands;
6970
import java.util.Arrays;
7071
import java.util.function.BiConsumer;
72+
import java.util.function.DoubleSupplier;
7173
import org.littletonrobotics.junction.networktables.LoggedDashboardChooser;
7274

7375
/**
@@ -476,23 +478,22 @@ private void configureDriverControllerBindings(
476478
private void configureOperatorControllerBindings(CommandXboxController xbox) {
477479

478480
new Trigger(DriverStation::isEnabled)
479-
.onTrue(
480-
Commands.runOnce(() -> coralWrist.setConstraints(1, 3))
481-
.andThen(superstructure.runAction(Superstructure.State.STOW))
482-
.finallyDo(coralWrist::resetContraints));
481+
.onTrue(superstructure.runAction(Superstructure.State.STOW_HIGH));
483482

484483
xbox.back().onTrue(drive.runOnce(drive::stop).withName("CANCEL and stop"));
485484

486485
// Primary scoring
487486
xbox.leftTrigger()
488487
.whileTrue(superstructure.run(State.INTAKE).alongWith(superstructure.intake()));
489488

490-
xbox.rightTrigger().and(xbox.a().negate()).whileTrue(superstructure.outtake());
491-
xbox.rightTrigger().and(xbox.a()).whileTrue(superstructure.outtakeL1());
492-
493489
final BiConsumer<Trigger, Superstructure.State> configureOperatorControllerBindingLevel =
494490
(trigger, level) -> {
495-
trigger.whileTrue(superstructure.run(level));
491+
trigger.whileTrue(
492+
superstructure
493+
.run(level)
494+
.alongWith(
495+
Commands.waitUntil(superstructure::atGoal)
496+
.andThen(superstructure.runWheels(level).asProxy())));
496497
};
497498

498499
configureOperatorControllerBindingLevel.accept(xbox.y(), Superstructure.State.L4);
@@ -505,11 +506,17 @@ private void configureOperatorControllerBindings(CommandXboxController xbox) {
505506
// Intake
506507

507508
coralIntake.setDefaultCommand(superstructure.passiveIntake());
508-
new Trigger(() -> !JoystickUtil.isDeadband(xbox.getLeftX()))
509+
510+
DoubleSupplier intakeSpeed =
511+
() ->
512+
MathUtil.clamp(
513+
JoystickUtil.applyDeadband(-xbox.getLeftX())
514+
+ JoystickUtil.applyDeadband(-xbox.getLeftY()),
515+
-1,
516+
+1);
517+
new Trigger(() -> intakeSpeed.getAsDouble() != 0)
509518
.and(DriverStation::isTeleopEnabled)
510-
.whileTrue(
511-
Commands.run(
512-
() -> coralIntake.setMotors(JoystickUtil.applyDeadband(xbox.getRightX()))));
519+
.whileTrue(coralIntake.run(() -> coralIntake.setMotors(intakeSpeed.getAsDouble())));
513520

514521
// Hang
515522
hang.setDefaultCommand(hang.run(() -> hang.set(JoystickUtil.applyDeadband(xbox.getLeftX()))));

src/main/java/frc/robot/subsystems/superstructure/Superstructure.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,11 @@ public Superstructure(Elevator elevator, Wrist coralWrist, Intake coralIntake) {
7373
public Command run(State goal) {
7474
return runPrepare(goal)
7575
.andThen(Commands.idle(elevator, coralWrist))
76-
.finallyDo(
77-
() -> {
78-
elevator.setGoalHeightMeters(STOW_HEIGHT);
79-
coralWrist.setGoalRotation(STOW_CORAL_ANGLE);
80-
});
76+
.finallyDo(this::setPositionStow);
8177
}
8278

8379
public Command runAction(State newGoal) {
84-
return runPrepare(newGoal)
85-
.andThen(Commands.idle(elevator, coralWrist))
86-
.until(() -> elevator.atGoalHeight() && coralWrist.atGoal());
80+
return runPrepare(newGoal).andThen(Commands.idle(elevator, coralWrist)).until(this::atGoal);
8781
}
8882

8983
public Command runPrepare(State goal) {
@@ -98,6 +92,18 @@ public Command runPrepare(State goal) {
9892
};
9993
}
10094

95+
public Command runWheels(State goal) {
96+
return switch (goal) {
97+
case STOW -> stopIntake();
98+
case STOW_HIGH -> stopIntake();
99+
case L1 -> outtakeL1();
100+
case L2 -> outtake();
101+
case L3 -> outtake();
102+
case L4 -> outtake();
103+
case INTAKE -> intake();
104+
};
105+
}
106+
101107
public static final double L1_HEIGHT = 0.151148670108898;
102108
public static final Rotation2d L1_CORAL_ANGLE = Rotation2d.fromDegrees(0);
103109

@@ -115,6 +121,8 @@ public Command runPrepare(State goal) {
115121

116122
public static final double STOW_HEIGHT = 0.054886473109919;
117123
public static final Rotation2d STOW_CORAL_ANGLE = Rotation2d.fromDegrees(-90);
124+
125+
public static final double STOW_HEIGHT_HIGH = 0;
118126
public static final Rotation2d STOW_CORAL_ANGLE_HIGH = Rotation2d.fromDegrees(90);
119127

120128
public Command prepareL1() {
@@ -150,7 +158,7 @@ public Command stowLow() {
150158

151159
public Command stowHigh() {
152160
return Commands.parallel(
153-
elevator.runPositionPrepare(STOW_HEIGHT),
161+
elevator.runPositionPrepare(STOW_HEIGHT_HIGH),
154162
coralWrist.runPositionPrepare(STOW_CORAL_ANGLE_HIGH));
155163
}
156164

@@ -174,6 +182,15 @@ public Command stopIntake() {
174182
return coralIntake.runMotors(0);
175183
}
176184

185+
public boolean atGoal() {
186+
return elevator.atGoalHeight() && coralWrist.atGoal();
187+
}
188+
189+
public void setPositionStow() {
190+
elevator.setGoalHeightMeters(STOW_HEIGHT_HIGH);
191+
coralWrist.setGoalRotation(STOW_CORAL_ANGLE_HIGH);
192+
}
193+
177194
@Override
178195
public void periodic() {
179196
SmartDashboard.putNumber("Elevator Height Meters", elevator.getMeasuredHeightMeters());

src/main/java/frc/robot/subsystems/superstructure/SuperstructureVisualizer.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ public class SuperstructureVisualizer {
1515

1616
private final LoggedMechanism2d mechanism =
1717
new LoggedMechanism2d(
18-
Units.inchesToMeters(28.0), Units.feetToMeters(9.0), new Color8Bit(Color.kDarkGray));
18+
Units.inchesToMeters(28.0), Units.feetToMeters(5.0), new Color8Bit(Color.kDarkGray));
1919

2020
private final LoggedMechanismRoot2d root;
21-
private final LoggedMechanismLigament2d carriageMechanism;
2221
private final LoggedMechanismLigament2d elevatorMechanism;
22+
private final LoggedMechanismLigament2d carriage, carriageJoint;
2323
private final LoggedMechanismLigament2d coralWristLigament;
2424
private final LoggedMechanismLigament2d coralIntake;
2525

@@ -33,7 +33,7 @@ public SuperstructureVisualizer(String name, Color color) {
3333
new LoggedMechanismLigament2d(
3434
name + " Elevator", Units.inchesToMeters(26.0), 90, 4.0, new Color8Bit(color)));
3535

36-
carriageMechanism =
36+
carriage =
3737
elevatorMechanism.append(
3838
new LoggedMechanismLigament2d(
3939
name + " Carriage",
@@ -42,10 +42,19 @@ public SuperstructureVisualizer(String name, Color color) {
4242
4.0,
4343
new Color8Bit(color)));
4444

45+
carriageJoint =
46+
carriage.append(
47+
new LoggedMechanismLigament2d(
48+
name + " Carriage Joint",
49+
Units.inchesToMeters(4),
50+
90.0,
51+
4.0,
52+
new Color8Bit(color)));
53+
4554
coralWristLigament =
46-
carriageMechanism.append(
55+
carriageJoint.append(
4756
new LoggedMechanismLigament2d(
48-
name + " Algae Wrist", Units.inchesToMeters(9.0), 0.0, 4.0, new Color8Bit(color)));
57+
name + " Algae Wrist", Units.inchesToMeters(9.0), 0, 4.0, new Color8Bit(color)));
4958

5059
coralIntake =
5160
coralWristLigament.append(
@@ -55,7 +64,7 @@ public SuperstructureVisualizer(String name, Color color) {
5564

5665
public void update(double carriageHeight, double wristRadians, double coralIntakeOutput) {
5766
elevatorMechanism.setLength(carriageHeight);
58-
coralWristLigament.setAngle(new Rotation2d(-wristRadians).plus(Rotation2d.kCCW_Pi_2));
67+
coralWristLigament.setAngle(new Rotation2d(-wristRadians));
5968

6069
coralIntake.setLength(coralIntakeOutput * Units.inchesToMeters(3.0));
6170

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import edu.wpi.first.math.trajectory.TrapezoidProfile;
88
import edu.wpi.first.math.trajectory.TrapezoidProfile.Constraints;
99
import edu.wpi.first.math.trajectory.TrapezoidProfile.State;
10+
import edu.wpi.first.math.util.Units;
1011
import edu.wpi.first.wpilibj.Alert;
1112
import edu.wpi.first.wpilibj.DriverStation;
1213
import edu.wpi.first.wpilibj.Timer;
@@ -45,13 +46,14 @@ public class Elevator extends SubsystemBase {
4546
factory.getNumber("MaxAcceleration", ElevatorConstants.maxCarriageAcceleration);
4647

4748
private static final LoggedTunableNumber tolerance =
48-
factory.getNumber("Tolerance", ElevatorConstants.carriagePositionTolerance);
49+
factory.getNumber(
50+
"ToleranceInches", Units.metersToInches(ElevatorConstants.carriagePositionTolerance));
4951

5052
private static final LoggedTunableNumber staticCharacterizationVelocityThresh =
51-
new LoggedTunableNumber("Elevator/StaticCharacterizationVelocityThresh", 0.1);
53+
factory.getNumber("/StaticCharacterizationVelocityThresh", 0.1);
5254

5355
private static final LoggedTunableNumber hardStopVelocityThresh =
54-
new LoggedTunableNumber("Elevator/StaticCharacterizationVelocityThresh", 0.05);
56+
factory.getNumber("/StaticCharacterizationVelocityThresh", 0.05);
5557

5658
private final ElevatorIO io;
5759
private final ElevatorIOInputsAutoLogged inputs = new ElevatorIOInputsAutoLogged();
@@ -144,10 +146,11 @@ public void periodic() {
144146

145147
setpoint = profile.calculate(Constants.LOOP_PERIOD_SECONDS, setpoint, goal);
146148

147-
io.runPosition(
148-
setpoint.position / ElevatorConstants.drumRadius,
149-
feedforward.calculate(setpoint.velocity));
149+
double feedforwardVolts = feedforward.calculate(setpoint.velocity);
150150

151+
io.runPosition(setpoint.position / ElevatorConstants.drumRadius, feedforwardVolts);
152+
153+
Logger.recordOutput("Wrist/Feedforward/Volts", feedforwardVolts);
151154
Logger.recordOutput("Elevator/Profile/SetpointPositionMeters", setpoint.position);
152155
Logger.recordOutput("Elevator/Profile/SetpointVelocityMetersPerSec", setpoint.velocity);
153156
Logger.recordOutput("Elevator/Profile/GoalPositionMeters", goal.position);
@@ -156,6 +159,7 @@ public void periodic() {
156159
} else {
157160
Logger.recordOutput("Elevator/Profile/ShouldRunProfiled", false);
158161

162+
Logger.recordOutput("Wrist/FeedForward/Volts", 0);
159163
Logger.recordOutput("Elevator/Profile/SetpointPositionMeters", 0.0);
160164
Logger.recordOutput("Elevator/Profile/SetpointVelocityMetersPerSec", 0.0);
161165
Logger.recordOutput("Elevator/Profile/GoalPositionMeters", 0.0);
@@ -199,7 +203,8 @@ public double getHeightPercent() {
199203

200204
@AutoLogOutput(key = "Elevator/atGoal")
201205
public boolean atGoalHeight() {
202-
return MathUtil.isNear(getGoalHeightMeters(), getGoalHeightMeters(), tolerance.get());
206+
return MathUtil.isNear(
207+
getMeasuredHeightMeters(), getGoalHeightMeters(), Units.inchesToMeters(tolerance.get()));
203208
}
204209

205210
public State getGoal() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static record ElevatorConfig(int leaderCanId, int followerCanId, boolean
3131
public static final double maxCarriageVelocity = 3.0;
3232
public static final double maxCarriageAcceleration = 3.5;
3333

34-
public static final double carriagePositionTolerance = Units.inchesToMeters(1);
34+
public static final double carriagePositionTolerance = Units.inchesToMeters(0.2);
3535

3636
public static final PIDConstants pid =
3737
switch (Constants.getRobot()) {

src/main/java/frc/robot/subsystems/superstructure/elevator/ElevatorIOSim.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import edu.wpi.first.math.system.plant.LinearSystemId;
77
import edu.wpi.first.wpilibj.simulation.ElevatorSim;
88
import frc.robot.Constants;
9-
import java.util.Random;
109

1110
/** Simulation implementation of the TemplateIO. */
1211
public class ElevatorIOSim implements ElevatorIO {
@@ -24,7 +23,7 @@ public class ElevatorIOSim implements ElevatorIO {
2423
0.0,
2524
ElevatorConstants.carriageMaxHeight,
2625
true,
27-
new Random().nextDouble() * ElevatorConstants.carriageMaxHeight);
26+
0);
2827

2928
private double appliedVolts = 0.0;
3029

src/main/java/frc/robot/subsystems/superstructure/wrist/Wrist.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public class Wrist extends SubsystemBase {
4040
private static final LoggedTunableNumber kA =
4141
factory.getNumber("kA", WristConstants.FEEDFORWARD.kA());
4242

43+
private static final LoggedTunableNumber toleranceDegrees =
44+
factory.getNumber("ToleranceDegrees", WristConstants.TOLERANCE_DEGREES);
45+
4346
private static final LoggedTunableNumber maxVelocity =
4447
factory.getNumber("maxVelocity", WristConstants.MAX_VELOCITY);
4548
private static final LoggedTunableNumber maxAcceleration =
@@ -51,7 +54,7 @@ public class Wrist extends SubsystemBase {
5154
private TrapezoidProfile profile;
5255
private ArmFeedforward feedforward;
5356

54-
private State setpoint = new State();
57+
private State setpoint = null;
5558
private Supplier<State> goalSupplier = State::new;
5659

5760
private final Alert motorConnectedAlert =
@@ -67,7 +70,6 @@ public Wrist(WristIO io) {
6770

6871
io.setPID(kP.get(), kI.get(), kD.get());
6972
io.setBrakeMode(true);
70-
7173
this.feedforward = new ArmFeedforward(kS.get(), kG.get(), kV.get(), kA.get());
7274
}
7375

@@ -79,20 +81,36 @@ public void periodic() {
7981

8082
io.setBrakeMode(!disabledDebouncer.calculate(DriverStation.isDisabled()));
8183

82-
setpoint = profile.calculate(Constants.LOOP_PERIOD_SECONDS, setpoint, goalSupplier.get());
84+
if (DriverStation.isEnabled()) {
85+
Logger.recordOutput("Elevator/shouldRunProfiled", true);
86+
87+
if (setpoint == null) {
88+
setpoint = new State(inputs.positionRad, inputs.velocityRadPerSec);
89+
}
90+
91+
setpoint = profile.calculate(Constants.LOOP_PERIOD_SECONDS, setpoint, goalSupplier.get());
92+
93+
double feedforwardVolts = feedforward.calculate(setpoint.position, setpoint.velocity);
94+
95+
io.runPosition(setpoint.position, feedforwardVolts);
8396

84-
double feedforwardVolts = feedforward.calculate(setpoint.position, setpoint.velocity);
97+
Logger.recordOutput("Wrist/Feedforward/Volts", feedforwardVolts);
8598

86-
io.runPosition(setpoint.position, feedforwardVolts);
99+
Logger.recordOutput("Wrist/Profile/SetpointPositionRotations", setpoint.position);
100+
Logger.recordOutput("Wrist/Profile/SetpointVelocityRPM", setpoint.velocity);
101+
Logger.recordOutput("Wrist/Profile/GoalPositionRotations", goalSupplier.get().position);
102+
Logger.recordOutput("Wrist/Profile/GoalVelocityRPM", goalSupplier.get().velocity);
103+
} else {
104+
Logger.recordOutput("Elevator/shouldRunProfiled", false);
87105

88-
Logger.recordOutput("Wrist/FeedForward/Volts", feedforwardVolts);
89-
Logger.recordOutput(
90-
"Wrist/FeedForward/MeasuredVolts[Test]", feedforward.calculate(inputs.positionRad, 0));
106+
Logger.recordOutput("Wrist/FeedForward/Volts", 0);
107+
Logger.recordOutput("Elevator/Profile/SetpointPositionMeters", 0.0);
108+
Logger.recordOutput("Elevator/Profile/SetpointVelocityMetersPerSec", 0.0);
109+
Logger.recordOutput("Elevator/Profile/GoalPositionMeters", 0.0);
110+
Logger.recordOutput("Elevator/Profile/GoalVelocityMetersPerSec", 0.0);
91111

92-
Logger.recordOutput("Wrist/Profile/SetpointPositionRotations", setpoint.position);
93-
Logger.recordOutput("Wrist/Profile/SetpointVelocityRPM", setpoint.velocity);
94-
Logger.recordOutput("Wrist/Profile/GoalPositionRotations", goalSupplier.get().position);
95-
Logger.recordOutput("Wrist/Profile/GoalVelocityRPM", goalSupplier.get().velocity);
112+
setpoint = new State(inputs.positionRad, inputs.velocityRadPerSec);
113+
}
96114

97115
LoggedTunableNumber.ifChanged(
98116
hashCode(),
@@ -140,7 +158,9 @@ public void setGoalSupplier(Supplier<State> positionSupplier) {
140158
@AutoLogOutput(key = "Wrist/atGoal")
141159
public boolean atGoal() {
142160
return MathUtil.isNear(
143-
inputs.positionRad, goalSupplier.get().position, WristConstants.TOLERANCE_DEGREES);
161+
inputs.positionRad,
162+
goalSupplier.get().position,
163+
Units.degreesToRadians(toleranceDegrees.get()));
144164
}
145165

146166
/** Get position in rotations */
@@ -175,7 +195,7 @@ public void setConstraints(double maxVelocity, double maxAcceleration) {
175195
profile = new TrapezoidProfile(new Constraints(maxVelocity, maxAcceleration));
176196
}
177197

178-
public void resetContraints() {
198+
public void resetConstraints() {
179199
setConstraints(WristConstants.MAX_VELOCITY, WristConstants.MAX_ACCELERATION);
180200
}
181201
}

0 commit comments

Comments
 (0)