Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
refactor: distance_confidence 0-1, docs, method naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Tropix126 committed Mar 8, 2024
1 parent 8110f8b commit 710fede
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions packages/pros-devices/src/smart/distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,40 @@ impl DistanceSensor {
}) as u32)
}

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

/// Get the current guess at relative object size.
/// Get the current guess at relative "object size".
///
/// This is a value that has a range of 0 to 400.
/// A 18" x 30" grey card will return a value of approximately 75
/// in typical room lighting.
pub fn object_size(&self) -> Result<u32, PortError> {
/// This is a value that has a range of 0 to 400. A 18" x 30" grey card will return
/// a value of approximately 75 in typical room lighting.
///
/// This sensor reading is unusual, as it is entirely unitless with the seemingly arbitrary
/// range of 0-400 existing due to VEXCode's [`vex::sizeType`] enum having four variants. It's
/// unknown what the sensor is *actually* measuring here either, so use this data with a grain
/// of salt.
///
/// [`vex::sizeType`]: https://api.vexcode.cloud/v5/search/sizeType/sizeType/enum
pub fn relative_size(&self) -> Result<u32, PortError> {
Ok(bail_on!(PROS_ERR, unsafe {
pros_sys::distance_get_object_size(self.port.index())
}) as u32)
}

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

Ok(confidence / 63.0)
}
}

Expand Down

0 comments on commit 710fede

Please sign in to comment.