AI Vision Sensor C++ API module

Contents

Classes

class pros::v5::AIVision

Functions

AIVision(const std::uint8_t port) explicit
Create a AI Vision Sensor object on the given port.
AIVision(const Device& device)
static std::vector<AIVision> get_all_devices()
Gets all vision sensors.
static bool is_type(const Object& object, AivisionDetectType type)
Check if the dected type is the same as the given type.
int32_t reset()
Resets the AI Vision sensor to the initial state.
int32_t get_enabled_detection_types()
Returns a bitfield of the types of objects the AI vision sensor is currently searching for, as per AivisionModeType.
int32_t enable_detection_types(AivisionModeType types_mask)
Enable detecting these types of objects, a bitmask as per aivision_mode_type_e_t.
template<class... Flags>
int32_t enable_detection_types(Flags... flags)
Enable detecting these types of objects, a bitmask as per aivision_mode_type_e_t.
int32_t disable_detection_types(AivisionModeType types_mask)
Disable detecting these types of objects, a bitmask as per aivision_mode_type_e_t.
template<class... Flags>
int32_t disable_detection_types(Flags... flags)
Disable detecting these types of objects, a bitmask as per aivision_mode_type_e_t.
int32_t set_tag_family(AivisionTagFamily family, bool override = false)
Sets the april tag family to detect.
int32_t set_color(const Color& color)
Set a color configuration that the AI vision sensor will detect.
AIVision::Color get_color(uint32_t id)
Get a color configuration that the AI vision sensor has stored.
uint32_t set_code(const Code& code)
Set a code that the AI vision sensor will detect for.
AIVision::Code get_code(uint32_t id)
Get a code that the AI vision sensor has stored.
int32_t start_awb()
Runs auto white balance to adjust to different lighting conditions.
int32_t get_class_name(int32_t id, char* class_name)
Get a class name that the AI vision sensor has stored.
std::optional<std::string> get_class_name(int32_t id)
Get a class name that the AI vision sensor has stored.
int32_t get_object_count()
Get the current number of objects detected by the AI vision sensor.
Object get_object(uint32_t object_index)
Get the detected object at a given object index; there are aivision_get_object_count objects and the index starts from 0.
std::vector<Object> get_all_objects()
Get all detected objects in a vector.

Enums

enum class AivisionDetectType: uint8_t { color = (1 << 0), code = (1 << 1), object = (1 << 2), tag = (1 << 3) }
Enum class for describing detection type of objects detected by the AI Vision Sensor.
enum class AivisionModeType: uint8_t { tags = (1 << 0), colors = (1 << 1), objects = (1 << 2), color_merge = (1 << 4), all = (1 << 0) | (1 << 1) | (1 << 2) }
Enum class for enabling/disabling detection types of AI Vision Sensor.
enum class AivisionTagFamily { tag_21H7 = 0, tag_16H5 = 1, tag_25H9 = 2, tag_61H11 = 3 }
Enum class for describing family of apriltags to detect.

Typedefs

using Color = aivision_color_s_t
using Code = aivision_code_s_t
using Object = aivision_object_s_t

Function documentation

AIVision(const std::uint8_t port) explicit

Create a AI Vision Sensor object on 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 AI vision sensor

Example

void opcontrol() {
  pros::AIVision ai_sensor(2); // Creates a vision sensor on port two
}

AIVision(const Device& device)

static std::vector<AIVision> get_all_devices()

Gets all vision sensors.

Returns A vector of AIVision sensor objects.

Example

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

static bool is_type(const Object& object, AivisionDetectType type)

Check if the dected type is the same as the given type.

Returns true if the type is the same, false otherwise

Example

void opcontrol() {
  pros::AIVision aivision(1);
  pros::AIVision::Object object = aivision.get_object(0);
  if (AIVision::is_type(AivisionDetectType::color, object)) {
      printf("is color\n");
  } else if (AIVision::is_type(AivisionDetectType::object, object)) {
      printf("is object\n");
  } else if (AIVision::is_type(AivisionDetectType::code, object)) {
      printf("is code\n");
  } else if (AIVision::is_type(AivisionDetectType::tag, object)) {
      printf("is tag\n");
  } else {
      printf("unknown\n");
  }
}

int32_t reset()

Resets the AI Vision sensor to the initial state.

Returns PROS_SUCCESS 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: 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

#define AIVISION_PORT 1
void initialize() {
  pros::AIVision aivision(AIVISION_PORT);
  aivision.reset();
}

int32_t get_enabled_detection_types()

Returns a bitfield of the types of objects the AI vision sensor is currently searching for, as per AivisionModeType.

Returns the bitfield 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: 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

#define AIVISION_PORT 1
void initialize() {
  pros::AIVision aivision(AIVISION_PORT);
  int32_t enabled_types = aivision.get_enabled_detection_types();
  printf("is tag: %d\n", enabled_types | AivisionModeType::tags);
}

