Utils

Timer

class Timer

Public Functions

Timer(uint32_t time)

Construct a new Timer.

Example

// create a timer that will wait for 1 second
Timer timer(1000);

Note

the timer will start counting down as soon as it is created

Note

the timer constructor is non-blocking so code after it will be executed immediately

Note

if the timer is constructed in a global scope, its behavior is undefined. You can call set() before using the timer if you absolutely need to construct it in a global scope

Parameters:

time – how long to wait, in milliseconds

uint32_t getTimeSet()

Get the amount of time the timer was set to.

Example

// create a timer that will wait for 1 second
Timer timer(1000);
// get the time the timer was set to
const uint32_t time = timer.getTimeSet(); // time = 1000

Returns:

uint32_t time, in milliseconds

uint32_t getTimeLeft()

Get the amount of time left on the timer.

Example

// create a timer that will wait for 1 second
Timer timer(1000);
// delay for 300ms
pros::delay(300);
// get the time left on the timer
const uint32_t time = timer.getTimeLeft(); // time = 700

Returns:

uint32_t time in milliseconds

uint32_t getTimePassed()

Get the amount of time passed on the timer.

Example

// create a timer that will wait for 1 second
Timer timer(1000);
// delay for 300ms
pros::delay(300);
// get the time passed on the timer
const uint32_t time = timer.getTimePassed(); // time = 300

Returns:

uint32_t time in milliseconds

bool isDone()

Get whether the timer is done or not.

Example

// create a timer that will wait for 1 second
Timer timer(1000);
// delay for 500ms
pros::delay(500);
// check if the timer is done
const bool done = timer.isDone(); // done = false
// delay for another 500ms
pros::delay(500);
// check if the timer is done
const bool done = timer.isDone(); // done = true

Returns:

true the timer is done

Returns:

false the timer is not done

bool isPaused()

Get whether the timer is paused or not.

Example

// create a timer that will wait for 1 second
Timer timer(1000);
// pause the timer
timer.pause();
// check if the timer is paused
bool paused = timer.isPaused(); // paused = true
// resume the timer
timer.resume();
// check if the timer is paused
paused = timer.isPaused(); // paused = false

Returns:

true the timer is paused

Returns:

false the timer is not paused

void set(uint32_t time)

Set the amount of time the timer should count down. Resets the timer.

Example

// create a timer that will wait for 1 second
Timer timer(1000);
// set the timer to wait for 2 seconds
timer.set(2000);

Parameters:

time – time in milliseconds

void reset()

reset the timer

Example

// create a timer that will wait for 1 second
Timer timer(1000);
// delay for 500ms
pros::delay(500);
// reset the timer
timer.reset();
// delay for another 500ms
pros::delay(500);
// check if the timer is done
const bool done = timer.isDone(); // done = false

void pause()

pause the timer

Example

// create a timer that will wait for 1 second
Timer timer(1000);
// pause the timer
timer.pause();
// delay for 2000ms
pros::delay(2000);
// check if the timer is done
const bool done = timer.isDone(); // done = false

void resume()

resume the timer

Example

// create a timer that will wait for 1 second
Timer timer(1000);
// pause the timer
timer.pause();
// delay for 500ms
pros::delay(500);
// resume the timer
timer.resume();
// delay for another 500ms
pros::delay(500);
// check if the timer is done
const bool done = timer.isDone(); // done = false

void waitUntilDone()

wait until the timer is done

Example

// create a timer that will wait for 1 second
Timer timer(1000);
// wait until the timer is done
timer.waitUntilDone();
std::cout << "done!" << std::endl;

PID

class PID

Public Functions

PID(float kP, float kI, float kD, float windupRange = 0, bool signFlipReset = false)

Construct a new PID.

Example

// create a PID
PID pid(5, // kP
        0.01, // kI
        20, // kD
        5, // integral anti windup range
        false); // don't reset integral when sign of error flips

