Analog Potentiometers - Software¶
This section covers analog potentiometers in software. For a hardware guide to analog potentiometers, see Analog Potentiometers - Hardware.
Potentiometers are variable resistors that allow information about position to be converted into an analog voltage signal. This signal can be read by the roboRIO to control whatever device is attached to the potentiometer.
While it is possible to read information from a potentiometer directly with an Analog Inputs - Software, WPILib provides an
AnalogPotentiometer class (Java, C++) that handles re-scaling the values into meaningful units for the user. It is strongly encouraged to use this class.
In fact, the
AnalogPotentiometer name is something of a misnomer - this class should be used for the vast majority of sensors that return their signal as a simple, linearly-scaled analog voltage.
The AnalogPotentiometer class¶
The “full range” or “scale” parameters in the
AnalogPotentiometer constructor are scale factors from a range of 0-1 to the actual range, not from 0-5. That is, they represent a native fractional scale, rather than a voltage scale.
An AnalogPotentiometer can be initialized as follows:
Customizing the underlying AnalogInput¶
If the user changes the scaling of the
AnalogInput with oversampling, this must be reflected in the scale setting passed to the
If the user would like to apply custom settings to the underlying
AnalogInput used by the
AnalogPotentiometer, an alternative constructor may be used in which the
AnalogInput is injected:
Reading values from the AnalogPotentiometer¶
The scaled value can be read by simply calling the
Using AnalogPotentiometers in code¶
Analog sensors can be used in code much in the way other sensors that measure the same thing can be. If the analog sensor is a potentiometer measuring an arm angle, it can be used similarly to an encoder. If it is an ultrasonic sensor, it can be used similarly to other ultrasonics.
It is very important to keep in mind that actual, physical potentiometers generally have a limited range of motion. Safeguards should be present in both the physical mechanism and the code to ensure that the mechanism does not break the sensor by traveling past its maximum throw.