Retry for SE2

This commit is contained in:
NoahLacks63 2024-12-18 02:06:22 +00:00
parent 496891cce4
commit 31ef6bb688
5 changed files with 139 additions and 109 deletions

View File

@ -19,109 +19,111 @@ import frc.robot.subsystems.Intake;
import frc.robot.subsystems.Shooter; import frc.robot.subsystems.Shooter;
public class RobotContainer { public class RobotContainer {
private Drivetrain drivetrain; private Drivetrain drivetrain;
private Indexer indexer; private Indexer indexer;
private Intake intake; private Intake intake;
private Shooter shooter; private Shooter shooter;
private CommandXboxController driver; private CommandXboxController driver;
//constructor// //constructor//
public RobotContainer() { public RobotContainer() {
drivetrain = new Drivetrain(); drivetrain = new Drivetrain();
indexer = new Indexer(); indexer = new Indexer();
intake = new Intake(); intake = new Intake();
shooter = new Shooter(); shooter = new Shooter();
driver = new CommandXboxController(OIConstants.kDriverUSB); driver = new CommandXboxController(OIConstants.kDriverUSB);
configureBindings(); configureBindings();
configureShuffleboard(); configureShuffleboard();
} }
private void configureBindings() { private void configureBindings() {
//default commands// //default commands//
drivetrain.setDefaultCommand( drivetrain.setDefaultCommand(
drivetrain.driveArcade( drivetrain.driveArcade(
null, driver::getLeftX,
null driver::getLeftY
) )
); );
indexer.setDefaultCommand(indexer.stopIndexer()); indexer.setDefaultCommand(indexer.stopIndexer());
intake.setDefaultCommand(intake.stopIntake()); intake.setDefaultCommand(intake.stopIntake());
shooter.setDefaultCommand(shooter.stopShooter()); shooter.setDefaultCommand(shooter.runShooter(driver::getRightTriggerAxis));
//driver inputs// //driver inputs//
driver.rightTrigger().whileTrue( driver.a().whileTrue(
shooter.runShooter(driver.getRightTriggerAxis()) intake.runIntake(1)
); .alongWith(indexer.runIndexer(() -> 1))
);
driver.a().whileTrue( driver.b().whileTrue(
intake.runIntake(1) intake.runIntake(-1)
.alongWith(indexer.runIndexer(1)) .alongWith(indexer.runIndexer(() -> -1))
); );
driver.b().whileTrue( driver.x().onTrue(smartShooter());
intake.runIntake(-1) }
.alongWith(indexer.runIndexer(-1))
);
driver.x().onTrue(smartShooter()); //configures shuffleboard//
} private void configureShuffleboard() {
ShuffleboardTab robotIndicatorTab = Shuffleboard.getTab(OIConstants.kRobotIndicatorsTabName);
//configures shuffleboard// robotIndicatorTab.addDouble("Left Drivetrain Encoder Distance", drivetrain::getLeftEncoderPos)
private void configureShuffleboard() { .withPosition(0, 0)
ShuffleboardTab robotIndicatorTab = Shuffleboard.getTab(OIConstants.kRobotIndicatorsTabName); .withSize(2, 1)
.withWidget(BuiltInWidgets.kTextView);
robotIndicatorTab.addDouble("Left Drivetrain Encoder Distance", drivetrain::getLeftEncoder) robotIndicatorTab.addDouble("Right Drivetrain Encoder Distance", drivetrain::getRightEncoderPos)
.withPosition(0, 0) .withPosition(2, 0)
.withSize(2, 1) .withSize(2, 1)
.withWidget(BuiltInWidgets.kTextView); .withWidget(BuiltInWidgets.kTextView);
robotIndicatorTab.addDouble("Right Drivetrain Encoder Distance", drivetrain::getRightEncoder) robotIndicatorTab.addDouble("Drivetrain Gyro", drivetrain::getGyroAngle)
.withPosition(2, 0) .withPosition(0, 1)
.withSize(2, 1) .withSize(2, 1)
.withWidget(BuiltInWidgets.kTextView); .withWidget(BuiltInWidgets.kGyro);
robotIndicatorTab.addDouble("Drivetrain Gyro", drivetrain::getGyroAngle) robotIndicatorTab.addDouble("Top Shooter Encoder Rate", shooter::getFrontEncoderRate)
.withPosition(0, 1) .withPosition(4, 0)
.withSize(2, 1) .withSize(2, 2)
.withWidget(BuiltInWidgets.kGyro); .withWidget(BuiltInWidgets.kDial)
.withProperties(Map.of(
"Max", ShooterConstants.kShooterFireSpeed)
);
robotIndicatorTab.addDouble("Top Shooter Encoder Rate", shooter::getFrontEncoder) robotIndicatorTab.addDouble("Bottom Shooter Encoder Rate", shooter::getFrontEncoderRate)
.withPosition(4, 0) .withPosition(6, 0)
.withSize(2, 2) .withSize(2, 2)
.withWidget(BuiltInWidgets.kDial) .withWidget(BuiltInWidgets.kDial)
.withProperties(Map.of( .withProperties(Map.of(
"Max", ShooterConstants.kShooterFireSpeed) "Max", ShooterConstants.kShooterFireSpeed)
); );
}
robotIndicatorTab.addDouble("Bottom Shooter Encoder Rate", shooter::getFrontEncoder) //moves, rotates, runs auto shooter//
.withPosition(6, 0) public Command getAutonomousCommand() {
.withSize(2, 2) drivetrain.resetGyro();
.withWidget(BuiltInWidgets.kDial) drivetrain.resetDriveEncoders();
.withProperties(Map.of(
"Max", ShooterConstants.kShooterFireSpeed)
);
}
public Command getAutonomousCommand() { return drivetrain.moveDistance(1.5, 5)
return drivetrain.moveDistance(1.5, 5) .andThen(
.andThen(drivetrain.goToAngle(45, 2)) drivetrain.goToAngle(
.andThen(shooter.runAutoShooter()) 45,
.until(shooter::shooterAtSpeed) 2
.andThen(shooter.runAutoShooter()) ),
.alongWith(indexer.runIndexer(1)) smartShooter()
.withTimeout(2); );
} }
//smart commands// //smart commands//
private Command smartShooter() { private Command smartShooter() {
return shooter.runAutoShooter() return shooter.runAutoShooter()
.until(shooter::shooterAtSpeed) .until(shooter::shooterAtSpeed)
.andThen(shooter.runAutoShooter()) .andThen(
.alongWith(indexer.runIndexer(1)) shooter.runAutoShooter(),
.withTimeout(2); indexer.runIndexer(() -> 1)
} ).withTimeout(2);
}
} }