int32_t enable_detection_types(AivisionModeType types_mask)

Enable detecting these types of objects, a bitmask as per aivision_mode_type_e_t.

Parameters
types_mask The types to enable
Returns PROS_SUCCESS if the operation was successful or PROS_ERR if the operation failed, setting errno.

Enabling any given type of object will not disable the detection of other objects. This must be done explicitly.

For this function you must use bitwise or to combine the types you want to enable.

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

#define AIVISION_PORT 1
void initialize() {
  pros::AIVision aivision(AIVISION_PORT);
  // start or continue looking for AI model objects
  // enable aivision to look for tags and objects
     aivision.enable_detection_types(AivisionModeType::tags | AivisionModeType::objects);
}

template<class... Flags>
int32_t enable_detection_types(Flags... flags)

Enable detecting these types of objects, a bitmask as per aivision_mode_type_e_t.

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

Enabling any given type of object will not disable the detection of other objects. This must be done explicitly.

For this function you can use comma separated values to combine the types you want to enable.

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

#define AIVISION_PORT 1
void initialize() {
  pros::AIVision aivision(AIVISION_PORT);
  // start or continue looking for AI model objects
  // enable aivision to look for tags and objects
     aivision.enable_detection_types(AivisionModeType::tags, AivisionModeType::objects);
}

int32_t disable_detection_types(AivisionModeType types_mask)

Disable detecting these types of objects, a bitmask as per aivision_mode_type_e_t.

Parameters
types_mask The types to enable
Returns PROS_SUCCESS if the operation was successful or PROS_ERR if the operation failed, setting errno.

Disabling any given type of object will not affect the detection of other objects.

For this function you must use bitwise or to combine the types you want to disable.

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

#define AIVISION_PORT 1
void initialize() {
  pros::AIVision aivision(AIVISION_PORT);
  // stop looking for AI model objects (competition elements, for example)
  // disable aivision to look for tags and objects
     aivision.disable_detection_types(AivisionModeType::tags | AivisionModeType::objects);
}

template<class... Flags>
int32_t disable_detection_types(Flags... flags)

Disable detecting these types of objects, a bitmask as per aivision_mode_type_e_t.

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

Disabling any given type of object will not affect the detection of other objects.

For this function you can use comma separated values to combine the types you want to disable.

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

#define AIVISION_PORT 1
void initialize() {
  pros::AIVision aivision(AIVISION_PORT);
  // stop looking for AI model objects (competition elements, for example)
  // disable aivision to look for tags and objects
     aivision.disable_detection_types(AivisionModeType::tags | AivisionModeType::objects);
}

int32_t set_tag_family(AivisionTagFamily family, bool override = false)

Sets the april tag family to detect.

Parameters
family the tag family to configure the AI Vision sensor to detect
override if true, the given family will be set as the only enabled tag family.
Returns PROS_SUCCESS if the operation was successful or PROS_ERR if the operation failed, setting errno.

If override is true, the AI vision sensor will only look for the given family. Otherwise, it will add the given tag to the list of enabled tags.

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

#define AIVISION_PORT 1
void initialize() {
  pros::AIVision aivision(AIVISION_PORT);
  // set the only tag family to look for to 21H7
  aivision.set_tag_family(AivisionTagFamily::tag_21H7);
  // add 16H5 to the list of enabled tag families
  aivision.set_tag_family(AivisionTagFamily::tag_16H5);
  // set the only tag family to look for to 25H9
  aivision.set_tag_family(AivisionTagFamily::tag_25H9, true);
}

int32_t set_color(const Color& color)

Set a color configuration that the AI vision sensor will detect.

Parameters
color the color to configure the AI Vision sensor to detect
Returns PROS_SUCCESS if the operation was successful or PROS_ERR if the operation failed, setting errno

The color detection type must be separately enabled. If a color with the same ID already is stored in the sensor, it will be overwritten.

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

#define AIVISION_PORT 1
void opcontrol() {
  pros::AIVision aivision(AIVISION_PORT);
  AIVision::Color color = {1, 207, 19, 25, 10.00, 0.20};
  aivision.set_color(color);
}

AIVision::Color get_color(uint32_t id)

Get a color configuration that the AI vision sensor has stored.

Parameters
id the id of color from 1-7
Returns PROS_SUCCESS if the operation was successful or PROS_ERR if the operation failed, setting errno

If you attempt to get a color configuration that has not been previously used, the behavior is not defined.

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

#define AIVISION_PORT 1 void opcontrol() { pros::AIVision aivision(AIVISION_PORT); AIVision::Code color = aivision.get_color(0); printf("id: %d, red: %d, green: %d, blue: %d, hue_range: %f, saturation_range: %f\n", color.id, color.red, color.green, color.blue, color.hue_range, color.saturation_range); }

uint32_t set_code(const Code& code)

