HoloLib
High-performance holonomic (X-Drive) control library for VEX V5
Loading...
Searching...
No Matches
motion_cancel_helper.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4namespace hololib {
5/**
6 * @class MotionCancelHelper
7 *
8 * @brief This class exists to simplify the cancellation logic in a motion function.
9 */
11 public:
12 /**
13 * @brief Construct a new Motion Cancel Helper object
14 *
15 * @param period how often to update (in millis)
16 *
17 * @b Example:
18 * @code {.cpp}
19 * void myMotion() {
20 * // construct the cancellation helper
21 * hololib::MotionCancelHelper helper(10);
22 * }
23 * @endcode
24 */
25 MotionCancelHelper(uint32_t period);
26 /**
27 * @brief wait a certain amount of time
28 *
29 * This function will return true normally. However, if the task has been notified
30 * (the motion handler requests the motion to end), or if the competition state changes,
31 * the task will return false, indicating that the motion should end.
32 *
33 * This function is meant to be used within the while loop of a motion function.
34 * While waiting, other tasks can execute.
35 * The amount of time it waits is dependent on how long each iteration of the
36 * while loop its in takes. See example below.
37 *
38 * @returns true if the motion should continue, false otherwise
39 *
40 * @b Example:
41 * @code {.cpp}
42 * void myMotion() {
43 * // create an instance of the motion cancellation helper
44 * hololib::MotionCancelHelper helper(10_msec);
45 *
46 * // if the loop starts at a global time of e.g 2015 msec, and each iteration
47 * // of the while loop takes 3 msec, the loop will still iterate at 2025,
48 * // 2035, 2045, etc.
49 * while(helper.wait()) {
50 * // motion stuff here
51 * }
52 *
53 * // put stuff that should happen when the motion ends here
54 * // e.g deallocating memory, log a message saying its done,
55 * // etc
56 * }
57 * @endcode
58 */
59 bool wait();
60 /**
61 * @brief Get the amount of time between the current iteration and the last iteration
62 *
63 * @return Time the time between the current iteration and the last iteration
64 *
65 * @b Example:
66 * @code {.cpp}
67 * void myMotion() {
68 * // create an instance of the motion cancellation helper
69 * hololib::MotionCancelHelper helper(10);
70 *
71 * while(helper.wait()) {
72 * helper.getDelta(); // this will return 10 milliseconds unless there isn't enough CPU time
73 * }
74 * }
75 * @endcode
76 */
77 uint32_t getDelta();
78 private:
79 bool m_firstIteration = true;
80 uint32_t m_prevTime;
81 uint32_t m_prevPrevTime = 0;
82 const int m_originalCompStatus;
83 const uint32_t m_period;
84};
85} // namespace hololib
This class exists to simplify the cancellation logic in a motion function.
bool wait()
wait a certain amount of time
uint32_t getDelta()
Get the amount of time between the current iteration and the last iteration.
MotionCancelHelper(uint32_t period)
Construct a new Motion Cancel Helper object.