-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRamp.java
More file actions
158 lines (136 loc) · 5.75 KB
/
Ramp.java
File metadata and controls
158 lines (136 loc) · 5.75 KB
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
package frc.robot.subsystems;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.interpolation.InterpolatingDoubleTreeMap;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.constants.Constants;
import frc.robot.utils.Alignable;
import frc.robot.utils.NeoPidMotor;
import frc.robot.utils.logging.Logger;
import frc.robot.utils.smartshuffleboard.SmartShuffleboard;
public class Ramp extends SubsystemBase {
private final String baseLogName = "/robot/ramp/";
private final NeoPidMotor neoPidMotor;
private double rampPos = Constants.RAMP_POS;
private final InterpolatingDoubleTreeMap rampAngleMap = new InterpolatingDoubleTreeMap();
private double neoModerFF = Constants.RAMP_PID_FAR_FF;
public Ramp() {
neoPidMotor = new NeoPidMotor(Constants.RAMP_ID);
configureMotor();
resetEncoder();
neoPidMotor.enableDiagnostics("Ramp", true, true);
rampAngleMap.put(1.49352, 3.0);
rampAngleMap.put(1.806956, 5.0);
rampAngleMap.put(2.3876, 7.56);
rampAngleMap.put(2.6162, 7.9);
}
private void configureMotor() {
neoPidMotor.setSmartMotionAllowedClosedLoopError(Constants.RAMP_ERROR_RANGE);
neoPidMotor.setMaxAccel(Constants.RAMP_MAX_RPM_ACCELERATION);
neoPidMotor.getPidController().setP(Constants.RAMP_PID_P);
neoPidMotor.getPidController().setFF(neoModerFF);
}
public void periodic() {
if (Constants.RAMP_DEBUG){
SmartShuffleboard.put("Ramp", "Encoder Value", getRampPos());
SmartShuffleboard.put("Ramp", "Desired pos", rampPos);
SmartShuffleboard.put("Ramp", "Reverse Switch Tripped", getReversedSwitchState());
SmartShuffleboard.put("Ramp", "Forward Switch Tripped", getForwardSwitchState());
SmartShuffleboard.put("Driver", "Speaker Close", isShootCloseAngle())
.withPosition(9, 0)
.withSize(1, 1);
SmartShuffleboard.put("Driver", "Speaker Away", isShootAwayAngle())
.withPosition(9, 1)
.withSize(1, 1);
SmartShuffleboard.put("Driver", "Amp", isShootAmpAngle())
.withPosition(8, 1)
.withSize(1, 1);
SmartShuffleboard.put("Ramp", "Forward Switch Tripped", getForwardSwitchState());
SmartShuffleboard.put("Ramp", "Forward Switch Tripped", getForwardSwitchState());
Logger.logDouble(baseLogName + "EncoderValue", getRampPos(), Constants.ENABLE_LOGGING);
Logger.logDouble(baseLogName + "DesiredPos", rampPos, Constants.ENABLE_LOGGING);
}
if (Constants.LOG_LIMIT_SWITCHES){
Logger.logBoolean(baseLogName + "FWD LMT", getForwardSwitchState(), Constants.ENABLE_LOGGING);
Logger.logBoolean(baseLogName + "REV LMT", getReversedSwitchState(), Constants.ENABLE_LOGGING);
}
}
public void setRampPos(double targetPosition) {
neoPidMotor.setPidPos(targetPosition);
this.rampPos = targetPosition;
}
/**
* This Java Docs is bad
* Sets the motor speed
* Check if forward had a positive or negative speed value
* @param speed the speed (0-1)
*/
public void setSpeed(double speed) {
neoPidMotor.getNeoMotor().set(speed);
}
public void stopMotor() {
neoPidMotor.getNeoMotor().set(0.0);
}
public double getRampPos() {
return neoPidMotor.getEncoder().getPosition();
}
/**
* @return If the Forward Limit Switch is pressed
*/
public boolean getForwardSwitchState() {
return neoPidMotor.forwardLimitSwitchIsPressed();
}
/**
* @return If the Reversed Limit Switch is pressed
*/
public boolean getReversedSwitchState() {
return neoPidMotor.reversedLimitSwitchIsPressed();
}
public double getDesiredPosition() {
return rampPos;
}
public void resetEncoder() {
this.rampPos = 0;
neoPidMotor.resetEncoderPosition();
}
public static double encoderToAngle(double encoderValue){
//y=mx+b
return 2.48 * encoderValue + 28.5;//needs be to measured again and put in constants
}
public boolean isShootCloseAngle(){
return (Math.abs(Constants.RAMP_POS_SHOOT_SPEAKER_CLOSE - getRampPos()) <= Constants.RAMP_POS_THRESHOLD);
}
public boolean isShootAwayAngle(){
return (Math.abs(Constants.RAMP_POS_SHOOT_SPEAKER_AWAY - getRampPos()) <= Constants.RAMP_POS_THRESHOLD);
}
public boolean isShootAmpAngle(){
return (Math.abs(Constants.RAMP_POS_SHOOT_AMP - getRampPos()) <= Constants.RAMP_POS_THRESHOLD);
}
public static double angleToEncoder(double angle){
//(y-b)/m=x
return (angle - 28.5) / 2.48;//needs be to measured again and put in constants
}
public void setAngle(Rotation2d angleFromGround) {
setRampPos(angleToEncoder(angleFromGround.getDegrees()));
}
public double calcPose(Pose2d pose2d, Alignable alignable) {
return rampAngleMap.get(pose2d.getTranslation().getDistance(new Translation2d(alignable.getX(), alignable.getY())));
}
public void setFF(double feedForward) {
neoPidMotor.getPidController().setFF(feedForward);
neoModerFF = feedForward;
}
public double getFF(){
return neoModerFF;
}
public void updateFF() {
if (Math.abs(getRampPos() - getDesiredPosition()) <= Constants.RAMP_ELIM_FF_THRESHOLD) {
if (getFF() != NeoPidMotor.DEFAULT_FF) {
setFF(NeoPidMotor.DEFAULT_FF);
}
} else if (getFF() != Constants.RAMP_PID_FAR_FF) {
setFF(Constants.RAMP_PID_FAR_FF);
}
}
}