This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Claw.java
210 lines (173 loc) · 7.07 KB
/
Claw.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
package hardware;
import auto.AutoAlignment;
import calc.Constants.ClawConstants;
import calc.Constants.FieldConstants;
import calc.Constants.NeoMotorConstants;
import calc.Constants.PlacementConstants;
import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkMaxLowLevel.MotorType;
import com.revrobotics.CANSparkMaxLowLevel.PeriodicFrame;
import com.revrobotics.RelativeEncoder;
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import frc.robot.DriverUI;
public class Claw {
private final CANSparkMax claw;
private final RelativeEncoder clawEncoder;
private double desiredSpeed = 0;
private boolean intakeMode = false;
// Timer values to have the claw auto outtake for X seconds
private boolean startedOuttakingBool = false;
private boolean finishedOuttaking = false;
private double outtakeSeconds = 0;
private double startedOuttakingTimestamp = 0;
private double startedIntakingTimestamp = 0;
private boolean hasGameElement = false;
private boolean hasGameElementOneLoopBehind = false;
// @Graph
private double current = 0;
public Claw() {
claw = new CANSparkMax(ClawConstants.CLAW_CAN_ID, MotorType.kBrushless);
claw.restoreFactoryDefaults();
clawEncoder = claw.getEncoder();
clawEncoder.setPositionConversionFactor(ClawConstants.CLAW_POSITION_CONVERSION_FACTOR);
claw.setSmartCurrentLimit(ClawConstants.CLAW_CURRENT_LIMIT);
// See https://docs.revrobotics.com/sparkmax/operating-modes/control-interfaces
claw.setPeriodicFramePeriod(PeriodicFrame.kStatus3, 65535);
claw.setPeriodicFramePeriod(PeriodicFrame.kStatus5, 65535);
claw.setInverted(false);
NeoMotorConstants.motors.add(claw);
setBrakeMode();
}
public void resetEncoder() {
clawEncoder.setPosition(0);
}
public void periodic() {
/**
* Output generally is -0.25 < appliedOutput < 0
* When grabbygrab
* Current is generally 15 < current < 30
* Which is a big range unfortunatly.
* I am not sure where the curernt > 30 came from
* I am having a hard time getting that read again on the graph anymore
* Let's just go with > 15 for now
*
* Laslty, desiredSpeed is ALWAYS > 0.45 when we are intaking
* Which is a good indicator of finding if the claw is just getting up
* to speed or if it is stalled
*
* The problem with having a boolean this sophisticated
* Is that there are too many independant variables to check
* And if a single one is off, the boolean will be false
*
* Idea: use a startedIntakingTimestamp to check
* if the claw has been intaking for a while
* i.e. is finished getting up to speed.
*/
if (hasGameElement) {
hasGameElement = desiredSpeed > 0;
}
else {
hasGameElement =
(-0.25 < claw.getAppliedOutput() && claw.getAppliedOutput() < 0) &&
(current > 15 && claw.getOutputCurrent() > 15) &&
(desiredSpeed > 0.45) &&
(DriverUI.currentTimestamp - startedIntakingTimestamp > 0.25);
}
// Check if our current current and our current (one loop behind, hasn't updated yet)
// is in the range that happens when we have a game element,
// but the claw is not yet in a stalled state.
if (FieldConstants.GAME_MODE == FieldConstants.GameMode.TELEOP &&
20 < current && current < 30 &&
20 < claw.getOutputCurrent() && claw.getOutputCurrent() < 30 &&
desiredSpeed > 0.45)
{
// Stop the claw temporarily
// It will be restarted in the next loop
claw.set(0);
return;
}
DriverUI.spicyClaw = claw.getMotorTemperature() > 60;
if (FieldConstants.GAME_MODE == FieldConstants.GameMode.TELEOP) {
// This is for automatically outtaking the game piece
if ((DriverUI.currentTimestamp - startedOuttakingTimestamp) > outtakeSeconds && startedOuttakingBool) {
finishedOuttaking = true;
}
// Slow down claw for cube mode
if (!AutoAlignment.coneMode &&
(FieldConstants.GAME_MODE == FieldConstants.GameMode.TELEOP || FieldConstants.GAME_MODE == FieldConstants.GameMode.TEST))
{
desiredSpeed = MathUtil.clamp(desiredSpeed, -0.3, 0.5);
}
// Speed up claw for cone mode
else if (desiredSpeed > 0.45) {
desiredSpeed = 1;
}
}
setSpeed(desiredSpeed);
}
private void setSpeed(double speed) {
claw.set(-speed);
}
public void setBrakeMode() {
claw.setIdleMode(CANSparkMax.IdleMode.kBrake);
}
public void setCoastMode() {
claw.setIdleMode(CANSparkMax.IdleMode.kCoast);
}
public void setDesiredSpeed(double speed) {
if (speed > 0) { intakeMode = true; }
else if (speed < 0) { intakeMode = false; }
if (intakeMode) {
if (speed > this.desiredSpeed) {
// If our desiredSpeed is <= 0, that means that we just started intaking
// So start a timer so that we can check if we have stalled
// without taking into account the time it takes to speed up
if (this.desiredSpeed <= 0) {
this.startedIntakingTimestamp = DriverUI.currentTimestamp;
}
// To prevent damage to game elements, the claw will not intake at full speed
this.desiredSpeed = speed;
}
}
else {
this.desiredSpeed = speed;
}
SmartDashboard.putNumber("Claw/DesiredSpeed", desiredSpeed);
}
public void stopClaw() {
this.desiredSpeed = 0;
}
public double getDesiredSpeed() {
return this.desiredSpeed;
}
public void outTakeforXSeconds(double X) {
setDesiredSpeed(PlacementConstants.CLAW_OUTTAKE_SPEED_CONE);
this.startedOuttakingBool = true;
this.outtakeSeconds = X;
this.finishedOuttaking = false;
this.startedOuttakingTimestamp = DriverUI.currentTimestamp;
}
public boolean getFinishedOuttaking() {
return this.finishedOuttaking;
}
public void setStartedOuttakingBool(boolean startedOuttakingBool) {
this.startedOuttakingBool = startedOuttakingBool;
}
public boolean getStartedOuttakingBool() {
return this.startedOuttakingBool;
}
public void setFinishedOuttaking(boolean finishedOuttaking) {
this.finishedOuttaking = finishedOuttaking;
}
public void updateOutputCurrent() {
current = claw.getOutputCurrent();
hasGameElementOneLoopBehind = hasGameElement;
}
public boolean hasGameElement() {
return hasGameElement;
}
public boolean justAquiredGameElement() {
return !hasGameElementOneLoopBehind && hasGameElement;
}
}