View File

@ -25,4 +25,5 @@ public class ShooterConstants {
public static final int kFFG = 0; public static final int kFFG = 0;
public static final int kFFV = 0; public static final int kFFV = 0;
public static final int kBangBangScale = 8;
} }

View File

@ -14,7 +14,6 @@ import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.constants.DrivetrainConstants; import frc.robot.constants.DrivetrainConstants;
public class Drivetrain extends SubsystemBase { public class Drivetrain extends SubsystemBase {
//creates instance variables// //creates instance variables//
private CANSparkMax leftFront; private CANSparkMax leftFront;
private CANSparkMax leftRear; private CANSparkMax leftRear;
@ -27,12 +26,12 @@ public class Drivetrain extends SubsystemBase {
private ADXRS450_Gyro gyro; private ADXRS450_Gyro gyro;
private PIDController turnController; private PIDController turnController;
private PIDController distanceController; private PIDController rightDistanceController;
private PIDController leftDistanceController;
private DifferentialDrive drive; private DifferentialDrive drive;
public Drivetrain() { public Drivetrain() {
//creates instances of each motor// //creates instances of each motor//
leftFront = new CANSparkMax(DrivetrainConstants.kLeftFrontCAN, MotorType.kBrushless); leftFront = new CANSparkMax(DrivetrainConstants.kLeftFrontCAN, MotorType.kBrushless);
leftRear = new CANSparkMax(DrivetrainConstants.kLeftRearCAN, MotorType.kBrushless); leftRear = new CANSparkMax(DrivetrainConstants.kLeftRearCAN, MotorType.kBrushless);
@ -57,29 +56,48 @@ public class Drivetrain extends SubsystemBase {
); );
turnController.setTolerance(DrivetrainConstants.kTurnControllerTolerance); turnController.setTolerance(DrivetrainConstants.kTurnControllerTolerance);
distanceController = new PIDController( rightDistanceController = new PIDController(
DrivetrainConstants.kDistanceControllerP, DrivetrainConstants.kDistanceControllerP,
DrivetrainConstants.kDistanceControllerI, DrivetrainConstants.kDistanceControllerI,
DrivetrainConstants.kDistanceControllerD DrivetrainConstants.kDistanceControllerD
); );
distanceController.setTolerance(DrivetrainConstants.kDistanceControllerTolerance); rightDistanceController.setTolerance(DrivetrainConstants.kDistanceControllerTolerance);
leftDistanceController = new PIDController(
DrivetrainConstants.kDistanceControllerP,
DrivetrainConstants.kDistanceControllerI,
DrivetrainConstants.kDistanceControllerD
);
leftDistanceController.setTolerance(DrivetrainConstants.kDistanceControllerTolerance);
drive = new DifferentialDrive(leftFront, rightFront); drive = new DifferentialDrive(leftFront, rightFront);
} }
//getters/ //gyro methods//
public double getGyroAngle() { public double getGyroAngle() {
return gyro.getAngle() * (DrivetrainConstants.kInvertGyro ? -1 : 1); return gyro.getAngle() * (DrivetrainConstants.kInvertGyro ? -1 : 1);
} }
public double getLeftEncoder() { public void resetGyro() {
gyro.reset();
return;
}
//encoder methods//
public double getLeftEncoderPos() {
return leftFrontEncoder.getPosition(); return leftFrontEncoder.getPosition();
} }
public double getRightEncoder() { public double getRightEncoderPos() {
return rightFrontEncoder.getPosition(); return rightFrontEncoder.getPosition();
} }
public void resetDriveEncoders() {
leftFrontEncoder.setPosition(0);
rightFrontEncoder.setPosition(0);
return;
}
//method to turn drivetrain a certain angle// //method to turn drivetrain a certain angle//
public Command goToAngle(double angle, double timeout) { public Command goToAngle(double angle, double timeout) {
return run(() -> { return run(() -> {
@ -94,12 +112,16 @@ public class Drivetrain extends SubsystemBase {
//method to move drivetrain a certain distance// //method to move drivetrain a certain distance//
public Command moveDistance(double distance, double timeout) { public Command moveDistance(double distance, double timeout) {
return run(() -> { return run(() -> {
double output = distanceController.calculate( double outputL = rightDistanceController.calculate(
(leftFrontEncoder.getPosition() + rightFrontEncoder.getPosition()) / 2, (leftFrontEncoder.getPosition() + rightFrontEncoder.getPosition()) / 2,
distance distance
); );
drive.arcadeDrive(output, 0.0); double outputR = rightDistanceController.calculate(
}).until(distanceController::atSetpoint).withTimeout(timeout); (leftFrontEncoder.getPosition() + rightFrontEncoder.getPosition()) / 2,
distance
);
drive.tankDrive(outputL, outputR);
}).until(rightDistanceController::atSetpoint).withTimeout(timeout);
} }
//methods that take 2 doubles and convert them to motor commands// //methods that take 2 doubles and convert them to motor commands//

View File

@ -1,5 +1,7 @@
package frc.robot.subsystems; package frc.robot.subsystems;
import java.util.function.DoubleSupplier;
import com.ctre.phoenix.motorcontrol.can.WPI_VictorSPX; import com.ctre.phoenix.motorcontrol.can.WPI_VictorSPX;
import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.Command;
@ -15,9 +17,9 @@ public class Indexer extends SubsystemBase {
} }
//indexer commands// //indexer commands//
public Command runIndexer(double speed) { public Command runIndexer(DoubleSupplier speed) {
return run(() -> { return run(() -> {
motor1.set(speed); motor1.set(speed.getAsDouble());
}); });
} }

View File

@ -1,5 +1,7 @@
package frc.robot.subsystems; package frc.robot.subsystems;
import java.util.function.DoubleSupplier;
import com.ctre.phoenix.motorcontrol.can.WPI_VictorSPX; import com.ctre.phoenix.motorcontrol.can.WPI_VictorSPX;
import edu.wpi.first.math.controller.BangBangController; import edu.wpi.first.math.controller.BangBangController;
@ -14,6 +16,7 @@ public class Shooter extends SubsystemBase {
private WPI_VictorSPX rearMotor; private WPI_VictorSPX rearMotor;
private BangBangController bangBangController; private BangBangController bangBangController;
private SimpleMotorFeedforward motorFeedforward; private SimpleMotorFeedforward motorFeedforward;
private Encoder frontMotorEncoder; private Encoder frontMotorEncoder;
@ -50,33 +53,33 @@ public class Shooter extends SubsystemBase {
} }
//getters// //getters//
public double getFrontEncoder() { public double getFrontEncoderRate() {
return frontMotorEncoder.getRate(); return frontMotorEncoder.getRate();
} }
public double getRearEncoder() { public double getRearEncoderRate() {
return rearMotorEncoder.getRate(); return rearMotorEncoder.getRate();
} }
public boolean shooterAtSpeed() { public boolean shooterAtSpeed() {
return (frontMotorEncoder.getRate() + rearMotorEncoder.getRate()) / 2 >= ShooterConstants.kShooterFireSpeed; return bangBangController.atSetpoint();
} }
//shooter commands// //shooter run/stop commands//
public Command runAutoShooter() { public Command runAutoShooter() {
return run(() -> { return run(() -> {
double output = bangBangController.calculate( double output = (bangBangController.calculate(
(frontMotorEncoder.getRate() + rearMotorEncoder.getRate()) / 2, (frontMotorEncoder.getRate() + rearMotorEncoder.getRate()) / 2,
ShooterConstants.kShooterFireSpeed ShooterConstants.kShooterFireSpeed) * ShooterConstants.kBangBangScale
) + motorFeedforward.calculate(ShooterConstants.kShooterFireSpeed); ) + motorFeedforward.calculate(ShooterConstants.kShooterFireSpeed);
frontMotor.setVoltage(output); frontMotor.setVoltage(output);
}); });
} }
public Command runShooter(double speed) { public Command runShooter(DoubleSupplier speed) {
return run(() -> { return run(() -> {
frontMotor.set(speed); frontMotor.set(speed.getAsDouble());
}); });
} }