-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: suryatho <[email protected]>
- Loading branch information
1 parent
399db96
commit 623ebc6
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/frc/robot/subsystems/drive/controllers/TeleopDriveController.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,68 @@ | ||
package frc.robot.subsystems.drive.controllers; | ||
|
||
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 frc.robot.subsystems.drive.DriveConstants; | ||
import frc.robot.util.LoggedTunableNumber; | ||
|
||
/** | ||
* Drive controller for outputting {@link ChassisSpeeds} from driver joysticks. | ||
*/ | ||
public class TeleopDriveController { | ||
private static final LoggedTunableNumber controllerDeadband = | ||
new LoggedTunableNumber("TeleopDrive/Deadband", 0.1); | ||
|
||
private double controllerX = 0; | ||
private double controllerY = 0; | ||
private double controllerOmega = 0; | ||
|
||
/** | ||
* Updates the controller with the currently stored state. | ||
* @return {@link ChassisSpeeds} with driver's requested speeds. | ||
*/ | ||
public ChassisSpeeds update() { | ||
// Apply deadband | ||
double linearMagnitude = | ||
MathUtil.applyDeadband( | ||
Math.hypot(controllerX, controllerY), controllerDeadband.get()); | ||
Rotation2d linearDirection = | ||
new Rotation2d(controllerX, controllerY); | ||
double omega = MathUtil.applyDeadband(controllerOmega, controllerDeadband.get()); | ||
|
||
// 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(); | ||
if (DriverStation.getAlliance().isPresent() | ||
&& DriverStation.getAlliance().get() == DriverStation.Alliance.Red) { | ||
linearVelocity = linearVelocity.rotateBy(Rotation2d.fromRadians(Math.PI)); | ||
} | ||
|
||
return new ChassisSpeeds( | ||
linearVelocity.getX() * DriveConstants.driveConfig.maxLinearVelocity(), | ||
linearVelocity.getY() * DriveConstants.driveConfig.maxLinearVelocity(), | ||
omega * DriveConstants.driveConfig.maxAngularVelocity()); | ||
} | ||
|
||
/** | ||
* Accepts new drive input from joysticks. | ||
* @param x Desired x velocity scalar, -1..1 | ||
* @param y Desired y velocity scalar, -1..1 | ||
* @param omega Desired omega velocity scalar, -1..1 | ||
*/ | ||
public void acceptDriveInput(double x, double y, double omega) { | ||
controllerX = x; | ||
controllerY = y; | ||
controllerOmega = omega; | ||
} | ||
} |