Skip to content

Commit e8ae194

Browse files
committed
made code not crash
1 parent c5fc0b7 commit e8ae194

File tree

9 files changed

+45
-53
lines changed

9 files changed

+45
-53
lines changed

src/main/java/frc/trigon/robot/RobotContainer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ private void bindDefaultCommands() {
7373
}
7474

7575
private void bindControllerCommands() {
76+
OperatorConstants.OPERATOR_CONTROLLER.semicolon().whileTrue(ShootingCommands.getShootAtTagCommand());
7677
OperatorConstants.RESET_HEADING_TRIGGER.onTrue(CommandConstants.RESET_HEADING_COMMAND);
7778
OperatorConstants.SET_GYRO_HEADING_TO_SOLVE_PNP_HEADING_TRIGGER.onTrue(CommandConstants.SET_GYRO_HEADING_TO_SOLVE_PNP_HEADING_COMMAND);
7879
OperatorConstants.DRIVE_FROM_DPAD_TRIGGER.whileTrue(CommandConstants.SELF_RELATIVE_DRIVE_FROM_DPAD_COMMAND);

src/main/java/frc/trigon/robot/commands/AlignToTagCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private Command getAlignToTagCommand() {
4444
private Command getDriveWhileAligningToNoteCommand() {
4545
return SwerveCommands.getClosedLoopSelfRelativeDriveCommand(
4646
() -> fieldRelativePowersToSelfRelativeXPower(OperatorConstants.DRIVER_CONTROLLER.getLeftY(), OperatorConstants.DRIVER_CONTROLLER.getLeftX()),
47-
() -> -PID_CONTROLLER.calculate(RobotContainer.POSE_ESTIMATOR.getBestTagYawRelativeToRobot().getDegrees()),
47+
() -> -PID_CONTROLLER.calculate(ShootingCalculations.getInstance().getAngleToTarget(new Translation2d(), RobotContainer.ROBOT_SHOWCASE.getBestTagTranslationRelativeToRobot()).get().getDegrees()),
4848
this::getTargetAngle
4949
);
5050
}

src/main/java/frc/trigon/robot/commands/factories/ShootingCommands.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,12 @@ public class ShootingCommands {
2020
private static final ShootingCalculations SHOOTING_CALCULATIONS = ShootingCalculations.getInstance();
2121

2222
public static Command getShootAtTagCommand() {
23-
return new ParallelCommandGroup(
23+
return new ParallelRaceGroup(
2424
getPrepareForShootingAtTargetCommand(),
2525
getFeedNoteForShootingCommand()
2626
);
2727
}
2828

29-
private static Command getPrepareForShootingAtTargetCommand() {
30-
return new ParallelCommandGroup(
31-
PitcherCommands.getReachTargetPitchFromShootingCalculationsCommand(),
32-
ShooterCommands.getReachTargetShootingVelocityFromShootingCalculationsCommand(),
33-
SwerveCommands.getClosedLoopFieldRelativeDriveCommand(
34-
() -> CommandConstants.calculateDriveStickAxisValue(OperatorConstants.DRIVER_CONTROLLER.getLeftY()),
35-
() -> CommandConstants.calculateDriveStickAxisValue(OperatorConstants.DRIVER_CONTROLLER.getLeftX()),
36-
() -> SHOOTING_CALCULATIONS.getTargetShootingState().targetRobotAngle()
37-
)
38-
);
39-
}
40-
41-
private static Command getUpdateShootingCalculationsCommandForShootingTarget() {
42-
return new RunCommand(() -> SHOOTING_CALCULATIONS.updateCalculationsForShot(RobotContainer.ROBOT_SHOWCASE.getBestTagTranslationRelativeToRobot()));
43-
}
44-
4529
/**
4630
* Creates a command that adjusts the shooting mechanism to aim at a target (either delivery target or speaker target), and feeds the note once the shooting mechanism is ready to shoot (all setpoints were reached).
4731
*
@@ -88,6 +72,23 @@ static Command getUpdateShootingCalculationsCommand(boolean isDelivery) {
8872
return new RunCommand(isDelivery ? SHOOTING_CALCULATIONS::updateCalculationsForDelivery : SHOOTING_CALCULATIONS::updateCalculationsForSpeakerShot);
8973
}
9074

75+
private static Command getPrepareForShootingAtTargetCommand() {
76+
return new ParallelCommandGroup(
77+
getUpdateShootingCalculationsCommandForShootingTarget(),
78+
PitcherCommands.getReachTargetPitchFromShootingCalculationsCommand(),
79+
ShooterCommands.getReachTargetShootingVelocityFromShootingCalculationsCommand(),
80+
SwerveCommands.getClosedLoopFieldRelativeDriveCommand(
81+
() -> CommandConstants.calculateDriveStickAxisValue(OperatorConstants.DRIVER_CONTROLLER.getLeftY()),
82+
() -> CommandConstants.calculateDriveStickAxisValue(OperatorConstants.DRIVER_CONTROLLER.getLeftX()),
83+
() -> SHOOTING_CALCULATIONS.getTargetShootingState().targetRobotAngle()
84+
)
85+
);
86+
}
87+
88+
private static Command getUpdateShootingCalculationsCommandForShootingTarget() {
89+
return new RunCommand(() -> SHOOTING_CALCULATIONS.updateCalculationsForShot(RobotContainer.ROBOT_SHOWCASE.getBestTagTranslationRelativeToRobot()));
90+
}
91+
9192
private static Command getPrepareForShootingCommand(boolean isDelivery) {
9293
return new ParallelCommandGroup(
9394
getUpdateShootingCalculationsCommand(isDelivery),

src/main/java/frc/trigon/robot/constants/CameraConstants.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package frc.trigon.robot.constants;
22

3+
import edu.wpi.first.math.geometry.Pose3d;
34
import edu.wpi.first.math.geometry.Rotation3d;
45
import edu.wpi.first.math.geometry.Transform3d;
56
import edu.wpi.first.math.geometry.Translation3d;
@@ -23,6 +24,15 @@ public class CameraConstants {
2324
new Translation3d(-0.325 + 0.00975, 0, 0.095),
2425
new Rotation3d(Math.PI - Units.degreesToRadians(0), Units.degreesToRadians(-33), Math.PI + Units.degreesToRadians(0))
2526
);
27+
public static final Pose3d
28+
FRONT_CENTER_TO_CAMERA_POSE = new Pose3d(
29+
new Translation3d(0.325, 0.0465, 0.192),
30+
new Rotation3d(0, Units.degreesToRadians(-31), 0)
31+
),
32+
REAR_CENTER_TO_CAMERA_POSE = new Pose3d(
33+
new Translation3d(-0.325 + 0.00975, 0, 0.095),
34+
new Rotation3d(Math.PI - Units.degreesToRadians(0), Units.degreesToRadians(-33), Math.PI + Units.degreesToRadians(0))
35+
);
2636

2737
public static final AprilTagCamera
2838
FRONT_TAG_CAMERA = new AprilTagCamera(

src/main/java/frc/trigon/robot/poseestimation/apriltagcamera/AprilTagCamera.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,6 @@ public double getDistanceToBestTagMeters() {
101101
return inputs.distanceFromBestTag;
102102
}
103103

104-
public Rotation2d getBestTagYawRelativeToRobot() {
105-
if (!inputs.hasResult)
106-
return Rotation2d.fromDegrees(180);
107-
final Rotation2d bestTagYawRelativeToCamera = Rotation2d.fromDegrees(inputs.bestTarget.getYaw());
108-
final Rotation2d robotCenterToCameraYaw = Rotation2d.fromRadians(robotCenterToCamera.getRotation().getZ());
109-
return bestTagYawRelativeToCamera.rotateBy(robotCenterToCameraYaw);
110-
}
111-
112-
public Translation3d getBestTagTanslation() {
113-
if (!inputs.hasResult)
114-
return new Translation3d();
115-
return inputs.bestTarget.getBestCameraToTarget().getTranslation();
116-
}
117-
118104
/**
119105
* If the robot is close enough to the tag, it calculates the pose using the solve PNP heading.
120106
* If it's too far, it assumes the robot's heading from the gyro and calculates its position from there.

src/main/java/frc/trigon/robot/poseestimation/apriltagcamera/AprilTagCameraIO.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import edu.wpi.first.math.geometry.Pose3d;
44
import edu.wpi.first.math.geometry.Transform3d;
55
import org.littletonrobotics.junction.AutoLog;
6-
import org.photonvision.targeting.PhotonTrackedTarget;
76

87
public class AprilTagCameraIO {
98
protected void updateInputs(AprilTagCameraInputsAutoLogged inputs) {
@@ -19,7 +18,6 @@ protected void addSimulatedCameraToVisionSimulation(Transform3d robotToCamera) {
1918

2019
@AutoLog
2120
public static class AprilTagCameraInputs {
22-
public PhotonTrackedTarget bestTarget = new PhotonTrackedTarget();
2321
public boolean hasResult = false;
2422
public double latestResultTimestampSeconds = 0;
2523
public Pose3d cameraSolvePNPPose = new Pose3d();

src/main/java/frc/trigon/robot/poseestimation/apriltagcamera/io/AprilTagPhotonCameraIO.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ private void updateHasResultInputs(AprilTagCameraInputsAutoLogged inputs, Photon
4141
final PhotonTrackedTarget bestTarget = getBestTarget(latestResult);
4242
final Rotation3d bestTargetRelativeRotation3d = getBestTargetRelativeRotation(bestTarget);
4343

44-
inputs.bestTarget = bestTarget;
4544
inputs.cameraSolvePNPPose = getSolvePNPPose(latestResult, bestTarget);
4645
inputs.latestResultTimestampSeconds = latestResult.getTimestampSeconds();
4746
inputs.bestTargetRelativePitchRadians = bestTargetRelativeRotation3d.getY();

src/main/java/frc/trigon/robot/poseestimation/poseestimator/PoseEstimator.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,6 @@ public boolean hasResult() {
106106
return true;
107107
}
108108

109-
public Rotation2d getBestTagYawRelativeToRobot() {
110-
Rotation2d bestTagYawRelativeToCamera = aprilTagCameras[0].getBestTagYawRelativeToRobot();
111-
for (AprilTagCamera camera : aprilTagCameras) {
112-
if (Math.abs(bestTagYawRelativeToCamera.getDegrees()) < Math.abs(camera.getBestTagYawRelativeToRobot().getDegrees()))
113-
bestTagYawRelativeToCamera = camera.getBestTagYawRelativeToRobot();
114-
}
115-
return bestTagYawRelativeToCamera;
116-
}
117-
118109
private void logTargetPath() {
119110
PathPlannerLogging.setLogActivePathCallback((pose) -> {
120111
field.getObject("path").setPoses(pose);

src/main/java/frc/trigon/robot/poseestimation/poseestimator/RobotShowcase.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package frc.trigon.robot.poseestimation.poseestimator;
22

3+
import edu.wpi.first.math.geometry.Transform3d;
34
import edu.wpi.first.math.geometry.Translation3d;
45
import frc.trigon.robot.constants.CameraConstants;
56
import org.photonvision.PhotonCamera;
@@ -17,28 +18,33 @@ public class RobotShowcase {
1718

1819
public MirrorableTranslation3d getBestTagTranslationRelativeToRobot() {
1920
final PhotonPipelineResult bestResult = getBestResult();
20-
final Translation3d bestTagTranslation = getBestTagTranslation(bestResult);
21+
if (bestResult == null)
22+
return new MirrorableTranslation3d(new Translation3d(), false);
23+
final Transform3d cameraToTagTransform = getBestTagTranslation(bestResult);
2124

2225
if (isGettingResultFromFrontCamera)
23-
return new MirrorableTranslation3d(bestTagTranslation.minus(CameraConstants.FRONT_CENTER_TO_CAMERA.getTranslation()), false);
24-
return new MirrorableTranslation3d(bestTagTranslation.minus(CameraConstants.REAR_CENTER_TO_CAMERA.getTranslation()), false);
26+
return new MirrorableTranslation3d(CameraConstants.FRONT_CENTER_TO_CAMERA_POSE.transformBy(cameraToTagTransform).getTranslation(), false);
27+
return new MirrorableTranslation3d(CameraConstants.REAR_CENTER_TO_CAMERA_POSE.transformBy(cameraToTagTransform).getTranslation(), false);
2528
}
2629

27-
private Translation3d getBestTagTranslation(PhotonPipelineResult result) {
30+
private Transform3d getBestTagTranslation(PhotonPipelineResult result) {
2831
final PhotonTrackedTarget bestTarget = result.getBestTarget();
29-
final Translation3d tagTranslation = bestTarget.getBestCameraToTarget().getTranslation();
30-
return tagTranslation;
32+
return bestTarget.getBestCameraToTarget();
3133
}
3234

3335
private PhotonPipelineResult getBestResult() {
3436
final PhotonPipelineResult frontResult = getLatestPipelineResult(frontTagCamera);
3537
final PhotonPipelineResult rearResult = getLatestPipelineResult(rearTagCamera);
3638

37-
if (frontResult == null && rearResult != null) {
39+
if (!frontResult.hasTargets()) {
40+
if (!rearResult.hasTargets())
41+
return null;
3842
isGettingResultFromFrontCamera = false;
3943
return rearResult;
4044
}
41-
if (rearResult == null) {
45+
if (!rearResult.hasTargets()) {
46+
if (!frontResult.hasTargets())
47+
return null;
4248
isGettingResultFromFrontCamera = true;
4349
return frontResult;
4450
}
@@ -52,6 +58,6 @@ private PhotonPipelineResult getBestResult() {
5258

5359
private PhotonPipelineResult getLatestPipelineResult(PhotonCamera camera) {
5460
final List<PhotonPipelineResult> unreadResults = camera.getAllUnreadResults();
55-
return unreadResults.isEmpty() ? null : unreadResults.get(unreadResults.size() - 1);
61+
return unreadResults.isEmpty() ? new PhotonPipelineResult() : unreadResults.get(unreadResults.size() - 1);
5662
}
5763
}

0 commit comments

Comments
 (0)