WPILibC++ 2023.4.3
frc::DutyCycleEncoder Class Reference

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. More...

#include <frc/DutyCycleEncoder.h>

Inheritance diagram for frc::DutyCycleEncoder:
wpi::Sendable wpi::SendableHelper< DutyCycleEncoder >

Public Member Functions

 DutyCycleEncoder (int channel)
 Construct a new DutyCycleEncoder on a specific channel. More...
 
 DutyCycleEncoder (DutyCycle &dutyCycle)
 Construct a new DutyCycleEncoder attached to an existing DutyCycle object. More...
 
 DutyCycleEncoder (DutyCycle *dutyCycle)
 Construct a new DutyCycleEncoder attached to an existing DutyCycle object. More...
 
 DutyCycleEncoder (std::shared_ptr< DutyCycle > dutyCycle)
 Construct a new DutyCycleEncoder attached to an existing DutyCycle object. More...
 
 DutyCycleEncoder (DigitalSource &digitalSource)
 Construct a new DutyCycleEncoder attached to a DigitalSource object. More...
 
 DutyCycleEncoder (DigitalSource *digitalSource)
 Construct a new DutyCycleEncoder attached to a DigitalSource object. More...
 
 DutyCycleEncoder (std::shared_ptr< DigitalSource > digitalSource)
 Construct a new DutyCycleEncoder attached to a DigitalSource object. More...
 
 ~DutyCycleEncoder () override=default
 
 DutyCycleEncoder (DutyCycleEncoder &&)=default
 
DutyCycleEncoderoperator= (DutyCycleEncoder &&)=default
 
int GetFrequency () const
 Get the frequency in Hz of the duty cycle signal from the encoder. More...
 
bool IsConnected () const
 Get if the sensor is connected. More...
 
void SetConnectedFrequencyThreshold (int frequency)
 Change the frequency threshold for detecting connection used by IsConnected. More...
 
void Reset ()
 Reset the Encoder distance to zero. More...
 
units::turn_t Get () const
 Get the encoder value since the last reset. More...
 
double GetAbsolutePosition () const
 Get the absolute position of the duty cycle encoder encoder. More...
 
double GetPositionOffset () const
 Get the offset of position relative to the last reset. More...
 
void SetPositionOffset (double offset)
 Set the position offset. More...
 
void SetDutyCycleRange (double min, double max)
 Set the encoder duty cycle range. More...
 
void SetDistancePerRotation (double distancePerRotation)
 Set the distance per rotation of the encoder. More...
 
double GetDistancePerRotation () const
 Get the distance per rotation for this encoder. More...
 
double GetDistance () const
 Get the distance the sensor has driven since the last reset as scaled by the value from SetDistancePerRotation. More...
 
int GetFPGAIndex () const
 Get the FPGA index for the DutyCycleEncoder. More...
 
int GetSourceChannel () const
 Get the channel of the source. More...
 
void InitSendable (wpi::SendableBuilder &builder) override
 Initializes this Sendable object. More...
 
- Public Member Functions inherited from wpi::Sendable
virtual ~Sendable ()=default
 
virtual void InitSendable (SendableBuilder &builder)=0
 Initializes this Sendable object. More...
 
- Public Member Functions inherited from wpi::SendableHelper< DutyCycleEncoder >
 SendableHelper (const SendableHelper &rhs)=default
 
 SendableHelper (SendableHelper &&rhs)
 
SendableHelperoperator= (const SendableHelper &rhs)=default
 
SendableHelperoperator= (SendableHelper &&rhs)
 

Additional Inherited Members

- Protected Member Functions inherited from wpi::SendableHelper< DutyCycleEncoder >
 SendableHelper ()=default
 
 ~SendableHelper ()
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ DutyCycleEncoder() [1/8]

frc::DutyCycleEncoder::DutyCycleEncoder ( int  channel)
explicit

Construct a new DutyCycleEncoder on a specific channel.

Parameters
channelthe channel to attach to

◆ DutyCycleEncoder() [2/8]

frc::DutyCycleEncoder::DutyCycleEncoder ( DutyCycle dutyCycle)
explicit

Construct a new DutyCycleEncoder attached to an existing DutyCycle object.

Parameters
dutyCyclethe duty cycle to attach to

◆ DutyCycleEncoder() [3/8]

frc::DutyCycleEncoder::DutyCycleEncoder ( DutyCycle dutyCycle)
explicit

Construct a new DutyCycleEncoder attached to an existing DutyCycle object.

Parameters
dutyCyclethe duty cycle to attach to

