-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathEndEffectorStateMachine.java
276 lines (233 loc) · 10.1 KB
/
EndEffectorStateMachine.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
package com.team254.frc2019.statemachines;
import com.team254.frc2019.states.TimedLEDState;
import com.team254.frc2019.subsystems.LED;
import edu.wpi.first.wpilibj.Timer;
public class EndEffectorStateMachine {
private final double kBallExhaustPower = 1.0;
private final double kBallIntakePower = -1.0;
private final double kTremorIntakePower = 1.0;
private final double kTremorExhaustPower = -1.0;
private final double kTremorExhaustForBallPower = 1.0;
private final double kMinTimeWithBall = 0.1;
private final double kMinTimeWithDisk = 0.5;
private final double kMinTimeBallIntaking = 0.25;
private final double kBlinkingDurationHaveBall = 0.5;
private final double kBlinkingDurationHaveDisk = 0.5;
public enum WantedAction {
INTAKE_CARGO, INTAKE_DISK, EXHAUST, IDLE
}
public enum SystemState {
IDLE, INTAKING_CARGO, INTAKING_DISK, HAVE_CARGO, HAVE_DISK, EXHAUSTING
}
public static class EndEffectorState {
public enum JawState {
OPENED,
CLOSED
}
public JawState jawState = JawState.OPENED;
public double tremorMotor = 0.0;
public double ballIntakeMotor = 0.0;
public boolean hasBall = false;
public boolean hasDisk = false;
@Override
public String toString() {
return "EndEffectorState{" +
"jawState=" + jawState +
", tremorMotor=" + tremorMotor +
", ballIntakeMotor=" + ballIntakeMotor +
", hasBall=" + hasBall +
", hasDisk=" + hasDisk +
'}';
}
}
private SystemState mSystemState = SystemState.IDLE;
private EndEffectorState mDesiredState = new EndEffectorState();
private LED mLED = LED.getInstance();
private double mCurrentStateStartTime = 0.0;
private double mLastNoBallTime = Double.NaN;
private double mLastNoDiskTime = Double.NaN;
public synchronized SystemState getSystemState() {
return mSystemState;
}
public synchronized EndEffectorState update(double timestamp, WantedAction wantedAction, EndEffectorState currentState) {
SystemState newState = mSystemState;
double timeInState = timestamp - mCurrentStateStartTime;
switch (mSystemState) {
case IDLE:
newState = handleIdleStateTransitions(wantedAction);
break;
case INTAKING_DISK:
newState = handleIntakingDiskTransitions(currentState, timestamp, wantedAction);
break;
case INTAKING_CARGO:
newState = handleIntakingCargoTransitions(currentState, timestamp, timeInState, wantedAction);
break;
case HAVE_CARGO:
newState = handleHaveCargoTransitions(currentState, wantedAction);
break;
case HAVE_DISK:
newState = handleHaveDiskTransitions(wantedAction);
break;
case EXHAUSTING:
newState = handleExhaustTransitions(wantedAction);
break;
default:
System.out.println("Unexpected end effector system state: " + mSystemState);
newState = mSystemState;
break;
}
if (newState != mSystemState) {
System.out.println(timestamp + ": Changed state: " + mSystemState + " -> " + newState);
mSystemState = newState;
mCurrentStateStartTime = Timer.getFPGATimestamp();
mLastNoDiskTime = timestamp;
mLastNoBallTime = timestamp;
timeInState = 0.0;
}
switch (mSystemState) {
case IDLE:
getIdleDesiredState(currentState, mDesiredState);
break;
case INTAKING_DISK:
getIntakingDiskDesiredState(currentState, mDesiredState);
break;
case INTAKING_CARGO:
getIntakingCargoDesiredState(currentState, mDesiredState);
break;
case HAVE_CARGO:
getHaveCargoDesiredState(currentState, mDesiredState, timeInState);
break;
case HAVE_DISK:
getHaveDiskDesiredState(currentState, mDesiredState, timeInState);
break;
case EXHAUSTING:
getExhaustingDesiredState(currentState, mDesiredState);
break;
default:
System.out.println("Unexpected end effector system state: " + mSystemState);
break;
}
return mDesiredState;
}
private SystemState defaultTransitions(WantedAction wantedAction) {
switch (wantedAction) {
case IDLE:
return SystemState.IDLE;
case EXHAUST:
return SystemState.EXHAUSTING;
case INTAKE_DISK:
return SystemState.INTAKING_DISK;
case INTAKE_CARGO:
return SystemState.INTAKING_CARGO;
}
return SystemState.IDLE;
}
// IDLE
private SystemState handleIdleStateTransitions(WantedAction wantedAction) {
return defaultTransitions(wantedAction);
}
private void getIdleDesiredState(EndEffectorState currentState, EndEffectorState desiredState) {
desiredState.jawState = currentState.jawState;
desiredState.ballIntakeMotor = 0.0;
desiredState.tremorMotor = 0.0;
mLED.setIntakeLEDState(TimedLEDState.StaticLEDState.kStaticOff);
}
// INTAKING_DISK
private SystemState handleIntakingDiskTransitions(EndEffectorState currentState,
double timestamp,
WantedAction wantedAction) {
if (!currentState.hasDisk) {
mLastNoDiskTime = timestamp;
}
if (!Double.isNaN(mLastNoDiskTime) && (timestamp - mLastNoDiskTime > kMinTimeWithDisk)) {
return SystemState.HAVE_DISK;
}
return defaultTransitions(wantedAction);
}
private void getIntakingDiskDesiredState(EndEffectorState currentState, EndEffectorState desiredState) {
desiredState.jawState = EndEffectorState.JawState.CLOSED;
desiredState.ballIntakeMotor = 0.0;
desiredState.tremorMotor = kTremorIntakePower;
mLED.setIntakeLEDState(TimedLEDState.StaticLEDState.kIntakingDisk);
}
// INTAKING_CARGO
private SystemState handleIntakingCargoTransitions(EndEffectorState currentState,
double timestamp,
double timeInState,
WantedAction wantedAction) {
if (!currentState.hasBall) {
mLastNoBallTime = timestamp;
}
if (!Double.isNaN(mLastNoBallTime) && (timestamp - mLastNoBallTime > kMinTimeWithBall)
&& (timeInState > kMinTimeBallIntaking)) {
return SystemState.HAVE_CARGO;
}
return defaultTransitions(wantedAction);
}
private void getIntakingCargoDesiredState(EndEffectorState currentState, EndEffectorState desiredState) {
desiredState.jawState = EndEffectorState.JawState.OPENED;
// Use same sign for both now.
desiredState.ballIntakeMotor = kBallIntakePower;
desiredState.tremorMotor = kBallIntakePower;
mLED.setIntakeLEDState(TimedLEDState.StaticLEDState.kIntakingCargo);
}
// HAVE_CARGO
private SystemState handleHaveCargoTransitions(EndEffectorState currentState,
WantedAction wantedAction) {
switch (wantedAction) {
case EXHAUST:
return SystemState.EXHAUSTING;
case INTAKE_DISK:
return SystemState.INTAKING_DISK;
case INTAKE_CARGO:
return SystemState.INTAKING_CARGO;
default:
break;
}
if (!currentState.hasBall) {
return SystemState.INTAKING_CARGO;
}
return SystemState.HAVE_CARGO;
}
private void getHaveCargoDesiredState(EndEffectorState currentState, EndEffectorState desiredState, double timeInState) {
desiredState.jawState = EndEffectorState.JawState.OPENED;
desiredState.ballIntakeMotor = 0.0;
desiredState.tremorMotor = 0.0;
if (timeInState < kBlinkingDurationHaveBall) {
mLED.setIntakeLEDState(TimedLEDState.BlinkingLEDState.kBlinkingIntakeCargo);
} else {
mLED.setIntakeLEDState(TimedLEDState.StaticLEDState.kIntakingCargo);
}
}
// HAVE_DISK
private SystemState handleHaveDiskTransitions(WantedAction wantedAction) {
return defaultTransitions(wantedAction);
}
private void getHaveDiskDesiredState(EndEffectorState currentState, EndEffectorState desiredState,
double timeInState) {
desiredState.jawState = EndEffectorState.JawState.CLOSED;
desiredState.ballIntakeMotor = 0.0;
desiredState.tremorMotor = kTremorIntakePower;
if (timeInState < kBlinkingDurationHaveDisk) {
mLED.setIntakeLEDState(TimedLEDState.BlinkingLEDState.kBlinkingIntakingDisk);
} else {
mLED.setIntakeLEDState(TimedLEDState.StaticLEDState.kIntakingDisk);
}
}
// EXHAUSTING
private SystemState handleExhaustTransitions(WantedAction wantedAction) {
return defaultTransitions(wantedAction);
}
private void getExhaustingDesiredState(EndEffectorState currentState, EndEffectorState desiredState) {
desiredState.jawState = currentState.jawState;
// Exhaust ball
if (desiredState.jawState == EndEffectorState.JawState.OPENED) {
desiredState.ballIntakeMotor = kBallExhaustPower;
desiredState.tremorMotor = kTremorExhaustForBallPower;
} else {
desiredState.ballIntakeMotor = 0.0;
desiredState.tremorMotor = kTremorExhaustPower;
}
mLED.setIntakeLEDState(TimedLEDState.StaticLEDState.kExhausting);
}
}