-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArm.java
162 lines (131 loc) · 6.25 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
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.SubsystemBase;
import frc.trigon.robot.utilities.Conversions;
public class Arm extends SubsystemBase {
private final static Arm INSTANCE = new Arm();
private final CANSparkMax
masterElevatorMotor = ArmConstants.MASTER_ELEVATOR_MOTOR,
followerElevatorMotor = ArmConstants.FOLLOWER_ELEVATOR_MOTOR,
masterAngleMotor = ArmConstants.MASTER_ELEVATOR_MOTOR,
followerAngleMotor = ArmConstants.FOLLOWER_ANGLE_MOTOR;
private final TalonSRX elevatorEncoder = ArmConstants.ELEVATOR_ENCODER;
private TrapezoidProfile
angleMotorProfile = null,
elevatorMotorProfile = null;
private double
lastAngleMotorProfileGeneration,
lastElevatorMotorProfileGeneration;
public static Arm getInstance() {
return INSTANCE;
}
private Arm() {
}
void generateElevatorMotorProfile(double targetPositionMeters, double speedPercentage) {
elevatorMotorProfile = new TrapezoidProfile(
Conversions.scaleConstraints(ArmConstants.ELEVATOR_CONSTRAINTS, speedPercentage),
new TrapezoidProfile.State(targetPositionMeters, 0),
new TrapezoidProfile.State(getElevatorPositionRevolutions(), getElevatorVelocityMetersPerSeconds())
);
lastElevatorMotorProfileGeneration = Timer.getFPGATimestamp();
}
void generateAngleMotorProfile(Rotation2d targetAngle, double speedPercentage) {
angleMotorProfile = new TrapezoidProfile(
Conversions.scaleConstraints(ArmConstants.ANGLE_CONSTRAINTS, speedPercentage),
new TrapezoidProfile.State(targetAngle.getDegrees(), 0),
new TrapezoidProfile.State(getAnglePosition().getDegrees(), getAngleVelocityDegreesPerSeconds())
);
lastAngleMotorProfileGeneration = Timer.getFPGATimestamp();
}
void setTargetAngleFromProfile() {
if (angleMotorProfile == null) {
stopAngleMotors();
return;
}
TrapezoidProfile.State targetState = angleMotorProfile.calculate(getAngleProfileTime());
setAngleMotorsVoltage(calculateAngleOutput(targetState));
}
void setTargetElevatorPositionFromProfile() {
if (angleMotorProfile == null) {
stopElevatorMotors();
return;
}
TrapezoidProfile.State targetState = elevatorMotorProfile.calculate(getElevatorProfileTime());
setElevatorMotorsVoltage(calculateElevatorOutput(targetState));
}
boolean atAngle(Rotation2d angle) {
return Math.abs(angle.getDegrees() - getAnglePosition().getDegrees()) < ArmConstants.ANGLE_TOLERANCE_DEGREES;
}
boolean atTargetPositionElevator(double positionMeters) {
return Math.abs(positionMeters - getElevatorPositionRevolutions()) < ArmConstants.ELEVATOR_TOLERANCE_METERS;
}
boolean isElevatorOpening(double targetMeters) {
return targetMeters > getElevatorPositionRevolutions();
}
private double getElevatorVelocityMetersPerSeconds() {
double elevatorVelocityMagTicksPer100ms = elevatorEncoder.getSelectedSensorVelocity();
double elevatorVelocityMagTicksPerSecond = Conversions.perHundredMsToPerSecond(elevatorVelocityMagTicksPer100ms);
double elevatorVelocityRevolutionPerSecond = Conversions.magTicksToRevolutions(elevatorVelocityMagTicksPerSecond);
return elevatorVelocityRevolutionPerSecond / ArmConstants.ELEVATOR_METERS_PER_REVOLUTION;
}
private Rotation2d getAnglePosition() {
double positionRevolutions = ArmConstants.ANGLE_ENCODER_POSITION_SIGNAL.refresh().getValue();
return Rotation2d.fromRotations(positionRevolutions);
}
private double getElevatorPositionRevolutions() {
double elevatorPositionMagTicks = elevatorEncoder.getSelectedSensorPosition();
double elevatorPositionRevolution = Conversions.magTicksToRevolutions(elevatorPositionMagTicks);
return elevatorPositionRevolution / ArmConstants.ELEVATOR_METERS_PER_REVOLUTION;
}
private double getAngleVelocityDegreesPerSeconds() {
return Conversions.revolutionsToDegrees(ArmConstants.ANGLE_ENCODER_VELOCITY_SIGNAL.refresh().getValue());
}
private double calculateElevatorOutput(TrapezoidProfile.State targetState) {
double pidOutput = ArmConstants.ELEVATOR_PID_CONTROLLER.calculate(
getElevatorPositionRevolutions(),
targetState.position
);
double feedforward = ArmConstants.ELEVATOR_MOTOR_FEEDFORWARD.calculate(
targetState.velocity
);
return pidOutput + feedforward;
}
private double calculateAngleOutput(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 pidOutput + feedforward;
}
private void setAngleMotorsVoltage(double outputVoltage) {
masterAngleMotor.setVoltage(outputVoltage);
followerAngleMotor.setVoltage(outputVoltage);
}
private void setElevatorMotorsVoltage(double outputVoltage) {
masterElevatorMotor.setVoltage(outputVoltage);
followerElevatorMotor.setVoltage(outputVoltage);
}
private double getAngleProfileTime() {
return Timer.getFPGATimestamp() - lastAngleMotorProfileGeneration;
}
private double getElevatorProfileTime() {
return Timer.getFPGATimestamp() - lastElevatorMotorProfileGeneration;
}
private void stopElevatorMotors() {
masterElevatorMotor.stopMotor();
followerElevatorMotor.stopMotor();
}
private void stopAngleMotors() {
masterAngleMotor.stopMotor();
followerAngleMotor.stopMotor();
}
}