Skip to content

Commit

Permalink
started working on getting the robot to drive to the nearest target p…
Browse files Browse the repository at this point in the history
…osition
  • Loading branch information
DylanTaylor29 committed Jan 25, 2025
1 parent 85e1da9 commit ce68a72
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/main/java/frc/robot/DriveToPoseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import frc.robot.vision.Vision;

public class DriveToPoseCommand extends Command {

public final Drivetrain swerve;
public final Vision vision;
public final Pose2d targetPose;
Expand Down
27 changes: 20 additions & 7 deletions src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public class Robot extends TimedRobot {
private final LEDs m_LEDs;
private final Vision m_vision;
private final Auto m_auto;
private final Pose2d m_targetPose;
private final DriveToPoseCommand moveToPoseCommand;

// private final AutoFactory m_autoFactory;

Expand All @@ -69,10 +67,6 @@ public Robot() {

m_vision = new Vision();

m_targetPose = new Pose2d(1.0, 4.0, Rotation2d.fromDegrees(0));

moveToPoseCommand = new DriveToPoseCommand(m_swerve, m_vision, m_targetPose);

configureButtonBindings();
configureEventBindings();
configureAutoOptions();
Expand Down Expand Up @@ -185,7 +179,7 @@ private void configureDriverButtonBindings() {
.ignoringDisable(true));

m_driver.AIn()
.whileTrue(moveToPoseCommand);
.whileTrue(new DriveToPoseCommand(m_swerve, m_vision, getNearestPose(m_swerve.getPose(), targetPoses)));

}
// spotless:on
Expand Down Expand Up @@ -213,6 +207,25 @@ public double getPDHCurrent(int CANBusPort) {
return m_powerDistribution.getCurrent(CANBusPort - 10);
}

Pose2d[] targetPoses = {
new Pose2d(1.0, 3.0, Rotation2d.fromDegrees(0.0)),
new Pose2d(1.0, 5.0, Rotation2d.fromDegrees(0.0))
};

public Pose2d getNearestPose(Pose2d currentPose, Pose2d[] targetPoses) {
Pose2d closestPose = new Pose2d(5, 5, Rotation2d.fromDegrees(0));
double minDistance = Double.MAX_VALUE;

for (Pose2d targetPose : targetPoses) {
double distance = currentPose.getTranslation().getDistance(targetPose.getTranslation());

if (distance < minDistance) {
closestPose = targetPose;
}
}
return closestPose;
}

private synchronized StructPublisher<Pose2d> getPose2dPublisher(String name) {
var publisher = posePublishers.get(name);
if (publisher == null) {
Expand Down

0 comments on commit ce68a72

Please sign in to comment.