◆ DutyCycleEncoder() [4/8]

frc::DutyCycleEncoder::DutyCycleEncoder ( std::shared_ptr< DutyCycle dutyCycle)
explicit

Construct a new DutyCycleEncoder attached to an existing DutyCycle object.

Parameters
dutyCyclethe duty cycle to attach to

◆ DutyCycleEncoder() [5/8]

frc::DutyCycleEncoder::DutyCycleEncoder ( DigitalSource digitalSource)
explicit

Construct a new DutyCycleEncoder attached to a DigitalSource object.

Parameters
digitalSourcethe digital source to attach to

◆ DutyCycleEncoder() [6/8]

frc::DutyCycleEncoder::DutyCycleEncoder ( DigitalSource digitalSource)
explicit

Construct a new DutyCycleEncoder attached to a DigitalSource object.

Parameters
digitalSourcethe digital source to attach to

◆ DutyCycleEncoder() [7/8]

frc::DutyCycleEncoder::DutyCycleEncoder ( std::shared_ptr< DigitalSource digitalSource)
explicit

Construct a new DutyCycleEncoder attached to a DigitalSource object.

Parameters
digitalSourcethe digital source to attach to

◆ ~DutyCycleEncoder()

frc::DutyCycleEncoder::~DutyCycleEncoder ( )
overridedefault

◆ DutyCycleEncoder() [8/8]

frc::DutyCycleEncoder::DutyCycleEncoder ( DutyCycleEncoder &&  )
default

Member Function Documentation

◆ Get()

units::turn_t frc::DutyCycleEncoder::Get ( ) const

Get the encoder value since the last reset.

This is reported in rotations since the last reset.

Returns
the encoder value in rotations

◆ GetAbsolutePosition()

double frc::DutyCycleEncoder::GetAbsolutePosition ( ) const

Get the absolute position of the duty cycle encoder 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

◆ GetDistance()

double frc::DutyCycleEncoder::GetDistance ( ) const

Get the distance the sensor has driven since the last reset as scaled by the value from SetDistancePerRotation.

Returns
The distance driven since the last reset

◆ GetDistancePerRotation()

double frc::DutyCycleEncoder::GetDistancePerRotation ( ) const

Get the distance per rotation for this encoder.

Returns
The scale factor that will be used to convert rotation to useful units.

◆ GetFPGAIndex()

int frc::DutyCycleEncoder::GetFPGAIndex ( ) const

Get the FPGA index for the DutyCycleEncoder.

Returns
the FPGA index

◆ GetFrequency()

int frc::DutyCycleEncoder::GetFrequency ( ) const

Get the frequency in Hz of the duty cycle signal from the encoder.

Returns
duty cycle frequency in Hz

◆ GetPositionOffset()

double frc::DutyCycleEncoder::GetPositionOffset ( ) const

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

◆ GetSourceChannel()

int frc::DutyCycleEncoder::GetSourceChannel ( ) const

Get the channel of the source.

Returns
the source channel

◆ InitSendable()

void frc::DutyCycleEncoder::InitSendable ( wpi::SendableBuilder builder)
overridevirtual

Initializes this Sendable object.

Parameters
buildersendable builder

Implements wpi::Sendable.

◆ IsConnected()

bool frc::DutyCycleEncoder::IsConnected ( ) const

Get if the sensor is connected.

This 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.

Returns
true if the sensor is connected

◆ operator=()

DutyCycleEncoder & frc::DutyCycleEncoder::operator= ( DutyCycleEncoder &&  )
default

◆ Reset()

void frc::DutyCycleEncoder::Reset ( )

Reset the Encoder distance to zero.

◆ SetConnectedFrequencyThreshold()

void frc::DutyCycleEncoder::SetConnectedFrequencyThreshold ( int  frequency)

Change the frequency threshold for detecting connection used by IsConnected.

Parameters
frequencythe minimum frequency in Hz.

◆ SetDistancePerRotation()

void frc::DutyCycleEncoder::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
distancePerRotationthe distance per rotation of the encoder

◆ SetDutyCycleRange()

void frc::DutyCycleEncoder::SetDutyCycleRange ( double  min,
double  max 
)

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
minminimum duty cycle (0-1 range)
maxmaximum duty cycle (0-1 range)

◆ SetPositionOffset()

void frc::DutyCycleEncoder::SetPositionOffset ( double  offset)

Set the position offset.

This must be in the range of 0-1.

Parameters
offsetthe offset

The documentation for this class was generated from the following file: