Class SmartMechanism

java.lang.Object
yams.core.mechanisms.SmartMechanism
Direct Known Subclasses:
SmartPositionalMechanism, SmartVelocityMechanism

public abstract class SmartMechanism extends Object
Generic implementation of a mechanism with advanced telemetry.

SmartMechanism is the abstract root of all YAMS mechanism implementations. It combines a SmartMotorController with integrated telemetry, simulation support, and WPILib Command / Trigger integration so that every concrete mechanism (Arm, Elevator, Flywheel, etc.) shares a consistent API for control, feedback, and visualization.

Mechanism Lifecycle

  1. Configure a motor controller: SmartMotorControllerConfig
  2. Instantiate the appropriate wrapper: SparkWrapper (REV), or yams.core.motorcontrollers.remote.TalonFXWrapper/yams.motorcontrollers.remote.TalonFXSWrapper
  3. Build a mechanism config (e.g., ArmConfig)
  4. Construct the concrete mechanism (e.g., Arm)
  5. Schedule setpoint commands and bind triggers

Periodic calls required: simIterate(), updateTelemetry(), and visualizationUpdate() must be called periodically typically from robotPeriodic() so that simulation state, telemetry, and the Mechanism2d visualization remain up to date.

Example

// 1. Motor config
SmartMotorControllerConfig motorConfig = new SmartMotorControllerConfig()
    .withClosedLoopController(0.2,0,0).withFeedforward(new
ArmFeedforawrd(0.05,0,0)).withStatorCurrentLimit(Amps.of(40));
// 2. Motor
SmartMotorController motor = new SparkWrapper(
    new SparkMax(1, MotorType.kBrushless), DCMotor.getNEO(1), motorConfig);
// 3. Mechanism config + 4. Mechanism
Arm arm = new Arm(new ArmConfig(motor).withLength(Meters.of(0.5)));
// 5. Schedule a command (in a subsystem or robot container)
arm.setAngle(Degrees.of(45));
// 5. Bind a trigger
arm.isNear(Degrees.of(45), Degrees.of(2)).onTrue(Commands.print("At target!"));
// Call in robotPeriodic():
arm.simIterate();
arm.updateTelemetry();
  • Field Details

    • m_smc

      protected SmartMotorController m_smc
      Motor for the subsystem.
    • m_telemetry

      protected MechanismTelemetry m_telemetry
      Mechanism telemetry.
    • m_mechanismWindow

      protected org.wpilib.smartdashboard.Mechanism2d m_mechanismWindow
      Mechanism Window.
  • Constructor Details

    • SmartMechanism

      protected SmartMechanism()
      Create the mechanism, subclasses set up the motor and telemetry.
  • Method Details

    • setMeasurementVelocitySetpoint

      public void setMeasurementVelocitySetpoint(org.wpilib.units.measure.LinearVelocity velocity)
      Set the SmartMotorController to the given speed.
      Parameters:
      velocity - LinearVelocity to go to.
    • setMechanismVelocitySetpoint

      public void setMechanismVelocitySetpoint(org.wpilib.units.measure.AngularVelocity velocity)
      Set the SmartMotorController to the given speed.
      Parameters:
      velocity - AngularVelocity to go to.
    • setMeasurementPositionSetpoint

      public void setMeasurementPositionSetpoint(org.wpilib.units.measure.Distance distance)
      Sets the SmartMotorController to the given distance.
      Parameters:
      distance - Distance to go to.
    • setMechanismPositionSetpoint

      public void setMechanismPositionSetpoint(org.wpilib.units.measure.Angle angle)
      Sets the SmartMotorController to the given angle.
      Parameters:
      angle - Angle to go to.
    • setVoltageSetpoint

      public void setVoltageSetpoint(org.wpilib.units.measure.Voltage voltage)
      Set the voltage of the SmartMotorController.
      Parameters:
      voltage - Voltage to go to.
    • setDutyCycleSetpoint

      public void setDutyCycleSetpoint(double dutycycle)
      Set the dutycycle of the SmartMotorController.
      Parameters:
      dutycycle - [-1,1] to set.
    • getMotorController

      public SmartMotorController getMotorController()
      Returns:
      SmartMotorController for the mechanism.
    • getMechanismSetpoint

      public Optional<org.wpilib.units.measure.Angle> getMechanismSetpoint()
      Get the SmartMechanism's setpoint as an Angle if it exists.
      Returns:
      Optional setpoint Angle of the mechanism..
    • getMeasurementSetpoint

      public Optional<org.wpilib.units.measure.Distance> getMeasurementSetpoint()
      Get the SmartMechanism's setpoint as a Distance if it exists: the mechanism setpoint converted with the mechanism circumference.
      Returns:
      Optional setpoint Distance of the mechanism, empty when there is no position setpoint or the mechanism circumference is not configured.
    • simIterate

      public abstract void simIterate()
      Iterate sim
    • updateTelemetry

      public abstract void updateTelemetry()
      Update the mechanism's telemetry.
    • getMechanismWindow

      public org.wpilib.smartdashboard.Mechanism2d getMechanismWindow()
      Get the Mechanism2d for the mechanism.
      Returns:
      Mechanism2d for the mechanism.
    • publishMechanismWindow

      protected void publishMechanismWindow()
      Publish m_mechanismWindow to NetworkTables under Mechanisms/<name>/mechanism.
    • visualizationUpdate

      public abstract void visualizationUpdate()
      Update the mechanism's visualization state.
    • getPose3d

      public abstract org.wpilib.math.geometry.Pose3d getPose3d()
      Get the mechanism's pose relative to the robot, worked out from its motor's position and where it is mounted, as set in its MechanismPositionConfig. Publish it, such as with Telemetry.log("Mechanisms/Arm/pose", arm.getPose3d()), as a component of an AdvantageScope robot model, whose part is modeled at the mechanism's zero position about its mount.
      Returns:
      Pose3d of the mechanism, relative to the robot.
    • getName

      public abstract String getName()
      Get the name of the mechanism.
      Returns:
      String name.