From eebdd10556c03fc8964d0f491cc989c387dc24f7 Mon Sep 17 00:00:00 2001 From: T Grinch <10247070+thenetworkgrinch@users.noreply.github.com> Date: Thu, 21 Mar 2024 14:30:57 -0500 Subject: [PATCH] Updated WPILib. Added example code to be able to turn to the speaker. Signed-off-by: thenetworkgrinch --- build.gradle | 2 +- .../swervedrive/SwerveSubsystem.java | 56 ++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 57468c69..dbfd1afd 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { id "java" - id "edu.wpi.first.GradleRIO" version "2024.3.1" + id "edu.wpi.first.GradleRIO" version "2024.3.2" } java { diff --git a/src/main/java/frc/robot/subsystems/swervedrive/SwerveSubsystem.java b/src/main/java/frc/robot/subsystems/swervedrive/SwerveSubsystem.java index 034aede1..0087541f 100644 --- a/src/main/java/frc/robot/subsystems/swervedrive/SwerveSubsystem.java +++ b/src/main/java/frc/robot/subsystems/swervedrive/SwerveSubsystem.java @@ -10,7 +10,11 @@ import com.pathplanner.lib.path.PathPlannerPath; import com.pathplanner.lib.util.HolonomicPathFollowerConfig; import com.pathplanner.lib.util.ReplanningConfig; +import edu.wpi.first.apriltag.AprilTagFieldLayout; +import edu.wpi.first.apriltag.AprilTagFields; +import edu.wpi.first.math.MathUtil; import edu.wpi.first.math.geometry.Pose2d; +import edu.wpi.first.math.geometry.Pose3d; import edu.wpi.first.math.geometry.Rotation2d; import edu.wpi.first.math.geometry.Translation2d; import edu.wpi.first.math.kinematics.ChassisSpeeds; @@ -18,12 +22,14 @@ import edu.wpi.first.math.trajectory.Trajectory; import edu.wpi.first.math.util.Units; import edu.wpi.first.wpilibj.DriverStation; +import edu.wpi.first.wpilibj.DriverStation.Alliance; import edu.wpi.first.wpilibj.Timer; import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.SubsystemBase; import edu.wpi.first.wpilibj2.command.sysid.SysIdRoutine.Config; import frc.robot.Constants.AutonConstants; import java.io.File; +import java.util.Optional; import java.util.function.DoubleSupplier; import org.photonvision.PhotonCamera; import org.photonvision.targeting.PhotonPipelineResult; @@ -130,6 +136,53 @@ public void setupPathPlanner() ); } + /** + * AprilTag field layout. + */ + private final AprilTagFieldLayout aprilTagFieldLayout = AprilTagFields.k2024Crescendo.loadAprilTagLayoutField(); + + /** + * Get the distance to the speaker. + * + * @return Distance to speaker in meters. + */ + public double getDistanceToSpeaker() + { + // Taken from PhotonUtils.getDistanceToPose + Pose3d speakerAprilTagPose = aprilTagFieldLayout.getTagPose( + DriverStation.getAlliance().get() == Alliance.Red ? 4 : 7).get(); + return getPose().getTranslation().getDistance(speakerAprilTagPose.toPose2d().getTranslation()); + } + + /** + * Get the yaw to aim at the speaker. + * + * @return {@link Rotation2d} of which you need to achieve. + */ + public Rotation2d getSpeakerYaw() + { + // Taken from PhotonUtils.getYawToPose() + Pose3d speakerAprilTagPose = aprilTagFieldLayout.getTagPose( + DriverStation.getAlliance().get() == Alliance.Red ? 4 : 7).get(); + Translation2d relativeTrl = speakerAprilTagPose.toPose2d().relativeTo(getPose()).getTranslation(); + return new Rotation2d(relativeTrl.getX(), relativeTrl.getY()); + } + + /** + * Aim the robot at the speaker. + * @param tolerance Tolerance in degrees. + * @return Command to turn the robot to the speaker. + */ + public Command aimAtSpeaker(double tolerance) + { + return run( + () -> { + drive(getTargetSpeeds(0, + 0, + getSpeakerYaw())); + }).until(() -> getSpeakerYaw().getDegrees() < tolerance); + } + /** * Aim the robot at the target returned by PhotonVision. * @@ -138,6 +191,7 @@ public void setupPathPlanner() */ public Command aimAtTarget(PhotonCamera camera) { + return run(() -> { PhotonPipelineResult result = camera.getLatestResult(); if (result.hasTargets()) @@ -153,7 +207,7 @@ public Command aimAtTarget(PhotonCamera camera) /** * Get the path follower with events. * - * @param pathName PathPlanner path name. + * @param pathName PathPlanner path name. * @return {@link AutoBuilder#followPath(PathPlannerPath)} path command. */ public Command getAutonomousCommand(String pathName)