Class ExpansionHubMotor
java.lang.Object
org.wpilib.hardware.expansionhub.ExpansionHubMotor
- All Implemented Interfaces:
AutoCloseable
This class controls a specific motor and encoder hooked up to an ExpansionHub.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe direction to follow a leader motor in when using the follow method.static enumNeutral mode applied when the motor output is commanded to 0. -
Constructor Summary
ConstructorsConstructorDescriptionExpansionHubMotor(int usbId, int channel) Constructs a motor at the requested channel on a specific USB port. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes a motor so another instance can be constructed.voidfollow(ExpansionHubMotor leader, ExpansionHubMotor.FollowDirection direction) Sets this motor to follow another motor on the same hub.Gets the current being pulled by the motor.doubleGets the current position of the motor encoder.doubleGets the current velocity of the motor encoder.Gets the motor constants object for position PID.Gets the motor constants object for velocity PID.booleanGets if the underlying ExpansionHub is connected.voidReset the encoder count to 0.voidsetDistancePerCount(double perCount) Sets the distance per count of the encoder.voidsetEnabled(boolean enabled) Sets if the motor output is enabled or not.voidSets if the motor should brake or coast when 0 is commanded.voidsetPositionSetpoint(double setpoint) Command the motor to drive to a specific position setpoint.voidsetReversed(boolean reversed) Sets if the motor and encoder should be reversed.voidsetThrottle(double throttle) Sets the throttle.voidsetVelocitySetpoint(double setpoint) Command the motor to drive to a specific velocity setpoint.voidsetVoltage(Voltage voltage) Sets the voltage to run the motor at.voidunfollow()Stops following the currently set leader motor.
-
Constructor Details
-
ExpansionHubMotor
public ExpansionHubMotor(int usbId, int channel) Constructs a motor at the requested channel on a specific USB port.- Parameters:
usbId- The USB port ID the hub is connected tochannel- The motor channel
-
-
Method Details
-
close
public void close()Closes a motor so another instance can be constructed.- Specified by:
closein interfaceAutoCloseable
-
setThrottle
public void setThrottle(double throttle) Sets the throttle.- Parameters:
throttle- The throttle where -1 represents full reverse and 1 represents full forward.
-
setVoltage
Sets the voltage to run the motor at. This value will be continuously scaled to match the input voltage.- Parameters:
voltage- The voltage to drive the motor at
-
setPositionSetpoint
public void setPositionSetpoint(double setpoint) Command the motor to drive to a specific position setpoint. This value will be scaled by setDistancePerCount and influenced by the PID constants.- Parameters:
setpoint- The position setpoint to drive the motor to
-
setVelocitySetpoint
public void setVelocitySetpoint(double setpoint) Command the motor to drive to a specific velocity setpoint. This value will be scaled by setDistancePerCount and influenced by the PID constants.- Parameters:
setpoint- The velocity setpoint to drive the motor to
-
setEnabled
public void setEnabled(boolean enabled) Sets if the motor output is enabled or not. Defaults to false.- Parameters:
enabled- True to enable, false to disable
-
setNeutralMode
Sets if the motor should brake or coast when 0 is commanded. Defaults to BRAKE mode.- Parameters:
mode- Neutral mode to apply when output is zero.
-
getCurrent
-
setDistancePerCount
public void setDistancePerCount(double perCount) Sets the distance per count of the encoder. Used to scale encoder readings.- Parameters:
perCount- The distance moved per count of the encoder
-
isHubConnected
public boolean isHubConnected()Gets if the underlying ExpansionHub is connected.- Returns:
- True if hub is connected, otherwise false
-
getEncoderVelocity
public double getEncoderVelocity()Gets the current velocity of the motor encoder. Scaled into distancePerCount units.- Returns:
- Encoder velocity
-
getEncoderPosition
public double getEncoderPosition()Gets the current position of the motor encoder. Scaled into distancePerCount units.- Returns:
- Encoder position
-
setReversed
public void setReversed(boolean reversed) Sets if the motor and encoder should be reversed.- Parameters:
reversed- True to reverse encoder, false otherwise
-
resetEncoder
public void resetEncoder()Reset the encoder count to 0. -
getVelocityConstants
Gets the motor constants object for velocity PID.- Returns:
- Velocity motor constants object
-
getPositionConstants
Gets the motor constants object for position PID.- Returns:
- Position motor constants object
-
follow
Sets this motor to follow another motor on the same hub.This does not support following motors that are also followers. Additionally, the direction of both motors will be the same.
- Parameters:
leader- The motor to followdirection- The direction to follow the leader
-
unfollow
public void unfollow()Stops following the currently set leader motor.
-