Skip to Content
VectorLib

Chassis

vectorlib::Chassis — fields, methods, parameters and return types.


pub struct Chassis

The chassis: drivetrain, controllers, odometry and every motion.

Chassis is a cheap handle (an Rc). Clone it to use the chassis from several tasks, e.g. a brain-screen task and autonomous, the same way a LemLib lemlib::Chassis global is used from several pros::Tasks.

View source

Methods

Prop

Type

new

pub fn new(
        drivetrain: Drivetrain,
        lateral_settings: ControllerSettings,
        angular_settings: ControllerSettings,
        sensors: OdomSensors,
        throttle_curve: impl DriveCurve + 'static,
        steer_curve: impl DriveCurve + 'static,
    ) -> Self

Create a chassis.

  • drivetrain — drivetrain to be used for the chassis
  • lateral_settings — settings for the lateral controller
  • angular_settings — settings for the angular controller
  • sensors — sensors to be used for odometry
  • throttle_curve — curve applied to throttle input during driver control (LemLib default: DEFAULT_DRIVE_CURVE(crate::DEFAULT_DRIVE_CURVE))
  • steer_curve — curve applied to steer input during driver control (LemLib default: DEFAULT_DRIVE_CURVE(crate::DEFAULT_DRIVE_CURVE))

Parameters

Prop

Type

Returns

Self

View source

calibrate

pub async fn calibrate(&self, calibrate_imu: bool)

Calibrate the chassis sensors. This should be called at the start of the program.

  • calibrate_imu — whether the IMU should be calibrated (LemLib default true)

Calibrates the IMU (up to 5 attempts, rumbling "---" on each failure and falling back to tracking wheels / motor encoders if all fail). Substitutes the drivetrain motors for any missing vertical tracking wheel, resets every wheel, then starts odometry and rumbles ".".

Parameters

Prop

Type

Returns

()

View source

set_pose

pub fn set_pose(&self, x: f32, y: f32, theta: f32, radians: bool)

Set the pose of the chassis.

  • radians — true if theta is in radians, false if degrees (LemLib default false)

Parameters

Prop

Type

Returns

()

View source

set_pose_from

pub fn set_pose_from(&self, pose: Pose, radians: bool)

