Struct AiVisionSensor

Source
pub struct AiVisionSensor { /* private fields */ }
Expand description

An AI Vision sensor.

Implementations§

Source§

impl AiVisionSensor

Source

pub const MAX_OBJECTS: usize = 24usize

Maximum number of objects that can be detected at once.

Source

pub const HORIZONTAL_RESOLUTION: u16 = 320u16

The horizontal resolution of the AI Vision sensor.

Source

pub const VERTICAL_RESOLUTION: u16 = 240u16

The vertical resolution of the AI Vision sensor.

Source

pub const HORIZONTAL_FOV: f32 = 74f32

The horizontal FOV of the vision sensor in degrees.

Source

pub const VERTICAL_FOV: f32 = 63f32

The vertical FOV of the vision sensor in degrees.

Source

pub const DIAGONAL_FOV: f32 = 87f32

The diagonal FOV of the vision sensor in degrees.

Source

pub fn new(port: SmartPort) -> Self

Create a new AI Vision sensor from a smart port with the given brightness and contrast.

§Examples
use vexide::prelude::*;

#[vexide::main]
async fn main(peripherals: Peripherals) {
    let mut ai_vision = AiVisionSensor::new(peripherals.port_1);
    // Do something with the AI Vision sensor
}
Source

pub fn temperature(&self) -> Result<f64, AiVisionError>

Returns the current temperature of the AI Vision sensor.

§Errors
  • A PortError is returned if an AI Vision is not connected to the Smart Port.
§Examples
use vexide::prelude::*;

#[vexide::main]
async fn main(peripherals: Peripherals) {
    let ai_vision = AiVisionSensor::new(peripherals.port_1);
    loop {
        println!("{:?}", ai_vision.temperature());
        sleep(AiVisionSensor::UPDATE_INTERVAL).await;
    }
}
Source

pub fn set_color_code( &mut self, id: u8, code: &AiVisionColorCode, ) -> Result<(), AiVisionError>

Sets a color code used to detect groups of colors.

§Panics
  • Panics if the given color code contains an ID that is not in the interval [1, 7].
  • Panics if the given ID is not in the interval [1, 8].
§Errors
  • A PortError is returned if an AI Vision is not connected to the Smart Port.
§Examples
use vexide::prelude::*;

#[vexide::main]
async fn main(peripherals: Peripherals) {
    let mut ai_vision = AiVisionSensor::new(peripherals.port_1);
    let color = AiVisionColor {
        rgb: Rgb::new(255, 0, 0),
        hue: 10.0,
        saturation: 1.0,
    };
    _ = ai_vision.set_color(1, color);
    let code = AiVisionColorCode::from([1]);
    _ = ai_vision.set_color_code(1, &code);
}
Source

pub fn color_code( &self, id: u8, ) -> Result<Option<AiVisionColorCode>, AiVisionError>

Returns the color code set on the AI Vision sensor with the given ID if it exists.

§Panics
  • Panics if the given ID is not in the interval [1, 8].
§Errors
  • A PortError is returned if an AI Vision is not connected to the Smart Port.
§Examples
use vexide::prelude::*;

#[vexide::main]
async fn main(peripherals: Peripherals) {
    let mut ai_vision = AiVisionSensor::new(peripherals.port_1);
    let code = AiVisionColorCode::from([1]);
    _ = ai_vision.set_color_code(1, &code);
    if let Ok(Some(code)) = ai_vision.color_code(1) {
         println!("{:?}", code);
    } else {
        println!("Something went wrong!");
    }
}
Source

pub fn color_codes( &self, ) -> Result<[Option<AiVisionColorCode>; 8], AiVisionError>

Returns all color codes set on the AI Vision sensor.

§Errors
  • A PortError is returned if an AI Vision is not connected to the Smart Port.
§Examples
use vexide::prelude::*;

