objectdetector#

Initializing the objectdetector Class#

An Object Sensor is created by using the following constructor:

The objectdetector constructor creates an objectdetector object in the specified Three Wire Port with the reverse flag set to the specified value.

Parameter

Description

port

The 3-Wire Port that the Object Sensor is connected to, whether it’s a port on the Brain, or a 3-Wire Expander.

reverse

Set this to reverse the detection logic. The default is false.

A Brain or 3-Wire Expander must be created first before they can be used to create an object with the objectdetector Class constructor.

// Create the Brain.
brain Brain;
// Construct an ObjectSensor "objdet" with the
// objectdetector class.
objectdetector objdet = objectdetector(Brain.ThreeWirePort.A, true);

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

Class Methods#

reflectivity()#

The reflectivity(units) method returns the reflectivity measured by the Object Sensor. The reflectivity is an estimation based on the raw value of the sensor.

A reflectivity of 0% is a raw value of 3000 or greater. A reflectivity of 100% is a raw value of 0.

Parameters

Description

units

The only valid unit for reflectivity is percent.

Returns: An integer representing reflectivity measured by the Object Sensor in the range 0% - 100%.

// Get Object Sensor reflectivity in range of 0% - 100%.
int value = ObjectDetectorA.reflectivity();

// Print the reflectivity to the Brain's screen.
Brain.Screen.print(value);

setDetectionThreshold()#

The setDetectionThreshold(value, units) method sets the detection threshold for the Object Sensor to a specific value.

Parameters

Description

value

The reflectivity threshold for object detection. In the range 0 - 100.

units

The only valid unit for reflectivity is percent.

Returns: None.

// Set the reflectivity threhold to 50%.
objdet.setDetectionThreshold(50);

isObjectDetected()#

The isObjectDetected() method returns if an object is detected.

Returns: true if an object is detected. false if one is not.

changed()#

The changed(callback) method registers a callback function for when the value of an Object Sensor changes.

Parameters

Description

callback

The callback function to be called when the value of an Object Sensor changes.

Returns: None.

// Define the detectorChanged function with a void return
// type, showing it doesn't return a value.
void detectorChanged() {
  // The Brain will print that the value of the Object Sensor
  // changed on the Brain's screen.
  Brain.Screen.print("Object Sensor value changed");
}

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

  // Run detectorChanged when the value of the Object Sensor changes.
  objdet.changed(detectorChanged);
}

objectDetected()#

The objectDetected(callback) method registers a callback function for when an object is detected.

Parameters

Description

callback

The callback function to be called when an object is detected.

Returns: None.

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

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

  // Run detected when the Object Sensor detects an object.
  objdet.objectDetected(detected);
}

objectLost()#

The objectLost(callback) method registers a callback function for when an object is lost.

This is a non-blocking method and allows the next method to run without delay.

Parameters

Description

callback

The callback function to be called when an object is lost.

Returns: None.

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

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

  // Run lost when the Object Sensor loses an object.
  objdet.objectLost(lost);
}