HoloLib
High-performance holonomic (X-Drive) control library for VEX V5
Loading...
Searching...
No Matches
motion_handler.h
Go to the documentation of this file.
1#pragma once
2
3#include "api.h"
4#include <functional>
5#include <memory>
6#include <queue>
7
8/**
9 * @brief Manages the execution of motion commands asynchronously.
10 *
11 * The MotionHandler class allows queueing of movement commands, which are executed
12 * in a separate PROS task. This permits asynchronous operation without blocking the
13 * main control loop.
14 */
16public:
17 /**
18 * @brief Construct a new MotionHandler object.
19 */
21
22 /**
23 * @brief Enqueue a new motion function.
24 *
25 * @param motion The function to execute.
26 * @param async If false, waits until the motion is done before returning.
27 */
28 void enqueue(std::function<void()> motion, bool async);
29
30 /**
31 * @brief Cancel all pending motions in the queue.
32 */
33 void cancelAll();
34
35 /**
36 * @brief Wait until all queued motions have finished executing.
37 */
39
40 /**
41 * @brief Cancel the currently executing motion.
42 */
44
45 /**
46 * @brief Check if the handler is currently executing a motion.
47 *
48 * @return true if a motion is currently running, false otherwise.
49 */
50 bool isInMotion();
51
52 /**
53 * @brief Get the ID of the last enqueued motion.
54 *
55 * @return uint32_t The ID of the last motion.
56 */
57 uint32_t getLastEnqueuedId() const { return lastEnqueuedId; }
58
59 /**
60 * @brief Get the ID of the currently running motion.
61 *
62 * @return uint32_t The ID of the currently running motion.
63 */
64 uint32_t getCurrentRunningId() const { return currentRunningId; }
65
66 /**
67 * @brief Check if the motion queue is empty.
68 *
69 * @return true if the queue is empty, false otherwise.
70 */
71 bool isQueueEmpty() {
72 mutex.take();
73 bool empty = queue.empty();
74 mutex.give();
75 return empty;
76 }
77
78 /**
79 * @brief Set a callback that is executed whenever a new motion starts.
80 *
81 * @param callback The callback function.
82 */
83 void setOnMotionStart(std::function<void()> callback) {
84 onMotionStartCallback = callback;
85 }
86
87private:
88 std::queue<std::function<void()>> queue;
89 pros::Mutex mutex;
90 pros::Task *task = nullptr;
91 uint32_t lastEnqueuedId = 0;
92 uint32_t currentRunningId = 0;
93 std::function<void()> onMotionStartCallback = nullptr;
94 bool running = false;
95
96 void loop();
97 friend void motionHandlerTask(void *param);
98};
Manages the execution of motion commands asynchronously.
void setOnMotionStart(std::function< void()> callback)
Set a callback that is executed whenever a new motion starts.
MotionHandler()
Construct a new MotionHandler object.
void cancelAll()
Cancel all pending motions in the queue.
void cancelMotion()
Cancel the currently executing motion.
void enqueue(std::function< void()> motion, bool async)
Enqueue a new motion function.
bool isQueueEmpty()
Check if the motion queue is empty.
uint32_t getCurrentRunningId() const
Get the ID of the currently running motion.
void waitUntilDone()
Wait until all queued motions have finished executing.
friend void motionHandlerTask(void *param)
bool isInMotion()
Check if the handler is currently executing a motion.
uint32_t getLastEnqueuedId() const
Get the ID of the last enqueued motion.