For the complete documentation index, see llms.txt. This page is also available as Markdown.

Organizing configs in constants

As your robot grows, managing configuration across multiple subsystems can become unwieldy. YAMS supports a clean pattern where you define all your configs in a centralized Constants file, then bind them to subsystems at construction time using .withSubsystem().

The problem

Without organization, each subsystem file contains its own configuration inline:

// ArmSubsystem.java - config mixed with subsystem logic
public class ArmSubsystem extends SubsystemBase {
  private final Arm arm;
  
  public ArmSubsystem() {
    SmartMotorControllerConfig smcConfig = new SmartMotorControllerConfig(this)
        .withGearing(new MechanismGearing(GearBox.fromReductionStages(5, 4, 3)))
        .withClosedLoopController(5, 0, 0.1)
        .withFeedforward(new ArmFeedforward(0.1, 0.3, 0.5, 0.01))
        .withTrapezoidalProfile(RotationsPerSecond.of(1.0), RotationsPerSecondPerSecond.of(2.0));
    
    SmartMotorController smc = new TalonFXWrapper(new TalonFX(1, new CANBus(CANPort.CAN_S0)), DCMotor.getKrakenX60(1), smcConfig);
    
    ArmConfig armConfig = new ArmConfig()
        .withLength(Inches.of(18))
        // ... more config
    
    this.arm = new Arm(armConfig, smc);
  }
}

This approach has drawbacks:

  • Configs are scattered across many files

  • Hard to compare settings between mechanisms

  • Tuning requires opening multiple files

  • CAN IDs, motor types, and gear ratios are buried in subsystem code

The solution: Centralized constants with withSubsystem()

YAMS configs can be created without a subsystem reference, then bound later using .withSubsystem(). This enables a clean separation:

This page uses Commands v2 (yams.commands2.config.SmartMotorControllerConfig). With Commands v3, import yams.commands3.config.SmartMotorControllerConfig and bind it with .withMechanism(this) from your Mechanism class instead of .withSubsystem(this). Everything else is the same.

Then in your subsystems, use .withSubsystem() to bind the config:

Benefits of this pattern

Benefit
Description

Single Source of Truth

All CAN IDs, gear ratios, PID gains in one file

Easy Comparison

See all mechanism configs side-by-side

Faster Tuning

Change gains without navigating subsystem files

Cleaner Subsystems

Subsystem classes focus on behavior, not configuration

Reusable Configs

Share base configs between similar mechanisms

Complete example: Constants file

Complete example: Subsystem using constants

After live tuning: copy the values into Constants

Live tuning changes gains while the robot runs, but the values are lost when the code restarts. With this pattern the gains live in Constants, so that is where you copy them:

If the config also has withSimClosedLoopController(...) or withSimFeedforward(...), values found in simulation belong in those lines, because the simulation uses them.

Organizing multiple config files

For larger robots, you may want to split configs into separate files:

Important: When using this pattern, you must call .withSubsystem() (.withMechanism() in Commands v3) before passing the SmartMotorControllerConfig to a SmartMotorController. It can only be called once per config and throws a SmartMotorControllerConfigurationException if a subsystem is already set, so call .clone() on the shared constant first and bind the copy. Never pass the same config instance to more than one SmartMotorController. clone() copies the settings but shares the gain objects (the PID controllers and feedforwards) with the original, so do not clone one constant for two motors that run their own closed loop. Give each mechanism its own constant. Pass the SmartMotorController directly as the second argument to the mechanism constructor (e.g., new Arm(armConfig, smc)).

When not to use this pattern

This pattern adds a layer of indirection. For simple robots with 1-2 mechanisms, inline configuration may be clearer. Consider using centralized configs when:

  • You have 3+ mechanisms

  • Multiple team members tune different subsystems

  • You want to quickly compare settings across mechanisms

  • You're building a constants dashboard or config loader

Last updated

Was this helpful?