2025-01-16 16:51:09 -07:00
|
|
|
package frc4388.utility;
|
|
|
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
import edu.wpi.first.math.geometry.Pose2d;
|
|
|
|
|
import edu.wpi.first.math.geometry.Rotation2d;
|
|
|
|
|
import edu.wpi.first.math.geometry.Translation2d;
|
|
|
|
|
import edu.wpi.first.wpilibj.DriverStation;
|
|
|
|
|
import edu.wpi.first.wpilibj.DriverStation.Alliance;
|
2025-02-28 11:46:48 -07:00
|
|
|
import frc4388.robot.Constants.AutoConstants;
|
2025-01-16 16:51:09 -07:00
|
|
|
import frc4388.robot.Constants.FieldConstants;
|
|
|
|
|
|
|
|
|
|
public class ReefPositionHelper {
|
|
|
|
|
public enum Side {
|
|
|
|
|
LEFT,
|
2025-02-21 16:54:18 -07:00
|
|
|
RIGHT,
|
|
|
|
|
CENTER
|
2025-01-16 16:51:09 -07:00
|
|
|
}
|
|
|
|
|
|
2025-01-16 19:41:05 -07:00
|
|
|
public static final Pose2d[] RED_TAGS = {
|
2025-01-16 16:51:09 -07:00
|
|
|
FieldConstants.kTagLayout.getTagPose(6).get().toPose2d(),
|
|
|
|
|
FieldConstants.kTagLayout.getTagPose(7).get().toPose2d(),
|
|
|
|
|
FieldConstants.kTagLayout.getTagPose(8).get().toPose2d(),
|
|
|
|
|
FieldConstants.kTagLayout.getTagPose(9).get().toPose2d(),
|
|
|
|
|
FieldConstants.kTagLayout.getTagPose(10).get().toPose2d(),
|
|
|
|
|
FieldConstants.kTagLayout.getTagPose(11).get().toPose2d()
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-16 19:41:05 -07:00
|
|
|
public static final Pose2d[] BLUE_TAGS = {
|
2025-01-16 16:51:09 -07:00
|
|
|
FieldConstants.kTagLayout.getTagPose(17).get().toPose2d(),
|
|
|
|
|
FieldConstants.kTagLayout.getTagPose(18).get().toPose2d(),
|
|
|
|
|
FieldConstants.kTagLayout.getTagPose(19).get().toPose2d(),
|
|
|
|
|
FieldConstants.kTagLayout.getTagPose(20).get().toPose2d(),
|
|
|
|
|
FieldConstants.kTagLayout.getTagPose(21).get().toPose2d(),
|
|
|
|
|
FieldConstants.kTagLayout.getTagPose(22).get().toPose2d()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static double distanceTo(Pose2d first, Pose2d second){
|
2025-01-16 19:41:05 -07:00
|
|
|
return Math.sqrt(Math.pow(first.getX() - second.getX(),2) + Math.pow(first.getY() - second.getY(),2));
|
2025-01-16 16:51:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Function to loop through a list of tag locations to figure out closest one
|
|
|
|
|
*/
|
|
|
|
|
public static Pose2d getNearestTag(Pose2d[] locations, Pose2d position){
|
|
|
|
|
if(locations.length <= 0) return new Pose2d();
|
|
|
|
|
|
|
|
|
|
Pose2d minPos = locations[0];
|
|
|
|
|
double minDistance = distanceTo(position,minPos);
|
|
|
|
|
|
|
|
|
|
for(int i = 1; i < locations.length; i++){
|
2025-01-16 19:41:05 -07:00
|
|
|
double dist = distanceTo(locations[i],position);
|
2025-01-16 16:51:09 -07:00
|
|
|
if(dist < minDistance){
|
|
|
|
|
minPos = locations[i];
|
|
|
|
|
minDistance = dist;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return minPos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Function to find closest tag location based on side
|
|
|
|
|
*/
|
|
|
|
|
public static Pose2d getNearestTag(Pose2d position) {
|
2025-02-24 17:59:44 -07:00
|
|
|
|
|
|
|
|
if(TimesNegativeOne.isRed)
|
2025-01-16 16:51:09 -07:00
|
|
|
return getNearestTag(RED_TAGS, position);
|
2025-02-24 17:59:44 -07:00
|
|
|
else
|
|
|
|
|
return getNearestTag(BLUE_TAGS, position);
|
2025-01-16 16:51:09 -07:00
|
|
|
}
|
|
|
|
|
|
2025-02-18 19:39:01 -07:00
|
|
|
public static Pose2d getNearestPosition(Pose2d position, Side side, double xtrim, double ydistance) {
|
2025-01-16 19:41:05 -07:00
|
|
|
return offset(getNearestTag(position),
|
2025-02-21 16:54:18 -07:00
|
|
|
getSide(side) + xtrim,
|
2025-02-18 19:39:01 -07:00
|
|
|
ydistance);
|
2025-01-16 16:51:09 -07:00
|
|
|
}
|
|
|
|
|
|
2025-02-21 16:54:18 -07:00
|
|
|
public static double getSide(Side side){
|
|
|
|
|
switch(side) {
|
|
|
|
|
case LEFT:
|
2025-02-28 11:46:48 -07:00
|
|
|
return -(AutoConstants.X_SCORING_POSITION_OFFSET);
|
2025-02-21 16:54:18 -07:00
|
|
|
case RIGHT:
|
2025-02-28 11:46:48 -07:00
|
|
|
return (AutoConstants.X_SCORING_POSITION_OFFSET);
|
2025-02-21 16:54:18 -07:00
|
|
|
case CENTER:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-16 16:51:09 -07:00
|
|
|
|
2025-01-16 19:41:05 -07:00
|
|
|
public static Pose2d offset(Pose2d oldPose, double xoffset, double yoffset){
|
2025-01-16 16:51:09 -07:00
|
|
|
Translation2d oldTranslation = oldPose.getTranslation();
|
2025-01-16 19:41:05 -07:00
|
|
|
|
|
|
|
|
double rot = oldPose.getRotation().getRadians();
|
|
|
|
|
|
2025-01-16 16:51:09 -07:00
|
|
|
return new Pose2d(new Translation2d(
|
2025-01-16 19:41:05 -07:00
|
|
|
oldTranslation.getX() + Math.cos(rot + Math.PI/2) * xoffset + Math.cos(rot) * yoffset,
|
|
|
|
|
oldTranslation.getY() + Math.sin(rot + Math.PI/2) * xoffset + Math.sin(rot) * yoffset
|
2025-02-15 15:42:17 -07:00
|
|
|
), oldPose.getRotation().rotateBy(Rotation2d.k180deg));
|
2025-01-16 16:51:09 -07:00
|
|
|
}
|
|
|
|
|
}
|