Class SmartMotorController
- Direct Known Subclasses:
NovaWrapper, SparkWrapper, TalonFXSWrapper, TalonFXWrapper
This abstract class is not instantiated directly. Instantiate the appropriate vendor wrapper:
SparkWrapper for REV SPARK hardware, or
yams.core.motorcontrollers.remote.TalonFXWrapper/yams.core.motorcontrollers.remote.TalonFXSWrapper for CTRE hardware. Configure all three via
SmartMotorControllerConfig.
Example
SmartMotorControllerConfig config = new SmartMotorControllerConfig()
.withClosedLoopController(0.1,0,0).withFeedforward(new
SimpleMotorFeedforward(0.05,0,0)).withStatorCurrentLimit(Amps.of(40)); SmartMotorController motor
= new SparkWrapper( new SparkMax(1, MotorType.kBrushless), DCMotor.getNEO(1), config);
// Use directly for duty-cycle or voltage control:
motor.setDutyCycle(0.5);
// Or attach to a mechanism:
Arm arm = new Arm(new ArmConfig(motor).withLength(Meters.of(0.5)));
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumCurrent closed loop controller slot. -
Field Summary
FieldsModifier and TypeFieldDescriptionfinal UUIDBattery simulation UUID used when standalone simulation.protected org.wpilib.system.NotifierThread of the closed loop controller.Hooks run onclose().protected SmartMotorControllerConfig<?> SmartMotorControllerConfigfor the motor.protected Optional<org.wpilib.math.trajectory.ExponentialProfile> Exponential profile for the closed loop controller.protected Optional<org.wpilib.math.trajectory.ExponentialProfile.State> Exponential profile state for the closed loop controller.protected Optional<org.wpilib.units.measure.Angle> Mechanism position the closed loop controller last used, unwrapped across the continuous wrapping point.protected Optional<SmartMotorController[]> Loosely coupled followers.protected Optional<LQRController> LQR controller for the motor controller.protected Optional<org.wpilib.math.controller.PIDController> Simple PID controller for the motor controller.protected Optional<SimSupplier> SimSupplierfor the mechanism.SmartMotorController.ClosedLoopControllerSlotfor the closed loop controller.protected Optional<org.wpilib.util.Alert> Alert shown when the closed loop controller is running on the SystemCore.protected Optional<org.wpilib.math.trajectory.TrapezoidProfile> Trapezoidal profile for the closed loop controller.protected Optional<org.wpilib.math.trajectory.TrapezoidProfile.State> Trapezoidal profile state for the closed loop controller.protected Optional<org.wpilib.networktables.NetworkTable> Parent table for telemetry.protected Optional<org.wpilib.units.measure.Force> Setpoint feedforward force, applied on top of the velocity setpoint, e.g.protected Optional<org.wpilib.units.measure.Angle> Setpoint positionprotected Optional<org.wpilib.units.measure.AngularVelocity> Setpoint velocity.protected SmartMotorControllerTelemetryTelemetry.protected Optional<SmartMotorControllerTelemetryConfig> Config for publishing specific telemetry.protected Optional<org.wpilib.networktables.NetworkTable> SmartMotorControllertelemetry table.protected Optional<org.wpilib.networktables.NetworkTable> SmartMotorControllertuning table. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreate the motor controller, subclasses wrap the vendor device. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCloseHook(Runnable hook) Register a hook to run whenclose()is called, so command-layer helpers (e.g.abstract booleanapplyConfig(SmartMotorControllerConfig<?> config) Apply theSmartMotorControllerConfigto theSmartMotorController.voidApply the live-tuned values (from the Tuning NetworkTable) to thisSmartMotorController.protected StringbuildAlertId(String motorControllerType, int deviceId, String alertType) Build a uniqueAlertidentifier for this motor controller, for use oncem_confighas been assigned by the implementing constructor.voidCheck config for safe values.voidclose()Close the SMC for unit testing.static SmartMotorControllercreate(Object motorController, org.wpilib.math.system.DCMotor motorSim, SmartMotorControllerConfig<?> cfg) Create aSmartMotorControllerwrapper from the provided motor controller object.org.wpilib.units.measure.TimeGet the period the closed loop controller runs at.Get the active closed loop controller slot.abstract SmartMotorControllerConfig<?> Get theSmartMotorControllerConfigfor theSmartMotorControllerabstract org.wpilib.math.system.DCMotorGet theDCMotormodeling the motor controlled by the motor controller.abstract doubleGet the duty cycle output of the motor controller.protected Optional<org.wpilib.math.trajectory.ExponentialProfile.State> Get the current encoder exponential profile state for the closed loop controller.abstract Optional<org.wpilib.units.measure.Angle> Get the mechanismAnglefrom the external (absolute) encoder, scaled by the configured external encoderMechanismGearing.abstract Optional<org.wpilib.units.measure.AngularVelocity> Get the mechanismAngularVelocityfrom the external (absolute) encoder, scaled by the configured external encoderMechanismGearing.Get the loosely coupled followers, which are sent the leader's setpoints and run their own closed loop controllers.abstract org.wpilib.units.measure.LinearAccelerationGet the usable measurement of the motor for mechanisms operating under distance units converted with theSmartMotorControllerConfigabstract org.wpilib.units.measure.DistanceGet the usable measurement of the motor for mechanisms operating under distance units converted with theSmartMotorControllerConfig.Optional<org.wpilib.units.measure.Distance> Get the Mechanism setpoint position as aDistance.abstract org.wpilib.units.measure.LinearVelocityGet the usable measurement of the motor for mechanisms operating under distance units converted with theSmartMotorControllerConfig.abstract org.wpilib.units.measure.AngularAccelerationGet the MechanismAngularAcceleration, calculating it on the robot controller if necessary by taking the derivative of the velocity.abstract org.wpilib.units.measure.AngleOptional<org.wpilib.units.measure.Angle> Get the Mechanism setpoint position.Optional<org.wpilib.units.measure.AngularVelocity> Get the Mechanism velocity setpoint.abstract org.wpilib.units.measure.AngularVelocityGet the MechanismAngularVelocitytaking the configuredMechanismGearinginto the measurement applied via theSmartMotorControllerConfig.abstract ObjectGet the Motor Controller Object passed into theSmartMotorController.abstract ObjectGet the motor controller object config generated bySmartMotorControllerbased off theSmartMotorControllerConfiggetName()Get the name of theSmartMotorControllerabstract org.wpilib.units.measure.AngleGet the mechanismAnglefrom the relative encoder in the motor, scaled by the configuredMechanismGearing.abstract org.wpilib.units.measure.AngularVelocityGet the mechanismAngularVelocityfrom the relative encoder in the motor, scaled by the configuredMechanismGearing.abstract org.wpilib.units.measure.AngleGet the rotations of the motor with the relative encoder since the motor controller powered on scaled to the mechanism rotations.abstract org.wpilib.units.measure.AngularVelocityGets the angular velocity of the motor.Optional<org.wpilib.units.measure.Force> Get the feedforwardForcelast supplied tosetVelocity(AngularVelocity, Force)orsetVelocity(LinearVelocity, Force).Get the sim supplier.abstract org.wpilib.units.measure.CurrentGet the stator current of the motor controller.abstract Optional<org.wpilib.units.measure.Current> Get the supply current of the motor controller.abstract org.wpilib.units.measure.TemperatureGet theSmartMotorControllertemperature.protected Optional<org.wpilib.math.trajectory.TrapezoidProfile.State> Get the current encoder trapezoidal profile state for the closed loop controller.abstract org.wpilib.util.Pair<Optional<List<SmartMotorControllerTelemetry.BooleanTelemetryField>>, Optional<List<SmartMotorControllerTelemetry.DoubleTelemetryField>>> Get a list of unsupported telemetry fields if any exist.Optional<org.wpilib.units.measure.Time> Get how far behind the mechanism the measured velocity is, from how the motor controller filters it, as WPILib's SysId works it out for its presets.abstract org.wpilib.units.measure.VoltageGet the voltage output of the motor.booleanisMotor(org.wpilib.math.system.DCMotor a, org.wpilib.math.system.DCMotor b) CompareDCMotors to identify the given motor.voidIterate the closed loop controller.abstract voidSeed the relative encoder with the position from the absolute encoder.abstract voidsetClosedLoopRampRate(org.wpilib.units.measure.Time rampRate) Set the closed loop ramp rate.abstract voidSet the closed loop controller slot to use.abstract voidsetDutyCycle(double dutyCycle) Set the dutycycle output of the motor controller.abstract voidsetEncoderInverted(boolean inverted) Set the phase of the encoder attached to the brushless motor.abstract voidsetEncoderPosition(org.wpilib.units.measure.Angle angle) Set the encoder positionabstract voidsetEncoderPosition(org.wpilib.units.measure.Distance distance) Set the encoder position.abstract voidsetEncoderVelocity(org.wpilib.units.measure.AngularVelocity velocity) Set the encoder velocityabstract voidsetEncoderVelocity(org.wpilib.units.measure.LinearVelocity velocity) Set the encoder velocity.abstract voidsetExponentialProfile(OptionalDouble kV, OptionalDouble kA, Optional<org.wpilib.units.measure.Voltage> maxInput) Set the exponential profile fields.abstract voidsetFeedback(double kP, double kI, double kD) Set the closed loop feedback controller PID.abstract voidsetFeedforward(double kS, double kV, double kA, double kG) Set the feedforward controller.abstract voidsetKa(double kA) Acceleration feedforward element.abstract voidsetKd(double kD) Set kD for the feedback controller PID.abstract voidsetKg(double kG) kSin feedforward element.abstract voidsetKi(double kI) Set kI for the feedback controller PID.abstract voidsetKp(double kP) Set kP for the feedback controller PID.abstract voidsetKs(double kS) Static feedforward element.abstract voidsetKv(double kV) Velocity feedforward element.abstract voidsetMeasurementLowerLimit(org.wpilib.units.measure.Distance lowerLimit) Set the measurement lower limit, only works if mechanism circumference is defined.abstract voidsetMeasurementUpperLimit(org.wpilib.units.measure.Distance upperLimit) Set the measurement upper limit, only works if mechanism circumference is defined.abstract voidsetMechanismCircumference(org.wpilib.units.measure.Distance circumference) Update the mechanism circumference used for linear position/velocity conversions.abstract voidsetMechanismGearing(MechanismGearing gearing) Update the mechanism gearing used for position/velocity conversions.abstract voidsetMechanismLimits(org.wpilib.units.measure.Angle lower, org.wpilib.units.measure.Angle upper) Set the Mechanism limits for the motor controller.abstract voidsetMechanismLimitsEnabled(boolean enabled) Enable or disable the mechanism/measurement limits in the motor controller.abstract voidsetMechanismLowerLimit(org.wpilib.units.measure.Angle lowerLimit) Set the mechanism lower limit.abstract voidsetMechanismUpperLimit(org.wpilib.units.measure.Angle upperLimit) Set the mechanism upper limit.abstract voidsetMotionProfileMaxAcceleration(org.wpilib.units.measure.AngularAcceleration maxAcceleration) Set the maximum acceleration for the trapezoidal profile for the feedback controller.abstract voidsetMotionProfileMaxAcceleration(org.wpilib.units.measure.LinearAcceleration maxAcceleration) Set the maximum acceleration of the trapezoidal profile for the feedback controller.abstract voidsetMotionProfileMaxJerk(org.wpilib.units.measure.Velocity<org.wpilib.units.AngularAccelerationUnit> maxJerk) Set the maximum jerk for the trapezoidal profile for the feedback controller.abstract voidsetMotionProfileMaxVelocity(org.wpilib.units.measure.AngularVelocity maxVelocity) Set the maximum velocity for the trapezoidal profile for the feedback controller.abstract voidsetMotionProfileMaxVelocity(org.wpilib.units.measure.LinearVelocity maxVelocity) Set the maximum velocity of the trapezoidal profile for the feedback controller.abstract voidsetMotorInverted(boolean inverted) Set the inversion state of the motor.abstract voidsetOpenLoopRampRate(org.wpilib.units.measure.Time rampRate) Set the open loop ramp rate.abstract voidsetPosition(org.wpilib.units.measure.Angle angle) Set the MechanismAngleusing the PID and feedforward fromSmartMotorControllerConfig.abstract voidsetPosition(org.wpilib.units.measure.Distance distance) Set the MechanismDistanceusing the PID and feedforward fromSmartMotorControllerConfig.voidsetSimSupplier(SimSupplier mechanismSupplier) Set theSimSupplierMechanism.abstract voidsetStatorCurrentLimit(org.wpilib.units.measure.Current currentLimit) Set the stator current limit for the device.abstract voidsetSupplyCurrentLimit(org.wpilib.units.measure.Current currentLimit) Set the supply current limit.voidSet up live tuning for thisSmartMotorControllerwhen its telemetry verbosity isTelemetryVerbosity.HIGH, so the values under the Tuning table are applied while the "Live Tuning" command runs.abstract voidSetup the simulation for the wrapper.voidSetup Telemetry with default NT pathvoidsetupTelemetry(org.wpilib.networktables.NetworkTable telemetry, org.wpilib.networktables.NetworkTable tuning) Update the telemetry under the motor name under the givenNetworkTableabstract voidsetVelocity(org.wpilib.units.measure.AngularVelocity angle) Set the MechanismAngularVelocityusing the PID and feedforward fromSmartMotorControllerConfig.abstract voidsetVelocity(org.wpilib.units.measure.AngularVelocity angle, org.wpilib.units.measure.Force feedforwardForce) Set the MechanismAngularVelocityusing the PID and feedforward fromSmartMotorControllerConfig, with an additional feedforwardForceapplied on top, e.g.voidsetVelocity(org.wpilib.units.measure.LinearVelocity velocity) Set the MechanismLinearVelocityusing the PID and feedforward fromSmartMotorControllerConfig.voidsetVelocity(org.wpilib.units.measure.LinearVelocity velocity, org.wpilib.units.measure.Force feedforwardForce) Set the MechanismLinearVelocityusing the PID and feedforward fromSmartMotorControllerConfig, with an additional feedforwardForceapplied on top, e.g.abstract voidsetVoltage(org.wpilib.units.measure.Voltage voltage) Set the voltage output of the motor controller.abstract voidsetZeroPower(MotorMode mode) Set the motor zero power mode from COAST or BRAKE.abstract voidSimulation iteration.voidStart the closed loop controller with the period, and the loosely coupled followers' closed loop controllers.voidStop the closed loop controller, and the loosely coupled followers' closed loop controllers.voidStop live tuning, as when the "Live Tuning" command ends.abstract voidCheck if the relative encoder is out of sync with absolute encoder within defined tolerances.toString()booleanWhether live tuning is enabled for thisSmartMotorController, i.e.voidUpdate the telemetry under the motor name under the givenNetworkTable
-
Field Details
-
telemetry
Telemetry. -
m_config
SmartMotorControllerConfigfor the motor. -
m_slot
SmartMotorController.ClosedLoopControllerSlotfor the closed loop controller. -
m_expoProfile
Exponential profile for the closed loop controller. -
m_expoState
Exponential profile state for the closed loop controller. -
m_trapezoidProfile
Trapezoidal profile for the closed loop controller. -
m_trapState
Trapezoidal profile state for the closed loop controller. -
m_lastClosedLoopMechanismPosition
Mechanism position the closed loop controller last used, unwrapped across the continuous wrapping point. -
m_pid
Simple PID controller for the motor controller. -
m_lqr
LQR controller for the motor controller. -
setpointPosition
Setpoint position -
setpointVelocity
Setpoint velocity. -
setpointFeedforwardForce
Setpoint feedforward force, applied on top of the velocity setpoint, e.g. from a PathPlanner set-point generator. -
m_closedLoopControllerThread
protected org.wpilib.system.Notifier m_closedLoopControllerThreadThread of the closed loop controller. -
m_closeHooks
-
parentTable
Parent table for telemetry. -
telemetryTable
SmartMotorControllertelemetry table. -
tuningTable
SmartMotorControllertuning table. -
telemetryConfig
Config for publishing specific telemetry. -
m_simSupplier
SimSupplierfor the mechanism. -
m_looseFollowers
Loosely coupled followers. -
m_systemCoreClosedLoopAlert
Alert shown when the closed loop controller is running on the SystemCore. Empty until constructed by theSmartMotorControllerimplementation once its telemetry name (or a generated fallback identifier) is known. -
m_batterySimUUID
Battery simulation UUID used when standalone simulation.
-
-
Constructor Details
-
SmartMotorController
protected SmartMotorController()Create the motor controller, subclasses wrap the vendor device.
-
-
Method Details
-
create
public static SmartMotorController create(Object motorController, org.wpilib.math.system.DCMotor motorSim, SmartMotorControllerConfig<?> cfg) Create aSmartMotorControllerwrapper from the provided motor controller object.- Parameters:
motorController- Motor controller object.motorSim-DCMotorwhich the motor controller is connected too.cfg-SmartMotorControllerConfigfor theSmartMotorController- Returns:
SmartMotorController.
-
isMotor
public boolean isMotor(org.wpilib.math.system.DCMotor a, org.wpilib.math.system.DCMotor b) CompareDCMotors to identify the given motor.- Parameters:
a-DCMotorab-DCMotorb- Returns:
- True if same DC motor.
-
checkConfigSafety
public void checkConfigSafety()Check config for safe values.- Throws:
SmartMotorControllerConfigurationException- if the motor is a single NEO 550 and the stator current limit is not defined.SmartMotorControllerConfigurationException- if the motor is a single NEO 550 and the stator current limit is above 40A.
-
getSimSupplier
-
setSimSupplier
Set theSimSupplierMechanism.- Parameters:
mechanismSupplier- Mechanism sim supplier.
-
getTrapezoidalProfileState
Get the current encoder trapezoidal profile state for the closed loop controller.- Returns:
TrapezoidProfile.Statefrom the encoders.- Throws:
SmartMotorControllerConfigurationException- if a trapezoidal profile and a linear closed loop controller are in use but the mechanism circumference is not configured.
-
getExponentialProfileState
protected Optional<org.wpilib.math.trajectory.ExponentialProfile.State> getExponentialProfileState()Get the current encoder exponential profile state for the closed loop controller.- Returns:
ExponentialProfile.Statefrom the encoders.- Throws:
SmartMotorControllerConfigurationException- if an exponential profile and a linear closed loop controller are in use but the mechanism circumference is not configured.
-
getVelocityMeasurementDelay
Get how far behind the mechanism the measured velocity is, from how the motor controller filters it, as WPILib's SysId works it out for its presets. Feedback gains worked out for the closed loop depend on it. In simulation, the simulation period, since the simulated sensor is updated once a period.- Returns:
- The delay, or empty if it is not known for this motor controller.
-
getClosedLoopControllerPeriod
public org.wpilib.units.measure.Time getClosedLoopControllerPeriod()Get the period the closed loop controller runs at. A closed loop controller run on the SystemCore, such as an LQR or an exponential profile, runs atSmartMotorControllerConfig.withClosedLoopControlPeriod(Time), 20 ms by default. One run on the motor controller runs at 1 kHz.- Returns:
- The closed loop controller's period.
-
stopClosedLoopController
public void stopClosedLoopController()Stop the closed loop controller, and the loosely coupled followers' closed loop controllers. The followers are stopped too, as they are not sent the duty cycle or voltage the leader is driven at instead. -
startClosedLoopController
public void startClosedLoopController()Start the closed loop controller with the period, and the loosely coupled followers' closed loop controllers.- Throws:
SmartMotorControllerConfigurationException- if the control mode is closed loop, a linear closed loop controller is in use, and the mechanism circumference is not configured.
-
iterateClosedLoopController
public void iterateClosedLoopController()Iterate the closed loop controller. Feedforward are only applied with profiled pid controllers.- Throws:
SmartMotorControllerConfigurationException- if the controller is running with a linear closed loop controller while the mechanism circumference is not configured.SmartMotorControllerConfigurationException- if the controller is running with a velocity setpoint that has a feedforward force while the mechanism circumference is not configured.
-
setupSimulation
public abstract void setupSimulation()Setup the simulation for the wrapper. -
seedRelativeEncoder
public abstract void seedRelativeEncoder()Seed the relative encoder with the position from the absolute encoder. -
synchronizeRelativeEncoder
public abstract void synchronizeRelativeEncoder()Check if the relative encoder is out of sync with absolute encoder within defined tolerances. -
simIterate
public abstract void simIterate()Simulation iteration. -
setZeroPower
-
setEncoderVelocity
public abstract void setEncoderVelocity(org.wpilib.units.measure.AngularVelocity velocity) Set the encoder velocity- Parameters:
velocity-AngularVelocityof the Mechanism.- Throws:
UnsupportedOperationException- if the motor controller does not support setting encoder velocity outside of simulation (e.g. REV Sparks).
-
setEncoderVelocity
public abstract void setEncoderVelocity(org.wpilib.units.measure.LinearVelocity velocity) Set the encoder velocity.- Parameters:
velocity- MeasurementLinearVelocity- Throws:
SmartMotorControllerConfigurationException- if the mechanism circumference is not configured.UnsupportedOperationException- if the motor controller does not support setting encoder velocity outside of simulation (e.g. REV Sparks).
-
setEncoderPosition
public abstract void setEncoderPosition(org.wpilib.units.measure.Angle angle) Set the encoder position- Parameters:
angle- Current MechanismAngle.
-
setEncoderPosition
public abstract void setEncoderPosition(org.wpilib.units.measure.Distance distance) Set the encoder position.- Parameters:
distance- Current MeasurementDistance.- Throws:
SmartMotorControllerConfigurationException- if the mechanism circumference is not configured.
-
setPosition
public abstract void setPosition(org.wpilib.units.measure.Angle angle) Set the MechanismAngleusing the PID and feedforward fromSmartMotorControllerConfig.- Parameters:
angle- Mechanism angle to set.- Throws:
SmartMotorControllerConfigurationException- if the configured vendor position control request is not supported by the motor controller.
-
setPosition
public abstract void setPosition(org.wpilib.units.measure.Distance distance) Set the MechanismDistanceusing the PID and feedforward fromSmartMotorControllerConfig.- Parameters:
distance- MechanismDistanceto set.- Throws:
SmartMotorControllerConfigurationException- if the mechanism circumference is not configured.SmartMotorControllerConfigurationException- if the configured vendor position control request is not supported by the motor controller.
-
setVelocity
public void setVelocity(org.wpilib.units.measure.LinearVelocity velocity) Set the MechanismLinearVelocityusing the PID and feedforward fromSmartMotorControllerConfig.- Parameters:
velocity- MechanismLinearVelocityto target.- Throws:
SmartMotorControllerConfigurationException- if the mechanism circumference is not configured.SmartMotorControllerConfigurationException- if the configured vendor velocity control request is not supported by the motor controller.
-
setVelocity
public abstract void setVelocity(org.wpilib.units.measure.AngularVelocity angle) Set the MechanismAngularVelocityusing the PID and feedforward fromSmartMotorControllerConfig.- Parameters:
angle- MechanismAngularVelocityto target.- Throws:
SmartMotorControllerConfigurationException- if the configured vendor velocity control request is not supported by the motor controller.
-
setVelocity
public void setVelocity(org.wpilib.units.measure.LinearVelocity velocity, org.wpilib.units.measure.Force feedforwardForce) Set the MechanismLinearVelocityusing the PID and feedforward fromSmartMotorControllerConfig, with an additional feedforwardForceapplied on top, e.g. from a PathPlanner set-point generator. Applied whether the motor controller's native closed-loop velocity control or an LQR/software motion profile loop is driving the mechanism.- Parameters:
velocity- MechanismLinearVelocityto target.feedforwardForce- Additional feedforwardForceapplied to the mechanism, ornullfor none.- Throws:
SmartMotorControllerConfigurationException- if the mechanism circumference is not configured.SmartMotorControllerConfigurationException- if the configured vendor velocity control request is not supported by the motor controller.
-
setVelocity
public abstract void setVelocity(org.wpilib.units.measure.AngularVelocity angle, org.wpilib.units.measure.Force feedforwardForce) Set the MechanismAngularVelocityusing the PID and feedforward fromSmartMotorControllerConfig, with an additional feedforwardForceapplied on top, e.g. from a PathPlanner set-point generator. Applied whether the motor controller's native closed-loop velocity control or an LQR/software motion profile loop is driving the mechanism.- Parameters:
angle- MechanismAngularVelocityto target.feedforwardForce- Additional feedforwardForceapplied to the mechanism, ornullfor none.- Throws:
SmartMotorControllerConfigurationException- if a feedforward force is given and the mechanism circumference is not configured.SmartMotorControllerConfigurationException- if the configured vendor velocity control request is not supported by the motor controller.
-
applyConfig
Apply theSmartMotorControllerConfigto theSmartMotorController.- Parameters:
config-SmartMotorControllerConfigto use.- Returns:
- Successful Application of the configuration.
- Throws:
SmartMotorControllerConfigurationException- if the config contains options that are invalid or unsupported for the motor controller.SmartMotorControllerConfigurationException- if a required config option is left unhandled during validation.IllegalArgumentException- if the config references an unsupported device or option for the motor controller (e.g. an unsupported external encoder or follower type).
-
getDutyCycle
public abstract double getDutyCycle()Get the duty cycle output of the motor controller.- Returns:
- DutyCycle of the motor controller.
-
setDutyCycle
public abstract void setDutyCycle(double dutyCycle) Set the dutycycle output of the motor controller.- Parameters:
dutyCycle- Value between [-1,1]
-
getSupplyCurrent
Get the supply current of the motor controller.- Returns:
- The supply current of the motor controller.
-
getStatorCurrent
public abstract org.wpilib.units.measure.Current getStatorCurrent()Get the stator current of the motor controller.- Returns:
- Stator current
-
getVoltage
public abstract org.wpilib.units.measure.Voltage getVoltage()Get the voltage output of the motor.- Returns:
- Voltage output of the motor.
-
setVoltage
public abstract void setVoltage(org.wpilib.units.measure.Voltage voltage) Set the voltage output of the motor controller. Useful for Sysid.- Parameters:
voltage- Voltage to set the motor controller output to.
-
getDCMotor
public abstract org.wpilib.math.system.DCMotor getDCMotor()Get theDCMotormodeling the motor controlled by the motor controller.- Returns:
DCMotorof the controlled motor.
-
getMeasurementVelocity
public abstract org.wpilib.units.measure.LinearVelocity getMeasurementVelocity()Get the usable measurement of the motor for mechanisms operating under distance units converted with theSmartMotorControllerConfig.- Returns:
- Measurement velocity of the mechanism post-gearing.
- Throws:
SmartMotorControllerConfigurationException- if the mechanism circumference is not configured.
-
getMeasurementPosition
public abstract org.wpilib.units.measure.Distance getMeasurementPosition()Get the usable measurement of the motor for mechanisms operating under distance units converted with theSmartMotorControllerConfig.- Returns:
- Measurement velocity of the mechanism post-gearing.
- Throws:
SmartMotorControllerConfigurationException- if the mechanism circumference is not configured.
-
getMeasurementAcceleration
public abstract org.wpilib.units.measure.LinearAcceleration getMeasurementAcceleration()Get the usable measurement of the motor for mechanisms operating under distance units converted with theSmartMotorControllerConfig- Returns:
- Measurement acceleration of the mechanism post-gearing.
- Throws:
SmartMotorControllerConfigurationException- if the mechanism circumference is not configured.
-
getMechanismVelocity
public abstract org.wpilib.units.measure.AngularVelocity getMechanismVelocity()Get the MechanismAngularVelocitytaking the configuredMechanismGearinginto the measurement applied via theSmartMotorControllerConfig. UsesgetExternalEncoderMechanismVelocity()when the external (absolute) encoder is enabled withSmartMotorControllerConfig.withUseExternalFeedbackEncoder(boolean), otherwisegetRelativeMechanismVelocity().- Returns:
- Mechanism
AngularVelocity
-
getMechanismAcceleration
public abstract org.wpilib.units.measure.AngularAcceleration getMechanismAcceleration()Get the MechanismAngularAcceleration, calculating it on the robot controller if necessary by taking the derivative of the velocity.- Returns:
- Mechanism
AngularAcceleration
-
getMechanismPosition
public abstract org.wpilib.units.measure.Angle getMechanismPosition()Get the mechanismAngletaking the configuredMechanismGearingfromSmartMotorControllerConfig. UsesgetExternalEncoderMechanismPosition()when the external (absolute) encoder is enabled withSmartMotorControllerConfig.withUseExternalFeedbackEncoder(boolean), otherwisegetRelativeMechanismPosition().- Returns:
- Mechanism
Angle
-
getRelativeMechanismVelocity
public abstract org.wpilib.units.measure.AngularVelocity getRelativeMechanismVelocity()Get the mechanismAngularVelocityfrom the relative encoder in the motor, scaled by the configuredMechanismGearing. Never uses the external (absolute) encoder.- Returns:
- Mechanism
AngularVelocityfrom the relative encoder.
-
getRelativeMechanismPosition
public abstract org.wpilib.units.measure.Angle getRelativeMechanismPosition()Get the mechanismAnglefrom the relative encoder in the motor, scaled by the configuredMechanismGearing. Never uses the external (absolute) encoder.- Returns:
- Mechanism
Anglefrom the relative encoder.
-
getRotorVelocity
public abstract org.wpilib.units.measure.AngularVelocity getRotorVelocity()Gets the angular velocity of the motor.- Returns:
AngularVelocityof the relative motor encoder.
-
getRotorPosition
public abstract org.wpilib.units.measure.Angle getRotorPosition()Get the rotations of the motor with the relative encoder since the motor controller powered on scaled to the mechanism rotations.- Returns:
Angleof the relative encoder in the motor.
-
getExternalEncoderMechanismPosition
Get the mechanismAnglefrom the external (absolute) encoder, scaled by the configured external encoderMechanismGearing. Available whether or not the external encoder is used for feedback.- Returns:
- Mechanism
Anglefrom the external encoder, or empty if none is attached.
-
getExternalEncoderMechanismVelocity
public abstract Optional<org.wpilib.units.measure.AngularVelocity> getExternalEncoderMechanismVelocity()Get the mechanismAngularVelocityfrom the external (absolute) encoder, scaled by the configured external encoderMechanismGearing. Available whether or not the external encoder is used for feedback.- Returns:
- Mechanism
AngularVelocityfrom the external encoder, or empty if none is attached.
-
setupTelemetry
public void setupTelemetry(org.wpilib.networktables.NetworkTable telemetry, org.wpilib.networktables.NetworkTable tuning) Update the telemetry under the motor name under the givenNetworkTable- Parameters:
telemetry-NetworkTableto create theSmartMotorControllerTelemetrysubtable under based off ofSmartMotorControllerConfig.getTelemetryName().tuning-NetworkTableto create the tunable telemetry fromSmartMotorControllerTelemetrysubtable under. Based off ofSmartMotorControllerConfig.getTelemetryName().
-
tuningEnabled
public boolean tuningEnabled()Whether live tuning is enabled for thisSmartMotorController, i.e. whether tuning values should be pulled from NetworkTables each loop viaapplyTuningValues().- Returns:
- True if live tuning is enabled.
-
applyTuningValues
public void applyTuningValues()Apply the live-tuned values (from the Tuning NetworkTable) to thisSmartMotorController.- Throws:
SmartMotorControllerConfigurationException- if the control mode is notCLOSED_LOOP.SmartMotorControllerConfigurationException- if the motor controller implementation rejects a tuned value.IllegalArgumentException- if the tuned closed loop controller slot is not supported by the motor controller implementation.
-
stopLiveTuning
public void stopLiveTuning()Stop live tuning, as when the "Live Tuning" command ends. A tuning voltage stops, it is turned off in the Tuning table, and the closed loop controller holds the setpoint again. -
getLooselyCoupledFollowers
Get the loosely coupled followers, which are sent the leader's setpoints and run their own closed loop controllers.- Returns:
- The loosely coupled followers, or empty if there are none.
-
addCloseHook
-
setupTelemetry
public void setupTelemetry()Setup Telemetry with default NT path -
setupLiveTuning
public void setupLiveTuning()Set up live tuning for thisSmartMotorControllerwhen its telemetry verbosity isTelemetryVerbosity.HIGH, so the values under the Tuning table are applied while the "Live Tuning" command runs. Only sets it up once, however many times it is called. -
updateTelemetry
public void updateTelemetry()Update the telemetry under the motor name under the givenNetworkTable -
setMotorInverted
public abstract void setMotorInverted(boolean inverted) Set the inversion state of the motor.- Parameters:
inverted- Inverted motor.
-
setEncoderInverted
public abstract void setEncoderInverted(boolean inverted) Set the phase of the encoder attached to the brushless motor.- Parameters:
inverted- Phase of the encoder.
-
setMotionProfileMaxVelocity
public abstract void setMotionProfileMaxVelocity(org.wpilib.units.measure.LinearVelocity maxVelocity) Set the maximum velocity of the trapezoidal profile for the feedback controller.- Parameters:
maxVelocity- Maximum velocity, will be translated to MetersPerSecond.- Throws:
SmartMotorControllerConfigurationException- if the mechanism circumference is not configured.
-
setMotionProfileMaxAcceleration
public abstract void setMotionProfileMaxAcceleration(org.wpilib.units.measure.LinearAcceleration maxAcceleration) Set the maximum acceleration of the trapezoidal profile for the feedback controller.- Parameters:
maxAcceleration- Maximum acceleration, will be translated to MetersPerSecondPerSecond.- Throws:
SmartMotorControllerConfigurationException- if the mechanism circumference is not configured.
-
setMotionProfileMaxVelocity
public abstract void setMotionProfileMaxVelocity(org.wpilib.units.measure.AngularVelocity maxVelocity) Set the maximum velocity for the trapezoidal profile for the feedback controller.- Parameters:
maxVelocity- Maximum velocity, will be translated to RotationsPerSecond.
-
setMotionProfileMaxAcceleration
public abstract void setMotionProfileMaxAcceleration(org.wpilib.units.measure.AngularAcceleration maxAcceleration) Set the maximum acceleration for the trapezoidal profile for the feedback controller.- Parameters:
maxAcceleration- Maximum acceleration, will be translated to RotationsPerSecondPerSecond.
-
setMotionProfileMaxJerk
public abstract void setMotionProfileMaxJerk(org.wpilib.units.measure.Velocity<org.wpilib.units.AngularAccelerationUnit> maxJerk) Set the maximum jerk for the trapezoidal profile for the feedback controller.- Parameters:
maxJerk- Maximum jerk, will be translated to RotationsPerSecondPerSecondPerSecond.
-
setExponentialProfile
public abstract void setExponentialProfile(OptionalDouble kV, OptionalDouble kA, Optional<org.wpilib.units.measure.Voltage> maxInput) Set the exponential profile fields.- Parameters:
kV- kV for the exponential profile.kA- kA for the exponential profile.maxInput- Maximum input for the exponential profile.
-
setKp
public abstract void setKp(double kP) Set kP for the feedback controller PID.- Parameters:
kP- kP
-
setKi
public abstract void setKi(double kI) Set kI for the feedback controller PID.- Parameters:
kI- kI.
-
setKd
public abstract void setKd(double kD) Set kD for the feedback controller PID.- Parameters:
kD- kD for the feedback controller PID.
-
setFeedback
public abstract void setFeedback(double kP, double kI, double kD) Set the closed loop feedback controller PID.- Parameters:
kP- kP; Proportional scalar.kI- kI; Integral scalar.kD- kD; derivative scalar.
-
setKs
public abstract void setKs(double kS) Static feedforward element.- Parameters:
kS- kS; Static feedforward.
-
setKv
public abstract void setKv(double kV) Velocity feedforward element.- Parameters:
kV- kV; Velocity feedforward.
-
setKa
public abstract void setKa(double kA) Acceleration feedforward element.- Parameters:
kA- kA; Acceleration feedforward.
-
setKg
public abstract void setKg(double kG) kSin feedforward element.- Parameters:
kG- kG; Gravity feedforward.
-
setFeedforward
public abstract void setFeedforward(double kS, double kV, double kA, double kG) Set the feedforward controller.- Parameters:
kS- kS; Static feedforward.kV- kV; Velocity feedforward.kA- kA; Acceleration feedforward.kG- kG; Gravity feedforward.
-
setStatorCurrentLimit
public abstract void setStatorCurrentLimit(org.wpilib.units.measure.Current currentLimit) Set the stator current limit for the device.- Parameters:
currentLimit- Stator current limit.
-
setSupplyCurrentLimit
public abstract void setSupplyCurrentLimit(org.wpilib.units.measure.Current currentLimit) Set the supply current limit.- Parameters:
currentLimit- Supply current limit.
-
setClosedLoopRampRate
public abstract void setClosedLoopRampRate(org.wpilib.units.measure.Time rampRate) Set the closed loop ramp rate. The ramp rate is how fast the motor can go from 0-100, measured in seconds.- Parameters:
rampRate- Time from 0 to 100.
-
setOpenLoopRampRate
public abstract void setOpenLoopRampRate(org.wpilib.units.measure.Time rampRate) Set the open loop ramp rate. The ramp rate is how fast the motor can go from 0 to 100, measured in Seconds.- Parameters:
rampRate- Time it takes to go from 0 to 100.
-
setMeasurementUpperLimit
public abstract void setMeasurementUpperLimit(org.wpilib.units.measure.Distance upperLimit) Set the measurement upper limit, only works if mechanism circumference is defined.- Parameters:
upperLimit- Upper limit, will be translated to meters.- Throws:
SmartMotorControllerConfigurationException- if a lower limit is configured and it is greater than or equal to the new upper limit.
-
setMeasurementLowerLimit
public abstract void setMeasurementLowerLimit(org.wpilib.units.measure.Distance lowerLimit) Set the measurement lower limit, only works if mechanism circumference is defined.- Parameters:
lowerLimit- Lower limit, will be translated to meters.- Throws:
SmartMotorControllerConfigurationException- if an upper limit is configured and the new lower limit is greater than or equal to it.
-
setMechanismUpperLimit
public abstract void setMechanismUpperLimit(org.wpilib.units.measure.Angle upperLimit) Set the mechanism upper limit.- Parameters:
upperLimit- Upper limit, will be translated to rotations.- Throws:
SmartMotorControllerConfigurationException- if a lower limit is configured and it is greater than or equal to the new upper limit.
-
setMechanismLowerLimit
public abstract void setMechanismLowerLimit(org.wpilib.units.measure.Angle lowerLimit) Set the mechanism lower limit.- Parameters:
lowerLimit- Lower limit, will be translated to rotations.- Throws:
SmartMotorControllerConfigurationException- if an upper limit is configured and the new lower limit is greater than or equal to it.
-
setMechanismLimits
public abstract void setMechanismLimits(org.wpilib.units.measure.Angle lower, org.wpilib.units.measure.Angle upper) Set the Mechanism limits for the motor controller.- Parameters:
lower- Lower limit, will be translated to rotations.upper- Upper limit, will be translated to rotations.- Throws:
SmartMotorControllerConfigurationException- if both limits are non-null and the lower limit is greater than or equal to the upper limit.
-
setMechanismLimitsEnabled
public abstract void setMechanismLimitsEnabled(boolean enabled) Enable or disable the mechanism/measurement limits in the motor controller.- Parameters:
enabled- Application of the limits
-
setMechanismGearing
Update the mechanism gearing used for position/velocity conversions.- Parameters:
gearing- NewMechanismGearingto apply.- Implementation Note:
- Forwards the gearing to all loosely coupled follower motors.
-
setMechanismCircumference
public abstract void setMechanismCircumference(org.wpilib.units.measure.Distance circumference) Update the mechanism circumference used for linear position/velocity conversions.- Parameters:
circumference- New mechanism circumference.
-
setClosedLoopSlot
Set the closed loop controller slot to use.- Parameters:
slot- Slot to use.- Throws:
IllegalArgumentException- if the slot is not supported by the motor controller (e.g. SLOT_3 on a TalonFX or TalonFXS).
-
getTemperature
public abstract org.wpilib.units.measure.Temperature getTemperature()Get theSmartMotorControllertemperature.- Returns:
Temperature
-
getConfig
Get theSmartMotorControllerConfigfor theSmartMotorController- Returns:
SmartMotorControllerConfigused.
-
getMotorController
Get the Motor Controller Object passed into theSmartMotorController.- Returns:
- Motor Controller object.
-
getMotorControllerConfig
Get the motor controller object config generated bySmartMotorControllerbased off theSmartMotorControllerConfig- Returns:
- Motor controller config.
-
getMechanismPositionSetpoint
Get the Mechanism setpoint position.- Returns:
- Mechanism Setpoint position.
-
getMeasurementPositionSetpoint
Get the Mechanism setpoint position as aDistance.- Returns:
- Measurement setpoint position, empty when there is no position setpoint or the mechanism circumference is not configured.
-
getMechanismSetpointVelocity
Get the Mechanism velocity setpoint.- Returns:
- Mechanism velocity setpoint.
-
getSetpointFeedforwardForce
Get the feedforwardForcelast supplied tosetVelocity(AngularVelocity, Force)orsetVelocity(LinearVelocity, Force).- Returns:
- Setpoint feedforward
Force.
-
getUnsupportedTelemetryFields
public abstract org.wpilib.util.Pair<Optional<List<SmartMotorControllerTelemetry.BooleanTelemetryField>>, Optional<List<SmartMotorControllerTelemetry.DoubleTelemetryField>>> getUnsupportedTelemetryFields()Get a list of unsupported telemetry fields if any exist.- Returns:
- Optional list of unsupported telemetry fields.
-
getName
Get the name of theSmartMotorController- Returns:
Stringname if present, else "SmartMotorController"
-
buildAlertId
Build a uniqueAlertidentifier for this motor controller, for use oncem_confighas been assigned by the implementing constructor.The motor controller type and device ID are always included as a prefix, matching how
SmartMotorControllerConfigurationExceptionmessages identify the device, e.g."TalonFX(3)". If a telemetry name is configured, the identifier ismotorControllerType(deviceId) + alertType + telemetryName. Otherwise a random UUID is used in place of the telemetry name so alerts from different motor controllers never collide:motorControllerType(deviceId) + UUID + alertType.- Parameters:
motorControllerType- Motor controller implementation type, e.g."TalonFX".deviceId- CAN device ID of the motor controller.alertType- Alert type/qualifier, e.g."ClosedLoop".- Returns:
- Unique
Alertidentifier.
-
close
public void close()Close the SMC for unit testing. -
toString
-
getClosedLoopControllerSlot
Get the active closed loop controller slot.- Returns:
- Active closed loop controller slot.
-