-
Notifications
You must be signed in to change notification settings - Fork 1
Autonomous #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Autonomous #37
Changes from all commits
712c813
5b53a6f
391332b
8100afe
c235a7d
a45a06c
b9c0cf6
5d88f91
89f8e63
39edd3b
8d81cfa
0a7fc56
738db4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,17 @@ | ||
| package frc.trigon.robot.commands.commandclasses; | ||
|
|
||
| import edu.wpi.first.math.MathUtil; | ||
| import edu.wpi.first.math.controller.ProfiledPIDController; | ||
| import edu.wpi.first.math.controller.PIDController; | ||
| import edu.wpi.first.math.geometry.Pose2d; | ||
| import edu.wpi.first.math.geometry.Rotation2d; | ||
| import edu.wpi.first.math.geometry.Translation2d; | ||
| import edu.wpi.first.math.trajectory.TrapezoidProfile; | ||
| import edu.wpi.first.wpilibj2.command.*; | ||
| import frc.trigon.lib.hardware.RobotHardwareStats; | ||
| import frc.trigon.lib.utilities.flippable.Flippable; | ||
| import frc.trigon.robot.RobotContainer; | ||
| import frc.trigon.robot.commands.CommandConstants; | ||
| import frc.trigon.robot.constants.OperatorConstants; | ||
| import frc.trigon.robot.misc.shootingphysics.ShootingCalculations; | ||
| import frc.trigon.robot.subsystems.swerve.SwerveCommands; | ||
| import frc.trigon.robot.subsystems.swerve.SwerveConstants; | ||
| import org.littletonrobotics.junction.Logger; | ||
|
|
@@ -21,26 +22,26 @@ | |
| * A command class to assist in the intaking of a game piece. | ||
| */ | ||
| public class IntakeAssistCommand extends ParallelCommandGroup { | ||
| private final ProfiledPIDController | ||
| private final PIDController | ||
| xPIDController = RobotHardwareStats.isSimulation() ? | ||
| new ProfiledPIDController(5, 0, 0, new TrapezoidProfile.Constraints(5, 15)) : | ||
| new ProfiledPIDController(2.4, 0, 0, new TrapezoidProfile.Constraints(2.65, 5.5)), | ||
| new PIDController(5, 0, 0) : | ||
| new PIDController(2.4, 0, 0), | ||
| yPIDController = RobotHardwareStats.isSimulation() ? | ||
| new ProfiledPIDController(3, 0, 0.01, new TrapezoidProfile.Constraints(5, 15)) : | ||
| new ProfiledPIDController(0.3, 0, 0.03, new TrapezoidProfile.Constraints(2.65, 5.5)), | ||
| new PIDController(0.3, 0, 0) : | ||
| new PIDController(0.26, 0, 0), | ||
| thetaPIDController = RobotHardwareStats.isSimulation() ? | ||
| new ProfiledPIDController(0.1, 0, 0, new TrapezoidProfile.Constraints(2.8, 5)) : | ||
| new ProfiledPIDController(2.4, 0, 0, new TrapezoidProfile.Constraints(2.65, 5.5)); | ||
| new PIDController(0.1, 0, 0) : | ||
| new PIDController(2.4, 0, 0); | ||
| private Translation2d distanceFromTrackedGamePiece; | ||
|
|
||
| /** | ||
| * Creates a new intake assist command that assists the driver with intaking a game piece based on the powers. | ||
| * Creates a new intake assist command that assists the driver with intaking a game piece. | ||
| * | ||
| * @param xAssistPower the amount of assist to apply to the X-axis (Forward and backwards) | ||
| * @param yAssistPower the amount of assist to apply to the Y-axis (Sideways) | ||
| * @param thetaAssistPower the amount of assist to apply to the rotation | ||
| * @param shouldAssistX should apply to the X-axis (Forward and backwards) | ||
| * @param shouldAssistY should apply to the Y-axis (Sideways) | ||
| * @param shouldAssistTheta should apply to the rotation | ||
| */ | ||
| public IntakeAssistCommand(double xAssistPower, double yAssistPower, double thetaAssistPower) { | ||
| public IntakeAssistCommand(boolean shouldAssistX, boolean shouldAssistY, boolean shouldAssistTheta) { | ||
| addCommands( | ||
| new RunCommand(this::trackGamePiece), | ||
| new SequentialCommandGroup( | ||
|
|
@@ -49,56 +50,54 @@ public IntakeAssistCommand(double xAssistPower, double yAssistPower, double thet | |
| new WaitUntilCommand(this::hasNoTrackedGamePiece) | ||
| ).repeatedly(), | ||
| SwerveCommands.getClosedLoopSelfRelativeDriveCommand( | ||
| () -> calculateTranslationPower(true, xAssistPower), | ||
| () -> calculateTranslationPower(false, yAssistPower), | ||
| () -> calculateThetaPower(thetaAssistPower) | ||
| () -> calculateTranslationPower(true, shouldAssistX), | ||
| () -> calculateTranslationPower(false, shouldAssistY), | ||
| () -> calculateThetaPower(shouldAssistTheta) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| private void resetPIDControllers() { | ||
| xPIDController.reset(distanceFromTrackedGamePiece.getX(), RobotContainer.SWERVE.getSelfRelativeVelocity().getX()); | ||
| yPIDController.reset(distanceFromTrackedGamePiece.getY(), RobotContainer.SWERVE.getSelfRelativeVelocity().getY()); | ||
| thetaPIDController.reset(distanceFromTrackedGamePiece.getAngle().getRadians(), RobotContainer.SWERVE.getRotationalVelocityRadiansPerSecond()); | ||
| xPIDController.reset(); | ||
| yPIDController.reset(); | ||
| thetaPIDController.reset(); | ||
|
|
||
| xPIDController.setGoal(0); | ||
| yPIDController.setGoal(0); | ||
| thetaPIDController.setGoal(0); | ||
| xPIDController.setSetpoint(0); | ||
| yPIDController.setSetpoint(0); | ||
| thetaPIDController.setSetpoint(0); | ||
| } | ||
|
|
||
| private double calculateTranslationPower(boolean isXAxis, double assistScalar) { | ||
| private double calculateTranslationPower(boolean isXAxis, boolean shouldAssist) { | ||
| final Translation2d selfRelativeJoystickValue = getSelfRelativeJoystickPosition(); | ||
| final double joystickValue = isXAxis ? selfRelativeJoystickValue.getX() : selfRelativeJoystickValue.getY(); | ||
| final double joystickValue = CommandConstants.calculateDriveStickAxisValue(isXAxis ? selfRelativeJoystickValue.getX() : selfRelativeJoystickValue.getY()); | ||
|
|
||
| if (hasNoTrackedGamePiece()) | ||
| if (!shouldAssist || hasNoTrackedGamePiece()) | ||
| return joystickValue; | ||
|
|
||
| final double | ||
| pidOutput = clampToSwerveOutputRange(isXAxis ? | ||
| xPIDController.calculate(distanceFromTrackedGamePiece.getX()) : | ||
| yPIDController.calculate(distanceFromTrackedGamePiece.getY())); | ||
|
|
||
| return calculateAssistPower(assistScalar, pidOutput, joystickValue); | ||
| return calculateAssistPower(pidOutput, joystickValue); | ||
| } | ||
|
|
||
| private double calculateThetaPower(double assistScalar) { | ||
| final double joystickValue = OperatorConstants.DRIVER_CONTROLLER.getRightX(); | ||
| private double calculateThetaPower(boolean shouldAssist) { | ||
| final double joystickValue = CommandConstants.calculateDriveStickAxisValue(OperatorConstants.DRIVER_CONTROLLER.getRightX()); | ||
|
|
||
| if (hasNoTrackedGamePiece()) | ||
| if (!shouldAssist || hasNoTrackedGamePiece()) | ||
| return joystickValue; | ||
|
|
||
| return calculateThetaAssistPower(assistScalar, joystickValue, distanceFromTrackedGamePiece.getAngle().unaryMinus()); | ||
| return calculateThetaAssistPower(joystickValue, distanceFromTrackedGamePiece.getAngle().unaryMinus()); | ||
| } | ||
|
|
||
| private double calculateThetaAssistPower(double assistScalar, double joystickValue, Rotation2d thetaOffset) { | ||
| private double calculateThetaAssistPower(double joystickValue, Rotation2d thetaOffset) { | ||
| final double pidOutput = clampToSwerveOutputRange(thetaPIDController.calculate(thetaOffset.getRadians())); | ||
| return calculateAssistPower(assistScalar, pidOutput, joystickValue); | ||
| return calculateAssistPower(pidOutput, joystickValue); | ||
| } | ||
|
|
||
| private double calculateAssistPower(double assistScalar, double pidOutput, double joystickValue) { | ||
| if (assistScalar == 0) | ||
| return joystickValue; | ||
| return (joystickValue * (1 - assistScalar)) + (pidOutput * assistScalar); | ||
| private double calculateAssistPower(double pidOutput, double joystickValue) { | ||
| return joystickValue + pidOutput; | ||
| } | ||
|
Comment on lines
+99
to
101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Result may exceed valid output range. Adding Consider clamping the final result: return MathUtil.clamp(joystickValue + pidOutput, -1, 1); |
||
|
|
||
| private boolean hasNoTrackedGamePiece() { | ||
|
|
@@ -165,28 +164,17 @@ private boolean isGamePieceWithinAssistFOV(Translation2d gamePiecePosition, Pose | |
| } | ||
|
|
||
| private boolean isDrivingTowardsGamePiece(Translation2d gamePieceFieldRelativePosition, Pose2d robotPose) { | ||
| final Translation2d fieldRelativeDistanceFromGamePiece = gamePieceFieldRelativePosition.minus(robotPose.getTranslation()); | ||
|
|
||
| final double velocityTowardsGamePiece = calculateVelocityTowardsGamePiece(fieldRelativeDistanceFromGamePiece); | ||
| final Translation2d velocityRelativeToGamePiece = ShootingCalculations.calculateVelocityRelativeToPoint(gamePieceFieldRelativePosition, robotPose.getTranslation(), getFieldRelativeJoystickPosition()); | ||
| if (velocityRelativeToGamePiece.getX() < Math.abs(velocityRelativeToGamePiece.getY())) | ||
| return false; | ||
| final double velocityTowardsGamePiece = velocityRelativeToGamePiece.getX() * SwerveConstants.MAXIMUM_SPEED_METERS_PER_SECOND; | ||
| return velocityTowardsGamePiece > OperatorConstants.MINIMUM_VELOCITY_TOWARDS_GAME_PIECE_FOR_INTAKE_ASSIST_METERS_PER_SECOND; | ||
| } | ||
|
|
||
| private double calculateMaximumAssistAngleDegrees(double gamePieceDistance) { | ||
| return OperatorConstants.INTAKE_ASSIST_MAXIMUM_ASSISTABLE_ANGLE_FORMULA.applyAsDouble(gamePieceDistance); | ||
| } | ||
|
|
||
| private double calculateVelocityTowardsGamePiece(Translation2d fieldRelativeDistanceFromGamePiece) { | ||
| final Translation2d fieldRelativeJoystickPosition = getFieldRelativeJoystickPosition(); | ||
|
|
||
| final Rotation2d joystickAngle = fieldRelativeJoystickPosition.getAngle(); | ||
| final Rotation2d fieldRelativeGamePieceAngleFromRobot = fieldRelativeDistanceFromGamePiece.getAngle(); | ||
| final Rotation2d angularOffset = joystickAngle.minus(fieldRelativeGamePieceAngleFromRobot); | ||
|
|
||
| final double desiredRobotVelocity = fieldRelativeJoystickPosition.getNorm() * SwerveConstants.MAXIMUM_SPEED_METERS_PER_SECOND; | ||
|
|
||
| return angularOffset.getCos() * desiredRobotVelocity; | ||
| } | ||
|
|
||
| private double clampToSwerveOutputRange(double value) { | ||
| return MathUtil.clamp(value, -1, 1); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong axis scaling method for rotation.
Line 86 uses
calculateDriveStickAxisValuefor the rotation stick (getRightX), but this method is intended for translation axes. Based on theCommandConstantsdocumentation, this should usecalculateRotationStickAxisValuewhich applies the correct rotation-specific divider and shift power.