RIT VEXU Core API
Toggle main menu visibility
Loading...
Searching...
No Matches
auto_command.h
1
6
7
#pragma once
8
9
#include <atomic>
10
#include <functional>
11
#include <queue>
12
#include <vector>
13
14
#include "vex.h"
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"
; }
44
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
}
71
double
timeout_seconds = default_timeout;
72
Condition* true_to_end =
nullptr
;
73
74
virtual
~AutoCommand() =
default
;
75
};
76
81
class
FunctionCommand :
public
AutoCommand {
82
public
:
83
FunctionCommand(std::function<
bool
(
void
)> f) : f(f) {}
84
bool
run()
override
{
return
f(); }
85
std::string toString()
override
{
return
"Function Command"
; }
86
87
private
:
88
std::function<bool(
void
)> f;
89
};
90
/*
91
* Times tested 3
92
* Test 1 -> false
93
* Test 2 -> false
94
* Test 3 -> true
95
* Returns false until the Nth time that it is called
96
* This is pretty much only good for implementing RepeatUntil
97
*/
98
class
TimesTestedCondition :
public
Condition
{
99
public
:
100
TimesTestedCondition(
size_t
N) : max(N) {}
101
bool
test()
override
{
102
count++;
103
if
(count >= max) {
104
return
true
;
105
}
106
return
false
;
107
}
108
109
private
:
110
size_t
count = 0;
111
size_t
max;
112
};
113
116
class
FunctionCondition :
public
Condition
{
117
public
:
118
FunctionCondition(
119
std::function<
bool
()> cond, std::function<
void
(
void
)> timeout = []() {}
120
)
121
: cond(cond), timeout(timeout) {}
122
bool
test()
override
;
123
124
private
:
125
std::function<bool()> cond;
126
std::function<void(
void
)> timeout;
127
};
128
133
class
IfTimePassed :
public
Condition
{
134
public
:
135
IfTimePassed(
double
time_s);
136
bool
test()
override
;
137
138
private
:
139
double
time_s;
140
vex::timer tmr;
141
};
142
144
class
WaitUntilCondition :
public
AutoCommand {
145
public
:
146
WaitUntilCondition(
Condition
* cond) : cond(cond) {}
147
bool
run()
override
{
return
cond->test(); }
148
std::string toString()
override
{
return
"waiting until "
+ cond->toString(); }
149
150
private
:
151
Condition
* cond;
152
};
153
158
class
InOrder :
public
AutoCommand {
159
public
:
160
InOrder(
const
InOrder& other) =
default
;
161
InOrder(std::queue<AutoCommand*> cmds);
162
InOrder(std::initializer_list<AutoCommand*> cmds);
163
bool
run()
override
;
164
void
on_timeout()
override
;
165
std::string toString()
override
;
166
167
private
:
168
AutoCommand* current_command =
nullptr
;
169
std::queue<AutoCommand*> cmds;
170
vex::timer tmr;
171
};
172
178
class
Parallel :
public
AutoCommand {
179
public
:
180
Parallel(std::initializer_list<AutoCommand*> cmds);
181
bool
run()
override
;
182
void
on_timeout()
override
;
183
std::string toString()
override
;
184
185
private
:
186
std::vector<AutoCommand*> cmds;
187
std::vector<vex::task*> runners;
188
};
189
194
class
Branch :
public
AutoCommand {
195
public
:
196
Branch(
Condition
* cond, AutoCommand* false_choice, AutoCommand* true_choice);
197
~Branch();
198
bool
run()
override
;
199
std::string toString()
override
;
200
void
on_timeout()
override
;
201
202
private
:
203
AutoCommand* false_choice;
204
AutoCommand* true_choice;
205
Condition
* cond;
206
bool
choice =
false
;
207
bool
chosen =
false
;
208
vex::timer tmr;
209
};
210
216
class
Async :
public
AutoCommand {
217
public
:
218
Async(AutoCommand* cmd) : cmd(cmd) {}
219
bool
run()
override
;
220
std::string toString()
override
;
221
222
private
:
223
AutoCommand* cmd =
nullptr
;
224
};
225
226
class
RepeatUntil :
public
AutoCommand {
227
public
:
233
RepeatUntil(
InOrder
cmds,
size_t
repeats);
239
RepeatUntil(
InOrder
cmds,
Condition
* true_to_end);
240
bool
run()
override
;
241
std::string toString()
override
;
242
void
on_timeout()
override
;
243
244
private
:
245
const
InOrder
cmds;
246
InOrder
* working_cmds;
247
Condition
* cond;
248
};
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:158
include
core
utils
command_structure
auto_command.h
Generated by
1.17.0