Vision Sensor C++ API module

Contents

Files

file vision.hpp

Classes

class pros::v5::Vision

Functions

Vision(std::uint8_t port, vision_zero_e_t zero_point = E_VISION_ZERO_TOPLEFT)
Create a Vision Sensor object on the given port.
Vision(const Device& device)
std::int32_t clear_led(void) const
Clears the vision sensor LED color, reseting it back to its default behavior, displaying the most prominent object signature color.
static vision_signature_s_t signature_from_utility(const std::int32_t id, const std::int32_t u_min, const std::int32_t u_max, const std::int32_t u_mean, const std::int32_t v_min, const std::int32_t v_max, const std::int32_t v_mean, const float range, const std::int32_t type)
Creates a signature from the vision sensor utility.
vision_color_code_t create_color_code(const std::uint32_t sig_id1, const std::uint32_t sig_id2, const std::uint32_t sig_id3 = 0, const std::uint32_t sig_id4 = 0, const std::uint32_t sig_id5 = 0) const
Creates a color code that represents a combination of the given signature IDs.
static std::vector<Vision> get_all_devices()
Gets all vision sensors.
vision_object_s_t get_by_size(const std::uint32_t size_id) const
Gets the nth largest object according to size_id.
vision_object_s_t get_by_sig(const std::uint32_t size_id, const std::uint32_t sig_id) const
Gets the nth largest object of the given signature according to size_id.
vision_object_s_t get_by_code(const std::uint32_t size_id, const vision_color_code_t color_code) const
Gets the nth largest object of the given color code according to size_id.
std::int32_t get_exposure(void) const
Gets the exposure parameter of the Vision Sensor.
std::int32_t get_object_count(void) const
Gets the number of objects currently detected by the Vision Sensor.
vision_signature_s_t get_signature(const std::uint8_t signature_id) const
Gets the object detection signature with the given id number.
std::int32_t get_white_balance(void) const
Get the white balance parameter of the Vision Sensor.
std::int32_t read_by_size(const std::uint32_t size_id, const std::uint32_t object_count, vision_object_s_t*const object_arr) const
Reads up to object_count object descriptors into object_arr.
std::int32_t read_by_sig(const std::uint32_t size_id, const std::uint32_t sig_id, const std::uint32_t object_count, vision_object_s_t*const object_arr) const
Reads up to object_count object descriptors into object_arr.
int32_t read_by_code(const std::uint32_t size_id, const vision_color_code_t color_code, const std::uint32_t object_count, vision_object_s_t*const object_arr) const
Reads up to object_count object descriptors into object_arr.
static std::int32_t print_signature(const vision_signature_s_t sig)
Prints the contents of the signature as an initializer list to the terminal.
std::int32_t set_auto_white_balance(const std::uint8_t enable) const
Enables/disables auto white-balancing on the Vision Sensor.
std::int32_t set_exposure(const std::uint8_t exposure) const
Sets the exposure parameter of the Vision Sensor.
std::int32_t set_led(const std::int32_t rgb) const
Sets the vision sensor LED color, overriding the automatic behavior.
std::int32_t set_signature(const std::uint8_t signature_id, vision_signature_s_t*const signature_ptr) const
Stores the supplied object detection signature onto the vision sensor.
std::int32_t set_white_balance(const std::int32_t rgb) const
Sets the white balance parameter of the Vision Sensor.
std::int32_t set_zero_point(vision_zero_e_t zero_point) const
Sets the (0,0) coordinate for the Field of View.
std::int32_t set_wifi_mode(const std::uint8_t enable) const
Sets the Wi-Fi mode of the Vision sensor.
static Vision get_vision()
Gets a vision sensor that is plugged in to the brain.

Function documentation

Vision(std::uint8_t port, vision_zero_e_t zero_point = E_VISION_ZERO_TOPLEFT)

Create a Vision Sensor object on the given port.

Parameters
port The V5 port number from 1-21
zero_point One of vision_zero_e_t to set the (0,0) coordinate for the FOV

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 a vision sensor

Example

void opcontrol() {
  pros::Vision vision_sensor(1); // Creates a vision sensor on port one, with the zero point set to top left
}

