VEX Optical Sensor C++ API module

Contents

Files

file optical.hpp

Classes

class pros::v5::Optical

Functions

Optical(const std::uint8_t port)
Creates an Optical Sensor object for the given port.
Optical(const Device& device)
static std::vector<Optical> get_all_devices()
Gets all optical sensors.
double get_hue() virtual
Get the detected color hue.
double get_saturation() virtual
Get the detected color saturation.
double get_brightness() virtual
Get the detected color brightness.
std::int32_t get_proximity() virtual
Get the detected proximity value.
std::int32_t set_led_pwm(uint8_t value) virtual
Set the pwm value of the White LED on the sensor.
std::int32_t get_led_pwm() virtual
Get the pwm value of the White LED on the sensor.
pros::c::optical_rgb_s_t get_rgb() virtual
Get the processed RGBC data from the sensor.
pros::c::optical_raw_s_t get_raw() virtual
Get the raw un-processed RGBC data from the sensor.
pros::c::optical_direction_e_t get_gesture() virtual
Get the most recent gesture data from the sensor.
pros::c::optical_gesture_s_t get_gesture_raw() virtual
Get the most recent raw gesture data from the sensor.
std::int32_t enable_gesture() virtual
Enable gesture detection on the sensor.
std::int32_t disable_gesture() virtual
Disable gesture detection on the sensor.
double get_integration_time()
Set integration time (update rate) of the optical sensor in milliseconds, with minimum time being 3 ms and maximum time being 712 ms.
std::int32_t set_integration_time(double time)
Get integration time (update rate) of the optical sensor in milliseconds.

Function documentation

Optical(const std::uint8_t port)

Creates an Optical Sensor object for the given port.

Parameters
port The V5 port number from 1-21

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as an Optical Sensor

Example: pros::Optical optical(1);

Optical(const Device& device)

static std::vector<Optical> get_all_devices()

Gets all optical sensors.

Returns A vector of Optical sensor objects.

Example

void opcontrol() {
  std::vector<Optical> optical_all = pros::Optical::get_all_devices();  // All optical sensors that are connected
}

double get_hue() virtual

Get the detected color hue.

Returns hue value if the operation was successful or PROS_ERR_F if the operation failed, setting errno.

This is not available if gestures are being detected. Hue has a range of 0 to 359.999

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as an Optical Sensor

Example:

void opcontrol() {
        pros::Optical optical(1);
        std::cout << "Hue: " << optical.get_hue() << std::endl;
}

double get_saturation() virtual

Get the detected color saturation.

Returns saturation value if the operation was successful or PROS_ERR_F if the operation failed, setting errno.

This is not available if gestures are being detected. Saturation has a range of 0 to 1.0

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as an Optical Sensor

Example:

void opcontrol() {
        pros::Optical optical(1);
        std::cout << "Saturation: " << optical.get_saturation() << std::endl;
}

double get_brightness() virtual

Get the detected color brightness.

Returns brightness value if the operation was successful or PROS_ERR_F if the operation failed, setting errno.

This is not available if gestures are being detected. Brightness has a range of 0 to 1.0

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as an Optical Sensor

Example:

void opcontrol() {
        pros::Optical optical(1);
        std::cout << "Brightness: " << optical.get_brightness() << std::endl;
}

std::int32_t get_proximity() virtual

Get the detected proximity value.

Returns Proximity value if the operation was successful or PROS_ERR if the operation failed, setting errno.

This is not available if gestures are being detected. proximity has a range of 0 to 255.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as an Optical Sensor

Example:

void opcontrol() {
        pros::Optical optical(1);
        std::cout << "Proximity: " << optical.get_proximity() << std::endl;
}

std::int32_t set_led_pwm(uint8_t value) virtual

Set the pwm value of the White LED on the sensor.

Returns The Error code encountered or PROS_SUCCESS.

value that ranges from 0 to 100

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as an Optical Sensor

Example:

void initialize() {
        pros::Optical optical(1);
        optical.set_led_pwm(100);
}

std::int32_t get_led_pwm() virtual

Get the pwm value of the White LED on the sensor.

Returns LED pwm value if the operation was successful or PROS_ERR if the operation failed, setting errno.

value that ranges from 0 to 100

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as an Optical Sensor

Example:

void opcontrol() {
        pros::Optical optical(1);
    optical.set_led_pwm(100);
        std::cout << "LED PWM: " << optical.get_led_pwm() << std::endl;
}

pros::c::optical_rgb_s_t get_rgb() virtual

Get the processed RGBC data from the sensor.

