RIT VEXU Core API
Toggle main menu visibility
Loading...
Searching...
No Matches
auto_command.h
1
6
7
#pragma once
8
9
#include "core/utils/formatting.h"
10
#include "vex.h"
11
#include <atomic>
12
#include <functional>
13
#include <queue>
14
#include <vector>
15
25
class
Condition
{
26
public
:
27
Condition
*Or(
Condition
*b);
28
Condition
*And(
Condition
*b);
29
virtual
bool
test() = 0;
30
virtual
std::string toString();
31
};
32
33
class
AutoCommand {
34
public
:
35
static
constexpr
double
default_timeout = 10.0;
41
virtual
bool
run() {
return
true
; }
42
43
virtual
std::string toString() {
return
"AutoCommand"
; }
47
virtual
void
on_timeout() {}
48
AutoCommand *withTimeout(
double
t_seconds) {
49
if
(this->timeout_seconds < 0) {
50
// should never be timed out
51
return
this
;
52
}
53
this->timeout_seconds = t_seconds;
54
return
this
;
55
}
56
AutoCommand *withCancelCondition(Condition *true_to_end) {
57
this->true_to_end = true_to_end;
58
return
this
;
59
}
69
double
timeout_seconds = default_timeout;
70
Condition *true_to_end =
nullptr
;
71
};
72
77
class
FunctionCommand :
public
AutoCommand {
78
public
:
79
FunctionCommand(std::function<
bool
(
void
)> f) : f(f) {}
80
bool
run() {
return
f(); }
81
std::string toString()
override
{
return
"Function Command"
; }
82
83
private
:
84
std::function<bool(
void
)> f;
85
};
86
87
// Times tested 3
88
// Test 1 -> false
89
// Test 2 -> false
90
// Test 3 -> true
91
// Returns false until the Nth time that it is called
92
// This is pretty much only good for implementing RepeatUntil
93
class
TimesTestedCondition :
public
Condition
{
94
public
:
95
TimesTestedCondition(
size_t
N) : max(N) {}
96
bool
test()
override
{
97
count++;
98
if
(count >= max) {
99
return
true
;
100
}
101
return
false
;
102
}
103
104
private
:
105
size_t
count = 0;
106
size_t
max;
107
};
108
110
class
FunctionCondition :
public
Condition
{
111
public
:
112
FunctionCondition(
113
std::function<
bool
()> cond, std::function<
void
(
void
)> timeout = []() {}
114
)
115
: cond(cond), timeout(timeout) {}
116
bool
test()
override
;
117
118
private
:
119
std::function<bool()> cond;
120
std::function<void(
void
)> timeout;
121
};
122
125
class
IfTimePassed :
public
Condition
{
126
public
:
127
IfTimePassed(
double
time_s);
128
bool
test()
override
;
129
130
private
:
131
double
time_s;
132
vex::timer tmr;
133
};
134
136
class
WaitUntilCondition :
public
AutoCommand {
137
public
:
138
WaitUntilCondition(
Condition
*cond) : cond(cond) {}
139
bool
run()
override
{
return
cond->test(); }
140
std::string toString()
override
{
return
"waiting until "
+ cond->toString(); }
141
142
private
:
143
Condition
*cond;
144
};
145
148
151
class
InOrder :
public
AutoCommand {
152
public
:
153
InOrder(
const
InOrder &other) =
default
;
154
InOrder(std::queue<AutoCommand *> cmds);
155
InOrder(std::initializer_list<AutoCommand *> cmds);
156
bool
run()
override
;
157
void
on_timeout()
override
;
158
std::string toString()
override
;
159
160
private
:
161
AutoCommand *current_command =
nullptr
;
162
std::queue<AutoCommand *> cmds;
163
vex::timer tmr;
164
};
165
168
class
Parallel :
public
AutoCommand {
169
public
:
170
Parallel(std::initializer_list<AutoCommand *> cmds);
171
bool
run()
override
;
172
void
on_timeout()
override
;
173
std::string toString()
override
;
174
175
private
:
176
std::vector<AutoCommand *> cmds;
177
std::vector<vex::task *> runners;
178
};
179
183
class
Branch :
public
AutoCommand {
184
public
:
185
Branch(
Condition
*cond, AutoCommand *false_choice, AutoCommand *true_choice);
186
~Branch();
187
bool
run()
override
;
188
std::string toString()
override
;
189
void
on_timeout()
override
;
190
191
private
:
192
AutoCommand *false_choice;
193
AutoCommand *true_choice;
194
Condition
*cond;
195
bool
choice =
false
;
196
bool
chosen =
false
;
197
vex::timer tmr;
198
};
199
203
class
Async :
public
AutoCommand {
204
public
:
205
Async(AutoCommand *cmd) : cmd(cmd) {}
206
bool
run()
override
;
207
std::string toString()
override
;
208
209
private
:
210
AutoCommand *cmd =
nullptr
;
211
};
212
213
class
RepeatUntil :
public
AutoCommand {
214
public
:
218
RepeatUntil(
InOrder
cmds,
size_t
repeats);
222
RepeatUntil(
InOrder
cmds,
Condition
*true_to_end);
223
bool
run()
override
;
224
std::string toString()
override
;
225
void
on_timeout()
override
;
226
227
private
:
228
const
InOrder
cmds;
229
InOrder
*working_cmds;
230
Condition
*cond;
231
};
Condition
Definition
auto_command.h:25
InOrder
InOrder runs its commands sequentially then continues. How to handle timeout in this case....
Definition
auto_command.h:151
include
core
utils
command_structure
auto_command.h
Generated by
1.17.0