HoloLib
High-performance holonomic (X-Drive) control library for VEX V5
Loading...
Searching...
No Matches
hololib::MotionCancelHelper Class Reference

This class exists to simplify the cancellation logic in a motion function. More...

#include <motion_cancel_helper.hpp>

Public Member Functions

 MotionCancelHelper (uint32_t period)
 Construct a new Motion Cancel Helper object.
 
bool wait ()
 wait a certain amount of time
 
uint32_t getDelta ()
 Get the amount of time between the current iteration and the last iteration.
 

Detailed Description

This class exists to simplify the cancellation logic in a motion function.

Definition at line 10 of file motion_cancel_helper.hpp.

Constructor & Destructor Documentation

◆ MotionCancelHelper()

hololib::MotionCancelHelper::MotionCancelHelper ( uint32_t  period)

Construct a new Motion Cancel Helper object.

Parameters
periodhow often to update (in millis)

Example:

void myMotion() {
// construct the cancellation helper
}
This class exists to simplify the cancellation logic in a motion function.

Member Function Documentation

◆ wait()

bool hololib::MotionCancelHelper::wait ( )

wait a certain amount of time

This function will return true normally. However, if the task has been notified (the motion handler requests the motion to end), or if the competition state changes, the task will return false, indicating that the motion should end.

This function is meant to be used within the while loop of a motion function. While waiting, other tasks can execute. The amount of time it waits is dependent on how long each iteration of the while loop its in takes. See example below.

Returns
true if the motion should continue, false otherwise

Example:

void myMotion() {
// create an instance of the motion cancellation helper
hololib::MotionCancelHelper helper(10_msec);
// if the loop starts at a global time of e.g 2015 msec, and each iteration
// of the while loop takes 3 msec, the loop will still iterate at 2025,
// 2035, 2045, etc.
while(helper.wait()) {
// motion stuff here
}
// put stuff that should happen when the motion ends here
// e.g deallocating memory, log a message saying its done,
// etc
}

◆ getDelta()

uint32_t hololib::MotionCancelHelper::getDelta ( )

Get the amount of time between the current iteration and the last iteration.

Returns
Time the time between the current iteration and the last iteration

Example:

void myMotion() {
// create an instance of the motion cancellation helper
while(helper.wait()) {
helper.getDelta(); // this will return 10 milliseconds unless there isn't enough CPU time
}
}