Vision(const Device& device)

std::int32_t clear_led(void) const

Clears the vision sensor LED color, reseting it back to its default behavior, displaying the most prominent object signature color.

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

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor

Example

void initialize() {
  pros::Vision vision_sensor(1);
  vision_sensor.clear_led();
}

static vision_signature_s_t signature_from_utility(const std::int32_t id, const std::int32_t u_min, const std::int32_t u_max, const std::int32_t u_mean, const std::int32_t v_min, const std::int32_t v_max, const std::int32_t v_mean, const float range, const std::int32_t type)

Creates a signature from the vision sensor utility.

Parameters
id The signature ID
u_min Minimum value on U axis
u_max Maximum value on U axis
u_mean Mean value on U axis
v_min Minimum value on V axis
v_max Maximum value on V axis
v_mean Mean value on V axis
range
type Signature type
Returns A vision_signature_s_t that can be set using Vision::set_signature

Example

#define VISION_PORT 1
#define EXAMPLE_SIG 1

void opcontrol() {
pros::Vision vision_sensor(VISION_PORT);
// values acquired from the vision utility
vision_signature_s_t RED_SIG =
  vision_signature_from_utility(EXAMPLE_SIG, 8973, 11143, 10058, -2119, -1053, -1586, 5.4, 0);
vision_sensor.set_signature(EXAMPLE_SIG, &RED_SIG);
while (true) {
  vision_signature_s_t rtn = vision_sensor.get_by_sig(VISION_PORT, 0, EXAMPLE_SIG);
  // Gets the largest object of the EXAMPLE_SIG signature
  printf("sig: %d", rtn.signature);
  // Prints "sig: 1"
  delay(2);
  }
}

vision_color_code_t create_color_code(const std::uint32_t sig_id1, const std::uint32_t sig_id2, const std::uint32_t sig_id3 = 0, const std::uint32_t sig_id4 = 0, const std::uint32_t sig_id5 = 0) const

Creates a color code that represents a combination of the given signature IDs.

Parameters
sig_id1 The first signature id [1-7] to add to the color code
sig_id2 The second signature id [1-7] to add to the color code
sig_id3 The third signature id [1-7] to add to the color code
sig_id4 The fourth signature id [1-7] to add to the color code
sig_id5 The fifth signature id [1-7] to add to the color code
Returns A vision_color_code_t object containing the color code information.

This function uses the following values of errno when an error state is reached: EINVAL - Fewer than two signatures have been provided or one of the signatures is out of its [1-7] range (or 0 when omitted).

Example

#define VISION_PORT 1
#define EXAMPLE_SIG 1
#define OTHER_SIG 2

void opcontrol() {
  pros::Vision vision_sensor(VISION_PORT);
  vision_color_code_t code1 = vision_sensor.create_color_code(EXAMPLE_SIG, OTHER_SIG);
}

static std::vector<Vision> get_all_devices()

Gets all vision sensors.

Returns A vector of Vision sensor objects.

Example

void opcontrol() {
  std::vector<Vision> vision_all = pros::Vision::get_all_devices();  // All vision sensors that are connected
}

vision_object_s_t get_by_size(const std::uint32_t size_id) const

Gets the nth largest object according to size_id.

Parameters
size_id The object to read from a list roughly ordered by object size (0 is the largest item, 1 is the second largest, etc.)
Returns The vision_object_s_t object corresponding to the given size id, or PROS_ERR if an error occurred.

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor EDOM - size_id is greater than the number of available objects. EAGAIN - Reading the vision sensor failed for an unknown reason.

Example

#define VISION_PORT 1

void opcontrol() {
pros::Vision vision_sensor(VISION_PORT);
while (true) {
  vision_object_s_t rtn = vision_sensor.get_by_size(0);
  // Gets the largest object
  printf("sig: %d", rtn.signature);
  delay(2);
  }
}

vision_object_s_t get_by_sig(const std::uint32_t size_id, const std::uint32_t sig_id) const

Gets the nth largest object of the given signature according to size_id.

