Entradas digitales - Software

Nota

Esta sección cubre entradas digitales en software. Para obtener una guía de hardware para entradas digitales, consulte Entradas digitales - Hardware.

The roboRIO’s FPGA supports up to 26 digital inputs. 10 of these are made available through the built-in DIO ports on the RIO itself, while the other 16 are available through the MXP breakout port.

Digital inputs read one of two states - «high» or «low.» By default, the built-in ports on the RIO will read «high» due to internal pull-up resistors (for more information, see Entradas digitales - Hardware). Accordingly, digital inputs are most-commonly used with switches of some sort. Support for this usage is provided through the DigitalInput class (Java, C++).

La clase DigitalInput

Una entrada digital DigitalInput se puede inicializar de la siguiente manera:

// Initializes a DigitalInput on DIO 0
DigitalInput input = new DigitalInput(0);
// Initializes a DigitalInput on DIO 0
frc::DigitalInput input{0};

Leyendo el valor de DigitalInput

El estado de DigitalInput se puede consultar con el método get:

// Gets the value of the digital input.  Returns true if the circuit is open.
input.get();
// Gets the value of the digital input.  Returns true if the circuit is open.
input.Get();

Crear una DigitalInput a partir de un AnalogInput.

Nota

Un AnalogTrigger construido con un argumento de número de puerto puede compartir ese puerto analógico con un AnalogInput separado, pero dos objetos` AnalogInput `pueden no compartir el mismo puerto.

Sometimes, it is desirable to use an analog input as a digital input. This can be easily achieved using the AnalogTrigger class (Java, C++).

Un AnalogTrigger disparador análogo puede iniciarse como sigue. Al igual que con el AnalogPotentiometer, un AnalogInput se puede pasar una Entrada Analógica explícitamente si el usuario desea personalizar la configuración de muestreo:

// Initializes an AnalogTrigger on port 0
AnalogTrigger trigger0 = new AnalogTrigger(0);

// Initializes an AnalogInput on port 1 and enables 2-bit oversampling
AnalogInput input = new AnalogInput(1);
input.setAverageBits(2);

// Initializes an AnalogTrigger using the above input
AnalogTrigger trigger1 = new AnalogTrigger(input);
// Initializes an AnalogTrigger on port 0
frc::AnalogTrigger trigger0{0};

// Initializes an AnalogInput on port 1 and enables 2-bit oversampling
frc::AnalogInput input{1};
input.SetAverageBits(2);

// Initializes an AnalogTrigger using the above input
frc::AnalogTrigger trigger1{input};

Estableciendo los puntos del gatillo

Nota

Para obtener detalles sobre la escala de los valores de AnalogInput «sin procesar», consulte Entradas análogas - Software.

Para convertir la señal analógica a digital, es necesario especificar a qué valores habilitará y deshabilitará el disparador. Estos valores pueden ser diferentes para evitar «dithering» alrededor del punto de transición:

// Sets the trigger to enable at a raw value of 3500, and disable at a value of 1000
trigger.setLimitsRaw(1000, 3500);

// Sets the trigger to enable at a voltage of 4 volts, and disable at a value of 1.5 volts
trigger.setLimitsVoltage(1.5, 4);
// Sets the trigger to enable at a raw value of 3500, and disable at a value of 1000
trigger.SetLimitsRaw(1000, 3500);

// Sets the trigger to enable at a voltage of 4 volts, and disable at a value of 1.5 volts
trigger.SetLimitsVoltage(1.5, 4);

Uso de entradas digitales en el código

As almost all switches on the robot will be used through a DigitalInput. This class is extremely important for effective robot control.

Limitar el rango de movimiento de un mecanismo

Los mecanismos motorizados (como brazos y elevadores) utilizados en FRC® deben recibir algún tipo de «limit switch» para evitar que estos se dañen al final de su rango de movimiento. A continuación, se muestra un breve ejemplo:

Spark spark = new Spark(0);

// Limit switch on DIO 2
DigitalInput limit = new DigitalInput(2);

public void autonomousPeriodic() {
    // Runs the motor forwards at half speed, unless the limit is pressed
    if(!limit.get()) {
        spark.set(.5);
    } else {
        spark.set(0);
    }
}
// Motor for the mechanism
frc::Spark spark{0};

// Limit switch on DIO 2
frc::DigitalInput limit{2};

void AutonomousPeriodic() {
    // Runs the motor forwards at half speed, unless the limit is pressed
    if(!limit.Get()) {
        spark.Set(.5);
    } else {
        spark.Set(0);
    }
}

Dirigiendo un mecanismo

Limit switches are very important for being able to «home» a mechanism with an encoder. For an example of this, see Homing a Mechanism.