Parameters:
  • kP – proportional gain

  • kI – integral gain

  • kD – derivative gain

  • windupRange – integral anti windup range

  • signFlipReset – whether to reset integral when sign of error flips

float update(float error)

Update the PID.

Example

void opcontrol() {
    // create a PID
    PID pid(5, 0, 20);
    // give the pid a test input
    // the pid will then return an output
    float output = pid.update(10);
}

Parameters:

error – target minus position - AKA error

Returns:

float output

void reset()

reset integral, derivative, and prevTime

Example

void opcontrol() {
    // create a PID
    PID pid(5, 0, 20);
    // give the pid a test input
    // the pid will then return an output
    float output = pid.update(10);
    // reset the pid
    pid.reset();
}

Misc

float lemlib::slew(float target, float current, float maxChange)

Slew rate limiter.

Example

float limited = slew(100, // target value
                     0, // current value
                     10); // maximum allowed change
// limited == 10
float limited2 = slew(4, // target value
                      0, // current value
                      10); // maximum allowed change
// limited2 == 4

Parameters:
  • target – target value

  • current – current value

  • maxChange – maximum change. No maximum if set to 0

Returns:

float - the limited value

constexpr float lemlib::radToDeg(float rad)

Convert radians to degrees.

Example

radToDeg(M_PI); // returns 180

Parameters:

rad – radians

Returns:

float degrees

constexpr float lemlib::degToRad(float deg)

Convert degrees to radians.

Example

degToRad(180); // returns 3.14159... (pi)

Parameters:

deg – degrees

Returns:

float radians

constexpr float lemlib::sanitizeAngle(float angle, bool radians = true)

Sanitize an angle so its positive and within the range of 0 to 2pi or 0 to 360.

Example

// sanitize angle in degrees
sanitizeAngle(-90, false); // returns 270
sanitizeAngle(370, false); // returns 10
// sanitize angle in radians
sanitizeAngle(-M_PI, true); // returns pi
sanitizeAngle(7 * M_PI, true); // returns pi
// you can also use the default value of radians
sanitizeAngle(-M_PI); // returns pi
sanitizeAngle(7 * M_PI); // returns pi

Parameters:
  • angle – the angle to sanitize

  • radians – whether the angle is in radians or no. True by default

Returns:

constexpr float

float lemlib::angleError(float target, float position, bool radians = true, AngularDirection direction = AngularDirection::AUTO)

Calculate the error between 2 angles. Useful when calculating the error between 2 headings.

Example

angleError(10, 350, false); // returns 20
angleError(350, 10, false); // returns -20

Parameters:
  • target – target angle

  • position – position angle

  • radians – true if angle is in radians, false if not. False by default

  • direction – which direction to turn to get to the target angle

Returns:

float wrapped angle

float lemlib::avg(std::vector<float> values)

Return the average of a vector of numbers.

Example

std::vector<float> values = {1, 2, 3, 4, 5};
avg(values); // returns 3

Parameters:

values

Returns:

float

float lemlib::ema(float current, float previous, float smooth)

Exponential moving average.

Example

ema(10, 0, 0.5); // returns 5

Parameters:
  • current – current measurement

  • previous – previous output

  • smooth – smoothing factor (0-1). 1 means no smoothing, 0 means no change

Returns:

float - the smoothed output

float lemlib::getCurvature(Pose pose, Pose other)

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

This is a very niche function that is only used in Pure Pursuit and Boomerang. It calculates the curvature of a circle that is tangent to the first pose and intersects the second pose. It’s also signed to indicate whether the robot should turn clockwise or counter-clockwise to get to the second pose

Example

Pose pose = {0, 0, 0};
Pose other = {0, 10, 0};
float curvature = getCurvature(pose, other);

Note

The circle will be tangent to the theta value of the first pose

Note

The curvature is signed. Positive curvature means the circle is going clockwise, negative means counter-clockwise

Note

Theta has to be in radians and in standard form. That means 0 is right and increases counter-clockwise

Parameters:
  • pose – the first pose

  • other – the second pose

Returns:

float curvature