-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPausableCommand.cpp
More file actions
68 lines (59 loc) · 1.71 KB
/
PausableCommand.cpp
File metadata and controls
68 lines (59 loc) · 1.71 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
/** Extension of the Command class that allows itself to be paused
* Uses a pausable timer for getTime rather than millis like Command
* Methods onPause and onResume get called once when the state of the
* robot switches to the respective state. Any time based commands
* will behave as expected, even when paused
* @author Jordan Burklund
* @date Oct. 2015
**/
#include "PausableCommand.h"
/** Constructor **/
PausableCommand::PausableCommand() : Command() {
robotPauser = RobotPauser::getInstance();
}
/** Constructor with given name **/
PausableCommand::PausableCommand(const String name) : Command(name) {
robotPauser = RobotPauser::getInstance();
}
/** Get the time from the start of the command
* @return time since the command has started in milliseconds
**/
unsigned long PausableCommand::getTime() {
return getPauseMillis() - this->startTime;
}
/* Background code for initialization */
void PausableCommand::_initialize() {
running = true;
startTime = getPauseMillis();
}
/** Execute an iteration of the command
**/
bool PausableCommand::cycle() {
bool finished = false;
bool isNowPaused = robotPauser->isPaused();
if(!initialized) {
initialize();
_initialize();
initialized = true;
} else if (isFinished()) {
finished = true;
end();
_end();
} else {
if(isNowPaused && (isNowPaused != wasPaused)) {
//Robot is paused, but wasn't last time
onPause();
} else if(!isNowPaused && (isNowPaused != wasPaused)) {
//Robot is resumed, but wasn't last time
onResume();
} else if(!isNowPaused) {
//Robot is not paused, and was not paused last time
_execute();
execute();
} else {
//Robot is paused. Do nothing
}
}
wasPaused = isNowPaused;
return finished;
}