Parameters
size_id The object to read from a list roughly ordered by object size (0 is the largest item, 1 is the second largest, etc.)
sig_id
Returns The vision_object_s_t object corresponding to the given signature and size_id, or PROS_ERR if an error occurred.

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor EDOM - size_id is greater than the number of available objects. EINVAL - sig_id is outside the range [1-8] EAGAIN - Reading the vision sensor failed for an unknown reason.

Example

#define VISION_PORT 1
#define EXAMPLE_SIG 1

void opcontrol() {
pros::Vision vision_sensor(VISION_PORT);
while (true) {
  vision_object_s_t rtn = vision_sensor.get_by_sig(0, EXAMPLE_SIG);
  // Gets the largest object of the EXAMPLE_SIG signature
  printf("sig: %d", rtn.signature);
  // Prints "sig: 1"
  delay(2);
  }
}

vision_object_s_t get_by_code(const std::uint32_t size_id, const vision_color_code_t color_code) const

Gets the nth largest object of the given color code according to size_id.

Parameters
size_id The object to read from a list roughly ordered by object size (0 is the largest item, 1 is the second largest, etc.)
color_code The vision_color_code_t for which an object will be returned
Returns The vision_object_s_t object corresponding to the given color code and size_id, or PROS_ERR if an error occurred.

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor EAGAIN - Reading the Vision Sensor failed for an unknown reason.

Example

#define VISION_PORT 1
#define EXAMPLE_SIG 1
#define OTHER_SIG 2

void opcontrol() {
     pros::Vision vision_sensor(VISION_PORT);
  vision_color_code_t code1 = vision_sensor.create_color_code(EXAMPLE_SIG, OTHER_SIG);
  while (true) {
    vision_object_s_t rtn = vision_sensor.get_by_code(0, code1);
    // Gets the largest object
    printf("sig: %d", rtn.signature);
    delay(2);
  }
}

std::int32_t get_exposure(void) const

Gets the exposure parameter of the Vision Sensor.

Returns The current exposure parameter from [0,150], PROS_ERR if an error occurred

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor

Example

#define VISION_PORT 1

void initialize() {
  pros::Vision vision_sensor(VISION_PORT);
  if (vision_sensor.get_exposure() < 50)
  vision_sensor.set_exposure(50);
}

std::int32_t get_object_count(void) const

Gets the number of objects currently detected by the Vision Sensor.

Returns The number of objects detected on the specified vision sensor. Returns PROS_ERR if the port was invalid or an error occurred.

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor

Example

#define VISION_PORT 1

void opcontrol() {
  pros::Vision vision_sensor(VISION_PORT);
  while (true) {
    printf("Number of Objects Detected: %d\n", vision_sensor.get_object_count());
    delay(2);
  }
}

vision_signature_s_t get_signature(const std::uint8_t signature_id) const

Gets the object detection signature with the given id number.

Parameters
signature_id The signature id to read
Returns A vision_signature_s_t containing information about the signature.

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor

Example

#define VISION_PORT 1
#define EXAMPLE_SIG 1

void opcontrol() {
  pros::Vision vision_sensor(VISION_PORT);
  vision_signature_s_t sig = vision_sensor.get_signature(EXAMPLE_SIG);
  vision_sensor.print_signature(sig);
}

std::int32_t get_white_balance(void) const

Get the white balance parameter of the Vision Sensor.

Returns The current RGB white balance setting of the sensor

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor

Example

#define VISION_PORT 1
#define VISION_WHITE 0xff

void initialize() {
     pros::Vision vision_sensor(VISION_PORT);
  if (vision_sensor.get_white_balance() != VISION_WHITE)
  vision_sensor.set_white_balance(VISION_WHITE);
}

std::int32_t read_by_size(const std::uint32_t size_id, const std::uint32_t object_count, vision_object_s_t*const object_arr) const

Reads up to object_count object descriptors into object_arr.

