Package edu.wpi.first.wpilibj
Class DutyCycleEncoder
- java.lang.Object
-
- edu.wpi.first.wpilibj.DutyCycleEncoder
-
- All Implemented Interfaces:
Sendable,AutoCloseable
public class DutyCycleEncoder extends Object implements Sendable, AutoCloseable
Class for supporting duty cycle/PWM encoders, such as the US Digital MA3 with PWM Output, the CTRE Mag Encoder, the Rev Hex Encoder, and the AM Mag Encoder.
-
-
Field Summary
Fields Modifier and Type Field Description protected SimDevicem_simDeviceprotected SimBooleanm_simIsConnectedprotected SimDoublem_simPosition
-
Constructor Summary
Constructors Constructor Description DutyCycleEncoder(int channel)Construct a new DutyCycleEncoder on a specific channel.DutyCycleEncoder(DigitalSource source)Construct a new DutyCycleEncoder attached to a DigitalSource object.DutyCycleEncoder(DutyCycle dutyCycle)Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()doubleget()Get the encoder value since the last reset.doublegetDistance()Get the distance the sensor has driven since the last reset as scaled by the value fromsetDistancePerRotation(double).doublegetDistancePerRotation()Get the distance per rotation for this encoder.intgetFrequency()Get the frequency in Hz of the duty cycle signal from the encoder.doublegetPositionOffset()Get the offset of position relative to the last reset.voidinitSendable(SendableBuilder builder)Initializes thisSendableobject.booleanisConnected()Get if the sensor is connectedvoidreset()Reset the Encoder distance to zero.voidsetConnectedFrequencyThreshold(int frequency)Change the frequency threshold for detecting connection used byisConnected().voidsetDistancePerRotation(double distancePerRotation)Set the distance per rotation of the encoder.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface edu.wpi.first.wpilibj.Sendable
addChild, getName, getSubsystem, setName, setName, setName, setName, setSubsystem
-
-
-
-
Field Detail
-
m_simDevice
protected SimDevice m_simDevice
-
m_simPosition
protected SimDouble m_simPosition
-
m_simIsConnected
protected SimBoolean m_simIsConnected
-
-
Constructor Detail
-
DutyCycleEncoder
public DutyCycleEncoder(int channel)
Construct a new DutyCycleEncoder on a specific channel.- Parameters:
channel- the channel to attach to
-
DutyCycleEncoder
public DutyCycleEncoder(DutyCycle dutyCycle)
Construct a new DutyCycleEncoder attached to an existing DutyCycle object.- Parameters:
dutyCycle- the duty cycle to attach to
-
DutyCycleEncoder
public DutyCycleEncoder(DigitalSource source)
Construct a new DutyCycleEncoder attached to a DigitalSource object.- Parameters:
source- the digital source to attach to
-
-
Method Detail
-
get
public double get()
Get the encoder value since the last reset.This is reported in rotations since the last reset.
- Returns:
- the encoder value in rotations
-
getPositionOffset
public double getPositionOffset()
Get the offset of position relative to the last reset.getPositionInRotation() - getPositionOffset() will give an encoder absolute position relative to the last reset. This could potentially be negative, which needs to be accounted for.
- Returns:
- the position offset
-
setDistancePerRotation
public void setDistancePerRotation(double distancePerRotation)
Set the distance per rotation of the encoder. This sets the multiplier used to determine the distance driven based on the rotation value from the encoder. Set this value based on the how far the mechanism travels in 1 rotation of the encoder, and factor in gearing reductions following the encoder shaft. This distance can be in any units you like, linear or angular.- Parameters:
distancePerRotation- the distance per rotation of the encoder
-
getDistancePerRotation
public double getDistancePerRotation()
Get the distance per rotation for this encoder.- Returns:
- The scale factor that will be used to convert rotation to useful units.
-
getDistance
public double getDistance()
Get the distance the sensor has driven since the last reset as scaled by the value fromsetDistancePerRotation(double).- Returns:
- The distance driven since the last reset
-
getFrequency
public int getFrequency()
Get the frequency in Hz of the duty cycle signal from the encoder.- Returns:
- duty cycle frequency in Hz
-
reset
public void reset()
Reset the Encoder distance to zero.
-
isConnected
public boolean isConnected()
Get if the sensor is connectedThis uses the duty cycle frequency to determine if the sensor is connected. By default, a value of 100 Hz is used as the threshold, and this value can be changed with
setConnectedFrequencyThreshold(int).- Returns:
- true if the sensor is connected
-
setConnectedFrequencyThreshold
public void setConnectedFrequencyThreshold(int frequency)
Change the frequency threshold for detecting connection used byisConnected().- Parameters:
frequency- the minimum frequency in Hz.
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable
-
initSendable
public void initSendable(SendableBuilder builder)
Description copied from interface:SendableInitializes thisSendableobject.- Specified by:
initSendablein interfaceSendable- Parameters:
builder- sendable builder
-
-