codificador#
Inicializando la clase del codificador#
Un sensor codificador se crea utilizando el siguiente constructor:
The encoder
constructor creates an encoder object in the specified Three Wire Port:
Parámetro |
Descripción |
---|---|
|
The 3-Wire Port “pair” that the Encoder Sensor is connected to, whether it’s a port on the Brain, or a 3-Wire Expander. |
Primero se debe crear un Brain o un 3-Wire Expander antes de poder usarlos para crear un objeto con el constructor de la clase Encoder.
// Create the Brain.
brain Brain;
// Construct an Encoder Sensor "Encoder" with the
// encoder class.
encoder Encoder = encoder(Brain.ThreeWirePort.A);
This Encoder
object will be used in all subsequent examples throughout this API documentation when referring to encoder class methods.
Métodos de clase#
resetRotation()#
The resetRotation()
method resets the rotation of the Encoder Sensor to 0.
Devoluciones: Ninguna.
setRotation()#
The setRotation(value, units)
method sets the rotation of the Encoder Sensor.
Parámetros |
Descripción |
---|---|
valor |
El valor de rotación a establecer. |
unidades |
Una rotationUnit válida. |
Devoluciones: Ninguna.
// Set the Encoder Sensor's value of rotation to 180 degrees.
Encoder.setRotation(180);
setPosition()#
The setPosition(value, units)
method sets the position of the Encoder Sensor.
Parámetros |
Descripción |
---|---|
valor |
El valor de rotación a establecer. |
unidades |
Una rotationUnit válida. |
Devoluciones: Ninguna.
// Set the Encoder Sensor's value of position to 180 degrees.
Encoder.setPosition(180);
rotation()#
The rotation(units)
method returns the rotation value of the Encoder Sensor.
Parámetros |
Descripción |
---|---|
unidades |
Una rotationUnit válida. |
Devuelve: Un doble que representa la rotación actual del codificador.
// Get the current encoder rotation.
double value = Encoder.rotation();
position()#
The position(units)
method returns the current position of the Encoder Sensor.
Parámetros |
Descripción |
---|---|
unidades |
Una rotationUnit válida. |
Devuelve: El valor de la posición actual del sensor codificador en las unidades especificadas.
// Get the current Encoder Sensor's position.
double value = Encoder.position();
velocity()#
The velocity(units)
method returns the current velocity of the Encoder Sensor.
Parámetros |
Descripción |
---|---|
unidades |
Una velocityUnit válida. |
Devuelve: Un doble que representa el valor de la velocidad actual del sensor del codificador en las unidades especificadas.
// Get the current Encoder Sensor's velocity in rpm.
double value = Encoder.velocity();
changed()#
The changed(callback)
method registers a function to be called when the value of the Encoder Sensor changes.
Parámetros |
Descripción |
---|---|
llamar de vuelta |
Una función que se llamará cuando cambie el valor del eje del sensor codificador. |
Devuelve: Una instancia de la clase Event.
// Define the encoderChanged function with a void return
// type, showing it doesn't return a value.
void encoderChanged() {
// The Brain will print that the Encoder Sensor changed on
// the Brain's screen.
Brain.Screen.print("encoder changed");
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Drive the robot forward.
Drivetrain.drive(forward);
// Run encoderChanged when the value of the
// Encoder Sensor changes.
Encoder.changed(encoderChanged);
}