forked from Team254/FRC-2019-Public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoModeBase.java
102 lines (80 loc) · 2.58 KB
/
AutoModeBase.java
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
package com.team254.frc2019.auto.modes;
import com.team254.frc2019.auto.AutoModeEndedException;
import com.team254.frc2019.auto.actions.Action;
import com.team254.frc2019.auto.actions.NoopAction;
import edu.wpi.first.wpilibj.DriverStation;
/**
* An abstract class that is the basis of the robot's autonomous routines. This is implemented in auto modes (which are
* routines that do actions).
*/
public abstract class AutoModeBase {
protected final double mUpdateRate = 1.0 / 50.0;
protected boolean mActive = false;
protected boolean mIsInterrupted = false;
protected abstract void routine() throws AutoModeEndedException;
public void run() {
mActive = true;
try {
routine();
} catch (AutoModeEndedException e) {
DriverStation.reportError("AUTO MODE DONE!!!! ENDED EARLY!!!!", false);
return;
}
done();
}
public void done() {
System.out.println("Auto mode done");
}
public void stop() {
mActive = false;
}
public boolean isActive() {
return mActive;
}
public boolean isActiveWithThrow() throws AutoModeEndedException {
if (!isActive()) {
throw new AutoModeEndedException();
}
return isActive();
}
public void waitForDriverConfirm() throws AutoModeEndedException {
if (!mIsInterrupted) {
interrupt();
}
runAction(new NoopAction());
}
public void interrupt() {
System.out.println("** Auto mode interrrupted!");
mIsInterrupted = true;
}
public void resume() {
System.out.println("** Auto mode resumed!");
mIsInterrupted = false;
}
public void runAction(Action action) throws AutoModeEndedException {
isActiveWithThrow();
long waitTime = (long) (mUpdateRate * 1000.0);
// Wait for interrupt state to clear
while (isActiveWithThrow() && mIsInterrupted) {
try {
Thread.sleep(waitTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
action.start();
// Run action, stop action on interrupt, non active mode, or done
while (isActiveWithThrow() && !action.isFinished() && !mIsInterrupted) {
action.update();
try {
Thread.sleep(waitTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
action.done();
}
public boolean getIsInterrupted() {
return mIsInterrupted;
}
}