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.
wpi::AnalogPotentiometer pot{0, 180, 30};
Personalizando la entrada analógica subyacente¶
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 1
AnalogInput input = new AnalogInput(1);
// 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 pot1 = new AnalogPotentiometer(input, 180, 30);
// Initializes an AnalogInput on port 1
wpi::AnalogInput input{1};
// 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.
wpi::AnalogPotentiometer pot1{&input, 180, 30};
Leyendo valores de AnalogPotentiometer¶
El valor escalado se puede leer simplemente llamando al método get :
pot1.get();
// Get the value of the potentiometer
pot.Get();
Cómo usar potenciómetros analógicos en el código¶
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.
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.