-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArm.java
180 lines (153 loc) · 6.98 KB
/
Arm.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
package frc.trigon.robot.subsystems.arm;
import com.ctre.phoenix.motorcontrol.can.TalonSRX;
import com.revrobotics.CANSparkMax;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.trajectory.TrapezoidProfile;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj2.command.*;
import frc.trigon.robot.utilities.Conversions;
import java.util.Set;
public class Arm extends SubsystemBase {
private final static Arm INSTANCE = new Arm();
private final CANSparkMax
masterAngleMotor = ArmConstants.MASTER_ANGLE_MOTOR,
followerAngleMotor = ArmConstants.FOLLOWER_ANGLE_MOTOR,
masterElevatorMotor = ArmConstants.MASTER_ELEVATOR_MOTOR,
followerElevatorMotor = ArmConstants.FOLLOWER_ELEVATOR_MOTOR;
private final TalonSRX elevatorEncoder = ArmConstants.ELEVATOR_ENCODER;
private TrapezoidProfile
angleMotorProfile = null,
elevatorMotorProfile = null;
private double
lastAngleProfileGenerationTime,
lastElevatorProfileGenerationTime;
public static Arm getInstance() {
return INSTANCE;
}
private Arm() {
}
Command getCurrentSetTargetStateCommand(double elevatorPosition, Rotation2d angle, double angleSpeedPercentage, double elevatorSpeedPercentage) {
if (isElevatorOpening(elevatorPosition)) {
return new SequentialCommandGroup(
getSetTargetAngleCommand(angle, angleSpeedPercentage),
getSetTargetElevatorPositionCommand(elevatorPosition, elevatorSpeedPercentage)
);
}
return new SequentialCommandGroup(
getSetTargetElevatorPositionCommand(elevatorPosition, elevatorSpeedPercentage),
getSetTargetAngleCommand(angle, angleSpeedPercentage)
);
}
private Command getSetTargetElevatorPositionCommand(double targetElevatorPosition, double speedPercentage) {
return new FunctionalCommand(
() -> generateElevatorMotorProfile(targetElevatorPosition, speedPercentage),
this::setTargetElevatorFromProfile,
(interrupted) -> {
},
() -> false,
this
);
}
private Command getSetTargetAngleCommand(Rotation2d angle, double angleSpeedPercentage) {
return new FunctionalCommand(
() -> generateAngleMotorProfile(angle, angleSpeedPercentage),
this::setTargetAngleFromProfile,
(interrupted) -> {
},
() -> false,
this
);
}
private boolean isElevatorOpening(double targetElevatorPosition) {
return targetElevatorPosition > getElevatorPositionRevolutions();
}
private void generateAngleMotorProfile(Rotation2d targetAngle, double speedPercentage) {
angleMotorProfile = new TrapezoidProfile(
Conversions.scaleConstraints(ArmConstants.ANGLE_CONSTRAINS, speedPercentage),
new TrapezoidProfile.State(targetAngle.getDegrees(), 0),
new TrapezoidProfile.State(getAnglePosition().getDegrees(), getAngleVelocityDegreesPerSecond())
);
lastAngleProfileGenerationTime = Timer.getFPGATimestamp();
}
private void generateElevatorMotorProfile(double targetElevator, double speedPercentage) {
elevatorMotorProfile = new TrapezoidProfile(
Conversions.scaleConstraints(ArmConstants.ELEVATOR_CONSTRAINS, speedPercentage),
new TrapezoidProfile.State(targetElevator, 0),
new TrapezoidProfile.State(getElevatorPositionRevolutions(), getElevatorVelocityRevolutionsPerSecond())
);
lastElevatorProfileGenerationTime = Timer.getFPGATimestamp();
}
private void setTargetAngleFromProfile() {
if (angleMotorProfile == null) {
stopAngleMotors();
return;
}
TrapezoidProfile.State targetState = angleMotorProfile.calculate(getAngleMotorProfileTime());
setAngleMotorsVoltage(calculateAngleMotorOutput(targetState));
}
private void setTargetElevatorFromProfile() {
if (elevatorMotorProfile == null) {
stopElevatorMotors();
return;
}
TrapezoidProfile.State targetState = elevatorMotorProfile.calculate(getElevatorMotorProfileTime());
setElevatorMotorsVoltage(calculateElevatorMotorOutput(targetState));
}
private double getAngleMotorProfileTime() {
return Timer.getFPGATimestamp() - lastAngleProfileGenerationTime;
}
private double getElevatorMotorProfileTime() {
return Timer.getFPGATimestamp() - lastElevatorProfileGenerationTime;
}
private double calculateAngleMotorOutput(TrapezoidProfile.State targetState) {
double pidOutput = ArmConstants.ANGLE_PID_CONTROLLER.calculate(
getAnglePosition().getDegrees(),
targetState.position
);
double feedforward = ArmConstants.ANGLE_MOTOR_FEEDFORWARD.calculate(
Units.degreesToRadians(targetState.position),
Units.degreesToRadians(targetState.velocity)
);
return feedforward + pidOutput;
}
private double calculateElevatorMotorOutput(TrapezoidProfile.State targetState) {
double pidOutput = ArmConstants.ELEVATOR_PID_CONTROLLER.calculate(
getElevatorPositionRevolutions(),
targetState.position
);
double feedforward = ArmConstants.ELEVATOR_MOTOR_FEEDFORWARD.calculate(targetState.velocity);
return feedforward + pidOutput;
}
private double getAngleVelocityDegreesPerSecond() {
double positionRevolutions = ArmConstants.ANGLE_VELOCITY_SIGNAL.refresh().getValue();
return Conversions.revolutionsToDegrees(positionRevolutions);
}
private double getElevatorVelocityRevolutionsPerSecond() {
double magTicksPerSecond = Conversions.perHundredMsToPerSecond(elevatorEncoder.getSelectedSensorVelocity());
return Conversions.magTicksToRevolutions(magTicksPerSecond);
}
private Rotation2d getAnglePosition() {
double positionRevolutions = ArmConstants.ANGLE_POSITION_SIGNAL.refresh().getValue();
return Rotation2d.fromRotations(positionRevolutions);
}
private double getElevatorPositionRevolutions() {
return Conversions.magTicksToRevolutions(elevatorEncoder.getSelectedSensorPosition());
}
private void stopAngleMotors() {
masterAngleMotor.stopMotor();
followerAngleMotor.stopMotor();
}
private void stopElevatorMotors() {
masterElevatorMotor.stopMotor();
followerElevatorMotor.stopMotor();
}
private void setAngleMotorsVoltage(double voltage) {
masterAngleMotor.setVoltage(voltage);
followerAngleMotor.setVoltage(voltage);
}
private void setElevatorMotorsVoltage(double voltage) {
masterElevatorMotor.setVoltage(voltage);
followerElevatorMotor.setVoltage(voltage);
}
}