Setting Robot Preferences

The Robot Preferences (Java, C++) class is used to store values in the flash memory on the roboRIO. The values might be for remembering preferences on the robot such as calibration settings for potentiometers, PID values, setpoints, etc. that you would like to change without having to rebuild the program. The values can be viewed on SmartDashboard or Shuffleboard and read and written by the robot program.

This example shows how to utilize Preferences to change the setpoint of a PID controller and the P constant. The code examples are adapted from the Arm Simulation example (Java, C++). You can run the Arm Simulation example in the Robot Simulator to see how to use the preference class and interact with it using the dashboards without needing a robot.

Initializing Preferences

  public static final String kArmPositionKey = "ArmPosition";
  public static final String kArmPKey = "ArmP";

  // The P gain for the PID controller that drives this arm.
  private static double kArmKp = 50.0;

  private static double armPositionDeg = 75.0;

  @Override
  public void robotInit() {
    // Set the Arm position setpoint and P constant to Preferences if the keys don't already exist
    if (!Preferences.containsKey(kArmPositionKey)) {
      Preferences.setDouble(kArmPositionKey, armPositionDeg);
    }
    if (!Preferences.containsKey(kArmPKey)) {
      Preferences.setDouble(kArmPKey, kArmKp);
    }
  }

Preferences are stored using a name, the key. It’s helpful to store the key in a constant, like kArmPositionKey and kArmPKey in the code above to avoid typing it multiple times and avoid typos. We also declare variables, kArmKp and armPositionDeg to hold the data retrieved from preferences.

In robotInit, each key is checked to see if it already exists in the Preferences database. The containsKey method takes one parameter, the key to check if data for that key already exists in the preferences database. If it doesn’t exist, a default value is written. The setDouble method takes two parameters, the key to write and the data to write. There are similar methods for other data types like booleans, ints, and strings.

If using the Command Framework, this type of code could be placed in the constructor of a Subsystem or Command.

Reading Preferences

  @Override
  public void teleopInit() {
    // Read Preferences for Arm setpoint and kP on entering Teleop
    armPositionDeg = Preferences.getDouble(kArmPositionKey, armPositionDeg);
    if (kArmKp != Preferences.getDouble(kArmPKey, kArmKp)) {
      kArmKp = Preferences.getDouble(kArmPKey, kArmKp);
      m_controller.setP(kArmKp);
    }
  }

Reading a preference is easy. The GetDouble method takes two parameters, the key to read, and a default value to use in case the preference doesn’t exist. There are similar methods for other data types like booleans, ints, and strings.

Depending on the data that is stored in preferences, you can use it when you read it, such as the proportional constant above. Or you can store it in a variable and use it later, such as the setpoint, which is used in telopPeriodic below.

  @Override
  public void teleopPeriodic() {
    if (m_joystick.getTrigger()) {
      // Here, we run PID control like normal, with a constant setpoint of 75 degrees.
      var pidOutput =
          m_controller.calculate(m_encoder.getDistance(), Units.degreesToRadians(armPositionDeg));
      m_motor.setVoltage(pidOutput);
    } else {
      // Otherwise, we disable the motor.
      m_motor.set(0.0);
    }
  }

Using Preferences in SmartDashboard

Displaying Preferences in SmartDashboard

Adding preferences from the Smartdashboard menu

In the SmartDashboard, the Preferences display can be added to the display by selecting View then Add… then Robot Preferences. This reveals the contents of the preferences file stored in the roboRIO flash memory.

Editing Preferences in SmartDashboard

Editing the robot preferences via the SmartDashboard widget.

The values are shown here with the default values from the code. If the values need to be adjusted they can be edited here and saved.

Using Preferences in Shuffleboard

Displaying Preferences in Shuffleboard

Adding preferences from the sources window in Shuffleboard

In Shuffleboard, the Preferences display can be added to the display by dragging the preferences field from the sources window. This reveals the contents of the preferences file stored in the roboRIO flash memory.

Editing Preferences in Shuffleboard

Editing the robot preferences via the Shuffleboard widget.

The values are shown here with the default values from the code. If the values need to be adjusted they can be edited here.