Skip to content

Commit

Permalink
TARSReboot: tune pid (should drive straight)
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhreng committed Dec 6, 2024
1 parent 5a0f3fa commit ed4b004
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
17 changes: 12 additions & 5 deletions TARSReboot/src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

package frc.robot;

import static edu.wpi.first.units.Units.*;
import static edu.wpi.first.units.Units.Minute;
import static edu.wpi.first.units.Units.Rotations;

import com.ctre.phoenix6.configs.CANcoderConfiguration;
import com.ctre.phoenix6.configs.CurrentLimitsConfigs;
Expand Down Expand Up @@ -73,7 +74,13 @@ public static class DrivetrainConstants {
// When using closed-loop control, the drive motor uses the control
// output type specified by SwerveModuleConstants.DriveMotorClosedLoopOutput
private static final Slot0Configs driveGains =
new Slot0Configs().withKP(3).withKI(0).withKD(0).withKS(0).withKV(0).withKA(0);
new Slot0Configs()
.withKP(0.1661)
.withKI(0)
.withKD(0)
.withKS(0.11102)
.withKV(0.11088)
.withKA(0.010752);

// The closed-loop output type to use for the steer motors;
// This affects the PID/FF gains for the steer motors
Expand Down Expand Up @@ -114,8 +121,8 @@ public static class DrivetrainConstants {
private static final double kSteerGearRatio = 12.8;
private static final double kWheelRadiusInches = 2;

private static final boolean kInvertLeftSide = true;
private static final boolean kInvertRightSide = false;
private static final boolean kInvertLeftSide = false;
private static final boolean kInvertRightSide = true;

private static final String kCANbusName = "";
private static final int kPigeonId = 0;
Expand Down Expand Up @@ -148,7 +155,7 @@ public static class DrivetrainConstants {
.withDriveInertia(kDriveInertia)
.withSteerFrictionVoltage(kSteerFrictionVoltage)
.withDriveFrictionVoltage(kDriveFrictionVoltage)
.withFeedbackSource(SteerFeedbackType.FusedCANcoder)
.withFeedbackSource(SteerFeedbackType.RemoteCANcoder)
.withCouplingGearRatio(kCoupleRatio)
.withDriveMotorInitialConfigs(driveInitialConfigs)
.withSteerMotorInitialConfigs(steerInitialConfigs)
Expand Down
2 changes: 1 addition & 1 deletion TARSReboot/src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private void configureBindings() {
drivetrain.runSwerveFC(
() -> -joystick.getLeftY() * MaxSpeed,
() -> -joystick.getLeftX() * MaxSpeed,
() -> joystick.getRightX() * MaxAngularRate));
() -> -joystick.getRightX() * MaxAngularRate));
joystick
.b()
.onTrue(
Expand Down
9 changes: 9 additions & 0 deletions TARSReboot/src/main/java/frc/robot/Telemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import com.ctre.phoenix6.mechanisms.swerve.SwerveDrivetrain.SwerveDriveState;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.kinematics.SwerveModuleState;
import edu.wpi.first.networktables.DoubleArrayPublisher;
import edu.wpi.first.networktables.DoublePublisher;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.networktables.StringPublisher;
import edu.wpi.first.networktables.StructArrayPublisher;
import edu.wpi.first.wpilibj.smartdashboard.Mechanism2d;
import edu.wpi.first.wpilibj.smartdashboard.MechanismLigament2d;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
Expand Down Expand Up @@ -41,6 +43,10 @@ public Telemetry(double maxSpeed) {
private final DoublePublisher velocityY = driveStats.getDoubleTopic("Velocity Y").publish();
private final DoublePublisher speed = driveStats.getDoubleTopic("Speed").publish();
private final DoublePublisher odomPeriod = driveStats.getDoubleTopic("Odometry Period").publish();
private final StructArrayPublisher<SwerveModuleState> moduleStates =
driveStats.getStructArrayTopic("moduleStates", SwerveModuleState.struct).publish();
private final StructArrayPublisher<SwerveModuleState> moduleTargets =
driveStats.getStructArrayTopic("moduleTargets", SwerveModuleState.struct).publish();

/* Keep a reference of the last pose to calculate the speeds */
private Pose2d m_lastPose = new Pose2d();
Expand Down Expand Up @@ -113,5 +119,8 @@ public void telemeterize(SwerveDriveState state) {

SmartDashboard.putData("Module " + i, m_moduleMechanisms[i]);
}

moduleStates.set(state.ModuleStates);
moduleTargets.set(state.ModuleTargets);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package frc.robot.subsystems;

import static edu.wpi.first.units.Units.*;
import static frc.robot.Constants.DrivetrainConstants.kSpeedAt12VoltsMps;

import com.ctre.phoenix6.SignalLogger;
import com.ctre.phoenix6.Utils;
Expand All @@ -17,8 +18,6 @@
import com.ctre.phoenix6.mechanisms.swerve.SwerveRequest.SysIdSwerveSteerGains;
import com.ctre.phoenix6.mechanisms.swerve.SwerveRequest.SysIdSwerveTranslation;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.DriverStation.Alliance;
import edu.wpi.first.wpilibj.Notifier;
import edu.wpi.first.wpilibj.RobotController;
import edu.wpi.first.wpilibj2.command.Command;
Expand Down Expand Up @@ -55,8 +54,8 @@ public static enum SysIdMode {
private FieldCentric fieldCentricRequest =
new FieldCentric()
.withSteerRequestType(SteerRequestType.MotionMagic)
.withDriveRequestType(DriveRequestType.OpenLoopVoltage)
.withDeadband(6 * 0.1)
.withDriveRequestType(DriveRequestType.Velocity)
.withDeadband(kSpeedAt12VoltsMps * 0.1)
.withRotationalDeadband(Math.PI * 0.1);

// Brake & point requests
Expand Down Expand Up @@ -135,17 +134,17 @@ public void periodic() {
* This ensures driving behavior doesn't change until an explicit disable event
* occurs during testing
*/
if (!hasAppliedOperatorPerspective || DriverStation.isDisabled()) {
DriverStation.getAlliance()
.ifPresent(
(allianceColor) -> {
this.setOperatorPerspectiveForward(
allianceColor == Alliance.Red
? RedAlliancePerspectiveRotation
: BlueAlliancePerspectiveRotation);
hasAppliedOperatorPerspective = true;
});
}
// if (!hasAppliedOperatorPerspective || DriverStation.isDisabled()) {
// DriverStation.getAlliance()
// .ifPresent(
// (allianceColor) -> {
// this.setOperatorPerspectiveForward(
// allianceColor == Alliance.Red
// ? RedAlliancePerspectiveRotation
// : BlueAlliancePerspectiveRotation);
// hasAppliedOperatorPerspective = true;
// });
// }
}

/**
Expand Down

0 comments on commit ed4b004

Please sign in to comment.