Skip to Content
VectorLib

util

Public functions and their Rust signatures.


slew

pub fn slew(target: f32, current: f32, max_change: f32) -> f32

Slew rate limiter.

  • target — target value
  • current — current value
  • max_change — maximum change. No maximum if set to 0

Returns the limited value.


assert_eq!(slew(100.0, 0.0, 10.0), 10.0);
assert_eq!(slew(100.0, 0.0, 0.0), 100.0);

Parameters

Prop

Type

Returns

f32

View source

rad_to_deg

pub fn rad_to_deg(rad: f32) -> f32

Convert radians to degrees (rad * 180 / M_PI).

Parameters

Prop

Type

Returns

f32

View source

deg_to_rad

pub fn deg_to_rad(deg: f32) -> f32

Convert degrees to radians (deg * M_PI / 180).

Parameters

Prop

Type

Returns

f32

View source

sanitize_angle

pub fn sanitize_angle(angle: f32, radians: bool) -> f32

Bound an angle to [0, 2π) (radians) or [0, 360) (degrees).

Parameters

Prop

Type

Returns

f32

View source

angle_error

pub fn angle_error(target: f32, position: f32, radians: bool, direction: AngularDirection) -> f32

Calculate the error between two angles. Useful when calculating the error between target and actual heading.

  • target — target angle
  • position — position angle
  • radians — true if angles are in radians (LemLib's default), false for degrees
  • direction — which direction to turn; AngularDirection::Auto picks the shortest path

assert_eq!(angle_error(10.0, 350.0, false, AngularDirection::Auto), 20.0);
assert_eq!(angle_error(350.0, 10.0, false, AngularDirection::CwClockwise), 340.0);

Parameters

Prop

Type

Returns

f32

View source

sgn

pub fn sgn<T: Signum>(value: T) -> T

Return the sign of a number: -1 if negative, 1 otherwise (including 0 and NaN).

This is LemLib's sgn, which is not f32::signum: sgn(0.0) == 1.0 and sgn(-0.0) == 1.0.

Parameters

Prop

Type

Returns

T

View source

avg

pub fn avg(values: &[f32]) -> f32

Return the average of a slice of numbers. An empty slice gives NaN, as in LemLib.

Parameters

Prop

Type

Returns

f32

View source

ema

pub fn ema(current: f32, previous: f32, smooth: f32) -> f32

Exponential moving average.

  • current — current measurement
  • previous — previous output
  • smooth — smoothing factor (0–1). 1 means no smoothing, 0 means no change.

Parameters

Prop

Type

Returns

f32

View source

get_curvature

pub fn get_curvature(pose: Pose, other: Pose) -> f32

Get the signed curvature of a circle that intersects the first pose and the second pose.

The circle is also tangent to the theta of the first pose (in radians, standard position). The sign follows LemLib's formula: a point to the left of the heading gives a negative curvature, a point to the right a positive one.

Parameters

Prop

Type

Returns

f32

View source