-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.cpp
More file actions
110 lines (94 loc) · 2.46 KB
/
Command.cpp
File metadata and controls
110 lines (94 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/** Commands encapsulate functionality of a task to execute
* iteratively with a timeout.
* Code is mostly copied from WPILib, and adapted/stripped
* down to work on the Arduino
* credits to PeterMitrano for some of the initial code
* @author Jordan Burkllund
* @date Sept. 2015
**/
#include "Command.h"
#include "Scheduler.h"
/** Generic Constructor **/
Command::Command() : initialized(false), parallel(false)
{}
/** Constructor that specifies a name for the command
* @param name Name of the command
**/
Command::Command(String name) : initialized(false), name(name), parallel(false)
{}
/** Sets the timeout period of the command
* @param timeout Timeout in milliseconds
**/
void Command::setTimeout(unsigned long timeout) {
this->timeout = timeout;
}
/** Get the time from the start of the command
* @return time since the command has started in milliseconds
**/
unsigned long Command::getTime() {
return millis() - this->startTime;
}
/** Check if the command has timed out
* @return True if the command has timed out
**/
bool Command::isTimedOut() {
return getTime() > timeout;
}
/** Checks if the command is currently running
* @return True if the command is currently running
**/
bool Command::isRunning() {
return running;
}
/** Schedule the command to start executing **/
void Command::start() {
Scheduler::getInstance()->addCommand(this);
}
/** Background code for initialization **/
void Command::_initialize() {
running = true;
startTime = millis();
}
/** Background code for executing **/
void Command::_execute() {
}
/** Background code for ending **/
void Command::_end() {
running = false;
}
/** Execute an iteration of the command
* @return True if the command is finished
**/
bool Command::cycle() {
bool finished = false;
if(!initialized) {
//Initialize the command if it hasn't been already
initialize();
_initialize();
initialized = true;
} else if (isFinished()) {
//Call the end method when the command is done
finished = true;
end();
_end();
} else {
//Otherwise execute one iteration of the command
execute();
_execute();
}
return finished;
}
/** Set the flag to run in parallel, or
* run sequentially
* @param value If true: parallel, if false: sequential
**/
void Command::setParallel(bool value) {
parallel = value;
}
/** Check if the flag is set to run this command
* in parallel
* @return True if the command should run in parallel
**/
bool Command::isParallel() {
return parallel;
}