Skip to content

Commit

Permalink
Merge pull request #23 from aaditsangvikar/main
Browse files Browse the repository at this point in the history
On-the-fly shooting - untested
  • Loading branch information
zaneal authored Feb 17, 2024
2 parents 9458f9b + 359bed1 commit c4d8b6f
Show file tree
Hide file tree
Showing 11 changed files with 370 additions and 59 deletions.
87 changes: 49 additions & 38 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public class RobotContainer {
// The robot's subsystems and commands are defined here...

public XboxController primaryController = new XboxController(0);
public XboxController secondaryController = new XboxController(1);
public SwerveSubsystem swerveSubsystem = new SwerveSubsystem();
// public XboxController secondaryController = new XboxController(1);
// public SwerveSubsystem swerveSubsystem = new SwerveSubsystem();

public IndexerSubsystem indexerSubsystem = new IndexerSubsystem();

public IntakeSubsystem intakeSubsystem = new IntakeSubsystem();
// public IndexerSubsystem indexerSubsystem = new IndexerSubsystem();
//
// public IntakeSubsystem intakeSubsystem = new IntakeSubsystem();

public ShooterSubsystem shooterSubsystem = new ShooterSubsystem();

Expand All @@ -42,46 +42,57 @@ public RobotContainer() {

public void configureButtonBindings(){

swerveSubsystem.setDefaultCommand(new DriveCommands(
swerveSubsystem,
() -> primaryController.getLeftY() * DrivetrainConstants.drivingSpeedScalar / 2.0,
() -> primaryController.getLeftX() * DrivetrainConstants.drivingSpeedScalar / 2.0,
() -> primaryController.getRightX() * DrivetrainConstants.rotationSpeedScalar / 2.0,
true,
true
));

indexerSubsystem.setDefaultCommand(new BaseCommand(indexerSubsystem));

// Primary controller
new JoystickButton(primaryController, XboxController.Button.kY.value).whileTrue(
new RunCommand(() -> swerveSubsystem.zeroGyro())
);

// Secondary controller
new JoystickButton(secondaryController, XboxController.Axis.kRightTrigger.value).whileTrue(
// swerveSubsystem.setDefaultCommand(new DriveCommands(
// swerveSubsystem,
// () -> primaryController.getLeftY() * DrivetrainConstants.drivingSpeedScalar / 2.0,
// () -> primaryController.getLeftX() * DrivetrainConstants.drivingSpeedScalar / 2.0,
// () -> primaryController.getRightX() * DrivetrainConstants.rotationSpeedScalar / 2.0,
// true,
// true
// ));
//
// indexerSubsystem.setDefaultCommand(new BaseCommand(indexerSubsystem));
//
// // Primary controller
// new JoystickButton(primaryController, XboxController.Button.kY.value).whileTrue(
// new RunCommand(() -> swerveSubsystem.zeroGyro())
// );
//
// // Secondary controller
// new JoystickButton(secondaryController, XboxController.Axis.kRightTrigger.value).whileTrue(
// new SpinUpCommand(shooterSubsystem)
// );
//
// new JoystickButton(secondaryController, XboxController.Button.kRightBumper.value).whileTrue(
// new SpeakerCommand(indexerSubsystem, swerveSubsystem.getFieldRelativeChassisSpeeds(), swerveSubsystem.getPose())
// );
//
// new JoystickButton(secondaryController, XboxController.Button.kLeftBumper.value).whileTrue(
// new AmpCommand(indexerSubsystem, secondaryController)
// );
//
// new JoystickButton(secondaryController, XboxController.Button.kB.value).whileTrue(
// new AmpCommand2(indexerSubsystem, secondaryController) // This is kinda fucked
// );
//
// new JoystickButton(secondaryController, XboxController.Axis.kLeftTrigger.value).whileTrue(
// new IntakeCommand(intakeSubsystem, indexerSubsystem)
// );
//
// new JoystickButton(secondaryController, XboxController.Button.kA.value).whileTrue(
// new PathCommand(swerveSubsystem)
// );

new JoystickButton(primaryController, XboxController.Button.kA.value).whileTrue(
new SpinUpCommand(shooterSubsystem)
);

new JoystickButton(secondaryController, XboxController.Button.kRightBumper.value).whileTrue(
new SpeakerCommand(indexerSubsystem)
);

new JoystickButton(secondaryController, XboxController.Button.kLeftBumper.value).whileTrue(
new AmpCommand(indexerSubsystem, secondaryController)
new JoystickButton(primaryController, XboxController.Button.kY.value).whileTrue(
new RunCommand(() -> shooterSubsystem.setVoltage(0.2,0.2))
);

new JoystickButton(secondaryController, XboxController.Button.kB.value).whileTrue(
new AmpCommand2(indexerSubsystem, secondaryController) // This is kinda fucked
);

new JoystickButton(secondaryController, XboxController.Axis.kLeftTrigger.value).whileTrue(
new IntakeCommand(intakeSubsystem, indexerSubsystem)
);

new JoystickButton(secondaryController, XboxController.Button.kA.value).whileTrue(
new PathCommand(swerveSubsystem)
);

}
/**
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/frc/robot/commands/AdvancedSpinUpCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package frc.robot.commands;

import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.constants.ShooterConstants;
import frc.robot.subsystems.ShooterSubsystem;

public class AdvancedSpinUpCommand extends Command {
private final ShooterSubsystem shooterSubsystem;

private final Pose2d robotPose;

/**
* Command to spin up the shooter
* @param shooterSubsystem Instance of {@link ShooterSubsystem}
* @param robotPose Instance of {@link Pose2d}
*/
public AdvancedSpinUpCommand(ShooterSubsystem shooterSubsystem, Pose2d robotPose) {
this.shooterSubsystem = shooterSubsystem;
this.robotPose = robotPose;

addRequirements(shooterSubsystem);
}

@Override
public void execute() {

if (robotPose.getX() < 9) {
shooterSubsystem.setSpeed(ShooterConstants.shooterRPM);
} else {
shooterSubsystem.setVoltage(0,0);
}

}

@Override
public void end(boolean interrupted) {
shooterSubsystem.setVoltage(0,0);
}
}
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/commands/AmpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class AmpCommand extends Command {


/**
* Moves the indexer to the amp
* Moves the indexer to the amp and scores
* @param indexerSubsystem The instance of {@link IndexerSubsystem}
* @param controller An instance of {@link XboxController}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package frc.robot.commands;

import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.constants.ShooterConstants;
import frc.robot.subsystems.ShooterSubsystem;

public class SpinUpCommand extends Command {
public class BasicSpinUpCommand extends Command {
private final ShooterSubsystem shooterSubsystem;

/**
* Command to spin up the shooter
* @param shooterSubsystem Instance of {@link ShooterSubsystem}
*/
public SpinUpCommand(ShooterSubsystem shooterSubsystem) {
public BasicSpinUpCommand(ShooterSubsystem shooterSubsystem) {
this.shooterSubsystem = shooterSubsystem;

addRequirements(shooterSubsystem);
}

@Override
public void execute() {
shooterSubsystem.setSpeed(6000);

shooterSubsystem.setSpeed(ShooterConstants.shooterRPM);

}

@Override
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/frc/robot/commands/RotateToNoteAndDriveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@ public class RotateToNoteAndDriveCommand extends Command {
private final double visionRotation;

private final ProfiledPIDController rotationPID = new ProfiledPIDController(
VisionConstants.rotateToNoteP,
VisionConstants.rotateToNoteI,
VisionConstants.rotateToNoteD,
VisionConstants.rotateToNoteConstraints
VisionConstants.rotateToP,
VisionConstants.rotateToI,
VisionConstants.rotateToD,
VisionConstants.rotateToConstraints
);

private final XboxController controller;

/**
* Rotates the robot to face the speaker, while still allowing the driver to control forward and backward movement
* @param swerveSubsystem The instance of {@link SwerveSubsystem}
* @param controller The instance of {@link XboxController}
* @param forward The desired forward percentage of the robot
* @param sideways The desired sideways percentage of the robot
* @param radians The desired rotation speed of the robot
*/
public RotateToNoteAndDriveCommand(SwerveSubsystem swerveSubsystem, XboxController controller, DoubleSupplier forward, DoubleSupplier sideways, DoubleSupplier radians) {
this.swerveSubsystem = swerveSubsystem;
this.forward = forward;
Expand All @@ -50,6 +58,7 @@ public void execute() {
true,
true
);
controller.setRumble(GenericHID.RumbleType.kBothRumble, 0.0);
} else {
swerveSubsystem.drive(
forward.getAsDouble(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package frc.robot.commands;

import edu.wpi.first.math.controller.ProfiledPIDController;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Transform2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.constants.ShooterConstants;
import frc.robot.constants.VisionConstants;
import frc.robot.subsystems.IndexerSubsystem;
import frc.robot.subsystems.SwerveSubsystem;

import java.util.function.DoubleSupplier;

public class RotateToSpeakerAndDriveCommand extends Command {
private final SwerveSubsystem swerveSubsystem;
private final DoubleSupplier forward;
private final DoubleSupplier sideways;
private final DoubleSupplier radians;
private final double odometryRotation;

private final ProfiledPIDController rotationPID = new ProfiledPIDController(
VisionConstants.rotateToP,
VisionConstants.rotateToI,
VisionConstants.rotateToD,
VisionConstants.rotateToConstraints
);

/**
* Rotates the robot to face the speaker, while still allowing the driver to control forward and backward movement
* @param swerveSubsystem The instance of {@link SwerveSubsystem}
* @param forward The desired forward percentage of the robot
* @param sideways The desired sideways percentage of the robot
* @param radians The desired rotation speed of the robot
*/
public RotateToSpeakerAndDriveCommand(SwerveSubsystem swerveSubsystem, DoubleSupplier forward, DoubleSupplier sideways, DoubleSupplier radians) {
this.swerveSubsystem = swerveSubsystem;
this.forward = forward;
this.sideways = sideways;
this.radians = radians;

//Rotation tolerance in radians
rotationPID.setTolerance(0.05);

//Robot speed toward/away from the speaker (y-direction)
double robotYSpeed = swerveSubsystem.getFieldRelativeChassisSpeeds().vyMetersPerSecond;
//Robot speed toward/away from the speaker (x-direction)
double robotXSpeed = swerveSubsystem.getFieldRelativeChassisSpeeds().vxMetersPerSecond;
//Note speed in x-direction(forward, as opposed to up)
double noteSpeedX = robotXSpeed + ShooterConstants.shooterNoteSpeedX;

//Getting speaker pose relative to alliance color
Pose2d speakerPose = (DriverStation.getAlliance().get() == DriverStation.Alliance.Blue)? ShooterConstants.speakerPoseBlue : ShooterConstants.speakerPoseRed;
//Current robot pose
Pose2d robotPose = swerveSubsystem.getPose();

//'Moving' speaker position based on robot speed
double speakerYTranslation = robotYSpeed * ((speakerPose.getX()-robotPose.getX()) / noteSpeedX);

//Calculated angle to rotate to
double angle = Math.atan2((speakerPose.getY() + speakerYTranslation) - robotPose.getY(), speakerPose.getX()-robotPose.getX());

//Rotation PID Calculations
this.odometryRotation = rotationPID.calculate(robotPose.getRotation().getRadians(), angle);

addRequirements(swerveSubsystem);

}

@Override
public void execute() {

swerveSubsystem.drive(
forward.getAsDouble(),
sideways.getAsDouble(),
odometryRotation,
true,
true
);

}
}
Loading

0 comments on commit c4d8b6f

Please sign in to comment.