Skip to content

Commit

Permalink
Updated WPILib. Added example code to be able to turn to the speaker.
Browse files Browse the repository at this point in the history
Signed-off-by: thenetworkgrinch <thenetworkgrinch@users.noreply.github.com>
  • Loading branch information
thenetworkgrinch committed Mar 21, 2024
1 parent b2ad475 commit eebdd10
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@
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;
import edu.wpi.first.math.kinematics.SwerveDriveKinematics;
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;
Expand Down Expand Up @@ -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.
*
Expand All @@ -138,6 +191,7 @@ public void setupPathPlanner()
*/
public Command aimAtTarget(PhotonCamera camera)
{

return run(() -> {
PhotonPipelineResult result = camera.getLatestResult();
if (result.hasTargets())
Expand All @@ -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)
Expand Down

0 comments on commit eebdd10

Please sign in to comment.