Constants
There are a lot of constants on the robot. This includes physical constants as well as constants determined through calibrations. In past years, I've just put all of them in one giant file. In recent years, though, I've been putting them in their own constants classes, which has been working well.
Example Constants
and VisionConstants
classes
public class Constants {
public static final boolean kMinibot = getMACAddress().equals("00-80-2f-33-d2-19");
// I have tried to determine which robot we're running on by MAC address, it wasn't working so I just set it here
public static final boolean kCompBot = true;
// It's a Singleton
static Constants mInstance = new Constants();
public static Constants getInstance() {
return mInstance;
}
// Constants "objects" per subsystem are here
public static final RobotPhysicalConstants physicalConstants = new RobotPhysicalConstants();
public static final SwerveDriveConstants swerveDrive = new SwerveDriveConstants();
public static final ArmConstants arm = new ArmConstants();
public static final WristConstants wrist = new WristConstants();
public static final IntakeConstants intake = new IntakeConstants();
public static final ClawPivotConstants clawPivot = new ClawPivotConstants();
public static final LoopRateConstants loopRates = new LoopRateConstants();
public static final VisionConstants visionConstants = new VisionConstants();
// There are always some constants that either don't belong to a subsystem or just didn't make it. They are listed below
public static boolean kNTDataStreamerPushEnable = false;
public static final double kCollisionDetectionJerkThreshold = 950;
public static final double kTippingThresholdDeg = 11;
public static double kTelemetryDefaultSecondsToBuffer = .2;
public static int kSparkMaxCanTimeout = 10;
public static int kSparkMaxCanRetryCount = 3;
public static int kTalonCanRetryCount = 3;
public static boolean kEnableTeleopDriveXLock = true;
}
Each constants object is just a class with fields in it
public class VisionConstants {
public boolean kIgnoreVision = false;
public boolean kEnableLightOn = true;
public final double kCameraLatencyMs = 40;
}
Here are examples of how to retrieve constants:
Constants.kNTDataStreamerPushEnable
Constants.swerveDrive.kBackRightDriveTalonId
Changing Constants on the fly with Network Tables
Sometimes it's really helpful to change constants on-the-fly while the robot is running.
For example, you may want to mess with the driversdisable the limelight for testing odometry.
In Robot.java
: robotInit()
add the following code:
ntMutableConstantsManager = new NTMutableConstantsManager(Constants.getInstance(), "Constants");
And that's it! (you can deploy the code now)
Open up OutlineViewer (you may need to install it, it's part of WPILib)
You can now set any field that isn't final
in the Constants
class
(Any field that is final in the code will be suffixed with "_FINAL" in NetworkTables/OutlineViewer).
You can also run lambdas that were defined in any of the constants files (by checking the box with the same name). See below for an example.
public class ArmConstants {
public double kHumanFeederCubesFrontFloatX = -33.737;
public double kHumanFeederCubesFrontFloatY = 30.7;
public double kHumanFeederCubesFrontWrist = 99.17;
public double kHumanFeederCubesFrontClawPivot = -44.95;
public Runnable reconfigure = () -> Arm.getInstance().configureSettings();
}