-
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
1 parent
e717d28
commit f9731c5
Showing
3 changed files
with
93 additions
and
0 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
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,71 @@ | ||
package frc.robot; | ||
|
||
import edu.wpi.first.math.controller.PIDController; | ||
import edu.wpi.first.math.geometry.Pose2d; | ||
import edu.wpi.first.math.kinematics.ChassisSpeeds; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import frc.robot.Constants.ModuleConstants.DriveToPoseControllerGains; | ||
import frc.robot.drivetrain.Drivetrain; | ||
import frc.robot.vision.Vision; | ||
|
||
public class DriveToPoseCommand extends Command { | ||
|
||
public final Drivetrain swerve; | ||
public final Vision vision; | ||
public final Pose2d targetPose; | ||
|
||
private final PIDController xController = new PIDController( | ||
DriveToPoseControllerGains.kTraP, | ||
DriveToPoseControllerGains.kTraI, | ||
DriveToPoseControllerGains.kTraD); | ||
private final PIDController yController = new PIDController( | ||
DriveToPoseControllerGains.kTraP, | ||
DriveToPoseControllerGains.kTraI, | ||
DriveToPoseControllerGains.kTraD); | ||
private final PIDController thetaController = new PIDController( | ||
DriveToPoseControllerGains.kRotP, | ||
DriveToPoseControllerGains.kRotI, | ||
DriveToPoseControllerGains.kRotD); | ||
|
||
public DriveToPoseCommand(Drivetrain drivetrain, Vision poseEstimator, Pose2d targetPosition) { | ||
swerve = drivetrain; | ||
vision = poseEstimator; | ||
targetPose = targetPosition; | ||
|
||
thetaController.enableContinuousInput(-Math.PI, Math.PI); | ||
|
||
addRequirements(swerve); | ||
} | ||
|
||
@Override | ||
public void initialize() {} | ||
|
||
@Override | ||
public void execute() { | ||
Pose2d currentPose = swerve.getPose(); | ||
|
||
ChassisSpeeds speeds = ChassisSpeeds.fromFieldRelativeSpeeds(new ChassisSpeeds( | ||
xController.calculate(currentPose.getX(), targetPose.getX()), | ||
yController.calculate(currentPose.getY(), targetPose.getY()), | ||
thetaController.calculate(currentPose.getRotation().getRadians(), targetPose.getRotation().getRadians())), | ||
swerve.getHeading()); | ||
|
||
swerve.setChassisSpeeds(speeds); | ||
} | ||
|
||
@Override | ||
public boolean isFinished() { | ||
Pose2d currentPose = swerve.getPose(); | ||
double xError = Math.abs(targetPose.getX() - currentPose.getX()); | ||
double yError = Math.abs(targetPose.getY() - currentPose.getY()); | ||
double thetaError = Math.abs(targetPose.getRotation().getRadians() - currentPose.getRotation().getRadians()); | ||
|
||
return xError < 0.1 && yError < 0.1 && thetaError < Math.toRadians(3); | ||
} | ||
|
||
@Override | ||
public void end(boolean interrupted) { | ||
ChassisSpeeds chassisSpeeds = new ChassisSpeeds(0, 0, 0); | ||
swerve.setChassisSpeeds(chassisSpeeds); | ||
} | ||
} |
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