Class SmartMechanism
java.lang.Object
yams.core.mechanisms.SmartMechanism
- Direct Known Subclasses:
SmartPositionalMechanism, SmartVelocityMechanism
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
- Configure a motor controller:
SmartMotorControllerConfig - Instantiate the appropriate
wrapper:
SparkWrapper(REV), oryams.core.motorcontrollers.remote.TalonFXWrapper/yams.motorcontrollers.remote.TalonFXSWrapper - Build a mechanism config (e.g.,
ArmConfig) - Construct the concrete mechanism (e.g.,
Arm) - 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 Summary
FieldsModifier and TypeFieldDescriptionprotected org.wpilib.smartdashboard.Mechanism2dMechanism Window.protected SmartMotorControllerMotor for the subsystem.protected MechanismTelemetryMechanism telemetry. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreate the mechanism, subclasses set up the motor and telemetry. -
Method Summary
Modifier and TypeMethodDescriptionOptional<org.wpilib.units.measure.Distance> Get theSmartMechanism's setpoint as aDistanceif it exists: the mechanism setpoint converted with the mechanism circumference.Optional<org.wpilib.units.measure.Angle> Get theSmartMechanism's setpoint as anAngleif it exists.org.wpilib.smartdashboard.Mechanism2dGet theMechanism2dfor the mechanism.Get theSmartMotorControllerabstract StringgetName()Get the name of the mechanism.abstract org.wpilib.math.geometry.Pose3dGet the mechanism's pose relative to the robot, worked out from its motor's position and where it is mounted, as set in itsMechanismPositionConfig.protected voidPublishm_mechanismWindowto NetworkTables underMechanisms/<name>/mechanism.voidsetDutyCycleSetpoint(double dutycycle) Set the dutycycle of theSmartMotorController.voidsetMeasurementPositionSetpoint(org.wpilib.units.measure.Distance distance) Sets theSmartMotorControllerto the given distance.voidsetMeasurementVelocitySetpoint(org.wpilib.units.measure.LinearVelocity velocity) Set theSmartMotorControllerto the given speed.voidsetMechanismPositionSetpoint(org.wpilib.units.measure.Angle angle) Sets theSmartMotorControllerto the given angle.voidsetMechanismVelocitySetpoint(org.wpilib.units.measure.AngularVelocity velocity) Set theSmartMotorControllerto the given speed.voidsetVoltageSetpoint(org.wpilib.units.measure.Voltage voltage) Set the voltage of theSmartMotorController.abstract voidIterate simabstract voidUpdate the mechanism's telemetry.abstract voidUpdate the mechanism's visualization state.
-
Field Details
-
m_smc
Motor for the subsystem. -
m_telemetry
Mechanism telemetry. -
m_mechanismWindow
protected org.wpilib.smartdashboard.Mechanism2d m_mechanismWindowMechanism 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 theSmartMotorControllerto the given speed.- Parameters:
velocity-LinearVelocityto go to.
-
setMechanismVelocitySetpoint
public void setMechanismVelocitySetpoint(org.wpilib.units.measure.AngularVelocity velocity) Set theSmartMotorControllerto the given speed.- Parameters:
velocity-AngularVelocityto go to.
-
setMeasurementPositionSetpoint
public void setMeasurementPositionSetpoint(org.wpilib.units.measure.Distance distance) Sets theSmartMotorControllerto the given distance.- Parameters:
distance-Distanceto go to.
-
setMechanismPositionSetpoint
public void setMechanismPositionSetpoint(org.wpilib.units.measure.Angle angle) Sets theSmartMotorControllerto the given angle.- Parameters:
angle-Angleto go to.
-
setVoltageSetpoint
public void setVoltageSetpoint(org.wpilib.units.measure.Voltage voltage) Set the voltage of theSmartMotorController.- Parameters:
voltage-Voltageto go to.
-
setDutyCycleSetpoint
public void setDutyCycleSetpoint(double dutycycle) Set the dutycycle of theSmartMotorController.- Parameters:
dutycycle- [-1,1] to set.
-
getMotorController
Get theSmartMotorController- Returns:
SmartMotorControllerfor the mechanism.
-
getMechanismSetpoint
Get theSmartMechanism's setpoint as anAngleif it exists.- Returns:
OptionalsetpointAngleof the mechanism..
-
getMeasurementSetpoint
Get theSmartMechanism's setpoint as aDistanceif it exists: the mechanism setpoint converted with the mechanism circumference.- Returns:
OptionalsetpointDistanceof 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 theMechanism2dfor the mechanism.- Returns:
Mechanism2dfor the mechanism.
-
publishMechanismWindow
protected void publishMechanismWindow()Publishm_mechanismWindowto NetworkTables underMechanisms/<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 itsMechanismPositionConfig. Publish it, such as withTelemetry.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:
Pose3dof the mechanism, relative to the robot.
-
getName
-