-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
bbdriverstation2
committed
Mar 8, 2025
1 parent
179912e
commit 66a62fd
Showing
5 changed files
with
117 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/main/java/frc/robot/commands/DriveCommands/FieldDriveElevatorLimitedCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package frc.robot.commands.DriveCommands; | ||
|
||
import edu.wpi.first.math.MathUtil; | ||
import edu.wpi.first.math.geometry.Pose2d; | ||
import edu.wpi.first.math.geometry.Rotation2d; | ||
import edu.wpi.first.math.geometry.Transform2d; | ||
import edu.wpi.first.math.geometry.Translation2d; | ||
import edu.wpi.first.math.kinematics.ChassisSpeeds; | ||
import edu.wpi.first.wpilibj.DriverStation; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import frc.robot.OperatorInput; | ||
import frc.robot.constants.DriveConstants; | ||
import frc.robot.constants.ElevatorConstants; | ||
import frc.robot.subsystems.DriveSubsystem.DriveSubsystem; | ||
import frc.robot.subsystems.ElevatorSubsystem.ElevatorSubsystem; | ||
|
||
import java.util.function.DoubleSupplier; | ||
import java.util.function.Supplier; | ||
|
||
public class FieldDriveElevatorLimitedCommand extends Command { | ||
private final DriveSubsystem drive; | ||
private final DoubleSupplier xSupplier; | ||
private final DoubleSupplier ySupplier; | ||
private final DoubleSupplier omegaSupplier; | ||
private final ElevatorSubsystem elevator; | ||
private final Supplier<Rotation2d> headingSupplier; | ||
|
||
public FieldDriveElevatorLimitedCommand(DriveSubsystem drive, ElevatorSubsystem elevatorSubsystem, DoubleSupplier xSupplier, DoubleSupplier ySupplier, DoubleSupplier omegaSupplier, Supplier<Rotation2d> headingSupplier) { | ||
this.drive = drive; | ||
this.xSupplier = xSupplier; | ||
this.ySupplier = ySupplier; | ||
this.omegaSupplier = omegaSupplier; | ||
this.headingSupplier = headingSupplier; | ||
this.elevator = elevatorSubsystem; | ||
} | ||
|
||
|
||
public void execute() { | ||
// Apply deadband | ||
double DEADBAND = 0.1; | ||
double linearMagnitude = | ||
MathUtil.applyDeadband( | ||
Math.hypot(xSupplier.getAsDouble(), ySupplier.getAsDouble()), DEADBAND); | ||
Rotation2d linearDirection = | ||
new Rotation2d(xSupplier.getAsDouble(), ySupplier.getAsDouble()); | ||
double omega = MathUtil.applyDeadband(omegaSupplier.getAsDouble(), DEADBAND); | ||
|
||
// Square values | ||
linearMagnitude = linearMagnitude * linearMagnitude; | ||
omega = Math.copySign(omega * omega, omega); | ||
|
||
// Calcaulate new linear velocity | ||
Translation2d linearVelocity = | ||
new Pose2d(new Translation2d(), linearDirection) | ||
.transformBy(new Transform2d(linearMagnitude, 0.0, new Rotation2d())) | ||
.getTranslation(); | ||
|
||
// Convert to field relative speeds & send command | ||
boolean isFlipped = | ||
DriverStation.getAlliance().isPresent() | ||
&& DriverStation.getAlliance().get() == DriverStation.Alliance.Red; | ||
|
||
|
||
double speedFactor; | ||
if (OperatorInput.slowModeHold.getAsBoolean()) { | ||
speedFactor = DriveConstants.slowSpeed; | ||
} else if (OperatorInput.turboModeHold.getAsBoolean()) { | ||
speedFactor = DriveConstants.turboSpeed; | ||
} else { | ||
speedFactor = DriveConstants.normalSpeed; | ||
} | ||
|
||
speedFactor *= speedRamp(elevator.getLoadHeight(), | ||
ElevatorConstants.minHeight + 0.2, ElevatorConstants.maxHeight, 0, 1); | ||
|
||
|
||
ChassisSpeeds speeds_robotOriented = new ChassisSpeeds( | ||
linearVelocity.getX() * speedFactor, //4.5 is the experimentally determined max velocity | ||
linearVelocity.getY() * speedFactor, | ||
omega * Math.PI * 1.5 | ||
); | ||
|
||
Rotation2d headingFlipped = headingSupplier.get(); | ||
|
||
if (isFlipped) { | ||
headingFlipped = headingFlipped.plus(Rotation2d.fromDegrees(180)); | ||
} | ||
|
||
drive.runVelocity(ChassisSpeeds.fromFieldRelativeSpeeds(speeds_robotOriented, headingFlipped)); | ||
} | ||
|
||
|
||
// returns a ramped speed value | ||
// a value between an min max, gets mapped to a different linear scale | ||
private double speedRamp(double value, double inputMin, double inputMax, double outputMin, double outputMax) { | ||
if (!(value <= inputMax && value >= inputMin)) {return 1;} | ||
double range1 = inputMax - inputMin; | ||
double diff1 = value - inputMin; | ||
double firstScalar = diff1 / range1; | ||
|
||
double range2 = outputMax - outputMin; | ||
return range2 * firstScalar; | ||
}; | ||
|
||
public void end(boolean interrupted) { | ||
drive.stop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters