Skip to content

Commit

Permalink
refactor command to accept translation2d, spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
JayK445 committed Jan 29, 2024
1 parent 0b53c3a commit 0eb0e16
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import edu.wpi.first.math.Matrix;
import edu.wpi.first.math.Nat;
import edu.wpi.first.math.Pair;
import edu.wpi.first.math.Pair;
import edu.wpi.first.math.geometry.Rotation3d;
import edu.wpi.first.math.geometry.Transform3d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.geometry.Translation3d;
import edu.wpi.first.math.numbers.N1;
import edu.wpi.first.math.numbers.N3;
Expand Down Expand Up @@ -216,7 +216,7 @@ public static final class Module4 { // historically back right
}

public static final class Setpoints {
public static final Pair<Double, Double> SPEAKER = new Pair<>(0d, 104.64d);
public static final Translation2d SPEAKER = new Translation2d(0, 104.64);
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,14 @@ private void configureButtonBindings() {

anthony.y().onTrue(new HaltDriveCommandsCommand(drivebaseSubsystem));

anthony.a().whileTrue(new TargetLockCommand(drivebaseSubsystem, translationXSupplier, translationYSupplier, Setpoints.SPEAKER.getFirst(), Setpoints.SPEAKER.getSecond()));
anthony
.a()
.whileTrue(
new TargetLockCommand(
drivebaseSubsystem,
translationXSupplier,
translationYSupplier,
Setpoints.SPEAKER));

anthony.leftStick().onTrue(new HaltDriveCommandsCommand(drivebaseSubsystem));

Expand Down
28 changes: 13 additions & 15 deletions src/main/java/frc/robot/commands/TargetLockCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,42 @@

import edu.wpi.first.math.Pair;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.Constants.Drive;
import frc.robot.Constants.Drive.Setpoints;
import frc.robot.subsystems.DrivebaseSubsystem;
import frc.util.Util;
import java.util.function.DoubleSupplier;
import java.util.function.Supplier;

/**
* This command takes a drive angle and a target x,y coordinate, and snaps the robot to face the target. This is
* useful to lock the robot to fixed target with a button.
* This command takes a drive angle and a target x,y coordinate, and snaps the robot to face the
* target. This is useful to lock the robot to fixed target with a button.
*/
public class TargetLockCommand extends Command {
private final DrivebaseSubsystem drivebaseSubsystem;

private final DoubleSupplier translationXSupplier;
private final DoubleSupplier translationYSupplier;

private final double targetX;
private final double targetY;
private final Translation2d targetPoint;

private int targetAngle;
private Supplier<Pose2d> pose;

/** Creates a new TargetLockCommand. */
public TargetLockCommand (
public TargetLockCommand(
DrivebaseSubsystem drivebaseSubsystem,
DoubleSupplier translationXSupplier,
DoubleSupplier translationYSupplier,
double targetX,
double targetY) {
Translation2d targetPoint) {

this.drivebaseSubsystem = drivebaseSubsystem;
this.translationXSupplier = translationXSupplier;
this.translationYSupplier = translationYSupplier;
this.targetX = targetX;
this.targetY = targetY;
this.targetPoint = targetPoint;

Supplier<Pose2d> pose = drivebaseSubsystem::getPose;
int targetAngle = 0;
targetAngle = 0;

addRequirements(drivebaseSubsystem);
}
Expand All @@ -56,9 +52,11 @@ public void initialize() {}
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
targetAngle = (int) Math.atan(
(targetY - pose.get().getY()) /
(targetX - pose.get().getX()));
targetAngle =
(int) // may want to change to double for more accuracy (likely unnecessary)
Math.atan(
(targetPoint.getY() - pose.get().getY())
/ (targetPoint.getX() - pose.get().getX()));
double x = translationXSupplier.getAsDouble();
double y = translationYSupplier.getAsDouble();

Expand Down

0 comments on commit 0eb0e16

Please sign in to comment.