This repository has been archived by the owner on Apr 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Robot.java
350 lines (317 loc) · 17.2 KB
/
Robot.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package com.first1444.frc.robot2020;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.first1444.dashboard.BasicDashboard;
import com.first1444.dashboard.shuffleboard.ComponentMetadataHelper;
import com.first1444.dashboard.shuffleboard.PropertyComponent;
import com.first1444.dashboard.shuffleboard.SendableComponent;
import com.first1444.dashboard.value.BasicValue;
import com.first1444.dashboard.value.ValueProperty;
import com.first1444.frc.robot2020.actions.*;
import com.first1444.frc.robot2020.actions.positioning.*;
import com.first1444.frc.robot2020.autonomous.AutonomousChooserState;
import com.first1444.frc.robot2020.autonomous.AutonomousModeCreator;
import com.first1444.frc.robot2020.autonomous.creator.AutonomousActionCreator;
import com.first1444.frc.robot2020.input.RobotInput;
import com.first1444.frc.robot2020.packets.transfer.*;
import com.first1444.frc.robot2020.perspective.FirstPersonPerspectiveOverride;
import com.first1444.frc.robot2020.perspective.PerspectiveHandler;
import com.first1444.frc.robot2020.perspective.PerspectiveProviderMultiplexer;
import com.first1444.frc.robot2020.sound.PacketSenderSoundCreator;
import com.first1444.frc.robot2020.sound.SoundMap;
import com.first1444.frc.robot2020.subsystems.*;
import com.first1444.frc.robot2020.subsystems.balltrack.BallTracker;
import com.first1444.frc.robot2020.subsystems.swerve.SwerveModuleEvent;
import com.first1444.frc.robot2020.vision.VisionProvider;
import com.first1444.frc.robot2020.vision.VisionState;
import com.first1444.sim.api.Clock;
import com.first1444.sim.api.Transform2;
import com.first1444.sim.api.Vector2;
import com.first1444.sim.api.drivetrain.swerve.FourWheelSwerveDrive;
import com.first1444.sim.api.drivetrain.swerve.FourWheelSwerveDriveData;
import com.first1444.sim.api.drivetrain.swerve.SwerveDrive;
import com.first1444.sim.api.drivetrain.swerve.SwerveModule;
import com.first1444.sim.api.frc.AdvancedIterativeRobotAdapter;
import com.first1444.sim.api.frc.FrcDriverStation;
import com.first1444.sim.api.frc.FrcLogger;
import com.first1444.sim.api.frc.FrcMode;
import com.first1444.sim.api.frc.sim.DriverStationSendable;
import com.first1444.sim.api.scheduler.match.DefaultMatchScheduler;
import com.first1444.sim.api.scheduler.match.MatchSchedulerRunnable;
import com.first1444.sim.api.scheduler.match.MatchTime;
import com.first1444.sim.api.sensors.OrientationHandler;
import me.retrodaredevil.action.*;
import me.retrodaredevil.controller.ControlConfig;
import me.retrodaredevil.controller.MutableControlConfig;
import me.retrodaredevil.controller.PartUpdater;
import me.retrodaredevil.controller.output.ControllerRumble;
import me.retrodaredevil.controller.types.ExtremeFlightJoystickControllerInput;
import me.retrodaredevil.controller.types.LogitechAttack3JoystickControllerInput;
import me.retrodaredevil.controller.types.StandardControllerInput;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
public class Robot extends AdvancedIterativeRobotAdapter {
private static final ControlConfig controlConfig;
static {
MutableControlConfig config = new MutableControlConfig();
// *edit values of controlConfig if desired*
config.switchToSquareInputThreshold = 1.2;
config.fullAnalogDeadzone = .075;
config.analogDeadzone = .02;
config.cacheAngleAndMagnitudeInUpdate = false;
config.useAbstractedIsDownIfPossible = false; // On PS4 controllers, this option is too sensitive
controlConfig = config;
}
@SuppressWarnings({"FieldCanBeLocal", "unused"})
private final FrcDriverStation driverStation; // We want to keep this as a field even though it isn't used yet
private final FrcLogger logger;
private final Clock clock;
private final DashboardMap dashboardMap;
private final PacketSender packetSender;
private final PacketQueueCreator packetQueueCreator;
private final Odometry odometry;
private final SwerveDrive drive;
private final Intake intake;
private final Turret turret;
private final BallShooter ballShooter;
private final WheelSpinner wheelSpinner;
private final Climber climber;
private final BallTracker ballTracker;
private final VisionProvider visionProvider;
private final VisionState visionState;
private final PartUpdater partUpdater = new PartUpdater();
private final RobotInput robotInput;
private final MatchSchedulerRunnable matchScheduler;
private final SoundMap soundMap;
/** Should be updated last in robotPeriodic. Usually updates actions that are used to monitor different things */
private final Action periodicAction;
/** The {@link ActionChooser} that handles an action that updates subsystems. (One action is active)*/
private final ActionChooser actionChooser;
/** The action that when updated, allows the driver and operator to control the robot */
private final Action teleopAction;
private final ActionMultiplexer dynamicAction;
private final AutonomousChooserState autonomousChooserState;
// region Initialize
public Robot(
FrcDriverStation driverStation,
FrcLogger logger,
Clock clock,
DashboardMap dashboardMap,
StandardControllerInput controller, ExtremeFlightJoystickControllerInput extremeJoystick, LogitechAttack3JoystickControllerInput attackJoystick, ControllerRumble rumble,
OrientationHandler rawOrientationHandler,
FourWheelSwerveDriveData fourWheelSwerveData,
Intake intake, Turret turret, BallShooter ballShooter, WheelSpinner wheelSpinner, Climber climber,
BallTracker ballTracker,
VisionProvider visionProvider,
VisionState visionState
){
this.driverStation = driverStation;
this.logger = logger;
this.clock = clock;
this.dashboardMap = dashboardMap;
this.intake = intake;
this.turret = turret;
this.ballShooter = ballShooter;
this.wheelSpinner = wheelSpinner;
this.climber = climber;
this.ballTracker = ballTracker;
this.visionProvider = visionProvider;
this.visionState = visionState;
robotInput = new RobotInput(
controller,
extremeJoystick,
attackJoystick, rumble
);
partUpdater.addPartAssertNotPresent(robotInput);
partUpdater.updateParts(controlConfig); // update this so when calling get methods don't throw exceptions
dashboardMap.getUserTab().add("PS4 Connected", new PropertyComponent(ValueProperty.createGetOnly(() -> BasicValue.makeBoolean(robotInput.isControllerConnected()))), (metadata) -> new ComponentMetadataHelper(metadata).setPosition(3, 4).setSize(1, 1));
dashboardMap.getUserTab().add("Extreme Connected", new PropertyComponent(ValueProperty.createGetOnly(() -> BasicValue.makeBoolean(robotInput.isExtremeConnected()))), (metadata) -> new ComponentMetadataHelper(metadata).setPosition(4, 4).setSize(1, 1));
dashboardMap.getUserTab().add("Attack Connected", new PropertyComponent(ValueProperty.createGetOnly(() -> BasicValue.makeBoolean(robotInput.isAttackConnected()))), (metadata) -> new ComponentMetadataHelper(metadata).setPosition(5, 4).setSize(1, 1));
dashboardMap.getUserTab().add("Ball Count", new PropertyComponent(ValueProperty.createGetOnly(() -> BasicValue.makeDouble(ballTracker.getBallCount()))), (metadata) -> new ComponentMetadataHelper(metadata).setSize(1, 1).setPosition(4, 2));
drive = new FourWheelSwerveDrive(fourWheelSwerveData);
{
PacketQueue packetQueue = ZMQPacketQueue.create(new ObjectMapper(), 5808);
packetSender = ZMQPacketSender.create(new ObjectMapper(), 5809);
packetQueueCreator = new PacketQueueMaster(packetQueue, true);
}
soundMap = new SoundMap(new PacketSenderSoundCreator(packetSender, false));
odometry = new Odometry(rawOrientationHandler, fourWheelSwerveData, robotInput, dashboardMap);
matchScheduler = new DefaultMatchScheduler(driverStation, clock);
PerspectiveHandler perspectiveHandler = new PerspectiveHandler(dashboardMap);
SwerveDriveAction swerveDriveAction = new SwerveDriveAction(
clock, drive, odometry.getAbsoluteAndVisionOrientation(), odometry.getAbsoluteAndVisionDistanceAccumulator(), robotInput,
new PerspectiveProviderMultiplexer(Arrays.asList(
new FirstPersonPerspectiveOverride(robotInput),
perspectiveHandler
))
);
perspectiveHandler.setPerspectiveLocation(Constants.DRIVER_STATION_2_DRIVER_LOCATION);
periodicAction = new Actions.ActionMultiplexerBuilder(
new FmsColorMonitorAction(driverStation, soundMap),
new SurroundingPositionCorrectAction(clock, dashboardMap, visionProvider, visionState, odometry.getAbsoluteAndVisionOrientation(), odometry.getAbsoluteAndVisionDistanceAccumulator()),
new AbsolutePositionPacketAction(packetQueueCreator.create(), odometry.getAbsoluteAndVisionDistanceAccumulator()),
new PerspectiveLocationPacketAction(packetQueueCreator.create(), perspectiveHandler),
new OutOfBoundsPositionCorrectAction(odometry.getAutonomousMovementDistanceAccumulator()),
new OutOfBoundsPositionCorrectAction(odometry.getAbsoluteAndVisionDistanceAccumulator()),
new SurroundingDashboardLoggerAction(clock, visionProvider, dashboardMap), // maybe only update this every .1 seconds if we get around to it
new VisionEnablerAction(clock, robotInput, visionState),
new BallCountMonitorAction(ballTracker, soundMap)
).build();
actionChooser = Actions.createActionChooser(WhenDone.CLEAR_ACTIVE);
teleopAction = new Actions.ActionMultiplexerBuilder(
swerveDriveAction,
new OperatorAction(this, robotInput)
).canBeDone(false).canRecycle(true).build();
dynamicAction = new Actions.ActionMultiplexerBuilder().canBeDone(true).canRecycle(true).build();
autonomousChooserState = new AutonomousChooserState(clock, new AutonomousModeCreator(new AutonomousActionCreator(this)), dashboardMap);
var driverStationSendable = new DriverStationSendable(driverStation);
dashboardMap.getUserTab().add("FMS", new SendableComponent<>((title, dashboard) -> {
dashboard.get(".type").getStrictSetter().setString("FMSInfo"); // temporary fix until we update robo-sim
return driverStationSendable.init(title, dashboard);
}), (metadata) -> {
new ComponentMetadataHelper(metadata).setSize(3, 1).setPosition(2, 3);
}); // 2,3
System.out.println("Finished constructor 1");
}
@Override
public void close() {
System.out.println("Closing");
try {
packetSender.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
packetQueueCreator.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// endregion
// region Overridden Methods
@Override
public void robotPeriodic() {
partUpdater.updateParts(controlConfig); // handles updating controller logic
odometry.run(); // we want to make sure our odometry is correct when we use it
periodicAction.update(); // does stuff to orientation and absolute position
actionChooser.update(); // update Actions that control the subsystems
if(robotInput.getSwerveQuickReverseCancel().isJustPressed()){
for(SwerveModule module : drive.getDrivetrainData().getModules()){
module.getEventHandler().handleEvent(SwerveModuleEvent.QUICK_REVERSE_ENABLED, false);
}
} else if(robotInput.getSwerveQuickReverseCancel().isJustReleased()){
for(SwerveModule module : drive.getDrivetrainData().getModules()){
module.getEventHandler().handleEvent(SwerveModuleEvent.QUICK_REVERSE_ENABLED, true);
}
}
if(robotInput.getSwerveRecalibrate().isJustPressed()){
for(SwerveModule module : drive.getDrivetrainData().getModules()){
module.getEventHandler().handleEvent(SwerveModuleEvent.RECALIBRATE, null);
}
}
if(robotInput.getBallCountIncrement().isJustPressed()){
ballTracker.addBall();
}
if(robotInput.getBallCountDecrement().isJustPressed()){
ballTracker.removeBall();
}
// update subsystems
drive.run();
intake.run();
turret.run();
ballShooter.run();
wheelSpinner.run();
climber.run();
visionState.run();
matchScheduler.run();
// Publish absolute position data to network tables
BasicDashboard dashboard = dashboardMap.getRawBundle().getRootDashboard().getSubDashboard("Absolute Position");
Vector2 position = odometry.getAbsoluteAndVisionDistanceAccumulator().getPosition();
dashboard.get("x").getStrictSetter().setDouble(position.getX());
dashboard.get("y").getStrictSetter().setDouble(position.getY());
dashboard.get("orientationRadians").getStrictSetter().setDouble(odometry.getAbsoluteAndVisionOrientation().getOrientationRadians());
dynamicAction.update(); // unimportant stuff
}
@Override
public void disabledInit(@Nullable FrcMode previousMode) {
dashboardMap.getLiveWindow().setEnabled(false);
actionChooser.setToClearAction();
if(previousMode == FrcMode.TELEOP){
soundMap.getTeleopDisable().play();
dynamicAction.add(new Actions.ActionQueueBuilder(
new TimedAction(false, clock, 5.0),
Actions.createRunOnce(() -> {
soundMap.getPostMatchFiveSeconds().play();
})
).build());
} else if(previousMode == FrcMode.AUTONOMOUS){
soundMap.getAutonomousDisable().play();
}
for(SwerveModule module : drive.getDrivetrainData().getModules()){
module.getEventHandler().handleEvent(SwerveModuleEvent.DISABLE, false);
}
}
@Override
public void disabledPeriodic() {
/*
While the robot is disabled, the "absolute only" position/orientation gets synced to the "absolute and vision" variant
*/
odometry.getAutonomousMovementDistanceAccumulator().setPosition(odometry.getAbsoluteAndVisionDistanceAccumulator().getPosition());
odometry.getAbsoluteOrientation().setOrientation(odometry.getAbsoluteAndVisionOrientation().getOrientation());
}
@Override
public void teleopInit() {
actionChooser.setNextAction(new Actions.ActionMultiplexerBuilder(
teleopAction
).canRecycle(false).canBeDone(true).build());
soundMap.getTeleopEnable().play();
matchScheduler.schedule(new MatchTime(7, MatchTime.Mode.TELEOP, MatchTime.Type.FROM_END), () -> {
System.out.println("rumbling");
final var rumble = robotInput.getDriverRumble();
if(rumble.isConnected()){
rumble.rumbleTime(150, .6);
}
});
System.out.println("Scheduled some stuff for end of teleop!");
}
@Override
public void autonomousInit() {
System.out.println("Autonomous init! match time: " + driverStation.getMatchTime());
/*
We use the "absolute and vision" variant because we assume that vision will be disabled when auto starts, so we assume that the
drive team will reset the position correctly while vision is not overriding the position/orientation because it's turned off
*/
Transform2 startingTransform = new Transform2(odometry.getAbsoluteAndVisionDistanceAccumulator().getPosition(), odometry.getAbsoluteAndVisionOrientation().getOrientation());
actionChooser.setNextAction(autonomousChooserState.createAutonomousAction(startingTransform));
soundMap.getAutonomousEnable().play();
// climber.storedPosition(); TODO not as simple as setting climber to stored position
}
@Override
public void autonomousPeriodic() {
}
@Override
public void testInit() {
dashboardMap.getLiveWindow().setEnabled(true);
for(SwerveModule module : drive.getDrivetrainData().getModules()){
module.getEventHandler().handleEvent(SwerveModuleEvent.DISABLE, true);
}
}
// endregion
public Clock getClock() { return clock; }
public FrcLogger getLogger(){ return logger; }
public DashboardMap getDashboardMap(){ return dashboardMap; }
public SwerveDrive getDrive(){ return drive; }
public Intake getIntake() { return intake; }
public BallShooter getBallShooter(){ return ballShooter; }
public BallTracker getBallTracker(){ return ballTracker; }
public Turret getTurret(){ return turret; }
public Climber getClimber(){ return climber; }
public Odometry getOdometry(){ return odometry; }
public VisionProvider getVisionProvider(){
return visionProvider;
}
public VisionState getVisionState(){ return visionState; }
public SoundMap getSoundMap(){ return soundMap; }
public double getBestEstimatedTargetRpm(){
return BallShooter.MAX_RPM; // Maybe in the future we will want to adjust this, but right now max rpm is fine
}
}