-
Notifications
You must be signed in to change notification settings - Fork 0
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
Speaker targeting #32
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e524577
fix: driver flipping needs to be done in execute for some reason (at …
mimizh2418 a9355eb
feat: drivetrain targets speaker
mimizh2418 cf41c8f
fix: correct speaker pose
mimizh2418 198e561
fix: use continuous output on the pid
mimizh2418 46b3631
fix: stop resetting rate limiters on init (smoother transition betwee…
mimizh2418 973ed09
fix: flipping actually works in init you just have to go disconnected…
mimizh2418 eaf26c3
fix: toggle speaker targeting on true
mimizh2418 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
72 changes: 72 additions & 0 deletions
72
src/main/java/org/team1540/robot2024/commands/DriveWithSpeakerTargetingCommand.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,72 @@ | ||
package org.team1540.robot2024.commands; | ||
|
||
import com.pathplanner.lib.util.GeometryUtil; | ||
import edu.wpi.first.math.MathUtil; | ||
import edu.wpi.first.math.controller.PIDController; | ||
import edu.wpi.first.math.filter.SlewRateLimiter; | ||
import edu.wpi.first.math.geometry.Pose2d; | ||
import edu.wpi.first.math.geometry.Rotation2d; | ||
import edu.wpi.first.wpilibj.DriverStation; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import edu.wpi.first.wpilibj2.command.button.CommandXboxController; | ||
import org.littletonrobotics.junction.Logger; | ||
import org.team1540.robot2024.subsystems.drive.Drivetrain; | ||
import org.team1540.robot2024.util.LoggedTunableNumber; | ||
|
||
import static org.team1540.robot2024.Constants.Targeting.*; | ||
|
||
public class DriveWithSpeakerTargetingCommand extends Command { | ||
private final Drivetrain drivetrain; | ||
private final CommandXboxController controller; | ||
|
||
private final SlewRateLimiter xLimiter = new SlewRateLimiter(2); | ||
private final SlewRateLimiter yLimiter = new SlewRateLimiter(2); | ||
private final PIDController rotController = new PIDController(ROT_KP, ROT_KI, ROT_KD); | ||
|
||
private final LoggedTunableNumber kP = new LoggedTunableNumber("Targeting/ROT_KP", ROT_KP); | ||
private final LoggedTunableNumber kI = new LoggedTunableNumber("Targeting/ROT_KI", ROT_KI); | ||
private final LoggedTunableNumber kD = new LoggedTunableNumber("Targeting/ROT_KD", ROT_KD); | ||
|
||
private boolean isFlipped; | ||
private Pose2d speakerPose; | ||
|
||
public DriveWithSpeakerTargetingCommand(Drivetrain drivetrain, CommandXboxController controller) { | ||
this.drivetrain = drivetrain; | ||
this.controller = controller; | ||
rotController.enableContinuousInput(-Math.PI, Math.PI); | ||
addRequirements(drivetrain); | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
rotController.reset(); | ||
|
||
isFlipped = | ||
DriverStation.getAlliance().isPresent() | ||
&& DriverStation.getAlliance().get() == DriverStation.Alliance.Red; | ||
speakerPose = isFlipped ? GeometryUtil.flipFieldPose(SPEAKER_POSE) : SPEAKER_POSE; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
if (kP.hasChanged(hashCode()) || kI.hasChanged(hashCode()) || kD.hasChanged(hashCode())) { | ||
rotController.setPID(kP.get(), kI.get(), kD.get()); | ||
} | ||
|
||
Rotation2d targetRot = | ||
drivetrain.getPose().minus(speakerPose).getTranslation().getAngle() | ||
.rotateBy(isFlipped ? Rotation2d.fromDegrees(0) : Rotation2d.fromDegrees(180)); | ||
Logger.recordOutput("Targeting/setpointPose", new Pose2d(drivetrain.getPose().getTranslation(), targetRot)); | ||
Logger.recordOutput("Targeting/speakerPose", speakerPose); | ||
|
||
double xPercent = MathUtil.applyDeadband(xLimiter.calculate(-controller.getLeftY()), 0.1); | ||
double yPercent = MathUtil.applyDeadband(yLimiter.calculate(-controller.getLeftX()), 0.1); | ||
double rotPercent = rotController.calculate(drivetrain.getRotation().getRadians(), targetRot.getRadians()); | ||
drivetrain.drivePercent(xPercent, yPercent, rotPercent, isFlipped); | ||
} | ||
|
||
@Override | ||
public void end(boolean interrupted) { | ||
drivetrain.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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
tuning mode?