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>
impl<'a> DriveBase<'a>
Sourcepub fn new<Number>(
left_motor: &'a Motor,
right_motor: &'a Motor,
wheel_diameter: Number,
axle_track: Number,
) -> Ev3Result<Self>where
Number: ToFixed,
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.
Sourcepub fn with_gyro<'b>(self, gyro_sensor: &'b GyroSensor) -> Ev3Result<Self>where
'b: 'a,
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)?;Sourcepub fn with_gyros<'b>(
self,
gyro_sensors: Vec<&'b GyroSensor>,
) -> Ev3Result<Self>where
'b: 'a,
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)?;Sourcepub fn use_gyro(&self, use_gyro: bool) -> Ev3Result<()>
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.
Sourcepub fn set_straight_speed<Number>(&self, straight_speed: Number)where
Number: ToFixed,
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.
Sourcepub fn set_turn_speed<Number>(&self, turn_speed: Number)where
Number: ToFixed,
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.
Sourcepub fn set_ramp_up_setpoint(&self, sp: u32) -> Ev3Result<()>
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.
Sourcepub fn set_ramp_down_setpoint(&self, sp: u32) -> Ev3Result<()>
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.
Sourcepub fn set_stop_action(&self, action: Stop) -> Ev3Result<()>
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
Sourcepub fn distance_pid_settings<Number>(
&self,
kp: Number,
ki: Number,
kd: Number,
integral_deadzone: Number,
integral_rate: Number,
)where
Number: ToFixed,
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
Sourcepub fn heading_pid_settings<Number>(
&self,
kp: Number,
ki: Number,
kd: Number,
integral_deadzone: Number,
integral_rate: Number,
)where
Number: ToFixed,
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
Sourcepub fn stop(&self) -> Ev3Result<()>
pub fn stop(&self) -> Ev3Result<()>
Stops the DriveBase with the selected stop action.
Async driving actions automatically do this
Sourcepub async fn straight<Number>(&self, distance: Number) -> Ev3Result<()>where
Number: ToFixed,
pub async fn straight<Number>(&self, distance: Number) -> Ev3Result<()>where
Number: ToFixed,
Drives straight by the given distance in mm.
Sourcepub async fn turn<Number>(&self, angle: Number) -> Ev3Result<()>where
Number: ToFixed,
pub async fn turn<Number>(&self, angle: Number) -> Ev3Result<()>where
Number: ToFixed,
Turns by the angle in degrees.
Sourcepub async fn curve<Number>(
&self,
radius: Number,
angle: Number,
) -> Ev3Result<()>where
Number: ToFixed,
pub async fn curve<Number>(
&self,
radius: Number,
angle: Number,
) -> Ev3Result<()>where
Number: ToFixed,
Curves with the given radius and a target angle.
Sourcepub async fn veer<Number>(
&self,
radius: Number,
distance: Number,
) -> Ev3Result<()>where
Number: ToFixed,
pub async fn veer<Number>(
&self,
radius: Number,
distance: Number,
) -> Ev3Result<()>where
Number: ToFixed,
Turns with the given radius and distance.
Sourcepub async fn find_calibrated_axle_track<Number>(
&mut self,
margin_of_error: Number,
) -> Ev3Result<I32F32>where
Number: ToFixed,
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.