Constants - hammerheads5000/Team-Documentation GitHub Wiki
The Constants.java file contains definitions for all the constants we use throughout the rest of the code. It avoids "magic numbers"--unexplained numbers in code that can cause confusion. These can include simple numbers like IDs, speeds, and setpoints, or more complex objects such as motors, mechanisms, and sensors.
We split the Constants file into classes to group different categories of constants. You can view the Constants.java file from the 2024 season for an example of what such a file can look like.
The Constants Class
The base constants class contains all the other classes and some stand-alone constants. These can include CAN bus names, Pigeon ID and mounting configuration, NetworkTable instances, etc.
Other Classes
Other classes in Constants can include:
- SwerveConstants: contains swerve characterization values
- VisionConstants: defines cameras and NetworkTable topics
- LoggingConstants: defines NetworkTables, publishers, and subscribers to be logged. This allows communication between different subsystems and easy debugging. We like to use AdvantageScope to view this data.
- Other classes for specific subsystems, such as IntakeConstants
Modifiers
You might notice that there are three words before every constant: public static final or private static final. These are known as modifiers, and you can learn more about them on W3Schools' modifiers page. Let's break down these specific modifiers:
public: A public variable can be accessed from other classes. We make most constants public so we can access them throughout our code.private: A private variable can only be accessed in its own class. We use these for intermediate constants like gear ratios which come together in more complex constants, like a SwerveDrivetrain or TalonFXConfiguration. This might look like:
private static final MotorOutputConfigs outputConfigs = new MotorOutputConfigs()
.withNeutralMode(NeutralModeValue.Brake);
public static final TalonFXConfiguration motorConfigs = new TalonFXConfiguration()
.withMotorOutput(outputConfigs);
static: Normally, you make instances of classes as objects (for example,PIDController pid = new PIDController(1.0, 0.0, 0.5);). You can then access the attributes of these instances (pid.kP). When an attribute is static, you access it directly from the base class (e.g.Constants.controllerDeadband). We do not want to create a whole bunch of Constants instances, so all constants are marked asstatic.final: This marks a variable as a constant, the whole point of a Constants class. A final variable cannot change after its creation.