#[vexide::main]
async fn main(peripherals: Peripherals) {
    let mut ai_vision = AiVisionSensor::new(peripherals.port_1);
    _ = ai_vision.set_color_code(1, &AiVisionColorCode::from([1]));
    _ = ai_vision.set_color_code(2, &AiVisionColorCode::from([1, 2]));
    println!("{:?}", ai_vision.color_codes());
}
Source

pub fn set_color( &mut self, id: u8, color: AiVisionColor, ) -> Result<(), AiVisionError>

Sets a color signature for the AI Vision sensor.

§Panics
  • Panics if the given ID is not in the range [1, 7].
§Errors
  • A PortError is returned if an AI Vision is not connected to the Smart Port.
§Examples
use vexide::prelude::*;

#[vexide::main]
async fn main(peripherals: Peripherals) {
    let mut ai_vision = AiVisionSensor::new(peripherals.port_1);
    let color = AiVisionColor {
        rgb: Rgb::new(255, 0, 0),
        hue: 10.0,
        saturation: 1.0,
    };
    _ = ai_vision.set_color(1, color);
    _ = ai_vision.set_color(2, color);
}
Source

pub fn color(&self, id: u8) -> Result<Option<AiVisionColor>, AiVisionError>

Returns the color signature set on the AI Vision sensor with the given ID if it exists.

§Panics
  • Panics if the given ID is not in the interval [1, 7].
§Errors
  • A PortError is returned if an AI Vision is not connected to the Smart Port.
§Examples
use vexide::prelude::*;

#[vexide::main]
async fn main(peripherals: Peripherals) {
    let ai_vision = AiVisionSensor::new(peripherals.port_1);
    let color = AiVisionColor {
        rgb: Rgb::new(255, 0, 0),
        hue: 10.0,
        saturation: 1.0,
    };
    _ = ai_vision.set_color(1, color);
    if let Ok(Some(color)) = ai_vision.color(1) {
        println!("{:?}", color);
    } else {
        println!("Something went wrong!");
    }
}
Source

pub fn colors(&self) -> Result<[Option<AiVisionColor>; 7], AiVisionError>

Returns all color signatures set on the AI Vision sensor.

§Errors
  • A PortError is returned if an AI Vision is not connected to the Smart Port.
§Examples
use vexide::prelude::*;

#[vexide::main]
async fn main(peripherals: Peripherals) {
    let ai_vision = AiVisionSensor::new(peripherals.port_1);
    let color = AiVisionColor {
        rgb: Rgb::new(255, 0, 0),
        hue: 10.0,
        saturation: 1.0,
    };
    _ = ai_vision.set_color(1, color);
    let colors = ai_vision.colors().unwrap();
    println!("{:?}", colors);
}
Source

pub fn set_detection_mode( &mut self, mode: AiVisionDetectionMode, ) -> Result<(), AiVisionError>

Sets the detection mode of the AI Vision sensor.

§Errors
  • A PortError is returned if an AI Vision is not connected to the Smart Port.
§Examples
use vexide::prelude::*;

#[vexide::main]
async fn main(peripherals: Peripherals) {
    let mut ai_vision = AiVisionSensor::new(peripherals.port_1);
    _ = ai_vision.set_detection_mode(AiVisionDetectionMode::COLOR | AiVisionDetectionMode::COLOR_MERGE);
}
Source

pub fn flags(&self) -> Result<AiVisionFlags, AiVisionError>

Returns the current flags of the AI Vision sensor including the detection mode flags set by Self::set_detection_mode.

§Errors
  • A PortError is returned if an AI Vision is not connected to the Smart Port.
§Examples
use vexide::prelude::*;

#[vexide::main]
async fn main(peripherals: Peripherals) {
    let ai_vision = AiVisionSensor::new(peripherals.port_1);
    println!("{:?}", ai_vision.flags());
}
Source

pub fn set_flags(&mut self, mode: AiVisionFlags) -> Result<(), AiVisionError>

Set the full flags of the AI Vision sensor, including the detection mode.

