Skip to content

Commit

Permalink
switched to shot vector in utilfuncs
Browse files Browse the repository at this point in the history
  • Loading branch information
PGgit08 committed Mar 19, 2024
1 parent f1c6cf5 commit 2ed7754
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 42 deletions.
3 changes: 1 addition & 2 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ public RobotContainer() {

configureBindings();

UtilFuncs.ShotDistance(_swerveSubsystem::speakerShotDistance);
UtilFuncs.ShootFast(_swerveSubsystem::speakerDistance);
UtilFuncs.ShotVector(_swerveSubsystem::shotVector);

_autonChooser = AutoBuilder.buildAutoChooser();

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/subsystems/ElevatorSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void periodic() {
* Get the calculated height needed to aim at the speaker.
*/
public double speakerHeight() {
double distance = UtilFuncs.ShotDistance();
double distance = UtilFuncs.ShotVector().getNorm();

return 0;
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/frc/robot/subsystems/ShooterSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Constants;
import frc.robot.Constants.FeedForward;
import frc.robot.Constants.FieldConstants;
import frc.robot.Constants.PID;
import frc.robot.Constants.Speeds;
import frc.robot.utils.UtilFuncs;
Expand Down Expand Up @@ -83,7 +84,7 @@ public void periodic() {
* Get the calculated angle needed to aim at the speaker.
*/
public double speakerAngle() {
double distance = UtilFuncs.ShotDistance();
double distance = UtilFuncs.ShotVector().getNorm();

return 0;
}
Expand Down Expand Up @@ -132,7 +133,7 @@ public void stopAngle() {
public void setShooterState(ShooterState state) {
switch (state) {
case SHOOT:
if (UtilFuncs.ShootFast()) {
if (UtilFuncs.ShotVector().getNorm() > FieldConstants.SHOOTER_SLOW_THRESHOLD) {
spinShooter(Speeds.SHOOTER_FAST_SPIN_SPEED);
} else {
spinShooter(Speeds.SHOOTER_SLOW_SPIN_SPEED);
Expand Down
16 changes: 3 additions & 13 deletions src/main/java/frc/robot/subsystems/SwerveDriveSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void periodic() {
SmartDashboard.putNumber("Gyro RAW", getHeadingRaw().getDegrees());
SmartDashboard.putBoolean("Field Oriented", fieldOriented);
SmartDashboard.putNumber("Match Time", DriverStation.getMatchTime());
SmartDashboard.putNumber("Speaker Distance", speakerDistance());
SmartDashboard.putNumber("Shot Distance", shotVector().getNorm());

// Update the bot's pose
_estimator.update(getHeadingRaw(), new SwerveModulePosition[]{
Expand Down Expand Up @@ -319,25 +319,15 @@ public Rotation2d getHeadingRaw() {
*
* (could be modified to shoot while moving)
*/
public double speakerShotDistance() {
return speakerDistance();
}

/**
* Get the distance of the chassis from the speaker shot point.
*
* @return Norm of the distance vector.
*/
public double speakerDistance() {
// Pose3d speakerPose = UtilFuncs.GetAlliance() == Alliance.Red ? FieldConstants.SPEAKER_POSE_RED : FieldConstants.SPEAKER_POSE_BLUE;
public Translation2d shotVector() {
Pose3d speakerPose = UtilFuncs.GetSpeakerPose();

Translation2d speakerTranslation = new Translation2d(speakerPose.getX(), speakerPose.getY());
Translation2d botTranslation = getPose().getTranslation();

Translation2d distanceVec = speakerTranslation.minus(botTranslation);

return distanceVec.getNorm();
return distanceVec;
}

/**
Expand Down
34 changes: 10 additions & 24 deletions src/main/java/frc/robot/utils/UtilFuncs.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
/* Copyright (C) 2024 Team 334. All Rights Reserved.*/
package frc.robot.utils;

import java.util.function.DoubleSupplier;
import java.util.function.Supplier;

import edu.wpi.first.apriltag.AprilTagFieldLayout;
import edu.wpi.first.apriltag.AprilTagFields;
import edu.wpi.first.apriltag.AprilTagFieldLayout.OriginPosition;
import edu.wpi.first.math.geometry.Pose3d;
import edu.wpi.first.math.geometry.Rotation3d;
import edu.wpi.first.math.geometry.Transform3d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.geometry.Translation3d;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.DriverStation.Alliance;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import frc.robot.Constants.FieldConstants;
import frc.robot.utils.helpers.AllianceHelper;

/** Any utility functions are here. */
public final class UtilFuncs {
private static DoubleSupplier _shotDistanceSupplier;
private static DoubleSupplier _distanceSupplier;

private static AprilTagFieldLayout _field;
private static Supplier<Translation2d> _shotVectorSupplier;

/**
* Loads the AprilTag field.
Expand Down Expand Up @@ -51,30 +47,20 @@ public static Pose3d GetSpeakerPose() {
);
}

public static void ShotDistance(DoubleSupplier distance) {
_shotDistanceSupplier = distance;
}

public static double ShotDistance() {
return _shotDistanceSupplier.getAsDouble();
}

/**
* Supply the ShootFast function with a distance supplier.
* Supply UtilFuncs with a shot vector supplier.
*
* @param distance Distance supplier - chassis distance from speaker shot point.
* @param shotVectorSupplier A supplier that returns the vector from the chassis to the shot point.
*/
public static void ShootFast(DoubleSupplier distance) {
_distanceSupplier = distance;
public static void ShotVector(Supplier<Translation2d> shotVectorSupplier) {
_shotVectorSupplier = shotVectorSupplier;
}

/**
* Whether to shoot fast based on distance of bot from speaker shot point.
* The vector between the chassis pose and the specific shot point.
*/
public static boolean ShootFast() {
if (_distanceSupplier.getAsDouble() > FieldConstants.SHOOTER_SLOW_THRESHOLD) return true;

return false;
public static Translation2d ShotVector() {
return _shotVectorSupplier.get();
}

/**
Expand Down

0 comments on commit 2ed7754

Please sign in to comment.