DriveBase

Struct DriveBase 

Source
pub struct DriveBase<'a> { /* private fields */ }
Expand description

A pybricks-like DriveBase.

Using gyroscope(s) is highly recommended in order to get the most accurate actions

§Examples

use ev3dev_rs::parameters::{Direction, MotorPort, SensorPort, Stop};
use ev3dev_rs::pupdevices::{Motor, GyroSensor};
use ev3dev_rs::robotics::DriveBase;

let left = Motor::new(MotorPort::OutA, Direction::CounterClockwise)?;
let right = Motor::new(MotorPort::OutD, Direction::CounterClockwise)?;

// no gyro
let drive = DriveBase::new(&left, &right, 62.4, 130.5)?;

// with gyro
let gyro = GyroSensor::new(SensorPort::In1)?;

let drive = DriveBase::new(&left, &right, 62.4, 130.5)?.with_gyro(&gyro)?;

// you have to explicitly enable the gyro
drive.use_gyro(true)?;

// default is 500
drive.set_straight_speed(600)?;

// default should be coast
// unlike pybricks, the stop action doesn't affect whether the robot tracks it's position and heading
drive.set_stop_action(Stop::Hold)?;

drive.straight(500).await?;
drive.turn(90).await?;

Implementations§

Source§

impl<'a> DriveBase<'a>

Source

pub fn new<Number>( left_motor: &'a Motor, right_motor: &'a Motor, wheel_diameter: Number, axle_track: Number, ) -> Ev3Result<Self>
where Number: ToFixed,

Creates a new DriveBase with the defined parameters.

Wheel diameter and axle track are in mm.

Using a gyroscope is highly recommended, see with_gyro or with_gyros.

Source

pub fn with_gyro<'b>(self, gyro_sensor: &'b GyroSensor) -> Ev3Result<Self>
where 'b: 'a,

Adds a single gyro sensor to the DriveBase.

§Examples
use ev3dev_rs::parameters::{Direction, MotorPort, SensorPort, Stop};

use ev3dev_rs::pupdevices::{Motor, GyroSensor};

use ev3dev_rs::robotics::DriveBase;

let left = Motor::new(MotorPort::OutA, Direction::CounterClockwise)?;

let right = Motor::new(MotorPort::OutD, Direction::CounterClockwise)?;

let gyro = GyroSensor::new(SensorPort::In1)?;

let drive = DriveBase::new(&left, &right, 62.4, 130.5)?.with_gyro(&gyro)?;

// you have to explicitly enable the gyro

drive.use_gyro(true)?;
Source

pub fn with_gyros<'b>( self, gyro_sensors: Vec<&'b GyroSensor>, ) -> Ev3Result<Self>
where 'b: 'a,

Adds multiple gyro sensors to the DriveBase.

§Examples
use ev3dev_rs::parameters::{Direction, MotorPort, SensorPort, Stop};

use ev3dev_rs::pupdevices::{Motor, GyroSensor};

use ev3dev_rs::robotics::DriveBase;

let left = Motor::new(MotorPort::OutA, Direction::CounterClockwise)?;

let right = Motor::new(MotorPort::OutD, Direction::CounterClockwise)?;

let left_gyro = GyroSensor::new(SensorPort::In1)?;

let right_gyro = GyroSensor::new(SensorPort::In4)?;

let drive = DriveBase::new(&left, &right, 62.4, 130.5)?.with_gyros(vec![ &left_gyro, &right_gyro ])?;

// you have to explicitly enable the gyro

drive.use_gyro(true)?;
Source

pub fn use_gyro(&self, use_gyro: bool) -> Ev3Result<()>

True makes the DriveBase use the gyro, while false makes the DriveBase use the motor encoders.

Using the gyro is highly recommended for accurate drive actions.

Source

pub fn set_straight_speed<Number>(&self, straight_speed: Number)
where Number: ToFixed,

Sets the straight speed in motor degrees per second.

The default is 500 and the max is 1000.

Source

pub fn set_turn_speed<Number>(&self, turn_speed: Number)
where Number: ToFixed,

Sets the max turn speed in motor degrees per second.

The default is 500 and the max is 1000.

Source

pub fn set_ramp_up_setpoint(&self, sp: u32) -> Ev3Result<()>

Units are in milliseconds and must be positive.

When set to a non-zero value, the motor speed will increase from 0 to 100% of max_speed over the span of this setpoint.

This is especially useful for avoiding wheel slip.

The default for DriveBase motors 2000.

Source

pub fn set_ramp_down_setpoint(&self, sp: u32) -> Ev3Result<()>

Units are in milliseconds and must be positive.

When set to a non-zero value, the motor speed will decrease from 0 to 100% of max_speed over the span of this setpoint.

This is especially useful for avoiding wheel slip.

The default for DriveBase motors 1800.

Source

pub fn set_stop_action(&self, action: Stop) -> Ev3Result<()>

Sets the stop action of the DriveBase

Unlike pybricks, this doesn’t affect whether the DriveBase tracks heading and distance

Source

pub fn distance_pid_settings<Number>( &self, kp: Number, ki: Number, kd: Number, integral_deadzone: Number, integral_rate: Number, )
where Number: ToFixed,

Sets the distance PID settings

default: 10, 0, 8, 0, 0

Source

pub fn heading_pid_settings<Number>( &self, kp: Number, ki: Number, kd: Number, integral_deadzone: Number, integral_rate: Number, )
where Number: ToFixed,

Sets the heading PID settings

default: 10, 0, 5, 0, 0

Source

pub fn stop(&self) -> Ev3Result<()>

Stops the DriveBase with the selected stop action.

Async driving actions automatically do this

Source

pub async fn straight<Number>(&self, distance: Number) -> Ev3Result<()>
where Number: ToFixed,

Drives straight by the given distance in mm.

Source

pub async fn turn<Number>(&self, angle: Number) -> Ev3Result<()>
where Number: ToFixed,

Turns by the angle in degrees.

Source

pub async fn curve<Number>( &self, radius: Number, angle: Number, ) -> Ev3Result<()>
where Number: ToFixed,

Curves with the given radius and a target angle.

Source

pub async fn veer<Number>( &self, radius: Number, distance: Number, ) -> Ev3Result<()>
where Number: ToFixed,

Turns with the given radius and distance.

Source

pub async fn find_calibrated_axle_track<Number>( &mut self, margin_of_error: Number, ) -> Ev3Result<I32F32>
where Number: ToFixed,

Experimental function to find the best axle track for the robot

This should print out the ideal axle track once it is finished testing.

If you are having trouble with inaccurate heading readings due to wheel slipping, see set_ramp_up_setpoint.

Note that the value can vary wildly based on surface.

Auto Trait Implementations§

§

impl<'a> !Freeze for DriveBase<'a>

§

impl<'a> !RefUnwindSafe for DriveBase<'a>

§

impl<'a> !Send for DriveBase<'a>

§

impl<'a> !Sync for DriveBase<'a>

§

impl<'a> Unpin for DriveBase<'a>

§

impl<'a> !UnwindSafe for DriveBase<'a>

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> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
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<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
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.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.