§Errors
  • A PortError is returned if an AI Vision is not connected to the Smart Port.
§Examples
use vexide::prelude::*;

#[vexide::main]
async fn main(peripherals: Peripherals) {
    let mut ai_vision = AiVisionSensor::new(peripherals.port_1);
    // Enable all detection modes except for custom model and disable USB overlay
    let flags = AiVisionFlags::DISABLE_USB_OVERLAY | AiVisionFlags::DISABLE_MODEL;
    _ = ai_vision.set_flags(flags);
}
Source

pub fn start_awb(&mut self) -> Result<(), AiVisionError>

Restarts the automatic white balance process.

§Errors
  • A PortError is returned if an AI Vision is not connected to the Smart Port.
Source

pub fn enable_test(&mut self, test: u8) -> Result<(), AiVisionError>

Unknown Use

§Errors
  • A PortError is returned if an AI Vision is not connected to the Smart Port.
Source

pub fn set_apriltag_family( &mut self, family: AprilTagFamily, ) -> Result<(), AiVisionError>

Sets the family of apriltag that will be detected

§Errors
  • A PortError is returned if an AI Vision is not connected to the Smart Port.
§Examples
use vexide::prelude::*;

#[vexide::main]
async fn main(peripherals: Peripherals) {
    let mut ai_vision = AiVisionSensor::new(peripherals.port_1);
    _ = ai_vision.set_apriltag_family(AprilTagFamily::Tag16h5);
}
Source

pub fn objects(&self) -> Result<Vec<AiVisionObject>, AiVisionError>

Returns all objects detected by the AI Vision sensor.

§Errors
  • A PortError is returned if an AI Vision is not connected to the Smart Port.
§Examples

Loop through all objects of a specific type

use vexide::prelude::*;

#[vexide::main]
async fn main(peripherals: Peripherals) {
    let mut ai_vision = AiVisionSensor::new(peripherals.port_1);
    loop {
        let objects = ai_vision.objects().unwrap();
        for object in objects {
            if let AiVisionObjectData::Color { position, .. } = object.data {
                println!("{:?}", position);
            }
        }
        sleep(AiVisionSensor::UPDATE_INTERVAL).await;
    }
}
Source

pub fn object_count(&self) -> Result<u32, AiVisionError>

Returns the number of objects currently detected by the AI Vision sensor.

§Errors
  • A PortError is returned if an AI Vision is not connected toMODE_MAGIC_BIT the Smart Port.
§Examples
use vexide::prelude::*;

#[vexide::main]
async fn main(peripherals: Peripherals) {
    let mut ai_vision = AiVisionSensor::new(peripherals.port_1);
    loop {
        println!("AI Vision sensor currently detects {:?} objects", ai_vision.object_count());
        sleep(AiVisionSensor::UPDATE_INTERVAL).await;
    }
}

Trait Implementations§

Source§

impl From<AiVisionSensor> for SmartPort

Source§

fn from(val: AiVisionSensor) -> Self

Converts to this type from the input type.
Source§

impl SmartDevice for AiVisionSensor

Source§

fn port_number(&self) -> u8

Returns the port number of the SmartPort this device is registered on. Read more
Source§

fn device_type(&self) -> SmartDeviceType

Returns the variant of SmartDeviceType that this device is associated with. Read more
Source§

const UPDATE_INTERVAL: Duration = _

The interval at which the V5 brain reads packets from Smart devices.
Source§

fn is_connected(&self) -> bool

Determine if this device type is currently connected to the SmartPort that it’s registered to. Read more
Source§

fn timestamp(&self) -> Result<SmartDeviceTimestamp, PortError>

Returns the timestamp recorded by this device’s internal clock. Read more
Source§

fn validate_port(&self) -> Result<(), PortError>

Verify that the device type is currently plugged into this port, returning an appropriate PortError if not available. Read more
Source§

impl Send for AiVisionSensor

Source§

impl Sync for AiVisionSensor

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.