Parameters
size_id The object to read from a list roughly ordered by object size (0 is the largest item, 1 is the second largest, etc.)
object_count The number of objects to read
object_arr out A pointer to copy the objects into
Returns The number of object signatures copied. This number will be less than object_count if there are fewer objects detected by the vision sensor. Returns PROS_ERR if the port was invalid, an error occurred, or fewer objects than size_id were found. All objects in object_arr that were not found are given VISION_OBJECT_ERR_SIG as their signature.

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor EDOM - size_id is greater than the number of available objects. EAGAIN - Reading the vision sensor failed for an unknown reason.

Example

#define VISION_PORT 1
#define NUM_VISION_OBJECTS 4

void opcontrol() {
  pros::Vision vision_sensor(VISION_PORT);
  vision_object_s_t object_arr[NUM_VISION_OBJECTS];
  while (true) {
    vision_sensor.read_by_size(0, NUM_VISION_OBJECTS, object_arr);
    printf("sig: %d", object_arr[0].signature);
    // Prints the signature of the largest object found
    delay(2);
  }
}

std::int32_t read_by_sig(const std::uint32_t size_id, const std::uint32_t sig_id, const std::uint32_t object_count, vision_object_s_t*const object_arr) const

Reads up to object_count object descriptors into object_arr.

Parameters
size_id The object to read from a list roughly ordered by object size (0 is the largest item, 1 is the second largest, etc.)
sig_id
object_count The number of objects to read
object_arr out A pointer to copy the objects into
Returns The number of object signatures copied. This number will be less than object_count if there are fewer objects detected by the vision sensor. Returns PROS_ERR if the port was invalid, an error occurred, or fewer objects than size_id were found. All objects in object_arr that were not found are given VISION_OBJECT_ERR_SIG as their signature.

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor EDOM - size_id is greater than the number of available objects. EINVAL - sig_id is outside the range [1-8] EAGAIN - Reading the vision sensor failed for an unknown reason.

Example

#define VISION_PORT 1
#define EXAMPLE_SIG 1
#define NUM_VISION_OBJECTS 4

void opcontrol() {
  pros::Vision vision_sensor(VISION_PORT);
  vision_object_s_t object_arr[NUM_VISION_OBJECTS];
  while (true) {
    vision_sensor.read_by_sig(0, EXAMPLE_SIG, NUM_VISION_OBJECTS, object_arr);
    printf("sig: %d", object_arr[0].signature);
    // Prints "sig: 1"
    delay(2);
  }
}

int32_t read_by_code(const std::uint32_t size_id, const vision_color_code_t color_code, const std::uint32_t object_count, vision_object_s_t*const object_arr) const

Reads up to object_count object descriptors into object_arr.

Parameters
size_id The object to read from a list roughly ordered by object size (0 is the largest item, 1 is the second largest, etc.)
color_code The vision_color_code_t for which objects will be returned
object_count The number of objects to read
object_arr out A pointer to copy the objects into
Returns The number of object signatures copied. This number will be less than object_count if there are fewer objects detected by the vision sensor. Returns PROS_ERR if the port was invalid, an error occurred, or fewer objects than size_id were found. All objects in object_arr that were not found are given VISION_OBJECT_ERR_SIG as their signature.

This function uses the following values of errno when an error state is reached: EDOM - size_id is greater than the number of available objects. ENODEV - The port cannot be configured as a vision sensor EAGAIN - Reading the vision sensor failed for an unknown reason.

Example

#define VISION_PORT 1
#define EXAMPLE_SIG 1
#define OTHER_SIG 2
#define NUM_VISION_OBJECTS 4

void opcontrol() {
  pros::Vision vision_sensor(VISION_PORT);
  vision_object_s_t object_arr[NUM_VISION_OBJECTS];
  vision_color_code_t code1 = vision_sensor.create_color_code(EXAMPLE_SIG, OTHER_SIG, 0, 0, 0);
  while (true) {
    vision_sensor.read_by_code(0, code1, NUM_VISION_OBJECTS, object_arr);
    printf("sig: %d", object_arr[0].signature);
    // Prints the signature of the largest object found
    delay(2);
  }
}

static std::int32_t print_signature(const vision_signature_s_t sig)

Prints the contents of the signature as an initializer list to the terminal.

