-
Notifications
You must be signed in to change notification settings - Fork 0
Configurable Classes
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.
OxConfig comes with four pre-made configurable classes for WPILib/RevLib (and may ship with more in the future):
-
ConfigurablePIDController(forPIDControllerfrom WPILib) -
ConfigurableSparkPIDController(forSparkMaxPIDControllerfrom RevLib) -
ConfigurablePhoenixMotorPIDController(forBaseMotorControllerfrom CTRE-Lib.) -
ConfigurableTalonSRXPIDController(forTalonSRXfrom CTRE-Lib.)
Feel free to suggest more classes to be built into OxConfig.
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.
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
OxConfig provides an interface for creating a custom configurable class. Check out ConfigurablePIDController for an example.
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")
}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 /*...*/)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.