Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/frc/robot/vision/LineupMaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import frc.crevolib.math.Conversions;
import frc.robot.drivetrain.CommandSwerveDrivetrain;
import frc.robot.vision.VisionConfig.ReefFace;
import frc.robot.vision.commands.AutoAlign;
import frc.robot.vision.commands.DriveToPoseCommand;

public class LineupMaster {
Expand Down Expand Up @@ -55,7 +56,7 @@ public static ReefFace getClosestReefFace(Pose2d robotPose){
}

public Command directDriveToPose(Pose2d targetPose) {
return new DriveToPoseCommand(targetPose);
return new AutoAlign(targetPose);
}

public Command directDriveToNearestLeftBranch() {
Expand Down
191 changes: 4 additions & 187 deletions src/main/java/frc/robot/vision/commands/AutoAlign.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public class AutoAlign extends Command {
private double x;
private double y;
private LinearVelocity kLineupSpeed = MetersPerSecond.of(.1);
private PhotonCamera leftCam = new PhotonCamera(CAM_NAMES[0]);
private int tagID;

private static final TrapezoidProfile.Constraints TRANSLATION_CONSTRAINTS = new TrapezoidProfile.Constraints(
VisionConfig.AlignmentConfig.MAX_ALIGN_TRANSLATION_VELOCITY.in(MetersPerSecond),
Expand Down Expand Up @@ -64,10 +62,10 @@ public class AutoAlign extends Command {


private static Pose2d currentPose;
private static Pose2d targetPose;
private final Pose2d targetPose;
private static boolean commandRan = false;

public AutoAlign(boolean left) {
public AutoAlign(Pose2d targetPose) {
//set tolerances of all PID controllers
xDistanceController.setTolerance(VisionConfig.AlignmentConfig.DISTANCE_TOLERANCE.in(Meters));
xDistanceController.setIntegratorRange(-.15, .15);
Expand All @@ -76,7 +74,7 @@ public AutoAlign(boolean left) {
thetaController.enableContinuousInput(Units.degreesToRadians(-180), Units.degreesToRadians(180));

currentPose = PoseEstimatorSubsystem.getInstance().getCurrentPose();
targetPose = getTargetReefPose(left);
this.targetPose = targetPose;

xDistanceController.reset(0);
yDistanceController.reset(0);
Expand All @@ -85,187 +83,7 @@ public AutoAlign(boolean left) {
addRequirements(CommandSwerveDrivetrain.getInstance());
}


public double getDistanceFromTag(Pose2d goalPose) {
double xDiff = goalPose.getX() - currentPose.getX();
double yDiff = goalPose.getY() - currentPose.getY();
double distance = Math.sqrt(Math.pow(xDiff, 2) + Math.pow(yDiff, 2));
return distance;
}

private Pose2d getTargetReefPose(boolean left){
var photonResult = leftCam.getAllUnreadResults();
var currAlliance = DriverStation.getAlliance();

for (var result : photonResult){
if (result.hasTargets()){
tagID = result.getBestTarget().fiducialId;
}
}

if (currAlliance.get() == Alliance.Blue){
// IF(LEFT)
// {
// CLOSESTDIST = NULL;
// TARGETPOSE = A;
// TEMPdIST = DISTANCE(CURPOS, TARGETPOSE);
// CLOSESTDIST = TEMPDIST;
// POSE = A

// TARGETPOSE = C;
// TEMPdIST = DISTANCE(CURPOS, TARGETPOSE)
// IF(TEMPDIST < CLOSESTDIST)
// POSE = C;

// }
if (left) {
double min = getDistanceFromTag(Ablue);
targetPose = Ablue;

if (min > getDistanceFromTag(Cblue)){
min = getDistanceFromTag(Cblue);
targetPose = Cblue;
}
if (min > getDistanceFromTag(Kblue)) {
min = getDistanceFromTag(Kblue);
targetPose = Kblue;
}
if (min > getDistanceFromTag(Iblue)) {
min = getDistanceFromTag(Iblue);
targetPose = Iblue;
}
if (min > getDistanceFromTag(Gblue)) {
min = getDistanceFromTag(Gblue);
targetPose = Gblue;
}
if (min > getDistanceFromTag(Eblue)) {
min = getDistanceFromTag(Eblue);
targetPose = Eblue;
}
} else {
double min = getDistanceFromTag(Bblue);
targetPose = Ablue;

if (min > getDistanceFromTag(Dblue)){
min = getDistanceFromTag(Dblue);
targetPose = Dblue;
}
if (min > getDistanceFromTag(Lblue)) {
min = getDistanceFromTag(Lblue);
targetPose = Lblue;
}
if (min > getDistanceFromTag(Jblue)) {
min = getDistanceFromTag(Jblue);
targetPose = Jblue;
}
if (min > getDistanceFromTag(Hblue)) {
min = getDistanceFromTag(Hblue);
targetPose = Hblue;
}
if (min > getDistanceFromTag(Fblue)) {
min = getDistanceFromTag(Fblue);
targetPose = Fblue;
}
}

// //DISTANCE FROM
// if (targetPose == Cblue){
// if (left) {
// return Cblue;
// } else if (!left) {
// return Dblue;
// }
// } else if (getDistanceFromTag(Ablue) < getDistanceFromTag(Kblue)) {
// if (left) {
// return Ablue;
// } else if (!left) {
// return Bblue;
// }
// } else if (getDistanceFromTag(Kblue) < getDistanceFromTag(Iblue)) {
// if (left) {
// return Kblue;
// } else if (!left) {
// return Lblue;
// }
// } else if (getDistanceFromTag(Iblue) < getDistanceFromTag(Gblue)) {
// if (left) {
// return Iblue;
// } else if (!left) {
// return Jblue;
// }
// } else if (getDistanceFromTag(Gblue) < getDistanceFromTag(Eblue)) {
// if (left) {
// return Gblue;
// } else if (!left) {
// return Hblue;
// }
// } else if (getDistanceFromTag(Eblue) < getDistanceFromTag(Cblue)) {
// if (left) {
// return Eblue;
// } else if (!left) {
// return Fblue;
// }
// }

} else if (currAlliance.get() == Alliance.Red){
if (left) {
double min = getDistanceFromTag(Ared);
targetPose = Ared;

if (min > getDistanceFromTag(Cred)){
min = getDistanceFromTag(Cred);
targetPose = Cred;
}
if (min > getDistanceFromTag(Kred)) {
min = getDistanceFromTag(Kred);
targetPose = Kblue;
}
if (min > getDistanceFromTag(Ired)) {
min = getDistanceFromTag(Ired);
targetPose = Ired;
}
if (min > getDistanceFromTag(Gred)) {
min = getDistanceFromTag(Gred);
targetPose = Gred;
}
if (min > getDistanceFromTag(Ered)) {
min = getDistanceFromTag(Ered);
targetPose = Ered;
}
} else {
double min = getDistanceFromTag(Bred);
targetPose = Ablue;

if (min > getDistanceFromTag(Dred)){
min = getDistanceFromTag(Dred);
targetPose = Dred;
}
if (min > getDistanceFromTag(Lred)) {
min = getDistanceFromTag(Lred);
targetPose = Lred;
}
if (min > getDistanceFromTag(Jred)) {
min = getDistanceFromTag(Jred);
targetPose = Jred;
}
if (min > getDistanceFromTag(Hred)) {
min = getDistanceFromTag(Hred);
targetPose = Hred;
}
if (min > getDistanceFromTag(Fred)) {
min = getDistanceFromTag(Fred);
targetPose = Fred;
}
}

} else if (currAlliance.get() != Alliance.Red && currAlliance.get() != Alliance.Blue){
System.out.println("ERROR: There's no such thing as purple alliance");
}

System.out.println("Error: no suitable target pose found (Lineup Command)");
return targetPose;

}

@Override
public void execute() {
Expand Down Expand Up @@ -306,8 +124,7 @@ public void execute() {

@Override
public boolean isFinished() {
return commandRan;
// return xDistanceController.atSetpoint() && yDistanceController.atSetpoint() && thetaController.atSetpoint();
return xDistanceController.atSetpoint() && yDistanceController.atSetpoint() && thetaController.atSetpoint();
}

@Override
Expand Down