pub struct RotationSensor { /* private fields */ }
Expand description
A rotation sensor plugged into a Smart Port.
Implementations§
Source§impl RotationSensor
impl RotationSensor
Sourcepub const MIN_DATA_INTERVAL: Duration
pub const MIN_DATA_INTERVAL: Duration
The minimum data rate that you can set a rotation sensor to.
Sourcepub const TICKS_PER_REVOLUTION: u32 = 36_000u32
pub const TICKS_PER_REVOLUTION: u32 = 36_000u32
The amount of unique sensor readings per one revolution of the sensor.
Sourcepub fn new(port: SmartPort, direction: Direction) -> Self
pub fn new(port: SmartPort, direction: Direction) -> Self
Creates a new rotation sensor on the given port.
Whether or not the sensor should be reversed on creation can be specified.
§Examples
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let sensor = RotationSensor::new(peripherals.port_1, Direction::Forward);
}
Sourcepub fn reset_position(&mut self) -> Result<(), PortError>
pub fn reset_position(&mut self) -> Result<(), PortError>
Reset’s the sensor’s position reading to zero.
§Errors
An error is returned if a rotation sensor is not currently connected to the Smart Port.
§Examples
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let mut sensor = RotationSensor::new(peripherals.port_1, Direction::Forward);
println!("Before reset: {:?}", sensor.position());
_ = sensor.reset_position();
println!("After reset: {:?}", sensor.position());
}
Sourcepub fn set_position(&mut self, position: Position) -> Result<(), PortError>
pub fn set_position(&mut self, position: Position) -> Result<(), PortError>
Sets the sensor’s position reading.
§Errors
An error is returned if a rotation sensor is not currently connected to the Smart Port.
§Examples
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let mut sensor = RotationSensor::new(peripherals.port_1, Direction::Forward);
// Set position to 15 degrees.
_ = sensor.set_position(Position::from_degrees(15.0));
}
Sourcepub fn set_direction(
&mut self,
new_direction: Direction,
) -> Result<(), PortError>
pub fn set_direction( &mut self, new_direction: Direction, ) -> Result<(), PortError>
Sets the sensor to operate in a given Direction
.
This determines which way the sensor considers to be “forwards”. You can use the marking on the top of the motor as a reference:
- When
Direction::Forward
is specified, positive velocity/voltage values will cause the motor to rotate with the arrow on the top. Position will increase as the motor rotates with the arrow. - When
Direction::Reverse
is specified, positive velocity/voltage values will cause the motor to rotate against the arrow on the top. Position will increase as the motor rotates against the arrow.
§Errors
- An error is returned if an rotation sensor is not currently connected to the Smart Port.
§Examples
Set the sensor’s direction to Direction::Reverse
.
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let mut sensor = RotationSensor::new(peripherals.port_1, Direction::Forward);
// Reverse the sensor
_ = sensor.set_direction(Direction::Reverse);
}
Reverse the sensor’s direction (set to opposite of the previous direction):
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let mut sensor = RotationSensor::new(peripherals.port_1, Direction::Forward);
// Reverse the sensor
_ = sensor.set_direction(!sensor.direction());
}
Sourcepub fn set_computation_interval(
&mut self,
interval: Duration,
) -> Result<(), PortError>
pub fn set_computation_interval( &mut self, interval: Duration, ) -> Result<(), PortError>
Sets the internal computation speed of the rotation sensor.
This method does NOT change the rate at which user code can read data off the sensor, as the brain will only talk to
the device every 10mS regardless of how fast data is being sent or computed. See RotationSensor::UPDATE_INTERVAL
.
This duration should be above Self::MIN_DATA_INTERVAL
(5 milliseconds).
§Errors
An error is returned if an rotation sensor is not currently connected to the Smart Port.
§Examples
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let mut sensor = RotationSensor::new(peripherals.port_1, Direction::Forward);
// Set to minimum interval.
_ = sensor.set_data_interval(RotationSensor::MIN_DATA_INTERVAL);
}
Sourcepub const fn direction(&self) -> Direction
pub const fn direction(&self) -> Direction
Returns the Direction
of this sensor.
§Examples
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let sensor = RotationSensor::new(peripherals.port_1, Direction::Forward);
println!(
"Sensor's direction is {}",
match sensor.direction() {
Direction::Forward => "forward",
Direction::Reverse => "reverse",
}
);
}
Sourcepub fn position(&self) -> Result<Position, PortError>
pub fn position(&self) -> Result<Position, PortError>
Returns the total number of degrees rotated by the sensor based on direction.
§Errors
An error is returned if an rotation sensor is not currently connected to the Smart Port.
§Examples
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let sensor = RotationSensor::new(peripherals.port_1, Direction::Forward);
if let Ok(position) = sensor.position() {
println!("Position in degrees: {}°", position.as_degrees());
println!("Position in radians: {}°", position.as_radians());
println!("Position in raw ticks (centidegrees): {}°", position.as_ticks(RotationSensor::TICKS_PER_REVOLUTION));
println!("Number of revolutions spun: {}°", position.as_revolutions());
}
}
Sourcepub fn angle(&self) -> Result<Position, PortError>
pub fn angle(&self) -> Result<Position, PortError>
Returns the angle of rotation measured by the sensor.
This value is reported from 0-360 degrees.
§Errors
An error is returned if an rotation sensor is not currently connected to the Smart Port.
§Examples
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let sensor = RotationSensor::new(peripherals.port_1, Direction::Forward);
if let Ok(angle) = sensor.angle() {
println!("Angle in degrees: {}°", angle.as_degrees());
println!("Angle in radians: {}°", angle.as_radians());
println!("Angle in raw ticks (centidegrees): {}°", angle.as_ticks(RotationSensor::TICKS_PER_REVOLUTION));
}
}
Sourcepub fn velocity(&self) -> Result<f64, PortError>
pub fn velocity(&self) -> Result<f64, PortError>
Returns the sensor’s current velocity in degrees per second.
§Errors
An error is returned if an rotation sensor is not currently connected to the Smart Port.
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let sensor = RotationSensor::new(peripherals.port_1, Direction::Forward);
if let Some(velocity) = sensor.velocity() {
println!(
"Velocity in RPM {}",
velocity / 6.0, // 1rpm = 6dps
);
}
}
Sourcepub fn status(&self) -> Result<u32, PortError>
pub fn status(&self) -> Result<u32, PortError>
Returns the sensor’s internal status code.
§Errors
An error is returned if an rotation sensor is not currently connected to the Smart Port.
§Examples
use vexide::prelude::*;
#[vexide::main]
async fn main(peripherals: Peripherals) {
let sensor = RotationSensor::new(peripherals.port_1, Direction::Forward);
if let Ok(status) = sensor.status() {
println!("Status: {:b}", status);
}
}
Trait Implementations§
Source§impl Debug for RotationSensor
impl Debug for RotationSensor
Source§impl From<RotationSensor> for SmartPort
impl From<RotationSensor> for SmartPort
Source§fn from(device: RotationSensor) -> Self
fn from(device: RotationSensor) -> Self
Source§impl PartialEq for RotationSensor
impl PartialEq for RotationSensor
Source§impl SmartDevice for RotationSensor
impl SmartDevice for RotationSensor
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