Skip to content

Commit dddb4c6

Browse files
committed
needs tuning
1 parent 916ea27 commit dddb4c6

3 files changed

Lines changed: 57 additions & 59 deletions

File tree

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,26 @@ public class RobotContainer {
2828
private CommandXboxController controller = new CommandXboxController(1);
2929
private SwerveDriveState driveState = new SwerveDriveState();
3030
public static boolean isExtended = false;
31+
public static boolean shootOnTheMove = false;
3132

3233
private Command shootGroup =
3334
Commands.parallel(
3435
new KickerCommand(kicker, KickerCommand.Position.INTAKE),
3536
Commands.runEnd(
36-
() -> hopper.slowMove(HopperConfig.HOPPER_RETRACT_ROTATION),
37-
() -> hopper.stop(),
38-
hopper).withDeadline(Commands.waitSeconds(2)),
37+
() -> hopper.slowMove(HopperConfig.HOPPER_RETRACT_ROTATION),
38+
() -> hopper.stop(),
39+
hopper)
40+
.withDeadline(Commands.waitSeconds(2)),
3941
new IntakeCommand(intake, IntakeCommand.Position.SLOW_INTAKE))
4042
.finallyDo(
4143
interrupt ->
4244
CommandScheduler.getInstance()
4345
.schedule(
4446
Commands.runEnd(
45-
() -> hopper.move(HopperConfig.HOPPER_EXTEND_ROTATION),
46-
() -> hopper.stop(),
47-
hopper).withDeadline(Commands.waitSeconds(2))));
47+
() -> hopper.move(HopperConfig.HOPPER_EXTEND_ROTATION),
48+
() -> hopper.stop(),
49+
hopper)
50+
.withDeadline(Commands.waitSeconds(2))));
4851

4952
public RobotContainer() {
5053
configureBindings();
@@ -74,9 +77,9 @@ private void configureBindings() {
7477
.toggleOnTrue(new IntakeCommand(intake, IntakeCommand.Position.INTAKE));
7578

7679
controller
77-
.povRight()
78-
.and(() -> !shootGroup.isScheduled())
79-
.toggleOnTrue(new IntakeCommand(intake, IntakeCommand.Position.OUTTAKE));
80+
.povRight()
81+
.and(() -> !shootGroup.isScheduled())
82+
.toggleOnTrue(new IntakeCommand(intake, IntakeCommand.Position.OUTTAKE));
8083
// Right Stick Down = Extend/Retract Hopper
8184
controller
8285
.rightStick()
@@ -112,6 +115,19 @@ private void configureBindings() {
112115
.toggleOnTrue(
113116
new FlywheelCommand(flywheel, FlywheelCommand.Position.CALCULATED_SHOT, drivetrain));
114117
controller.povDown().onTrue(Commands.runOnce(() -> hopper.zero(), hopper));
118+
// B = Shoot on the move toggle - placeholder button
119+
controller
120+
.b()
121+
.whileTrue(
122+
Commands.parallel(
123+
new DrivetrainCommand(
124+
drivetrain,
125+
DrivetrainCommand.Position.SOTM,
126+
controller::getLeftX,
127+
controller::getLeftY,
128+
controller::getRightX),
129+
new FlywheelCommand(
130+
flywheel, FlywheelCommand.Position.CALCULATED_SHOT, drivetrain)));
115131
}
116132

117133
public Command getAutonomousCommand() {

src/main/java/frc/robot/commands/DrivetrainCommand.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
import com.ctre.phoenix6.swerve.SwerveRequest;
77
import edu.wpi.first.epilogue.Logged;
88
import edu.wpi.first.math.geometry.Rotation2d;
9+
import edu.wpi.first.math.geometry.Translation2d;
10+
import edu.wpi.first.math.kinematics.ChassisSpeeds;
911
import edu.wpi.first.math.kinematics.SwerveModuleState;
1012
import edu.wpi.first.wpilibj2.command.Command;
1113
import frc.robot.config.TunerConstants;
1214
import frc.robot.helpers.ApplyModuleStates;
15+
import frc.robot.helpers.ShotCalculator;
1316
import frc.robot.subsystems.CommandSwerveDrivetrain;
1417
import java.util.function.DoubleSupplier;
1518

@@ -40,6 +43,8 @@ public static enum Position {
4043
SwerveModule.DriveRequestType
4144
.OpenLoopVoltage); // Use open-loop control for drive motors
4245

46+
private static final double NOTE_EXIT_VELOCITY = 10.0; // This needs to be tuned
47+
4348
SwerveModuleState[] states = {
4449
new SwerveModuleState(0, Rotation2d.fromDegrees(45)),
4550
new SwerveModuleState(0, Rotation2d.fromDegrees(135)),
@@ -88,12 +93,35 @@ public void execute() {
8893
System.out.println("Drivetrain: Still Shot Configuration");
8994
break;
9095

91-
// (Incomplete) Mode for moving shots
96+
// (Incomplete) Mode for moving shots; pretty much finished but needs tuning and testing, and
97+
// may need to be changed to use a different method of calculating the shot
9298
case SOTM:
93-
// shoot on the move, reference Mechanical Advantage build log
94-
System.out.println("Drivetrain: SOTM Drive");
95-
break;
99+
double vx = -leftY.getAsDouble() * MaxSpeed;
100+
double vy = -leftX.getAsDouble() * MaxSpeed;
101+
Translation2d robotPosition = subsystem.getState().Pose.getTranslation();
102+
ChassisSpeeds speeds = subsystem.getState().Speeds;
103+
Translation2d robotVelocity =
104+
new Translation2d(speeds.vxMetersPerSecond, speeds.vyMetersPerSecond);
105+
Translation2d goal = ShotCalculator.calculateGoalPosition();
106+
double distance = robotPosition.getDistance(goal);
107+
double flightTime = distance / NOTE_EXIT_VELOCITY;
108+
Translation2d toGoal = goal.minus(robotPosition);
109+
Translation2d toGoalDir =
110+
toGoal.getNorm() > 1e-6 ? toGoal.div(toGoal.getNorm()) : new Translation2d();
111+
Translation2d lateralVelocity =
112+
robotVelocity.minus(toGoalDir.times(robotVelocity.dot(toGoalDir)));
113+
Translation2d virtualGoal = goal.minus(lateralVelocity.times(flightTime));
114+
Rotation2d targetAngle = virtualGoal.minus(robotPosition).getAngle();
115+
double currentHeading = subsystem.getState().Pose.getRotation().getRadians();
116+
double targetHeading = targetAngle.getRadians();
117+
double error = targetHeading - currentHeading;
118+
error = Math.atan2(Math.sin(error), Math.cos(error));
119+
double kP = 4.0; // tune
120+
double omega = error * kP;
121+
omega = Math.max(-MaxAngularRate, Math.min(MaxAngularRate, omega));
122+
subsystem.setControl(drive.withVelocityX(vx).withVelocityY(vy).withRotationalRate(omega));
96123

124+
break;
97125
default:
98126
break;
99127
}

src/main/java/frc/robot/commands/ShootOnMoveCommand.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)