githubEdit

slidersSetpoint Methods vs Command Helpers

YAMS Mechanism classes provide two ways to control mechanisms: direct setpoint methods inherited from SmartMechanism, and command helper methods that return Command objects. Understanding when to use each is key to writing clean, effective robot code.

Overview

Approach
Example
Returns
Use Case

Command Helpers

arm.run(Degrees.of(45))

Command

Button bindings, autonomous sequences

Direct Setpoint Methods

arm.setMechanismPositionSetpoint(Degrees.of(45))

void

Custom commands, AdvantageKit IO, low-level control

Command Helper Methods

Command helpers are methods on Mechanism classes that return Command objects. These are the recommended approach for most use cases.

Why Use Command Helpers?

  1. Subsystem Requirements - Commands automatically require their subsystem, preventing conflicts

  2. Lifecycle Management - Commands handle starting/stopping the closed loop controller

  3. Composability - Easy to chain with .andThen(), .alongWith(), etc.

  4. Trigger Integration - Work directly with button triggers like controller.a().onTrue()

  5. Naming - Commands are automatically named for debugging

Available Command Helpers by Mechanism

Arm / Pivot

Elevator

FlyWheel

Example: Button Bindings

Direct Setpoint Methods

Direct setpoint methods are inherited from SmartMechanism and set the motor controller's target immediately without returning a Command.

SmartMechanism Setpoint Methods

All Mechanism classes inherit these methods from SmartMechanism:

When to Use Direct Setpoint Methods

  1. AdvantageKit IO Classes - When implementing IO interfaces that don't use Commands

  2. Custom Command Logic - When building complex commands with custom execute() methods

  3. State Machine Control - When a state machine directly controls mechanism state

  4. Performance Critical - Avoiding Command overhead in tight loops (rare)

Example: AdvantageKit IO

Example: Custom Command

Setpoint Method Support Matrix

Not all setpoint methods are supported by all Mechanism classes. Methods that don't apply throw UnsupportedOperationException or RuntimeException.

Position Setpoint Methods

Method
Arm
Pivot
Elevator
FlyWheel

setMechanismPositionSetpoint(Angle)

Yes

Yes

Yes

No

setMeasurementPositionSetpoint(Distance)

No

No

Yes

No

Velocity Setpoint Methods

Method
Arm
Pivot
Elevator
FlyWheel

setMechanismVelocitySetpoint(AngularVelocity)

Yes

Yes

Yes

Yes

setMeasurementVelocitySetpoint(LinearVelocity)

No

No

Yes

Yes*

*FlyWheel requires circumference to be configured for linear velocity.

Open Loop Setpoint Methods

Method
Arm
Pivot
Elevator
FlyWheel

setMechanismVoltageSetpoint(Voltage)

Yes

Yes

Yes

Yes

setDutyCycleSetpoint(double)

Yes

Yes

Yes

Yes

circle-exclamation

Checking Position with isNear()

To check if a mechanism is near a target position, use the isNear() method which returns a Trigger:

These methods use WPILib's built-in isNear() function on the Angle, Distance, and AngularVelocity measure classes for accurate tolerance checking.

Using isNear() in Commands

Decision Guide

Best Practices

  1. Prefer Command Helpers - They handle subsystem requirements and closed loop lifecycle

  2. Use run() for Teleop - Continuous control that responds to interruption

  3. Use runTo() for Sequences - When you need to wait for the mechanism to reach a target

  4. Use Direct Methods for IO - When implementing AdvantageKit IO or custom commands

  5. Always Check Support - Verify the method is supported before using direct setpoint methods

  6. Use Triggers for State - Use isNear(), max(), min() triggers instead of polling

See Also

Last updated