-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCatzLift.java
326 lines (256 loc) · 11.3 KB
/
CatzLift.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
package mechanisms;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.Spark;
import edu.wpi.first.wpilibj.SpeedControllerGroup;
import edu.wpi.first.wpilibj.Timer;
import robot.CatzConstants;
import robot.Robot;
/*
* Author : Derek Duenas
*
* Revision History :
* 2-19-18 Added debug data printouts
* 3-16-18 Removed lift to scale height 2 method
* 3-17-18 Using the bottom limit switch instead of encoder for the lift to go down all the way
* 4-5-18 Changed lift method to go to different heights
*
* Methods : lift
* Functionality : Move the lift up and down
*/
public class CatzLift {
/******************************************************************************************************************
* Constant Values
******************************************************************************************************************/
final private static double LIFT_COUNTS_PER_INCH = 505026.0 / 80.0; //527221.0/80.0; // 674898.0 / 60.0 - wrong counts per inch value
final private static double LIFT_SCALE_HEIGHT = 68.0 * LIFT_COUNTS_PER_INCH;
final private static double LIFT_SWITCH_HEIGHT = 17 * LIFT_COUNTS_PER_INCH; //TODO test
final private static double LIFTER_ERROR_THRESHOLD_PULSES = LIFT_COUNTS_PER_INCH / 2.0;
final private static double LIFT_SPEED = 1.0;
/******************************************************************************************************************
* State of the Thread
******************************************************************************************************************/
static boolean threadComplete;
public static boolean liftThreadRunning;
/******************************************************************************************************************
* Timer
******************************************************************************************************************/
private static Timer timeout = new Timer();
/******************************************************************************************************************
* Limit Switch
******************************************************************************************************************/
public static DigitalInput lifterLimitTop;
public static DigitalInput lifterLimitBot;
/******************************************************************************************************************
* Encoder
******************************************************************************************************************/
public static Encoder liftEncoder;
/******************************************************************************************************************
* Spark Assignment/Placement
******************************************************************************************************************/
public static Spark lifterRTRT;
public static Spark lifterRTLT;
public static Spark lifterLTRT;
public static Spark lifterLTLT;
/******************************************************************************************************************
* Controller Grouping
******************************************************************************************************************/
public static SpeedControllerGroup LTLift;
public static SpeedControllerGroup RTLift;
/******************************************************************************************************************
*
* Method
* Gives out statistics of robot
*
*
******************************************************************************************************************/
public CatzLift()
{
CatzLift.lifterLimitTop = new DigitalInput(CatzConstants.TOP_LIFT_LIMIT_DIO);
CatzLift.lifterLimitBot = new DigitalInput(CatzConstants.BOT_LIFT_LIMIT_DIO);
CatzLift.lifterRTRT = new Spark(CatzConstants.RIGHT_RIGHT_LIFTER_PWM);
CatzLift.lifterRTLT = new Spark(CatzConstants.RIGHT_LEFT_LIFTER_PWM);
CatzLift.lifterLTRT = new Spark(CatzConstants.LEFT_RIGHT_LIFTER_PWM);
CatzLift.lifterLTLT = new Spark(CatzConstants.LEFT_LEFT_LIFTER_PWM);
threadComplete = false;
liftThreadRunning = false;
printOutDebugData("Successfully initialized CatzLift");
RTLift = new SpeedControllerGroup(lifterRTRT, lifterRTLT);
LTLift = new SpeedControllerGroup(lifterLTRT, lifterLTLT);
LTLift.setInverted(true);
}
/**
* liftingTo____Height: The three methods below will move the lifter to the
* desired height in parallel with other code. For example, you can run one of
* these lift loops and drive/turn at the same time by using threading.
*/
//RENAME listToScaleHeight() to liftToHeight() AND PASS IN HEIGHT IN INCHES
//THEN GET RID OF SWITCH HEIGHT AND USE COMMON FUNCTION SO WE DON'T HAVE TO MAINTAIN TWO
//VERSIONS OF THE SAME CODE
/******************************************************************************************************************
*
* Method
* Measures the amount of counts the robot's lift takes to reach a certain heights.
*
*
******************************************************************************************************************/
public void liftToHeight(double heightInches) {
Thread t = new Thread(() -> {
double error = 0;
int count = 0;
double counts = heightInches * LIFT_COUNTS_PER_INCH;
double targetCount = liftEncoder.get() + counts;
boolean done = false;
boolean limitSwitchState = false;
while (!Thread.interrupted()) {
printOutDebugData("Lifter scale thread beginning");
timeout.reset();
timeout.start();
while(liftThreadRunning == true) {
Timer.delay(0.005);
}
liftThreadRunning = true;
threadComplete = false;
double startingCount = liftEncoder.get();
targetCount = startingCount + counts;
this.liftUp();
/******************************************************************************************************************
*
* Loop
* This scans for data and completes the loop and moves onto the remaining code in two conditions -
* when the threshold of counts is more than the current incompleted amount of counts and when the limit switch is reached to the limit.
*
*
******************************************************************************************************************/
while (done == false && timeout.get() < 4.2) //Get rid of 4.2, magic number
{
count = liftEncoder.get();
error = Math.abs(targetCount - count);
if(error < LIFTER_ERROR_THRESHOLD_PULSES)
{
done = true;
}
limitSwitchState = lifterLimitTop.get();
if (limitSwitchState == true)
{
done = true;
System.out.println("limitTripped");
}
}
this.stopLift();
liftThreadRunning = false;
threadComplete = true;
printOutDebugData("Lift to scale height thread complete");
System.out.println(timeout.get() + ": " + count + ", " + error + ", " + startingCount);
timeout.stop();
timeout.reset();
Thread.currentThread().interrupt();
}
});
t.start(); //what does t mean??? be more descriptive!
}
/******************************************************************************************************************
*
* Method
* Initiates a thread.-
*
*
******************************************************************************************************************/
public void dropToGroundHeight() {
Thread t = new Thread(() -> {
int count = 0;
boolean done = false;
boolean limitSwitchState = false;
while (!Thread.interrupted()) {
int limitSwitchStateCounter = 0;
printOutDebugData("dropToGroundHeight thread beginning");
timeout.reset();
timeout.start();
limitSwitchState = false;
while(liftThreadRunning == true) {
//wait for previous thread to finish
Timer.delay(0.005);
}
liftThreadRunning = true;
threadComplete = false;
this.liftDown();
System.out.println("Limit Switch: " + limitSwitchState);
while (!done && timeout.get() < 4.0) { //wait for bottom limit switch to read true
// previous code: "done == false" avoided double negative. -Kazuma
count = liftEncoder.get();
limitSwitchState = lifterLimitBot.get();
if(limitSwitchState)
{
limitSwitchStateCounter++;
}
else
{
limitSwitchStateCounter = 0;
}
if(limitSwitchStateCounter > 5)
{
done = true;
}
System.out.println("Limit Switch: " + limitSwitchState);
Timer.delay(0.010);
}
this.stopLift();
liftThreadRunning = false;
threadComplete = true;
printOutDebugData("Drop to ground height thread complete");
System.out.println(timeout.get() + ": " + count + ", " + limitSwitchState);
timeout.stop();
timeout.reset();
Thread.currentThread().interrupt();
}
});
t.start(); //be a little more specific on naming this thread
}
/******************************************************************************************************************
* Initiates code for lift to move upwards
******************************************************************************************************************/
public void liftUp() {
RTLift.set(LIFT_SPEED);
LTLift.set(-LIFT_SPEED);
}
/******************************************************************************************************************
* Initiates code for lift to move downwards
******************************************************************************************************************/
public void liftDown() {
RTLift.set(-1.0);
LTLift.set(-1.0);
}
/******************************************************************************************************************
* Stops lift movement
******************************************************************************************************************/
public void stopLift() {
RTLift.set(0);
LTLift.set(0);
}
/******************************************************************************************************************
* After all the requirements are met the robot is ready to lift.
******************************************************************************************************************/
public void setReadyToLift(boolean ready) {
threadComplete = ready;
}
/******************************************************************************************************************
* Debugging
******************************************************************************************************************/
private static void printOutDebugData(String info) {
if (Robot.debugMode == true) {
double currentTime = Robot.globalTimer.get();
System.out.println(currentTime + " -" + info);
}
}
/******************************************************************************************************************
* Raise / Lower lift based on input
******************************************************************************************************************/
public void setLiftMotors(double percentOutput) {
if(lifterLimitTop.get() == true || lifterLimitBot.get() == true)
{
percentOutput = 0;
}
RTLift.set(percentOutput);
LTLift.set(percentOutput);
}
}