Set a code that the AI vision sensor will detect for.

Parameters
code The code to set
Returns PROS_SUCCESS if the operation was successful or PROS_ERR if the operation failed, setting errno

The id of the code is stored in the aivision_code_s_t struct. If there is already a code stored in the AI vision sensor with the id, this function will overwrite.

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

#define AIVISION_PORT 1
void opcontrol() {
 pros::AIVision aivision(AIVISION_PORT);
 AIVision::Code code = {1, 207, 19, 25, 10.00, 0.20};
 aivision.set_code(code);
}

AIVision::Code get_code(uint32_t id)

Get a code that the AI vision sensor has stored.

Parameters
id The id from 1-5
Returns the code, or a struct with an invalid ID 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 a vision sensor

#define AIVISION_PORT 1
void opcontrol() {
 pros::AIVision aivision(AIVISION_PORT);
 AIVision::Code code = aivision.get_code(0);
 printf("id: %d, length: %d, c1: %d, c2: %d, c3: %d, c4: %d, c5: %d\n",
 code.id, code.length, code.c1, code.c2, code.c3, code.c4, code.c5);
 )
}

int32_t start_awb()

Runs auto white balance to adjust to different lighting conditions.

Returns PROS_SUCCESS 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: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as a vision sensor

int32_t get_class_name(int32_t id, char* class_name)

Get a class name that the AI vision sensor has stored.

Parameters
id the id of the class name from 0-(AIVISION_MAX_CLASSNAME_COUNT - 1)
class_name a string of length >=20 to store the classname.
Returns PROS_SUCCESS if the operation was successful or PROS_ERR if the operation failed, setting errno

The AI Vision sensor may not correctly report classnames for the first several hundred milliseconds of being plugged in. By passing in -1 for the id, the function will return the number of class names the AI vision sensor reports. For other values of id, the function return value is undefined

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

#define AIVISION_PORT 1
void opcontrol() {
 pros::AIVision aivision(AIVISION_PORT);
 char* class_name = new char[21];
 aivision.get_class_name(0, class_name);
 printf("%s\n", class_name);
 delete[] class_name;
}

std::optional<std::string> get_class_name(int32_t id)

Get a class name that the AI vision sensor has stored.

Parameters
id the id of the class name from 0-(AIVISION_MAX_CLASSNAME_COUNT - 1)
Returns the class name string in std::optional if the operation was successful or an empty optional if the operation failed, setting errno

The AI Vision sensor may not correctly report classnames for the first several hundred milliseconds of being plugged in. By passing in -1 for the id, the function will return the number of class names the AI vision sensor reports. For other values of id, the function return value is undefined

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

#define AIVISION_PORT 1
void opcontrol() {
 pros::AIVision aivision(AIVISION_PORT);
 auto name = aivision.get_class_name(1);

 if(name.has_value()) {
   printf("Class name: %s\n", name.value().c_str());
 } else {
   printf("Error: %ld\n", errno);
 }
}

int32_t get_object_count()

Get the current number of objects detected by the AI vision sensor.

Returns the number of objects 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: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as a vision sensor

#define AIVISION_PORT 1
void opcontrol() {
 pros::AIVision aivision(AIVISION_PORT);
 int32_t object_count = aivision.get_object_count();
 printf("%d\n", object_count);
}

Object get_object(uint32_t object_index)

Get the detected object at a given object index; there are aivision_get_object_count objects and the index starts from 0.

Parameters
object_index the object index
Returns the detected object if the operation was successful or an invalid object type 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 a vision sensor

#define AIVISION_PORT 1
void opcontrol() {
 pros::AIVision aivision(AIVISION_PORT);
 int32_t object_count = aivision.get_object_count();
 for (int i = 0; i < object_count; i++) {
    pros::AIVision::Object object = aivision.get_object(i);
    printf("Object %d: %d\n", i, object.type);
 }
}

std::vector<Object> get_all_objects()

Get all detected objects in a vector.

Returns a vector of all detected objects

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

#define AIVISION_PORT 1
void opcontrol() {
 pros::AIVision aivision(AIVISION_PORT);
 auto objects = aivision.get_all_objects();
 for (const auto& object : objects) {
   printf("Object %d: %d\n", object.id, object.type);
 }
}

Enum documentation

enum class AivisionDetectType: uint8_t

Enum class for describing detection type of objects detected by the AI Vision Sensor.

Enumerators
color

object was detected based on color descriptor

code

object was detected based on code descriptor

object

object was detected using AI model

tag

object was detected as an AprilTag

enum class AivisionModeType: uint8_t

Enum class for enabling/disabling detection types of AI Vision Sensor.

Enumerators
tags

AprilTag detection.

colors

color and code detection

objects

AI model object detection.

color_merge

merge adjacent color detections

all

enum class AivisionTagFamily

Enum class for describing family of apriltags to detect.

Typedef documentation