-
Notifications
You must be signed in to change notification settings - Fork 3
/
CommandRobotBase.java
215 lines (192 loc) · 6.15 KB
/
CommandRobotBase.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package org.usfirst.frc4904.standard;
import org.usfirst.frc4904.standard.custom.CommandSendableChooser;
import org.usfirst.frc4904.standard.custom.TypedNamedSendableChooser;
import org.usfirst.frc4904.standard.humaninput.Driver;
import org.usfirst.frc4904.standard.humaninput.Operator;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj2.command.CommandBase;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
* IterativeRobot is normally the base class for command based code, but we
* think certain features will almost always be needed, so we created the
* CommandRobotBase class. Robot should extend this instead of iterative robot.
*/
public abstract class CommandRobotBase extends TimedRobot {
private CommandBase autonomousCommand;
protected CommandBase teleopCommand;
protected CommandSendableChooser autoChooser;
protected TypedNamedSendableChooser<Driver> driverChooser;
protected TypedNamedSendableChooser<Operator> operatorChooser;
/**
* This displays our choosers. The default choosers are for autonomous type,
* driver control, sand operator control.
*/
protected final void displayChoosers() {
SmartDashboard.putData("Auton Routine Selector", autoChooser);
SmartDashboard.putData("Driver control scheme chooser", driverChooser);
SmartDashboard.putData("Operator control scheme chooser", operatorChooser);
}
/**
* This stops all commands from running on initialization of a state so as to
* prevent commands from the previous state from interfering with the current
* robot mode.
*/
private void cleanup() {
if (autonomousCommand != null) {
autonomousCommand.cancel();
}
if (teleopCommand != null) {
teleopCommand.cancel();
}
}
/**
* This initializes the entire robot. It is called by WPILib on robot code
* launch. Year-specific code should be written in the initialize function.
*/
@Override
public final void robotInit() {
// Initialize choosers
autoChooser = new CommandSendableChooser();
driverChooser = new TypedNamedSendableChooser<Driver>();
operatorChooser = new TypedNamedSendableChooser<Operator>();
// Run user-provided initialize function
initialize();
// Display choosers on SmartDashboard
displayChoosers();
}
/**
* Function for year-specific code to be run on robot code launch.
* setHealthChecks should be called here if needed.
*/
public abstract void initialize();
/**
* This initializes the teleoperated portion of the robot code. It is called by
* WPILib on teleop enable. Year-specific code should be written in the
* teleopInitialize() function.
*/
@Override
public final void teleopInit() {
cleanup();
if (driverChooser.getSelected() != null) {
LogKitten.d("Loading driver " + driverChooser.getSelected().getName());
driverChooser.getSelected().bindCommands();
}
if (operatorChooser.getSelected() != null) {
LogKitten.d("Loading operator " + operatorChooser.getSelected().getName());
operatorChooser.getSelected().bindCommands();
}
teleopInitialize();
if (teleopCommand != null) {
teleopCommand.schedule();
}
}
/**
* Function for year-specific code to be run on teleoperated initialize.
* teleopCommand should be set in this function.
*/
public abstract void teleopInitialize();
/**
* This function is called by WPILib periodically during teleop. Year-specific
* code should be written in the teleopExecute() function.
*/
@Override
public final void teleopPeriodic() {
CommandScheduler.getInstance().run();
teleopExecute();
alwaysExecute();
}
/**
* Function for year-specific code to be run during teleoperated time.
*/
public abstract void teleopExecute();
/**
* This initializes the autonomous portion of the robot code. It is called by
* WPILib on auton enable. Year-specific code should be written in the
* autonomousInitialize() function.
*/
@Override
public final void autonomousInit() {
cleanup();
autonomousCommand = autoChooser.getSelected();
if (autonomousCommand != null) {
autonomousCommand.schedule();
}
autonomousInitialize();
}
/**
* Function for year-specific code to be run on autonomous initialize.
*/
public abstract void autonomousInitialize();
/**
* This function is called by WPILib periodically during auton. Year-specific
* code should be written in the autonomousExecute() function.
*/
@Override
public final void autonomousPeriodic() {
CommandScheduler.getInstance().run();
autonomousExecute();
alwaysExecute();
}
/**
* Function for year-specific code to be run during autonomous.
*/
public abstract void autonomousExecute();
/**
* This function is called by WPILib when the robot is disabled. Year-specific
* code should be written in the disabledInitialize() function.
*/
@Override
public final void disabledInit() {
cleanup();
disabledInitialize();
}
/**
* Function for year-specific code to be run on disabled initialize.
*/
public abstract void disabledInitialize();
/**
* This function is called by WPILib periodically while disabled. Year-specific
* code should be written in the disabledExecute() function.
*/
@Override
public final void disabledPeriodic() {
CommandScheduler.getInstance().run();
disabledExecute();
alwaysExecute();
}
/**
* Function for year-specific code to be run while disabled.
*/
public abstract void disabledExecute();
/**
* This function is called by WPILib when the robot is in test mode.
* Year-specific-code should be written in the disabledInitialize() function.
*/
@Override
public final void testInit() {
cleanup();
testInitialize();
}
/**
* Function for year-specific code to be run on disabled initialize.
*/
public abstract void testInitialize();
/**
* This function is called by WPILib periodically while in test mode.
* Year-specific code should be written in the testExecute() function.
*/
@Override
public void testPeriodic() {
testExecute();
alwaysExecute();
}
/**
* Function for year-specific code to be run while in test mode.
*/
public abstract void testExecute();
/**
* Function for year-specific code to be run in every robot mode.
*/
public abstract void alwaysExecute();
}