ロボットのプリファレンスを設定する
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.
プリファレンスを初期化する
public static final String kArmPositionKey = "ArmPosition";
public static final String kArmPKey = "ArmP";
// The P gain for the PID controller that drives this arm.
public static final double kDefaultArmKp = 50.0;
public static final double kDefaultArmSetpointDegrees = 75.0;
// The P gain for the PID controller that drives this arm.
private double m_armKp = Constants.kDefaultArmKp;
private double m_armSetpointDegrees = Constants.kDefaultArmSetpointDegrees;
public Arm() {
m_encoder.setDistancePerPulse(Constants.kArmEncoderDistPerPulse);
// Set the Arm position setpoint and P constant to Preferences if the keys don't already exist
Preferences.initDouble(Constants.kArmPositionKey, m_armSetpointDegrees);
Preferences.initDouble(Constants.kArmPKey, m_armKp);
}
inline constexpr std::string_view kArmPositionKey = "ArmPosition";
inline constexpr std::string_view kArmPKey = "ArmP";
inline constexpr double kDefaultArmKp = 50.0;
inline constexpr units::degree_t kDefaultArmSetpoint = 75.0_deg;
Arm::Arm() {
// Set the Arm position setpoint and P constant to Preferences if the keys
// don't already exist
frc::Preferences::InitDouble(kArmPositionKey, m_armSetpoint.value());
frc::Preferences::InitDouble(kArmPKey, m_armKp);
}
kArmPositionKey = "ArmPosition"
kArmPKey = "ArmP"
# The P gain for the PID controller that drives this arm.
kDefaultArmKp = 50.0
kDefaultArmSetpointDegrees = 75.0
# The P gain for the PID controller that drives this arm.
self.armKp = Constants.kDefaultArmKp
self.armSetpointDegrees = Constants.kDefaultArmSetpointDegrees
# Set the Arm position setpoint and P constant to Preferences if the keys don't already exist
wpilib.Preferences.initDouble(
Constants.kArmPositionKey, self.armSetpointDegrees
)
wpilib.Preferences.initDouble(Constants.kArmPKey, self.armKp)
プリファレンス (Preferences) は名前をキーとして保存されます。キーを複数回入力することやタイプミスを避けるために、定数である kArmPositionKey と kArmPKey のようなキーを保存することが役立ちます。変数 kArmKp と armPositionDeg も宣言し、設定から取得したデータを保持します。
In Arm constructor, 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.
Command Frameworkを使用する場合、このタイプのコードはSubsystemまたはCommandのコンストラクタに配置することができます。
プリファレンスからの読み込み
public void loadPreferences() {
// Read Preferences for Arm setpoint and kP on entering Teleop
m_armSetpointDegrees = Preferences.getDouble(Constants.kArmPositionKey, m_armSetpointDegrees);
if (m_armKp != Preferences.getDouble(Constants.kArmPKey, m_armKp)) {
m_armKp = Preferences.getDouble(Constants.kArmPKey, m_armKp);
m_controller.setP(m_armKp);
}
}
void Arm::LoadPreferences() {
// Read Preferences for Arm setpoint and kP on entering Teleop
m_armSetpoint = units::degree_t{
frc::Preferences::GetDouble(kArmPositionKey, m_armSetpoint.value())};
if (m_armKp != frc::Preferences::GetDouble(kArmPKey, m_armKp)) {
m_armKp = frc::Preferences::GetDouble(kArmPKey, m_armKp);
m_controller.SetP(m_armKp);
}
}
def loadPreferences(self):
# Read Preferences for Arm setpoint and kP on entering Teleop
self.armSetpointDegrees = wpilib.Preferences.getDouble(
Constants.kArmPositionKey, self.armSetpointDegrees
)
if self.armKp != wpilib.Preferences.getDouble(Constants.kArmPKey, self.armKp):
self.armKp = wpilib.Preferences.getDouble(Constants.kArmPKey, self.armKp)
self.controller.setP(self.armKp)
プリファレンスを読み込むことは簡単です。 getDouble メソッドは2つのパラメータを取ります。読み取るキーと、プリファレンスが存在しない場合に使用するデフォルト値です。ブール値、整数、文字列などの他のデータ型に対する類似したメソッドもあります。
データが設定されている場合、プリファレンスに保存されたデータを読み取る際に使用できます。例えば、上記の比例定数 (P値) のように使用することができます。また、変数に保存して後で使用することもできます。例えば、下記の telopPeriodic で使用されるセットポイントのように使用することができます。
@Override
public void teleopPeriodic() {
if (m_joystick.getTrigger()) {
// Here, we run PID control like normal.
m_arm.reachSetpoint();
} else {
// Otherwise, we disable the motor.
m_arm.stop();
}
}
/** Run the control loop to reach and maintain the setpoint from the preferences. */
public void reachSetpoint() {
var pidOutput =
m_controller.calculate(
m_encoder.getDistance(), Units.degreesToRadians(m_armSetpointDegrees));
m_motor.setVoltage(pidOutput);
}
void Robot::TeleopPeriodic() {
if (m_joystick.GetTrigger()) {
// Here, we run PID control like normal.
m_arm.ReachSetpoint();
} else {
// Otherwise, we disable the motor.
m_arm.Stop();
}
}
void Arm::ReachSetpoint() {
// Here, we run PID control like normal, with a setpoint read from
// preferences in degrees.
double pidOutput = m_controller.Calculate(
m_encoder.GetDistance(), (units::radian_t{m_armSetpoint}.value()));
m_motor.SetVoltage(units::volt_t{pidOutput});
}
def teleopPeriodic(self):
if self.joystick.getTrigger():
# Here, we run PID control like normal.
self.arm.reachSetpoint()
else:
# Otherwise, we disable the motor.
self.arm.stop()
def reachSetpoint(self):
pidOutput = self.controller.calculate(
self.encoder.getDistance(),
units.degreesToRadians(self.armSetpointDegrees),
)
self.motor.setVoltage(pidOutput)
SmartDashboardでプリファレンスを使用する
SmartDashboardでプリファレンスを表示する

SmartDashboardでは、 View を選択し、 Add... を選択し、 Robot Preferences を選択することで、Preferencesディスプレイを画面に追加することができます。これにより、roboRIOフラッシュメモリに保存されているプリファレンスファイルの内容が表示されます。
SmartDashboardでプリファレンスを編集する

値はここにコードからのデフォルト値と共に表示されます。値を調整する必要がある場合は、ここで編集して保存することができます。
Shuffleboardでプリファレンスを利用する
Shuffleboardでプリファレンスを表示する

Shuffleboardで、Preferencesディスプレイは、ソースウィンドウからPreferencesフィールドをドラッグして画面に追加することができます。これにより、roboRIOフラッシュメモリに保存されているプリファレンスファイルの内容が表示されます。
Shuffleboardでプリファレンスを編集する

値はここにコードからのデフォルト値と共に表示されます。値を調整する必要がある場合は、ここで編集することができます。