RIT VEXU Core API
Loading...
Searching...
No Matches
auto_command.h
1
7#pragma once
8
9#include "vex.h"
10#include <atomic>
11#include <functional>
12#include <queue>
13#include <vector>
14
24class Condition {
25public:
26 Condition *Or(Condition *b);
27 Condition *And(Condition *b);
28 virtual bool test() = 0;
29};
30
31class AutoCommand {
32public:
33 static constexpr double default_timeout = 10.0;
39 virtual bool run() { return true; }
43 virtual void on_timeout() {}
44 AutoCommand *withTimeout(double t_seconds) {
45 if (this->timeout_seconds < 0) {
46 // should never be timed out
47 return this;
48 }
49 this->timeout_seconds = t_seconds;
50 return this;
51 }
52 AutoCommand *withCancelCondition(Condition *true_to_end) {
53 this->true_to_end = true_to_end;
54 return this;
55 }
65 double timeout_seconds = default_timeout;
66 Condition *true_to_end = nullptr;
67};
68
73class FunctionCommand : public AutoCommand {
74public:
75 FunctionCommand(std::function<bool(void)> f) : f(f) {}
76 bool run() { return f(); }
77
78private:
79 std::function<bool(void)> f;
80};
81
82// Times tested 3
83// Test 1 -> false
84// Test 2 -> false
85// Test 3 -> true
86// Returns false until the Nth time that it is called
87// This is pretty much only good for implementing RepeatUntil
88class TimesTestedCondition : public Condition {
89public:
90 TimesTestedCondition(size_t N) : max(N) {}
91 bool test() override {
92 count++;
93 if (count >= max) {
94 return true;
95 }
96 return false;
97 }
98
99private:
100 size_t count = 0;
101 size_t max;
102};
103
106public:
108 std::function<bool()> cond, std::function<void(void)> timeout = []() {})
109 : cond(cond), timeout(timeout) {}
110 bool test() override;
111
112private:
113 std::function<bool()> cond;
114 std::function<void(void)> timeout;
115};
116
119class IfTimePassed : public Condition {
120public:
121 IfTimePassed(double time_s);
122 bool test() override;
123
124private:
125 double time_s;
126 vex::timer tmr;
127};
128
130class WaitUntilCondition : public AutoCommand {
131public:
132 WaitUntilCondition(Condition *cond) : cond(cond) {}
133 bool run() override { return cond->test(); }
134
135private:
136 Condition *cond;
137};
138
141
144class InOrder : public AutoCommand {
145public:
146 InOrder(const InOrder &other) = default;
147 InOrder(std::queue<AutoCommand *> cmds);
148 InOrder(std::initializer_list<AutoCommand *> cmds);
149 bool run() override;
150 void on_timeout() override;
151
152private:
153 AutoCommand *current_command = nullptr;
154 std::queue<AutoCommand *> cmds;
155 vex::timer tmr;
156};
157
160class Parallel : public AutoCommand {
161public:
162 Parallel(std::initializer_list<AutoCommand *> cmds);
163 bool run() override;
164 void on_timeout() override;
165
166private:
167 std::vector<AutoCommand *> cmds;
168 std::vector<vex::task *> runners;
169};
170
174class Branch : public AutoCommand {
175public:
176 Branch(Condition *cond, AutoCommand *false_choice, AutoCommand *true_choice);
177 ~Branch();
178 bool run() override;
179 void on_timeout() override;
180
181private:
182 AutoCommand *false_choice;
183 AutoCommand *true_choice;
184 Condition *cond;
185 bool choice = false;
186 bool chosen = false;
187 vex::timer tmr;
188};
189
193class Async : public AutoCommand {
194public:
195 Async(AutoCommand *cmd) : cmd(cmd) {}
196 bool run() override;
197
198private:
199 AutoCommand *cmd = nullptr;
200};
201
202class RepeatUntil : public AutoCommand {
203public:
207 RepeatUntil(InOrder cmds, size_t repeats);
211 RepeatUntil(InOrder cmds, Condition *true_to_end);
212 bool run() override;
213 void on_timeout() override;
214
215private:
216 const InOrder cmds;
217 InOrder *working_cmds;
218 Condition *cond;
219};
Async runs a command asynchronously will simply let it go and never look back THIS HAS A VERY NICHE U...
Definition auto_command.h:193
Branch chooses from multiple options at runtime. the function decider returns an index into the choic...
Definition auto_command.h:174
Definition auto_command.h:24
Definition auto_command.h:73
FunctionCondition is a quick and dirty Condition to wrap some expression that should be evaluated at ...
Definition auto_command.h:105
IfTimePassed tests based on time since the command controller was constructed. Returns true if elapse...
Definition auto_command.h:119
InOrder runs its commands sequentially then continues. How to handle timeout in this case....
Definition auto_command.h:144
Parallel runs multiple commands in parallel and waits for all to finish before continuing....
Definition auto_command.h:160
Waits until the condition is true.
Definition auto_command.h:130