Potenciómetros analógicos - Software

Nota

Esta sección cubre el software del analizador de potenciómetros. Para una guía de hardware a potenciómetros analógicos, ver Potenciómetros análogos - Hardware.

Los potenciómetros son resistencias variables que permiten que la información sobre la posición se convierta en una señal de voltaje analógica. La roboRIO puede leer esta señal para controlar cualquier dispositivo conectado al potenciómetro.

While it is possible to read information from a potentiometer directly with an Entradas análogas - 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.

De hecho, el nombre AnalogPotentiometer es un nombre poco apropiado: esta clase se debe utilizar para la gran mayoría de los sensores que devuelven su señal como un voltaje analógico simple y de escala lineal.

La clase AnalogPotentiometer

Nota

Los parámetros de «rango completo» o «escala» en el constructor AnalogPotentiometer son factores de escala de un rango de 0-1 al rango real, no de 0-5. Es decir, representan una escala fraccional nativa, en lugar de una escala de voltaje.

Para inicializar la AnalogPotentiometer puede utilizar lo siguiente:

// Initializes an AnalogPotentiometer on analog port 0
// The full range of motion (in meaningful external units) is 0-180 (this could be degrees, for instance)
// The "starting point" of the motion, i.e. where the mechanism is located when the potentiometer reads 0v, is 30.

AnalogPotentiometer pot = new AnalogPotentiometer(0, 180, 30);
// Initializes an AnalogPotentiometer on analog port 0
// The full range of motion (in meaningful external units) is 0-180 (this could be degrees, for instance)
// The "starting point" of the motion, i.e. where the mechanism is located when the potentiometer reads 0v, is 30.

frc::AnalogPotentiometer pot{0, 180, 30};

Personalizando la entrada analógica subyacente

Nota

If the user changes the scaling of the AnalogInput with oversampling, this must be reflected in the scale setting passed to the AnalogPotentiometer.

Si el usuario desea aplicar configuraciones personalizadas a la entrada AnalogInput utilizada por el AnalogPotentiometer, se puede usar un constructor alternativo en el que se introduzca la entrada analógica:

// Initializes an AnalogInput on port 0, and enables 2-bit averaging
AnalogInput input = new AnalogInput(0);
input.setAverageBits(2);

// Initializes an AnalogPotentiometer with the given AnalogInput
// The full range of motion (in meaningful external units) is 0-180 (this could be degrees, for instance)
// The "starting point" of the motion, i.e. where the mechanism is located when the potentiometer reads 0v, is 30.

AnalogPotentiometer pot = new AnalogPotentiometer(input, 180, 30);
// Initializes an AnalogInput on port 0, and enables 2-bit averaging
frc::AnalogInput input{0};
input.SetAverageBits(2);

// Initializes an AnalogPotentiometer with the given AnalogInput
// The full range of motion (in meaningful external units) is 0-180 (this could be degrees, for instance)
// The "starting point" of the motion, i.e. where the mechanism is located when the potentiometer reads 0v, is 30.

frc::AnalogPotentiometer pot{input, 180, 30};

Leyendo valores de AnalogPotentiometer

El valor escalado se puede leer simplemente llamando al método get :

pot.get();
pot.Get();

Cómo usar potenciómetros analógicos en el código

Los sensores analógicos se pueden usar en código de la misma manera que otros sensores que miden lo mismo. Si el sensor analógico es un potenciómetro que mide el ángulo de un brazo, puede usarse de manera similar a un codificador.<encoders-software> Si es un sensor ultrasónico, puede usarse de manera similar a otros ultrasonidos.<ultrasonics-software>

Es muy importante tener en cuenta que los potenciómetros físicos reales generalmente tienen un rango de movimiento limitado. Las salvaguardas deben estar presentes tanto en el mecanismo físico como en el código para garantizar que el mecanismo no rompa el sensor al pasar su alcance máximo.