-
Notifications
You must be signed in to change notification settings - Fork 35
/
SuctionClimbingStateMachine.java
156 lines (140 loc) · 5.76 KB
/
SuctionClimbingStateMachine.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
package com.team254.frc2019.statemachines;
import com.team254.frc2019.states.TimedLEDState;
import com.team254.frc2019.subsystems.*;
import com.team254.lib.geometry.Rotation2d;
import com.team254.lib.util.Util;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
* Climbing state machine used at the FIRST Championship in Houston using the
* suction mechanism
*/
public class SuctionClimbingStateMachine {
private final double kRobotRelativeTurretSetpoint = 180.0;
enum SystemState {
MOVING_TO_PRECLIMB,
IN_PRECLIMB,
WAITING_FOR_PRESSURE,
CLIMBING,
}
private Superstructure mSuperstructure;
private Kickstand mKickstand;
private Vacuum mVacuum;
private Elevator mElevator;
private boolean isAtContactPoint = false;
private boolean mSentVaccumPoint = false;
SystemState mSystemState = SystemState.MOVING_TO_PRECLIMB;
public SuctionClimbingStateMachine() {
mSuperstructure = Superstructure.getInstance();
mKickstand = Kickstand.getInstance();
mVacuum = Vacuum.getInstance();
mElevator = Elevator.getInstance();
}
public synchronized void reset() {
mVacuum.setOn(false);
mKickstand.setDisengaged();
mKickstand.setRachetDisengaged();
mSuperstructure.setDisableElevator(false);
mSuperstructure.setUseElevatorManual(false);
SuperstructureCommands.goToPrepareForVacuumClimb();
mSystemState = SystemState.MOVING_TO_PRECLIMB;
isAtContactPoint = false;
mSentVaccumPoint = false;
}
public synchronized void handle(double timestamp, boolean moveToSuction, boolean sendIt) {
if (mVacuum.isAtClimbingPressure()) {
LED.getInstance().setClimbLEDState(TimedLEDState.StaticLEDState.kHangOptimalPressure);
} else if (mVacuum.isAlmostAtClimbingPressure()) {
LED.getInstance().setClimbLEDState(TimedLEDState.StaticLEDState.kHangMinimalPressure);
} else {
LED.getInstance().setClimbLEDState(TimedLEDState.BlinkingLEDState.kHangNoPressure);
}
switch (mSystemState) {
case MOVING_TO_PRECLIMB: // turret to battery side, elevator up to prepare for climb setpoint
SuperstructureCommands.goToPrepareForVacuumClimb();
mSuperstructure.setWantRobotRelativeTurret();
SuperstructureCommands.setTurretManualHeading(Rotation2d.fromDegrees(kRobotRelativeTurretSetpoint));
break;
case IN_PRECLIMB: // engage kickstand
mKickstand.setEngaged();
break;
case WAITING_FOR_PRESSURE: // start vacuum, move elevator down to contact position
mVacuum.setOn(true);
if (!isAtContactPoint) {
SuperstructureCommands.goToVacuumHab3ContactPoint();
if (Util.epsilonEquals(mElevator.getActiveTrajectoryUnits(),
SuperstructureCommands.VacuumHab3ContactPoint.elevator, 1.0)) {
System.out.println("At contact point!");
isAtContactPoint = true;
}
} else {
mSuperstructure.setElevatorManual(-0.15);
mSuperstructure.setUseElevatorManual(true);
}
break;
case CLIMBING: // Do the climb
mElevator.removeCurrentLimits();
if (!mSentVaccumPoint) {
SuperstructureCommands.goToVacuumClimbing();
mSentVaccumPoint = true;
} else {
mSuperstructure.setUseElevatorManual(true);
if (mElevator.getPosition() > SuperstructureCommands.VacuumClimb.elevator) {
if (mElevator.getPosition() > SuperstructureCommands.VacuumClimb.elevator + 5.0) {
mSuperstructure.setElevatorManual(-1.0);
} else {
mSuperstructure.setElevatorManual(-0.5);
}
} else {
mSuperstructure.setElevatorManual(0.0);
}
}
mKickstand.setRachetEngaged();
break;
default:
break;
}
SystemState nextState = mSystemState;
switch (mSystemState) {
case MOVING_TO_PRECLIMB:
if (mSuperstructure.isAtDesiredState()) {
nextState = SystemState.IN_PRECLIMB;
}
break;
case IN_PRECLIMB:
if (moveToSuction) {
nextState = SystemState.WAITING_FOR_PRESSURE;
}
break;
case WAITING_FOR_PRESSURE:
if (mVacuum.isAtClimbingPressure() || (mVacuum.isAlmostAtClimbingPressure())) {
nextState = SystemState.CLIMBING;
}
break;
case CLIMBING:
break;
default:
break;
}
if (nextState != mSystemState) {
mSystemState = nextState;
System.out.println("Transitioned from : " + mSystemState + " to " + nextState);
}
}
public String getStateString() {
switch (mSystemState) {
case MOVING_TO_PRECLIMB:
return "MOVING_TO_PRECLIMB";
case IN_PRECLIMB:
return "IN_PRECLIMB";
case WAITING_FOR_PRESSURE:
return "WAITING_FOR_PRESSURE";
case CLIMBING:
return "CLIMBING";
default:
return "DEFAULT";
}
}
public void outputToSmartDashboard() {
SmartDashboard.putString("Vacuum SM: State", getStateString());
}
}