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 SimDouble
m_simAbsolutePosition
protected SimDevice
m_simDevice
protected SimDouble
m_simDistancePerRotation
protected SimBoolean
m_simIsConnected
protected SimDouble
m_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
Modifier and Type Method Description void
close()
double
get()
Get the encoder value since the last reset.double
getAbsolutePosition()
Get the absolute position of the duty cycle encoder.double
getDistance()
Get the distance the sensor has driven since the last reset as scaled by the value fromsetDistancePerRotation(double)
.double
getDistancePerRotation()
Get the distance per rotation for this encoder.int
getFPGAIndex()
Get the FPGA index for the DutyCycleEncoder.int
getFrequency()
Get the frequency in Hz of the duty cycle signal from the encoder.double
getPositionOffset()
Get the offset of position relative to the last reset.int
getSourceChannel()
Get the channel of the source.void
initSendable(SendableBuilder builder)
Initializes thisSendable
object.boolean
isConnected()
Get if the sensor is connectedvoid
reset()
Reset the Encoder distance to zero.void
setConnectedFrequencyThreshold(int frequency)
Change the frequency threshold for detecting connection used byisConnected()
.void
setDistancePerRotation(double distancePerRotation)
Set the distance per rotation of the encoder.void
setDutyCycleRange(double min, double max)
Set the encoder duty cycle range.void
setPositionOffset(double offset)
Set the position offset.
-
Field Details
-
Constructor Details
-
DutyCycleEncoder
Construct a new DutyCycleEncoder on a specific channel.- Parameters:
channel
- the channel to attach to
-
DutyCycleEncoder
Construct a new DutyCycleEncoder attached to an existing DutyCycle object.- Parameters:
dutyCycle
- the duty cycle to attach to
-
DutyCycleEncoder
Construct a new DutyCycleEncoder attached to a DigitalSource object.- Parameters:
source
- the digital source to attach to
-
-
Method Details
-
get
Get the encoder value since the last reset.This is reported in rotations since the last reset.
- Returns:
- the encoder value in rotations
-
getAbsolutePosition
Get the absolute position of the duty cycle encoder.getAbsolutePosition() - getPositionOffset() will give an encoder absolute position relative to the last reset. This could potentially be negative, which needs to be accounted for.
This will not account for rollovers, and will always be just the raw absolute position.
- Returns:
- the absolute position
-
getPositionOffset
Get the offset of position relative to the last reset.getAbsolutePosition() - 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
-
setPositionOffset
Set the position offset.This must be in the range of 0-1.
- Parameters:
offset
- the offset
-
setDutyCycleRange
Set the encoder duty cycle range. As the encoder needs to maintain a duty cycle, the duty cycle cannot go all the way to 0% or all the way to 100%. For example, an encoder with a 4096 us period might have a minimum duty cycle of 1 us / 4096 us and a maximum duty cycle of 4095 / 4096 us. Setting the range will result in an encoder duty cycle less than or equal to the minimum being output as 0 rotation, the duty cycle greater than or equal to the maximum being output as 1 rotation, and values in between linearly scaled from 0 to 1.- Parameters:
min
- minimum duty cycle (0-1 range)max
- maximum duty cycle (0-1 range)
-
setDistancePerRotation
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 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
Get the distance per rotation for this encoder.- Returns:
- The scale factor that will be used to convert rotation to useful units.
-
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
Get the frequency in Hz of the duty cycle signal from the encoder.- Returns:
- duty cycle frequency in Hz
-
reset
Reset the Encoder distance to zero. -
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
Change the frequency threshold for detecting connection used byisConnected()
.- Parameters:
frequency
- the minimum frequency in Hz.
-
close
- Specified by:
close
in interfaceAutoCloseable
-
getFPGAIndex
Get the FPGA index for the DutyCycleEncoder.- Returns:
- the FPGA index
-
getSourceChannel
Get the channel of the source.- Returns:
- the source channel
-
initSendable
Description copied from interface:Sendable
Initializes thisSendable
object.- Specified by:
initSendable
in interfaceSendable
- Parameters:
builder
- sendable builder
-