|
| 1 | +package frc.robot.Commands; |
| 2 | + |
| 3 | +import static edu.wpi.first.units.Units.*; |
| 4 | + |
| 5 | +import com.ctre.phoenix6.swerve.SwerveModule; |
| 6 | +import com.ctre.phoenix6.swerve.SwerveRequest; |
| 7 | +import edu.wpi.first.epilogue.Logged; |
| 8 | +import edu.wpi.first.wpilibj2.command.Command; |
| 9 | +import frc.robot.config.TunerConstants; |
| 10 | +import frc.robot.subsystems.CommandSwerveDrivetrain; |
| 11 | + |
| 12 | +@Logged |
| 13 | +public class DrivetrainCommand extends Command { |
| 14 | + public static enum Position { |
| 15 | + TELEOP, |
| 16 | + STILL_SHOT |
| 17 | + } |
| 18 | + |
| 19 | + private CommandSwerveDrivetrain subsystem; |
| 20 | + private DrivetrainCommand.Position pose; |
| 21 | + private double leftY; |
| 22 | + private double leftX; |
| 23 | + private double rightX; |
| 24 | + |
| 25 | + private final double MaxSpeed = |
| 26 | + TunerConstants.kSpeedAt12Volts.in(MetersPerSecond); // kSpeedAt12Volts desired top speed |
| 27 | + private final double MaxAngularRate = |
| 28 | + RotationsPerSecond.of(0.75) |
| 29 | + .in(RadiansPerSecond); // 3/4 of a rotation per second max angular velocity |
| 30 | + private final SwerveRequest.FieldCentric drive = |
| 31 | + new SwerveRequest.FieldCentric() |
| 32 | + .withDeadband(MaxSpeed * 0.2) |
| 33 | + .withRotationalDeadband(MaxAngularRate * 0.1) // Add a 10% deadband |
| 34 | + .withDriveRequestType( |
| 35 | + SwerveModule.DriveRequestType |
| 36 | + .OpenLoopVoltage); // Use open-loop control for drive motors |
| 37 | + |
| 38 | + public DrivetrainCommand( |
| 39 | + CommandSwerveDrivetrain subsystem, |
| 40 | + DrivetrainCommand.Position pose, |
| 41 | + double leftX, |
| 42 | + double leftY, |
| 43 | + double rightX) { |
| 44 | + this.pose = pose; |
| 45 | + this.subsystem = subsystem; |
| 46 | + this.leftX = leftX; |
| 47 | + this.leftY = leftY; |
| 48 | + this.rightX = rightX; |
| 49 | + |
| 50 | + addRequirements(subsystem); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void initialize() { |
| 55 | + switch (pose) { |
| 56 | + case TELEOP: |
| 57 | + subsystem.applyRequest( |
| 58 | + () -> |
| 59 | + drive |
| 60 | + .withVelocityX(-leftY * MaxSpeed) // Drive forward with negative Y (forward) |
| 61 | + .withVelocityY(-leftX * MaxSpeed) // Drive left with negative X |
| 62 | + .withRotationalRate( |
| 63 | + -rightX * MaxAngularRate)); // Drive counterclockwise with negative X |
| 64 | + System.out.println("Drivetrain: Teleop Drive"); |
| 65 | + break; |
| 66 | + case STILL_SHOT: |
| 67 | + // make X with wheels |
| 68 | + System.out.println("Drivetrain: Still Shot Configuration"); |
| 69 | + break; |
| 70 | + default: |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void end(boolean interrupted) {} |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean isFinished() { |
| 80 | + return false; |
| 81 | + } |
| 82 | +} |
0 commit comments