Parameters
sig The signature for which the contents will be printed
Returns 1 if no errors occured, PROS_ERR otherwise

Example

#define VISION_PORT 1
#define EXAMPLE_SIG 1

void opcontrol() {
  pros::Vision vision_sensor(VISION_PORT);
  vision_signature_s_t sig = visionsensor.get_signature(EXAMPLE_SIG);
  vision_print_signature(sig);
}

std::int32_t set_auto_white_balance(const std::uint8_t enable) const

Enables/disables auto white-balancing on the Vision Sensor.

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

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor

Example

#define VISION_PORT 1

void initialize() {
     pros::Vision vision_sensor(VISION_PORT);
  vision_sensor.set_auto_white_balance(true);
}

std::int32_t set_exposure(const std::uint8_t exposure) const

Sets the exposure parameter of the Vision Sensor.

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

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor

Example

#define VISION_PORT 1

void initialize() {
     pros::Vision vision_sensor(VISION_PORT);
  if (vision_sensor.get_exposure() < 50)
  vision_sensor.set_exposure(50);
}

std::int32_t set_led(const std::int32_t rgb) const

Sets the vision sensor LED color, overriding the automatic behavior.

Parameters
rgb An RGB code to set the LED to
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor

Example

#define VISION_PORT 1

void initialize() {
  pros::Vision vision_sensor(VISION_PORT);
  vision_sensor.set_led(COLOR_BLANCHED_ALMOND);
}

std::int32_t set_signature(const std::uint8_t signature_id, vision_signature_s_t*const signature_ptr) const

Stores the supplied object detection signature onto the vision sensor.

Parameters
signature_id The signature id to store into
signature_ptr in A pointer to the signature to save
Returns 1 if no errors occured, PROS_ERR otherwise

NOTE: This saves the signature in volatile memory, and the signature will be lost as soon as the sensor is powered down.

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor EINVAL - sig_id is outside the range [1-8]

Example

#define VISION_PORT 1
#define EXAMPLE_SIG 1

void opcontrol() {
  pros::Vision vision_sensor(VISION_PORT);
  vision_signature_s_t sig = vision_sensor.get_signature(EXAMPLE_SIG);
  sig.range = 10.0;
  vision_sensor.set_signature(EXAMPLE_SIG, &sig);
}

std::int32_t set_white_balance(const std::int32_t rgb) const

Sets the white balance parameter of the Vision Sensor.

Parameters
rgb The new RGB white balance setting of the sensor
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor

Example

#define VISION_PORT 1
#define VISION_WHITE 0xff

void initialize() {
  pros::Vision vision_sensor(VISION_PORT);
  vision_sensor.set_white_balance(VISION_WHITE);
}

std::int32_t set_zero_point(vision_zero_e_t zero_point) const

Sets the (0,0) coordinate for the Field of View.

Parameters
zero_point One of vision_zero_e_t to set the (0,0) coordinate for the FOV
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This will affect the coordinates returned for each request for a vision_object_s_t from the sensor, so it is recommended that this function only be used to configure the sensor at the beginning of its use.

This function uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor

Example

#define VISION_PORT 1

void initialize() {
     pros::Vision vision_sensor(VISION_PORT);
  vision_sensor.set_zero_point(E_VISION_ZERO_CENTER);
}

std::int32_t set_wifi_mode(const std::uint8_t enable) const

Sets the Wi-Fi mode of the Vision sensor.

Parameters
enable Disable Wi-Fi on the Vision sensor if 0, enable otherwise (e.g. 1)
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This functions uses the following values of errno when an error state is reached: ENODEV - The port cannot be configured as a vision sensor

Example

#define VISION_PORT 1

void initialize() {
     pros::Vision vision_sensor(VISION_PORT);
  vision_sensor.set_wifi_mode(0);
}

static Vision get_vision()

Gets a vision sensor that is plugged in to the brain.

Returns A vision object corresponding to a port that a vision sensor is connected to the brain If no vision sensor is plugged in, it returns a vision sensor on port PROS_ERR_BYTE

This functions uses the following values of errno when an error state is reached: ENODEV - No vision sensor is plugged into the brain