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 |
|---|---|---|
|
|
The 3-Wire Port that the Object Sensor is connected to, written as |
|
|
An optional boolean to reverse the direction of the Object Sensor:
|
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 Functionsint32_t reflectivity(
percentUnits units = percentUnits::pct );
Parameters |
Type |
Description |
|---|---|---|
|
|
The unit that represents the reflectivity:
|
Returns an int32_t representing the reflectivity measured by the Object Detector as a percent in the range 0 - 100%.
// 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 Functionsbool isObjectDetected();
This function does not take any parameters.
Return ValuesReturns 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 Functionsvoid setDetectionThreshold(
int32_t threshold,
percentUnits units = percentUnits::pct );
Parameters |
Type |
Description |
|---|---|---|
|
|
The reflectivity threshold for object detection, in the range 0 - 100. |
|
|
The unit that represents the new threshold:
|
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 Functionsvoid objectDetected(void (* callback)(void));
Parameter |
Type |
Description |
|---|---|---|
|
|
The callback function to be called when an object is detected. |
This function does not return a value.
Examplesvoid 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 Functionsvoid objectLost(void (* callback)(void));
Parameter |
Type |
Description |
|---|---|---|
|
|
The callback function to be called when an object is lost. |
This function does not return a value.
Examplesvoid lost() {
Brain.Screen.print("object lost");
}
// Run lost when the Object Detector loses an object
ObjectSensorA.objectLost(lost);