Object Sensor#

Introduction#

The Object Sensor is an infrared proximity sensor that detects when an object is close by using reflected IR light.

The VEX Object Sensor.

This page uses object_sensor as the example Object Sensor name. Replace it with your own configured name as needed.

Below is a list of available methods:

  • reflectivity – Returns the amount of light reflected from the object.

  • set_threshold – Sets the amount of reflected light needed before an object is considered to be detected.

  • is_object_detected – Returns whether the Object Sensor is detecting an object.

  • changed – Registers a function to be called whenever the Object Sensor’s value changes.

  • object_detected – Registers a function to be called whenever the Object Sensor detects a new object.

  • object_lost – Registers a function to be called whenever the Object Sensor loses a detected object.

Constructor – Manually initialize an Object Sensor.

reflectivity#

reflectivity returns the amount of light reflected from the object as a percent.

Note:

  • 0% reflectivity means the object is very dark. The sensor sees a raw value of 3000 or higher.

  • 100% reflectivity means the object is very bright. The sensor sees a raw value of 0.

Usage:
object_sensor.reflectivity()

Parameters

Description

This method has no parameters.

# Drive forward when the reflectivity is greater than 50%.
while True:
    if object_sensor.reflectivity() > 50:
        drivetrain.drive(FORWARD)
    else:
        drivetrain.stop()
    wait(5, MSEC)

set_threshold#

set_threshold sets the amount of reflected light needed as a percent before an object is considered to be detected.

Usage:
object_sensor.set_threshold(value)

Parameters

Description

value

The reflectivity value required before an object is considered to be detected, from 0 to 100.

# Set the reflectivity threshold to 50%.
object_sensor.set_threshold(50)
# Drive forward when the object sensor is detecting an object.
while True:
    if object_sensor.is_object_detected():
        drivetrain.drive(FORWARD)
    else:
        drivetrain.stop()
    wait(5, MSEC)

is_object_detected#

is_object_detected returns whether the Object Sensor is detecting an object.

  • True – The Object Sensor is detecting an object.

  • False – The Object Sensor is not detecting an object.

Usage:
object_sensor.is_object_detected()

Parameters

Description

This method has no parameters.

# Drive forward when the object sensor is detecting an object.
while True:
    if object_sensor.is_object_detected():
        drivetrain.drive(FORWARD)
    else:
        drivetrain.stop()
    wait(5, MSEC)

changed#

changed registers a function to be called whenever the Object Sensor’s value changes.

Usage:
object_sensor.changed(callback, arg)

Parameters

Description

callback

A previously defined function that executes when the Object Sensor’s value changes.

arg

Optional. A tuple containing arguments to pass to the callback function. See Functions with Parameters for more information.

def my_function():
  brain.screen.print("Reflectivity changed.")

# Call my_function whenever object_sensor's value changes
object_sensor.changed(my_function)

object_detected#

object_detected registers a function to be called whenever the Object Sensor detects a new object.

Usage:
object_sensor.object_detected(callback, arg)

Parameters

Description

callback

A previously defined function that executes when the Object Sensor detects a new object.

arg

Optional. A tuple containing arguments to pass to the callback function. See Functions with Parameters for more information.

def my_function():
  brain.screen.print("Object detected.")

# Call my_function whenever object_sensor detects an object
object_sensor.object_detected(my_function)

object_lost#

object_lost registers a function to be called whenever the Object Sensor loses a detected object.

Usage:
object_sensor.object_lost(callback, arg)

Parameters

Description

callback

A previously defined function that executes when the Object Sensor loses a detected object.

arg

Optional. A tuple containing arguments to pass to the callback function. See Functions with Parameters for more information.

def my_function():
  brain.screen.print("Object lost.")

# Call my_function whenever object_sensor loses an object
object_sensor.object_lost(my_function)

Constructors#

Constructors are used to manually create ObjectDetector objects, which are necessary for configuring Object Sensors.

ObjectDetector#

ObjectDetector creates an Object Sensor.

Usage:
ObjectDetector(port, reverse_det)

Parameter

Description

port

The 3-Wire Port that the Object Sensor is connected to:

  • On the V5 Brainbrain.three_wire_port.x where x is the number of the port.
  • On a 3-Wire Expanderexpander.a where expander is the name of the expander instance.

reverse_det

Optional. Whether to reverse the detection logic:

  • True – When an object is lost, return that an object is detected, and when an object is detected, return that it is lost.
  • False (default) – When an object is lost, return that an object is lost, and when an object is detected, return that it is detected.

# Create an Object Sensor in Port A
object_sensor = ObjectDetector(brain.three_wire_port.a)