Skip to content
This repository was archived by the owner on Apr 22, 2026. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/pros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ slab = { version = "0.4.9", default-features = false }
hashbrown = { version = "0.14.1", default-features = true }
async-task = { version = "4.5.0", default-features = false }
waker-fn = "1.1.1"
uom = { version = "0.35.0", default-features = false, features = ["si", "autoconvert", "f64"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
dlmalloc = { version = "0.2.4", features = ["global"] }
27 changes: 17 additions & 10 deletions packages/pros/src/sensors/distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
use core::ffi::c_double;

use pros_sys::PROS_ERR;
use uom::si::{
length::millimeter,
quantities::{Length, Ratio, Velocity},
ratio::ratio,
velocity::meter_per_second,
};

use crate::error::{bail_on, PortError};

Expand All @@ -22,27 +28,28 @@ impl DistanceSensor {
}

/// Returns the distance to the object the sensor detects in millimeters.
pub fn distance(&self) -> Result<u32, PortError> {
Ok(unsafe { bail_on!(PROS_ERR, pros_sys::distance_get(self.port)) as u32 })
pub fn distance(&self) -> Result<Length<f64>, PortError> {
let raw = unsafe { bail_on!(PROS_ERR, pros_sys::distance_get(self.port)) };
Ok(Length::new::<millimeter>(raw.into()))
}

/// returns the velocity of the object the sensor detects in m/s
pub fn object_velocity(&self) -> Result<f64, PortError> {
pub fn object_velocity(&self) -> Result<Velocity<f64>, PortError> {
// all VEX Distance Sensor functions return PROS_ERR on failure even though
// some return floating point values (not PROS_ERR_F)
Ok(unsafe {
let raw = unsafe {
bail_on!(
PROS_ERR as c_double,
pros_sys::distance_get_object_velocity(self.port)
)
})
};
Ok(Velocity::new::<meter_per_second>(raw))
}

/// Returns the confidence in the distance measurement from 0% to 100%.
pub fn distance_confidence(&self) -> Result<f32, PortError> {
/// Returns the confidence of the distance measurement.
pub fn distance_confidence(&self) -> Result<Ratio<f64>, PortError> {
// 0 -> 63
let confidence =
unsafe { bail_on!(PROS_ERR, pros_sys::distance_get_confidence(self.port)) } as f32;
Ok(confidence * 100.0 / 63.0)
let raw = unsafe { bail_on!(PROS_ERR, pros_sys::distance_get_confidence(self.port)) };
Ok(Ratio::new::<ratio>(raw as f64 / 63.0))
}
}