Skip to content

Configurable Classes

nab138 edited this page Feb 28, 2025 · 15 revisions

Configurable classes is a very useful feature of OxConfig. It allows for any class to able to be sent as a controller to the Tuner. This allows for these classes to be easily tuned in real-time.

Pre-made configurable Classes

OxConfig comes with four pre-made configurable classes for WPILib/RevLib (and may ship with more in the future):

  • ConfigurablePIDController (for PIDController from WPILib)
  • ConfigurableSparkPIDController (for SparkMaxPIDController from RevLib)
  • ConfigurablePhoenixMotorPIDController (for BaseMotorController from CTRE-Lib.)
  • ConfigurableTalonSRXPIDController (for TalonSRX from CTRE-Lib.)

Feel free to suggest more classes to be built into OxConfig.

Declaring pre-made controllers

To declare a new ConfigurablePIDController, you can use the constructor as follows:

ConfigurablePIDController controller = new ConfigurablePIDController(kP, kI, kD, /* Default Values */
key /* Key of the controller itself (supports nesting with /, e.g. "drive/left") */)

The ConfigurableSparkPIDController, ConfigurablePhoenixMotorPIDController, and ConfigurableTalonFXPIDController classes are similar:

ConfigurableSparkPIDController controller = new ConfigurableSparkPIDController(controller /* The SparkMaxPIDController (sparkmax.getPIDController())*/
key, /* Key of the controller itself (supports nesting with /, e.g. "drive/left") */)

Please avoid using commas (,) in the key names, it can cause issues over the NT interface.

Currently those three classes class doesn't support default values, they will all be 0. This may change in the future if I can find I good way of handling them.

Using pre-made controllers

The ConfigurablePIDController class extends WPILib PIDControllers, meaning they work functionally the same way. You can call .calculate() or whatever other methods you may need. Note it is not recommended to call setP, I, or D functions as it could mess with OxConfig.

The ConfigurableSparkPIDController class takes in a SparkMaxPIDController, and will maintain its values automatically. You can continue to use the regular SparkMaxPIDController like you would normally after passing it into the configurable class. If you would like default values, call setP(), setI(), etc on the pid controller BEFORE creating this class.

ConfigurablePhoenixMotorPIDController and ConfigurableTalonSRXPIDController are used very similarly to ConfigurableSparkPIDController

Making your own configurable class

OxConfig provides an interface for creating a custom configurable class. Check out ConfigurablePIDController for an example.

Creating the class

Simply create a normal class that implements the ConfigurableClass interface. Your IDE will auto fill the interface methods for you. You will need to impliment 3 methods:

public class ConfigurableClassExample implements ConfigurableClass {
    public ArrayList<ConfigurableClassParam<?>> getParameters(){}; // Arraylist of class params (will talk about later)
    public String getKey(){}; // The key of this class (e.g. "motors/leftController")
    public String getPrettyName(){}; // The display name of this class (e.g. "Left Controller")
}

Creating parameters

ConfigurableClassParams are created like this:

param = new ConfigurableClassParam<Type>(
myClass, // The class it belongs too, most of the time you should pass in "this"
default, // The default value of the parameter
consumer, // (Optional) Setter method of the parameter (e.g. controller::setP, exists because of things like SparkPIDControllers where you can't override the parameter directly.")
key, // The key of the parameter. This will be put under the class's key itself (To be stored in "motors/leftController/P", you only have to pass in "P" here as the class key is "motors/leftController")
);

Please avoid using commas (,) in the key names, it can cause issues over the NT interface.

Make sure to add all parameters to a list for the getParameters method. You can easily do this using List.of():

List.of(param1, param2, param3 /*...*/)

Registering the class

After all these methods are implemented, you can register this class. You can do it wherever you want, but it is recommended to put it in the constructor.

public ConfigurableClassExample(){
    OxConfig.registerConfigurableClass(this);
}

Note: Before calling register, make sure your getParameters function will return the list will all parameters created. Register should be the last thing in your constructor.

Clone this wiki locally