Skip to content

Commit

Permalink
Add velocity scalars for DT
Browse files Browse the repository at this point in the history
  • Loading branch information
srimanachanta committed Jan 27, 2025
1 parent d6ed458 commit 276e05c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
20 changes: 15 additions & 5 deletions src/main/java/frc/robot/commands/DriveCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import frc.robot.subsystems.drive.DriveBase;
import frc.robot.subsystems.drive.DriveConstants;
import frc.robot.util.AllianceFlipUtil;
import frc.robot.util.LoggedTunableNumber;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.LinkedList;
Expand All @@ -25,6 +26,12 @@
public class DriveCommands {
// Drive
private static final double DEADBAND = 0.1;

private static final LoggedTunableNumber LINEAR_VELOCITY_SCALAR =
new LoggedTunableNumber("TeleopDrive/LinearVelocityScalar", 1.0, true);
private static final LoggedTunableNumber ANGULAR_VELOCITY_SCALAR =
new LoggedTunableNumber("TeleopDrive/AngularVelocityScalar", 1.0, true);

private static final double ANGLE_KP = 5.0;
private static final double ANGLE_KD = 0.4;
private static final double ANGLE_MAX_VELOCITY = 8.0;
Expand Down Expand Up @@ -57,11 +64,13 @@ public static Command joystickDrive(
omega = Math.copySign(Math.pow(omega, 2), omega);

// Generate robot relative speeds
double linearVelocityScalar = LINEAR_VELOCITY_SCALAR.get();
double angularVelocityScalar = ANGULAR_VELOCITY_SCALAR.get();
var speeds =
new ChassisSpeeds(
x * DriveConstants.maxLinearVelocityMetersPerSec,
y * DriveConstants.maxLinearVelocityMetersPerSec,
omega * DriveConstants.maxAngularVelocityRadPerSec);
x * DriveConstants.maxLinearVelocityMetersPerSec * linearVelocityScalar,
y * DriveConstants.maxLinearVelocityMetersPerSec * linearVelocityScalar,
omega * DriveConstants.maxAngularVelocityRadPerSec * angularVelocityScalar);

// Convert to field relative
Rotation2d rotation = RobotState.getInstance().getRotation();
Expand Down Expand Up @@ -109,10 +118,11 @@ public static Command joystickDriveAtAngle(
rotation.getRadians(), rotationSupplier.get().getRadians());

// Generate robot relative speeds
double linearVelocityScalar = LINEAR_VELOCITY_SCALAR.get();
var speeds =
new ChassisSpeeds(
x * DriveConstants.maxLinearVelocityMetersPerSec,
y * DriveConstants.maxLinearVelocityMetersPerSec,
x * DriveConstants.maxLinearVelocityMetersPerSec * linearVelocityScalar,
y * DriveConstants.maxLinearVelocityMetersPerSec * linearVelocityScalar,
omega);

// Convert to field relative
Expand Down
31 changes: 28 additions & 3 deletions src/main/java/frc/robot/util/LoggedTunableNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,38 @@ public class LoggedTunableNumber {

private LoggedNetworkNumber dashboardNumber;

private final boolean ntPubEnabled;

/**
* Create a new LoggedTunableNumber
*
* @param dashboardKey Key on dashboard
* @param alwaysEnabled Always publish modifiers to NT, even if not in Tuning Mode
*/
public LoggedTunableNumber(String dashboardKey) {
public LoggedTunableNumber(String dashboardKey, boolean alwaysEnabled) {
this.key = tableKey + "/" + dashboardKey;
this.ntPubEnabled = alwaysEnabled;
}

/**
* Create a new LoggedTunableNumber
*
* @param dashboardKey Key on dashboard
*/
public LoggedTunableNumber(String dashboardKey) {
this(dashboardKey, Constants.TUNING_MODE);
}

/**
* Create a new LoggedTunableNumber with the default value
*
* @param dashboardKey Key on dashboard
* @param defaultValue Default value
* @param alwaysEnabled Always publish modifiers to NT, even if not in Tuning Mode
*/
public LoggedTunableNumber(String dashboardKey, double defaultValue, boolean alwaysEnabled) {
this(dashboardKey, alwaysEnabled);
initDefault(defaultValue);
}

/**
Expand Down Expand Up @@ -55,7 +80,7 @@ public void initDefault(double defaultValue) {
}

this.defaultValue = defaultValue;
if (Constants.TUNING_MODE) {
if (ntPubEnabled) {
dashboardNumber = new LoggedNetworkNumber(key, defaultValue);
}
}
Expand All @@ -74,7 +99,7 @@ public double get() {
key));
}

return Constants.TUNING_MODE ? dashboardNumber.get() : defaultValue;
return ntPubEnabled ? dashboardNumber.get() : defaultValue;
}

/**
Expand Down

0 comments on commit 276e05c

Please sign in to comment.