pub struct AiVisionSensor { /* private fields */ }
Expand description
An AI Vision sensor.
Implementations§
Source§impl AiVisionSensor
impl AiVisionSensor
Sourcepub const MAX_OBJECTS: usize = 24usize
pub const MAX_OBJECTS: usize = 24usize
Maximum number of objects that can be detected at once.
Sourcepub const HORIZONTAL_RESOLUTION: u16 = 320u16
pub const HORIZONTAL_RESOLUTION: u16 = 320u16
The horizontal resolution of the AI Vision sensor.
Sourcepub const VERTICAL_RESOLUTION: u16 = 240u16
pub const VERTICAL_RESOLUTION: u16 = 240u16
The vertical resolution of the AI Vision sensor.
Sourcepub const HORIZONTAL_FOV: f32 = 74f32
pub const HORIZONTAL_FOV: f32 = 74f32
The horizontal FOV of the vision sensor in degrees.
Sourcepub const VERTICAL_FOV: f32 = 63f32
pub const VERTICAL_FOV: f32 = 63f32
The vertical FOV of the vision sensor in degrees.
Sourcepub const DIAGONAL_FOV: f32 = 87f32
pub const DIAGONAL_FOV: f32 = 87f32
The diagonal FOV of the vision sensor in degrees.
Sourcepub fn new(port: SmartPort) -> Self
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
}
Sourcepub fn temperature(&self) -> Result<f64, AiVisionError>
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;
}
}
Sourcepub fn set_color_code(
&mut self,
id: u8,
code: &AiVisionColorCode,
) -> Result<(), AiVisionError>
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);
}
Sourcepub fn color_code(
&self,
id: u8,
) -> Result<Option<AiVisionColorCode>, AiVisionError>
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!");
}
}
Sourcepub fn color_codes(
&self,
) -> Result<[Option<AiVisionColorCode>; 8], AiVisionError>
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());
}
Sourcepub fn set_color(
&mut self,
id: u8,
color: AiVisionColor,
) -> Result<(), AiVisionError>
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);
}
Sourcepub fn color(&self, id: u8) -> Result<Option<AiVisionColor>, AiVisionError>
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!");
}
}
Sourcepub fn colors(&self) -> Result<[Option<AiVisionColor>; 7], AiVisionError>
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);
}
Sourcepub fn set_detection_mode(
&mut self,
mode: AiVisionDetectionMode,
) -> Result<(), AiVisionError>
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);
}
Sourcepub fn flags(&self) -> Result<AiVisionFlags, AiVisionError>
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());
}
Sourcepub fn set_flags(&mut self, mode: AiVisionFlags) -> Result<(), AiVisionError>
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);
}
Sourcepub fn start_awb(&mut self) -> Result<(), AiVisionError>
pub fn start_awb(&mut self) -> Result<(), AiVisionError>
Sourcepub fn enable_test(&mut self, test: u8) -> Result<(), AiVisionError>
pub fn enable_test(&mut self, test: u8) -> Result<(), AiVisionError>
Sourcepub fn set_apriltag_family(
&mut self,
family: AprilTagFamily,
) -> Result<(), AiVisionError>
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);
}
Sourcepub fn objects(&self) -> Result<Vec<AiVisionObject>, AiVisionError>
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;
}
}
Sourcepub fn object_count(&self) -> Result<u32, AiVisionError>
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
impl From<AiVisionSensor> for SmartPort
Source§fn from(val: AiVisionSensor) -> Self
fn from(val: AiVisionSensor) -> Self
Source§impl SmartDevice for AiVisionSensor
impl SmartDevice for AiVisionSensor
Source§fn port_number(&self) -> u8
fn port_number(&self) -> u8
Source§fn device_type(&self) -> SmartDeviceType
fn device_type(&self) -> SmartDeviceType
SmartDeviceType
that this device is associated with. Read more