Writing Your Own Sendable Classes

Since the Sendable interface only has one method, writing your own classes that implement Sendable (and thus automatically log values to and/or consume values from the dashboard) is extremely easy: just provide an implementation for the overrideable initSendable method, in which setters and getters for your class’s fields are declaratively bound to key values (their display names on the dashboard).

For example, here is the implementation of initSendable from WPILib’s BangBangController:

150  @Override
151  public void initSendable(SendableBuilder builder) {
152    builder.setSmartDashboardType("BangBangController");
153    builder.addDoubleProperty("tolerance", this::getTolerance, this::setTolerance);
154    builder.addDoubleProperty("setpoint", this::getSetpoint, this::setSetpoint);
155    builder.addDoubleProperty("measurement", this::getMeasurement, null);
156    builder.addDoubleProperty("error", this::getError, null);
157    builder.addBooleanProperty("atSetpoint", this::atSetpoint, null);
158  }

To enable the automatic updating of values by WPILib “in the background”, Sendable data names are bound to getter and setter functions rather than specific data values. If a field that you wish to log has no defined setters and getters, they can be defined inline with a lambda expression.

The SendableBuilder Class

As seen above, the initSendable method takes a single parameter, builder, of type SendableBuilder (Java, C++). This builder exposes methods that allow binding of getters and setters to dashboard names, as well as methods for safely ensuring that values consumed from the dashboard do not cause unsafe robot behavior.

Databinding with addProperty Methods

Like all WPILib dashboard code, Sendable fields are ultimately transmitted over NetworkTables, and thus the databinding methods provided by SendableBuilder match the supported NetworkTables data types:

  • boolean: addBooleanProperty

  • boolean[]: addBooleanArrayProperty

  • double: addDoubleProperty

  • double[]: addDoubleArrayProperty

  • string: addStringProperty

  • string[]: addStringArrayProperty

  • byte[]: addRawProperty

Ensuring Safety with setSafeState and setActuator

Since Sendable allows users to consume arbitrary values from the dashboard, it is possible for users to pipe dashboard controls directly to robot actuations. This is extremely unsafe if not done with care; dashboards are not a particularly good interface for controlling robot movement, and users generally do not expect the robot to move in response to a change on the dashboard.

To help users ensure safety when interfacing with dashboard values, SendableBuilder exposes a setSafeState method, which is called to place any Sendable mechanism that actuates based on dashboard input into a safe state. Any potentially hazardous user-written Sendable implementation should call setSafeState with a suitable safe state implementation. For example, here is the implementation from the WPILib PWMMotorController class:

118  @Override
119  public void initSendable(SendableBuilder builder) {
120    builder.setSmartDashboardType("Motor Controller");
121    builder.setActuator(true);
122    builder.setSafeState(this::disable);
123    builder.addDoubleProperty("Value", this::get, this::set);
124  }

Additionally, users may call builder.setActuator(true) to mark any mechanism that might move as a result of Sendable input as an actuator. Currently, this is used by Shuffleboard to disable actuator widgets when not in LiveWindow mode.