Returns rgb value if the operation was successful or an optical_rgb_s_t with all fields set to PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as an Optical Sensor

Example:

void opcontrol() {
        pros::Optical optical(1);
        pros::c::optical_rgb_s_t rgb = optical.get_rgb();
        while(1) {
            std::cout << "Red: " << rgb.red << std::endl;
            std::cout << "Green: " << rgb.green << std::endl;
            std::cout << "Blue: " << rgb.blue << std::endl;
            std::cout << "Brightness: " << rgb.brightness << std::endl;
            pros::delay(20);
        }
}

pros::c::optical_raw_s_t get_raw() virtual

Get the raw un-processed RGBC data from the sensor.

Returns raw rgb value if the operation was successful or an optical_raw_s_t with all fields set to PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as an Optical Sensor

Example:

void opcontrol() {
        pros::Optical optical(1);
        pros::c::optical_raw_s_t raw = optical.get_raw();
        while (1) {
            std::cout << "Red: " << raw.red << std::endl;
            std::cout << "Green: " << raw.green << std::endl;
            std::cout << "Blue: " << raw.blue << std::endl;
            std::cout << "Clear: " << raw.clear << std::endl;
            pros::delay(20);
        }
}

pros::c::optical_direction_e_t get_gesture() virtual

Get the most recent gesture data from the sensor.

Returns gesture value if the operation was successful or PROS_ERR if the operation failed, setting errno.

Gestures will be cleared after 500mS

0 = no gesture, 1 = up (towards cable), 2 = down, 3 = right, 4 = left

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as an Optical Sensor

Example:

void opcontrol() {
        pros::Optical optical(1);
        while(1) {
            std::cout << "Gesture: " << optical.get_gesture() << std::endl;
            pros::delay(20);
        }
}

pros::c::optical_gesture_s_t get_gesture_raw() virtual

Get the most recent raw gesture data from the sensor.

Returns gesture value if the operation was successful or an optical_gesture_s_t with all fields set to PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as an Optical Sensor

Example:

void opcontrol() {
        pros::Optical optical(1);
    optical.enable_gesture();
        while(1) {
            pros::c::optical_gesture_s_t gesture = optical.get_gesture_raw();
            std::cout << "Gesture raw data: " << std::endl;
            std::cout << "Up data: " << gesture.udata << std::endl;
            std::cout << "Down data: " << gesture.ddata << std::endl;
            std::cout << "Left data: " << gesture.ldata << std::endl;
            std::cout << "Right data: " << gesture.rdata << std::endl;
            std::cout << "Type: " << gesture.type << std::endl;
            std::cout << "Count: " << gesture.count << std::endl;
            std::cout << "Time: " << gesture.time << std::endl;
            pros::delay(20);
        }
}

std::int32_t enable_gesture() virtual

Enable gesture detection on the sensor.

Returns 1 if the operation is successful or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as an Optical Sensor

Example:

void opcontrol() {
        pros::Optical optical(1);
    optical.enable_gesture();
        while(1) {
            pros::c::optical_gesture_s_t gesture = optical.get_gesture_raw();
            std::cout << "Gesture raw data: " << std::endl;
            std::cout << "Up data: " << gesture.udata << std::endl;
            std::cout << "Down data: " << gesture.ddata << std::endl;
            std::cout << "Left data: " << gesture.ldata << std::endl;
            std::cout << "Right data: " << gesture.rdata << std::endl;
            std::cout << "Type: " << gesture.type << std::endl;
            std::cout << "Count: " << gesture.count << std::endl;
            std::cout << "Time: " << gesture.time << std::endl;
            pros::delay(20);
        }
}

std::int32_t disable_gesture() virtual

Disable gesture detection on the sensor.

Returns 1 if the operation is successful or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as an Optical Sensor

Example:

void opcontrol() {
        pros::Optical optical(1);
    optical.enable_gesture();
        while(1) {
            if(optical.get_gesture() != 0) {
                std::cout << "Gesture detected!"<< std::endl;
                optical.disable_gesture();
            }
            pros::delay(20);
        }
}

double get_integration_time()

Set integration time (update rate) of the optical sensor in milliseconds, with minimum time being 3 ms and maximum time being 712 ms.

Returns 1 if the operation is successful or PROS_ERR_F if the operation failed, setting errno.

Default is 100 ms, with the optical sensor communciating with the V5 brain every 20 ms.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as an Optical Sensor

std::int32_t set_integration_time(double time)

Get integration time (update rate) of the optical sensor in milliseconds.

Parameters
time The desired integration time in milliseconds
Returns Integration time in milliseconds if the operation is successful or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as an Optical Sensor