WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
frc::AnalogInput Class Reference

Analog input class. More...

#include <frc/AnalogInput.h>

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

Public Member Functions

 AnalogInput (int channel)
 Construct an analog input.
 
 AnalogInput (AnalogInput &&)=default
 
AnalogInputoperator= (AnalogInput &&)=default
 
 ~AnalogInput () override=default
 
int GetValue () const
 Get a sample straight from this channel.
 
int GetAverageValue () const
 Get a sample from the output of the oversample and average engine for this channel.
 
double GetVoltage () const
 Get a scaled sample straight from this channel.
 
double GetAverageVoltage () const
 Get a scaled sample from the output of the oversample and average engine for this channel.
 
int GetChannel () const
 Get the channel number.
 
void SetAverageBits (int bits)
 Set the number of averaging bits.
 
int GetAverageBits () const
 Get the number of averaging bits previously configured.
 
void SetOversampleBits (int bits)
 Set the number of oversample bits.
 
int GetOversampleBits () const
 Get the number of oversample bits previously configured.
 
int GetLSBWeight () const
 Get the factory scaling least significant bit weight constant.
 
int GetOffset () const
 Get the factory scaling offset constant.
 
void SetSimDevice (HAL_SimDeviceHandle device)
 Indicates this input is used by a simulated device.
 
void InitSendable (wpi::SendableBuilder &builder) override
 Initializes this Sendable object.
 
- Public Member Functions inherited from wpi::Sendable
virtual constexpr ~Sendable ()=default
 
- Public Member Functions inherited from wpi::SendableHelper< AnalogInput >
constexpr SendableHelper (const SendableHelper &rhs)=default
 
constexpr SendableHelper (SendableHelper &&rhs)
 
constexpr SendableHelperoperator= (const SendableHelper &rhs)=default
 
constexpr SendableHelperoperator= (SendableHelper &&rhs)
 

Static Public Member Functions

static void SetSampleRate (double samplesPerSecond)
 Set the sample rate per channel for all analog channels.
 
static double GetSampleRate ()
 Get the current sample rate for all channels.
 

Additional Inherited Members

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

Detailed Description

Analog input class.

Connected to each analog channel is an averaging and oversampling engine. This engine accumulates the specified ( by SetAverageBits() and SetOversampleBits() ) number of samples before returning a new value. This is not a sliding window average. The only difference between the oversampled samples and the averaged samples is that the oversampled samples are simply accumulated effectively increasing the resolution, while the averaged samples are divided by the number of samples to retain the resolution, but get more stable values.

Constructor & Destructor Documentation

◆ AnalogInput() [1/2]

frc::AnalogInput::AnalogInput ( int channel)
explicit

Construct an analog input.

Parameters
channelThe channel number on the roboRIO to represent. 0-3 are on-board 4-7 are on the MXP port.

◆ AnalogInput() [2/2]

frc::AnalogInput::AnalogInput ( AnalogInput && )
default

◆ ~AnalogInput()

frc::AnalogInput::~AnalogInput ( )
overridedefault

Member Function Documentation

◆ GetAverageBits()

int frc::AnalogInput::GetAverageBits ( ) const

Get the number of averaging bits previously configured.

This gets the number of averaging bits from the FPGA. The actual number of averaged samples is 2^bits. The averaging is done automatically in the FPGA.

Returns
Number of bits of averaging previously configured.

◆ GetAverageValue()

int frc::AnalogInput::GetAverageValue ( ) const

Get a sample from the output of the oversample and average engine for this channel.

The sample is 12-bit + the bits configured in SetOversampleBits(). The value configured in SetAverageBits() will cause this value to be averaged 2**bits number of samples.

This is not a sliding window. The sample will not change until 2**(OversampleBits + AverageBits) samples have been acquired from the module on this channel.

Use GetAverageVoltage() to get the analog value in calibrated units.

Returns
A sample from the oversample and average engine for this channel.

◆ GetAverageVoltage()

double frc::AnalogInput::GetAverageVoltage ( ) const

Get a scaled sample from the output of the oversample and average engine for this channel.

The value is scaled to units of Volts using the calibrated scaling data from GetLSBWeight() and GetOffset().

Using oversampling will cause this value to be higher resolution, but it will update more slowly.

Using averaging will cause this value to be more stable, but it will update more slowly.

Returns
A scaled sample from the output of the oversample and average engine for this channel.

◆ GetChannel()

int frc::AnalogInput::GetChannel ( ) const

Get the channel number.

Returns
The channel number.

◆ GetLSBWeight()

int frc::AnalogInput::GetLSBWeight ( ) const

Get the factory scaling least significant bit weight constant.

Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)

Returns
Least significant bit weight.

◆ GetOffset()

int frc::AnalogInput::GetOffset ( ) const

Get the factory scaling offset constant.

Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)

Returns
Offset constant.

◆ GetOversampleBits()

int frc::AnalogInput::GetOversampleBits ( ) const

Get the number of oversample bits previously configured.

This gets the number of oversample bits from the FPGA. The actual number of oversampled values is 2^bits. The oversampling is done automatically in the FPGA.

Returns
Number of bits of oversampling previously configured.

◆ GetSampleRate()

static double frc::AnalogInput::GetSampleRate ( )
static

Get the current sample rate for all channels.

Returns
Sample rate.

◆ GetValue()

int frc::AnalogInput::GetValue ( ) const

Get a sample straight from this channel.

The sample is a 12-bit value representing the 0V to 3.3V range of the A/D converter in the module. The units are in A/D converter codes. Use GetVoltage() to get the analog value in calibrated units.

Returns
A sample straight from this channel.

◆ GetVoltage()

double frc::AnalogInput::GetVoltage ( ) const

Get a scaled sample straight from this channel.

The value is scaled to units of Volts using the calibrated scaling data from GetLSBWeight() and GetOffset().

Returns
A scaled sample straight from this channel.

◆ InitSendable()

void frc::AnalogInput::InitSendable ( wpi::SendableBuilder & builder)
overridevirtual

Initializes this Sendable object.

Parameters
buildersendable builder

Implements wpi::Sendable.

◆ operator=()

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

◆ SetAverageBits()

void frc::AnalogInput::SetAverageBits ( int bits)

Set the number of averaging bits.

This sets the number of averaging bits. The actual number of averaged samples is 2^bits.

Use averaging to improve the stability of your measurement at the expense of sampling rate. The averaging is done automatically in the FPGA.

Parameters
bitsNumber of bits of averaging.

◆ SetOversampleBits()

void frc::AnalogInput::SetOversampleBits ( int bits)

Set the number of oversample bits.

This sets the number of oversample bits. The actual number of oversampled values is 2^bits. Use oversampling to improve the resolution of your measurements at the expense of sampling rate. The oversampling is done automatically in the FPGA.

Parameters
bitsNumber of bits of oversampling.

◆ SetSampleRate()

static void frc::AnalogInput::SetSampleRate ( double samplesPerSecond)
static

Set the sample rate per channel for all analog channels.

The maximum rate is 500kS/s divided by the number of channels in use. This is 62500 samples/s per channel.

Parameters
samplesPerSecondThe number of samples per second.

◆ SetSimDevice()

void frc::AnalogInput::SetSimDevice ( HAL_SimDeviceHandle device)

Indicates this input is used by a simulated device.

Parameters
devicesimulated device handle

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