Skip to content

Commit 230dc4a

Browse files
committed
absolute rotation combined with trigger turning
1 parent 468422f commit 230dc4a

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/main/java/frc/robot/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public static final class ControllerConstants {
88
public static final int kDriverControllerPort = 0;
99
public static final int kOperatorControllerPort = 1;
1010
public static final double kDeadzone = 0.05;
11+
public static final double kAbsoluteTurnDeadzone = 0.5;
1112
public static final double kTriggerDeadzone = .05;
1213
}
1314

@@ -36,6 +37,7 @@ public static final class DriveConstants {
3637

3738
public static final double kTeleopMaxVoltage = 12;
3839
public static final double kTeleopMaxTurnVoltage = 7.2;
40+
public static final double kTurnSpeedMultiplier = 3;
3941
public static final double kGearRatio = 6.12;
4042
public static final double kWheelDiameter = Units.inchesToMeters(4);
4143
public static final double kWheelCircumference = Math.PI * kWheelDiameter;

src/main/java/frc/robot/Robot.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ public void bindDriveControls() {
4444
m_driveSubsystem.driveCommand(
4545
() -> -m_driverController.getLeftY(),
4646
() -> -m_driverController.getLeftX(),
47-
() -> m_driverController.getR2Axis() - m_driverController.getL2Axis(),
47+
() -> m_driverController.getL2Axis() - m_driverController.getR2Axis(),
48+
() -> -m_driverController.getRightX(),
49+
() -> -m_driverController.getRightY(),
4850
m_driverController.getHID()::getSquareButton));
51+
m_driverController.triangle().onTrue(m_driveSubsystem.resetHeadingCommand());
4952
}
5053

5154
@Override

src/main/java/frc/robot/subsystems/DriveSubsystem.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
import com.studica.frc.AHRS.NavXComType;
1515

1616
import edu.wpi.first.math.MathUtil;
17+
import edu.wpi.first.math.controller.PIDController;
1718
import edu.wpi.first.math.geometry.Pose2d;
1819
import edu.wpi.first.math.geometry.Rotation2d;
20+
import edu.wpi.first.math.geometry.Translation2d;
1921
import edu.wpi.first.math.kinematics.ChassisSpeeds;
2022
import edu.wpi.first.math.kinematics.SwerveDriveKinematics;
2123
import edu.wpi.first.math.kinematics.SwerveDriveOdometry;
@@ -46,9 +48,12 @@ public class DriveSubsystem extends SubsystemBase {
4648
private final StructPublisher<Pose2d> m_posePublisher;
4749
private final StructArrayPublisher<SwerveModuleState> m_targetModuleStatePublisher;
4850
private final StructArrayPublisher<SwerveModuleState> m_currentModuleStatePublisher;
51+
private final PIDController m_absoluteAngleController;
4952

5053
/** Creates a new DriveSubsystem. */
5154
public DriveSubsystem() {
55+
m_absoluteAngleController = new PIDController(5, 0, 0);
56+
m_absoluteAngleController.enableContinuousInput(-Math.PI, Math.PI);
5257
m_posePublisher = NetworkTableInstance.getDefault().getStructTopic("/SmartDashboard/Pose", Pose2d.struct)
5358
.publish();
5459
m_targetModuleStatePublisher = NetworkTableInstance.getDefault()
@@ -190,20 +195,29 @@ public void periodic() {
190195
* @return A command to drive the robot.
191196
*/
192197
public Command driveCommand(DoubleSupplier forwardSpeed, DoubleSupplier strafeSpeed,
193-
DoubleSupplier rotation, BooleanSupplier isFieldRelative) {
198+
DoubleSupplier rotation, DoubleSupplier absoluteRotationForward, DoubleSupplier absoluteRotationStrafe,
199+
BooleanSupplier isFieldRelative) {
194200
return run(() -> {
201+
Translation2d t = new Translation2d(absoluteRotationStrafe.getAsDouble(),
202+
absoluteRotationForward.getAsDouble());
203+
195204
// Get the forward, strafe, and rotation speed, using a deadband on the joystick
196205
// input so slight movements don't move the robot
197206
double rotSpeed = MathUtil.applyDeadband(rotation.getAsDouble(), ControllerConstants.kDeadzone);
198-
rotSpeed = Math.signum(rotSpeed) * Math.pow(rotSpeed, 2) * kTeleopMaxTurnVoltage;
207+
double threshold = Math.max(ControllerConstants.kAbsoluteTurnDeadzone, rotSpeed);
208+
rotSpeed *= kTurnSpeedMultiplier;
209+
210+
if (t.getNorm() > threshold)
211+
rotSpeed = m_absoluteAngleController.calculate(getHeading().getRadians(), t.getAngle().getRadians());
212+
rotSpeed = Math.signum(rotSpeed) * Math.min(Math.abs(rotSpeed), kTeleopMaxTurnVoltage);
199213

200214
double fwdSpeed = MathUtil.applyDeadband(forwardSpeed.getAsDouble(), ControllerConstants.kDeadzone);
201215
fwdSpeed = Math.signum(fwdSpeed) * Math.pow(fwdSpeed, 2) * kTeleopMaxVoltage;
202216

203217
double strSpeed = MathUtil.applyDeadband(strafeSpeed.getAsDouble(), ControllerConstants.kDeadzone);
204218
strSpeed = Math.signum(strSpeed) * Math.pow(strSpeed, 2) * kTeleopMaxVoltage;
205219

206-
drive(fwdSpeed, strSpeed, rotSpeed, isFieldRelative.getAsBoolean());
220+
drive(fwdSpeed, strSpeed, rotSpeed, !isFieldRelative.getAsBoolean());
207221
}).withName("DefaultDriveCommand");
208222
}
209223

0 commit comments

Comments
 (0)