Object Sensor#

Introduction#

The objectdetector class is used to measure reflectivity and detect objects by the Object Sensor.

Class Constructor#

objectdetector( 
  triport::port &port, 
  bool           bReverse = false );

Class Destructor#

Destroys the objectdetector object and releases associated resources.

virtual ~objectdetector();

Parameters#

Parameter

Type

Description

port

triport::port &

The 3-Wire Port that the Object Sensor is connected to, written as Brain.ThreeWirePort.X or ExpanderName.X, where X is the port letter (for example, Brain.ThreeWirePort.A or Expander1.A).

bReverse

bool

An optional boolean to reverse the direction of the Object Sensor:

  • false (default) — Do not reverse the direction
  • true — Reverse the direction

Examples#

// Create an objectdetector instance in Port A
objectdetector ObjectSensorA = objectdetector(Brain.ThreeWirePort.A, true);

Member Functions#

The objectdetector class includes the following member functions:

  • reflectivity — Returns the reflectivity detected by the Object Sensor.

  • isObjectDetected — Returns whether the Object Sensor detects an object.

  • setDetectionThreshold — Sets the detection range of the Object Sensor.

  • objectDetected — Registers a function to be called when the Object Sensor detects an object.

  • objectLost — Registers a function to be called when the Object Sensor loses an object.

Before calling any objectdetector member functions, a objectdetector instance must be created, as shown below:

// Create an objectdetector instance in Port A
objectdetector ObjectSensorA = objectdetector(Brain.ThreeWirePort.A, true);

reflectivity#

Returns the reflectivity measured by the Object Sensor.

Available Functions
int32_t reflectivity( 
  percentUnits units = percentUnits::pct );

Parameters

Parameters

Type

Description

units

percentUnits

The unit that represents the reflectivity:

  • percent / pct — percent

Return Values

Returns an int32_t representing the reflectivity measured by the Object Detector as a percent in the range 0 - 100%.

Examples
// Display the detected reflectivity on the Brain's screen
int32_t value = ObjectSensorA.reflectivity();
Brain.Screen.print(value);

isObjectDetected#

Returns whether an object is currently detected by the Object Detector.

Available Functions
bool isObjectDetected();

Parameters

This function does not take any parameters.

Return Values

Returns a Boolean indicating whether an object is detected:

  • true — An object is detected.
  • false — No object is detected.

setDetectionThreshold#

Sets the detection threshold for the Object Detector to a specific value.

Available Functions
void setDetectionThreshold( 
  int32_t      threshold, 
  percentUnits units = percentUnits::pct );

Parameters

Parameters

Type

Description

threshold

int32_t

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

units

percentUnits

The unit that represents the new threshold:

  • percent / pct — percent

Return Values

This function does not return a value.

Examples
// Set the reflectivity threshold to 50%
ObjectSensorA.setDetectionThreshold(50);

objectDetected#

Registers a callback function for when an object is detected.

Available Functions
void objectDetected(void (* callback)(void));

Parameters

Parameter

Type

Description

callback

void (*)(void)

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

Return Values

This function does not return a value.

Examples
void detected() {
  Brain.Screen.print("object detected");
}

// Run detected when the Object Detector detects an object
ObjectSensorA.objectDetected(detected);

objectLost#

Registers a callback function for when an object is lost.

Available Functions
void objectLost(void (* callback)(void));

Parameters

Parameter

Type

Description

callback

void (*)(void)

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

Return Values

This function does not return a value.

Examples
void lost() {
  Brain.Screen.print("object lost");
}

// Run lost when the Object Detector loses an object
ObjectSensorA.objectLost(lost);