|
14 | 14 | import com.studica.frc.AHRS.NavXComType; |
15 | 15 |
|
16 | 16 | import edu.wpi.first.math.MathUtil; |
| 17 | +import edu.wpi.first.math.controller.PIDController; |
17 | 18 | import edu.wpi.first.math.geometry.Pose2d; |
18 | 19 | import edu.wpi.first.math.geometry.Rotation2d; |
| 20 | +import edu.wpi.first.math.geometry.Translation2d; |
19 | 21 | import edu.wpi.first.math.kinematics.ChassisSpeeds; |
20 | 22 | import edu.wpi.first.math.kinematics.SwerveDriveKinematics; |
21 | 23 | import edu.wpi.first.math.kinematics.SwerveDriveOdometry; |
@@ -46,9 +48,12 @@ public class DriveSubsystem extends SubsystemBase { |
46 | 48 | private final StructPublisher<Pose2d> m_posePublisher; |
47 | 49 | private final StructArrayPublisher<SwerveModuleState> m_targetModuleStatePublisher; |
48 | 50 | private final StructArrayPublisher<SwerveModuleState> m_currentModuleStatePublisher; |
| 51 | + private final PIDController m_absoluteAngleController; |
49 | 52 |
|
50 | 53 | /** Creates a new DriveSubsystem. */ |
51 | 54 | public DriveSubsystem() { |
| 55 | + m_absoluteAngleController = new PIDController(5, 0, 0); |
| 56 | + m_absoluteAngleController.enableContinuousInput(-Math.PI, Math.PI); |
52 | 57 | m_posePublisher = NetworkTableInstance.getDefault().getStructTopic("/SmartDashboard/Pose", Pose2d.struct) |
53 | 58 | .publish(); |
54 | 59 | m_targetModuleStatePublisher = NetworkTableInstance.getDefault() |
@@ -190,20 +195,29 @@ public void periodic() { |
190 | 195 | * @return A command to drive the robot. |
191 | 196 | */ |
192 | 197 | public Command driveCommand(DoubleSupplier forwardSpeed, DoubleSupplier strafeSpeed, |
193 | | - DoubleSupplier rotation, BooleanSupplier isFieldRelative) { |
| 198 | + DoubleSupplier rotation, DoubleSupplier absoluteRotationForward, DoubleSupplier absoluteRotationStrafe, |
| 199 | + BooleanSupplier isFieldRelative) { |
194 | 200 | return run(() -> { |
| 201 | + Translation2d t = new Translation2d(absoluteRotationStrafe.getAsDouble(), |
| 202 | + absoluteRotationForward.getAsDouble()); |
| 203 | + |
195 | 204 | // Get the forward, strafe, and rotation speed, using a deadband on the joystick |
196 | 205 | // input so slight movements don't move the robot |
197 | 206 | 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); |
199 | 213 |
|
200 | 214 | double fwdSpeed = MathUtil.applyDeadband(forwardSpeed.getAsDouble(), ControllerConstants.kDeadzone); |
201 | 215 | fwdSpeed = Math.signum(fwdSpeed) * Math.pow(fwdSpeed, 2) * kTeleopMaxVoltage; |
202 | 216 |
|
203 | 217 | double strSpeed = MathUtil.applyDeadband(strafeSpeed.getAsDouble(), ControllerConstants.kDeadzone); |
204 | 218 | strSpeed = Math.signum(strSpeed) * Math.pow(strSpeed, 2) * kTeleopMaxVoltage; |
205 | 219 |
|
206 | | - drive(fwdSpeed, strSpeed, rotSpeed, isFieldRelative.getAsBoolean()); |
| 220 | + drive(fwdSpeed, strSpeed, rotSpeed, !isFieldRelative.getAsBoolean()); |
207 | 221 | }).withName("DefaultDriveCommand"); |
208 | 222 | } |
209 | 223 |
|
|
0 commit comments