Struct RotationSensor

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

A rotation sensor plugged into a Smart Port.

Implementations§

Source§

impl RotationSensor

Source

pub const MIN_DATA_INTERVAL: Duration

The minimum data rate that you can set a rotation sensor to.

Source

pub const TICKS_PER_REVOLUTION: u32 = 36_000u32

The amount of unique sensor readings per one revolution of the sensor.

Source

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);
}
Source

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());
}
Source

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));
}
Source

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());
}
Source

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);
}
Source

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",
        }
    );
}
Source

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());
    }
}
Source

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));
    }
}
Source

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
        );
    }
}
Source

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<RotationSensor> for SmartPort

Source§

fn from(device: RotationSensor) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for RotationSensor

Source§

fn eq(&self, other: &RotationSensor) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl SmartDevice for RotationSensor

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 RotationSensor

Source§

impl StructuralPartialEq for RotationSensor

Source§

impl Sync for RotationSensor

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.