Skip to content

Commit

Permalink
Removed duplicate implementations of isFacingSpeaker()
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Mar 31, 2024
1 parent e8c9588 commit 6d6e582
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 46 deletions.
1 change: 1 addition & 0 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ public static class ShooterConstants {
}

public static final class AutoShootConstants {
public static final double speakerEdgeTolerance = Units.inchesToMeters(2.0);
// (distance, angle)
// This isn't being used, but the data could still be important at some point
public static final Point2D[] autoShootArmAngles =
Expand Down
26 changes: 3 additions & 23 deletions src/main/java/frc/robot/commands/LookAheadSOTM.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.Constants.AutoShootConstants;
import frc.robot.Constants.FieldConstants;
import frc.robot.Constants.SwerveConstants;
import frc.robot.subsystems.Arm;
import frc.robot.subsystems.Intake;
import frc.robot.subsystems.Shooter;
import frc.robot.subsystems.Swerve;
import frc.robot.util.AllianceUtil;
import frc.robot.util.AutoShootMathUtil;
import frc.robot.util.MovingShotData;
import frc.robot.util.ShooterState;
import java.util.function.DoubleSupplier;
Expand Down Expand Up @@ -137,27 +137,6 @@ public void initialize() {
recalculateSetpoint = true;
}

private boolean isFacingSpeaker(Pose2d robotPose, Translation2d virtualGoalLocation) {
if (Math.abs(robotPose.getRotation().getCos()) == 0.0) {
return false;
}
// y = m(goalx) + b
// b = roboty - m(robotx)
double slope = Math.tan(robotPose.getRotation().getRadians());
if (Double.isInfinite(slope) || Double.isNaN(slope)) {
return false;
}

double b = robotPose.getY() - slope * robotPose.getX();

double yIntersect = slope * virtualGoalLocation.getX() + b;

double upperBound = virtualGoalLocation.getY() + FieldConstants.speakerWidth / 2;
double lowerBound = virtualGoalLocation.getY() - FieldConstants.speakerWidth / 2;

return yIntersect >= lowerBound && yIntersect <= upperBound;
}

private MovingShotData updateCurrentState(
MovingShotData currentState,
Pose2d robotPose,
Expand Down Expand Up @@ -300,7 +279,8 @@ public void execute() {

SmartDashboard.putNumber("Auto Shoot/Drive Angle Error", driveAngleError.getDegrees());

boolean facingSpeaker = isFacingSpeaker(robotPose, currentState.virtualGoalLocation);
boolean facingSpeaker =
AutoShootMathUtil.isFacingSpeaker(robotPose, currentState.virtualGoalLocation);

SmartDashboard.putBoolean("Auto Shoot/Facing Speaker", facingSpeaker);

Expand Down
25 changes: 2 additions & 23 deletions src/main/java/frc/robot/commands/ShootWhileMoving.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.Constants.AutoShootConstants;
import frc.robot.Constants.FieldConstants;
import frc.robot.Constants.SwerveConstants;
import frc.robot.subsystems.Arm;
import frc.robot.subsystems.Intake;
import frc.robot.subsystems.Shooter;
import frc.robot.subsystems.Swerve;
import frc.robot.util.AllianceUtil;
import frc.robot.util.AutoShootMathUtil;
import frc.robot.util.ShooterState;
import java.util.function.DoubleSupplier;

Expand Down Expand Up @@ -125,27 +125,6 @@ public void initialize() {
swerve.setIgnoreArducam(true);
}

private boolean isFacingSpeaker(Pose2d robotPose, Translation2d virtualGoalLocation) {
if (Math.abs(robotPose.getRotation().getCos()) == 0.0) {
return false;
}
// y = m(goalx) + b
// b = roboty - m(robotx)
double slope = Math.tan(robotPose.getRotation().getRadians());
if (Double.isInfinite(slope) || Double.isNaN(slope)) {
return false;
}

double b = robotPose.getY() - slope * robotPose.getX();

double yIntersect = slope * virtualGoalLocation.getX() + b;

double upperBound = virtualGoalLocation.getY() + FieldConstants.speakerWidth / 2;
double lowerBound = virtualGoalLocation.getY() - FieldConstants.speakerWidth / 2;

return yIntersect >= lowerBound && yIntersect <= upperBound;
}

// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
Expand Down Expand Up @@ -256,7 +235,7 @@ public void execute() {

SmartDashboard.putNumber("Auto Shoot/Drive Angle Error", driveAngleError.getDegrees());

boolean facingSpeaker = isFacingSpeaker(robotPose, virtualGoalLocation);
boolean facingSpeaker = AutoShootMathUtil.isFacingSpeaker(robotPose, virtualGoalLocation);

SmartDashboard.putBoolean("Auto Shoot/Facing Speaker", facingSpeaker);

Expand Down
33 changes: 33 additions & 0 deletions src/main/java/frc/robot/util/AutoShootMathUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package frc.robot.util;

import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Translation2d;
import frc.robot.Constants.AutoShootConstants;
import frc.robot.Constants.FieldConstants;

public class AutoShootMathUtil {
public static boolean isFacingSpeaker(Pose2d robotPose, Translation2d virtualGoalLocation) {
if (Math.abs(robotPose.getRotation().getCos()) == 0.0) {
return false;
}
// y = m(goalx) + b
// b = roboty - m(robotx)
double slope = Math.tan(robotPose.getRotation().getRadians());
if (Double.isInfinite(slope) || Double.isNaN(slope)) {
return false;
}

double b = robotPose.getY() - slope * robotPose.getX();

double yIntersect = slope * virtualGoalLocation.getX() + b;

double upperBound =
virtualGoalLocation.getY()
+ (FieldConstants.speakerWidth - AutoShootConstants.speakerEdgeTolerance) / 2;
double lowerBound =
virtualGoalLocation.getY()
- (FieldConstants.speakerWidth - AutoShootConstants.speakerEdgeTolerance) / 2;

return yIntersect >= lowerBound && yIntersect <= upperBound;
}
}

0 comments on commit 6d6e582

Please sign in to comment.