Set the pose of the chassis from a Pose (LemLib's setPose(Pose, bool) overload).

Parameters

Prop

Type

Returns

()

View source

get_pose

pub fn get_pose(&self) -> Pose

Get the pose of the chassis in degrees (LemLib's getPose() with default arguments).

Returns

Pose

View source

get_pose_with

pub fn get_pose_with(&self, radians: bool, standard_pos: bool) -> Pose

Get the pose of the chassis.

  • radians — whether theta should be in radians (true) or degrees (false)
  • standard_pos — whether theta should be in standard position (0 = +x, counter-clockwise positive) instead of compass heading

Parameters

Prop

Type

Returns

Pose

View source

wait_until

pub async fn wait_until(&self, dist: f32)

Wait until the robot has traveled a certain distance along the path.

Units are inches if the current motion is move_to_point, move_to_pose or follow, degrees for everything else.

Parameters

Prop

Type

Returns

()

View source

wait_until_done

pub async fn wait_until_done(&self)

Wait until the robot has completed the motion.

Returns

()

View source

set_brake_mode

pub fn set_brake_mode(&self, mode: BrakeMode)

Set the brake mode of the drivetrain motors.

Parameters

Prop

Type

Returns

()

View source

cancel_motion

pub async fn cancel_motion(&self)

Cancel the currently running motion. If there is a queued motion, it will run.

Returns

()

View source

cancel_all_motions

pub async fn cancel_all_motions(&self)

Cancel all motions, even those that are queued. After this, the chassis will not be in motion.

Returns

()

View source

is_in_motion

pub fn is_in_motion(&self) -> bool

Whether a motion is currently running.

Returns

bool

View source

reset_local_position

pub fn reset_local_position(&self)

Reset the x and y position of the robot without changing the heading.

Returns

()

View source

lateral_pid

pub fn lateral_pid(&self) -> RefMut<'_, Pid>

The lateral PID, exposed so advanced users can implement things like gain scheduling. Changes are immediate and affect a motion in progress.

Do not hold the returned borrow across an .await.

Returns

RefMut<'_, Pid>

View source

angular_pid

pub fn angular_pid(&self) -> RefMut<'_, Pid>

The angular PID, exposed so advanced users can implement things like gain scheduling. Changes are immediate and affect a motion in progress.

Do not hold the returned borrow across an .await.

Returns

RefMut<'_, Pid>

View source

drivetrain

pub fn drivetrain(&self) -> &Drivetrain

The drivetrain this chassis drives.

Returns

&Drivetrain

View source

tank

pub fn tank(&self, left: i32, right: i32, disable_drive_curve: bool)

Control the robot during driver control using the tank drive scheme: one joystick axis drives the left side, the other drives the right side.

  • left — speed to move the left wheels, -127 to 127
  • right — speed to move the right wheels, -127 to 127
  • disable_drive_curve — use a linear curve with no deadzone or minimum power (LemLib default false)
loop {
    let state = controller.state().unwrap_or_default();
    chassis.tank(state.left_stick.y_raw().into(), state.right_stick.y_raw().into(), false);
    sleep(Duration::from_millis(25)).await;
}

Parameters

Prop

Type

Returns

()

View source

arcade

pub fn arcade(
        &self,
        mut throttle: i32,
        mut turn: i32,
        disable_drive_curve: bool,
        desaturate_bias: f32,
    )

Control the robot during driver control using the arcade drive scheme: one joystick axis drives forwards/backwards, the other turns.

  • throttle — speed to move forward or backward, -127 to 127
  • turn — speed to turn, -127 to 127
  • disable_drive_curve — use a linear curve with no deadzone or minimum power (LemLib default false)
  • desaturate_bias — how much to favor angular motion over lateral motion when the motors are saturated. 0 fully prioritizes lateral, 1 fully prioritizes angular (LemLib default 0.5)
loop {
    let state = controller.state().unwrap_or_default();
    chassis.arcade(state.left_stick.y_raw().into(), state.right_stick.x_raw().into(), false, 0.5);
    sleep(Duration::from_millis(25)).await;
}

Parameters

Prop

Type

Returns

()

View source

curvature

pub fn curvature(&self, mut throttle: i32, mut turn: i32, disable_drive_curve: bool)

Control the robot during driver control using the curvature drive scheme. Like arcade, except the second axis sets the radius of the turn instead of its speed, so the driver can accelerate through a turn without changing its radius. Falls back to arcade when throttle is zero.

  • throttle — speed to move forward or backward, -127 to 127
  • turn — curvature of the turn, -127 to 127
  • disable_drive_curve — use a linear curve with no deadzone or minimum power (LemLib default false)

Parameters

Prop

Type

Returns

()

View source

calculate_odom_offsets

pub async fn calculate_odom_offsets(&self) -> [f32; 4]

A 10-20 second routine that measures the offsets of the tracking wheels. LemLib adapted version of the EZ-Template utility (LemLib-TitanTron's calculateOdomOffsets).

The robot turns in place ten times, alternating between headings 90 and 270 at half power, and divides the distance each wheel has rolled by the angle turned.

Returns the offsets of [vertical1, vertical2, horizontal1, horizontal2]. A wheel that is not connected reports -9999.

The angular PID must be tuned, and so must the IMU scale (see ScalingImu(crate::ScalingImu)).

As in TitanTron, the wheels are reset once before the first turn, not before each one, so each turn divides the total distance rolled so far by that turn's angle alone.

Returns

[f32; 4]

View source

turn_to_point

pub fn turn_to_point(&self, x: f32, y: f32, timeout: i32) -> TurnToPoint<'_>

Turn the chassis so it is facing the target point.

  • x, y — target location, inches
  • timeout — longest time the robot can spend moving, ms
chassis.set_pose(0.0, 0.0, 0.0, false);
// turn the robot to face the point x = 45, y = -45, with a timeout of 1000ms
chassis.turn_to_point(45.0, -45.0, 1000).await;
// ... but face the point with the back of the robot
chassis.turn_to_point(45.0, -45.0, 1000).forwards(false).await;
// ... with a maximum speed of 60
chassis.turn_to_point(-20.0, 32.5, 2000).max_speed(60).await;
// ... counterclockwise
chassis.turn_to_point(-30.0, 22.5, 1500).direction(AngularDirection::CcwCounterclockwise).await;
// ... with a minSpeed of 60, exiting within 5 degrees of the target
chassis.turn_to_point(7.5, 7.5, 2000).min_speed(60).early_exit_range(5.0).await;

Parameters

Prop

Type

Returns

TurnToPoint<'_>

View source

turn_to_heading

pub fn turn_to_heading(&self, theta: f32, timeout: i32) -> TurnToHeading<'_>

Turn the chassis so it is facing the target heading (degrees).

chassis.turn_to_heading(135.0, 1000).await;
chassis.turn_to_heading(230.5, 2000).max_speed(60).await;
chassis.turn_to_heading(-90.0, 1500).direction(AngularDirection::CcwCounterclockwise).await;
chassis.turn_to_heading(90.0, 500).max_speed(60).min_speed(20).await;

Parameters

Prop

Type

Returns

TurnToHeading<'_>

View source

swing_to_heading

pub fn swing_to_heading(
        &self,
        theta: f32,
        locked_side: DriveSide,
        timeout: i32,
    ) -> SwingToHeading<'_>

Turn the chassis so it is facing the target heading, moving only one side of the drivetrain. locked_side is the side that holds still.

chassis.swing_to_heading(135.0, DriveSide::Left, 1000).await;
chassis.swing_to_heading(230.5, DriveSide::Right, 2000).max_speed(60.0).await;

Parameters

Prop

Type

Returns

SwingToHeading<'_>

View source

swing_to_point

pub fn swing_to_point(
        &self,
        x: f32,
        y: f32,
        locked_side: DriveSide,
        timeout: i32,
    ) -> SwingToPoint<'_>

Turn the chassis so it is facing the target point, moving only one side of the drivetrain. locked_side is the side that holds still.

chassis.swing_to_point(45.0, -45.0, DriveSide::Left, 1000).await;
chassis.swing_to_point(45.0, -45.0, DriveSide::Right, 1000).forwards(false).await;

Parameters

Prop

Type

Returns

SwingToPoint<'_>

View source

move_to_pose

pub fn move_to_pose(&self, x: f32, y: f32, theta: f32, timeout: i32) -> MoveToPose<'_>

Move the chassis towards the target pose using the boomerang controller.

  • x, y — target location, inches
  • theta — target heading, degrees
  • timeout — longest time the robot can spend moving, ms
chassis.move_to_pose(20.0, 15.0, 90.0, 4000).await;
chassis.move_to_pose(20.0, 15.0, 90.0, 4000).forwards(false).await;
chassis.move_to_pose(-20.0, 32.5, 90.0, 4000).max_speed(60.0).await;
chassis.move_to_pose(0.0, 0.0, 0.0, 4000).lead(0.3).await;

Parameters

Prop

Type

Returns

MoveToPose<'_>

View source

move_to_point

pub fn move_to_point(&self, x: f32, y: f32, timeout: i32) -> MoveToPoint<'_>

Move the chassis towards a target point.

chassis.move_to_point(20.0, 15.0, 4000).await;
chassis.move_to_point(20.0, 15.0, 4000).forwards(false).await;
chassis.move_to_point(10.0, 10.0, 4000).max_speed(60.0).min_speed(20.0).await;

Parameters

Prop

Type

Returns

MoveToPoint<'_>

View source

follow

pub fn follow(&self, path: impl Into<FollowPath>, lookahead: f32, timeout: i32) -> Follow<'_>

Move the chassis along a path with pure pursuit.

  • path — the path asset to follow (see asset!(crate::asset!)), or, as in LemLib-TitanTron, a Vec<Pose> of points with the speed in theta
  • lookahead — the lookahead distance, inches. Larger values make the robot move faster but follow the path less accurately
  • timeout — the maximum time the robot can spend moving, ms
vectorlib::asset!(MY_PATH_TXT = "myPath.txt"); // static/myPath.txt
chassis.follow(&MY_PATH_TXT, 10.0, 4000).await;
chassis.follow(&MY_PATH_TXT, 10.0, 4000).forwards(false).await;
// LemLib-TitanTron: points built at runtime
let points = vec![Pose::new(0.0, 0.0, 127.0), Pose::new(0.0, 24.0, 127.0), Pose::new(0.0, 48.0, 0.0)];
chassis.follow(points, 10.0, 4000).await;

Parameters

Prop

Type

Returns

Follow<'_>

View source