sonar#

Inicializando la clase de sonar#

Un telémetro se crea utilizando el siguiente constructor:

The sonar constructor creates a sonar object in the specified Three Wire Port.

Parámetro

Descripción

port

Un Puerto inteligente válido al que está conectado el telémetro.

// Create the Brain.
brain Brain;
// Construct a Range Finder "Sonar" with the
// sonar class.
sonar Sonar = sonar(PORT1);

This Sonar object will be used in all subsequent examples throughout this API documentation when referring to sonar class methods.

Métodos de clase#

distance()#

The distance(units) method returns the current distance the Range Finder is detecting on abject at. The Range Finder will return a large positive number if no object is detected in range.

Parámetros

Descripción

unidades

Una unidad de distancia válida.

Devuelve: Un doble que representa la distancia medida por el telémetro.

// Get the Range Finder distance in millimeters.
double value = Sonar.distance(mm);

// Print the current distance detected by the Range Finder to the
// Brain's screen.
Brain.Screen.print(value);

foundObject()#

The foundObject() method checks if an object is detected by the Range Finder in the range 0 - 1000 millimeters. The Range Finder will return true if an object is detected closer than 1000mm.

Returns: true if an object is detected by the Range Finder. false if one is not.

// Check if an object is 1000mm or closer to the Range Finder.
if (Sonar.foundObject()){
    // Print to screen on the brain that an object was found.
    Brain.Screen.print("object found");
}

setMaximum()#

The setMaximum(distance, units) method sets the maximum distance the foundObject() method will return true for.

Parámetros

Descripción

distancia

Un doble que representa la distancia máxima a la que el telémetro encontrará objetos.

unidades

Una unidad de distancia válida.

Devoluciones: Ninguna.

objectDetected()#

The objectDetected(callback) command calls a function when the Range Finder detects an object.

Parámetros

Descripción

llamar de vuelta

Una referencia a una función a llamar cuando se detecta un objeto.

Devoluciones: Ninguna.

// Define the detected function with a void return type,
// showing it doesn't return a value.
void detected() {
  // The Brain will print that the Range Finder detected 
  // an object on the Brain's screen.
  Brain.Screen.print("Range Finder Detected Object");
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Run detected when the Range Finder detect an object.
  Sonar.objectDetected(detected);
}

changed()#

Parámetros

Descripción

llamar de vuelta

Una función de devolución de llamada para ejecutarse cuando cambia el valor del telémetro.

Devoluciones: Ninguna.

// Define the sonarChanged function with a void return type,
// showing it doesn't return a value.
void sonarChanged() {
  // The Brain will print that the value detected by the
  // Range Finder changed on the Brain's screen.
  Brain.Screen.print("Range Finder value changed");
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Run sonarChanged when the value of the
  // Range Finder changes.
  Sonar.changed(sonarChanged);
}

timestamp()#

The timestamp() method requests the timestamp of the last received status packet from the Range Finder.

Devuelve: Marca de tiempo del último paquete de estado como un entero de 32 bits sin signo en milisegundos.

installed()#

The installed() command returns if the Range Finder is installed.

Devuelve: Un valor booleano que indica si el telémetro está instalado.