Skip to content

Commit 2af80ff

Browse files
authored
Merge pull request #12 from Prosper-FRC/pivot-redux
Merge tuned pivot to pivot base
2 parents 7ae78ff + 76bcfd1 commit 2af80ff

File tree

5 files changed

+82
-13
lines changed

5 files changed

+82
-13
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,6 @@ logs/
176176

177177
# Folder that has CTRE Phoenix Sim device config storage
178178
ctre_sim/
179+
180+
# GVersion
181+
BuildConstants.java

src/main/java/frc/robot/RobotContainer.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import frc.robot.subsystems.pivot.Pivot.PivotGoal;
1212
import frc.robot.subsystems.pivot.PivotConstants;
1313
import frc.robot.subsystems.pivot.PivotKrakenHardware;
14+
import frc.robot.utils.debugging.LoggedTunableNumber;
1415

1516
public class RobotContainer {
1617
private final Pivot kPivot =
@@ -26,8 +27,34 @@ public RobotContainer() {
2627
private void configureBindings() {
2728
kPilotController
2829
.a()
30+
.whileTrue(Commands.runOnce(() -> kPivot.setGoal(PivotGoal.CUSTOM), kPivot))
31+
.whileFalse(Commands.runOnce(() -> kPivot.stop(), kPivot));
32+
33+
kPilotController
34+
.x()
2935
.whileTrue(Commands.runOnce(() -> kPivot.setGoal(PivotGoal.DEMO), kPivot))
3036
.whileFalse(Commands.runOnce(() -> kPivot.stop(), kPivot));
37+
38+
kPilotController
39+
.y()
40+
.whileTrue(
41+
Commands.runOnce(
42+
// Get the desired voltage from the LoggedTunableNumber
43+
() -> kPivot.setVoltage(new LoggedTunableNumber("Flywheel/Voltage", 0.0).get()),
44+
kPivot))
45+
.whileFalse(Commands.runOnce(() -> kPivot.stop(), kPivot));
46+
47+
// Reset the arm position to zero
48+
kPilotController.b().onTrue(Commands.runOnce(() -> kPivot.resetPosition(), kPivot));
49+
50+
kPilotController
51+
.povUp()
52+
.whileTrue(Commands.runOnce(() -> kPivot.setVoltage(3.0), kPivot))
53+
.whileFalse(Commands.runOnce(() -> kPivot.stop(), kPivot));
54+
kPilotController
55+
.povDown()
56+
.whileTrue(Commands.runOnce(() -> kPivot.setVoltage(-3.0), kPivot))
57+
.whileFalse(Commands.runOnce(() -> kPivot.stop(), kPivot));
3158
}
3259

3360
public Command getAutonomousCommand() {

src/main/java/frc/robot/subsystems/pivot/Pivot.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public class Pivot extends SubsystemBase {
1616
public enum PivotGoal {
1717
// NOTE These setpoints must be updated per robot, "DEMO" is merely an example setpoint.
1818

19-
DEMO(() -> Rotation2d.fromDegrees(10.0));
19+
DEMO(() -> Rotation2d.fromDegrees(90.0)),
20+
/** We get the setpoint in degrees from the LoggedTunableNumber */
21+
CUSTOM(() -> Rotation2d.fromDegrees(new LoggedTunableNumber("Pivot/Custom", 0.0).get()));
2022

2123
// Suppliers are used to keep in line with this team's programming convention. In some
2224
// instances, we may need to have a goal that dynamically changes based on a state from another
@@ -81,6 +83,10 @@ public void periodic() {
8183
if (currentPivotGoal != null) {
8284
currentPivotGoalPosition = currentPivotGoal.getGoal();
8385
setPosition(currentPivotGoalPosition.getRadians());
86+
87+
Logger.recordOutput("Pivot/Goal", currentPivotGoal);
88+
} else {
89+
Logger.recordOutput("Pivot/Goal", "NONE");
8490
}
8591

8692
// Update feedback, feedforward, and motion magic gains if we change them from network tables
@@ -122,7 +128,15 @@ public void setGoal(PivotGoal desiredGoal) {
122128
* @param voltage The voltage to set the motor to
123129
*/
124130
public void setVoltage(double voltage) {
125-
kPivotHardware.setVoltage(voltage);
131+
if (getPosition().getDegrees() > PivotConstants.kUpperPositionLimit.getDegrees()
132+
&& voltage > 0.0) {
133+
// do nothing
134+
} else if (getPosition().getDegrees() < PivotConstants.kLowerPositionLimit.getDegrees()
135+
&& voltage < 0.0) {
136+
// do nothing
137+
} else {
138+
kPivotHardware.setVoltage(voltage);
139+
}
126140
}
127141

128142
/** Sets the motor control to neutral, then switching to the default neutral control mode */
@@ -131,6 +145,10 @@ public void stop() {
131145
kPivotHardware.stop();
132146
}
133147

148+
public void resetPosition() {
149+
kPivotHardware.resetEncoder();
150+
}
151+
134152
/**
135153
* Sets the desired position of the motor. Runs using internal PID controller
136154
*

src/main/java/frc/robot/subsystems/pivot/PivotConstants.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ public record PivotGains(
3737
// If using a CANivore, set this boolean to true and set the CANivore name to
3838
// what is is. Consult your electrical lead if you are unsure if your team
3939
// is using a CANivore. The name can be configured using Phoenix Tuner.
40-
public static final boolean kUseCANivore = true;
41-
public static final String kCANBusName = "*";
40+
public static final boolean kUseCANivore = false;
41+
public static final String kCANBusName = "drivebase";
4242

4343
// The gearing between your motor shaft and output shaft, consult the
4444
// mechanical team for this value
45-
public static final double kGearRatio = 1.0 / 1.0;
45+
public static final double kGearRatio = 96.9 / 1.0;
4646

47-
public static final int kLeaderMotorID = 0;
48-
public static final int kFollowerMotorID = 0;
47+
public static final int kLeaderMotorID = 41;
48+
public static final int kFollowerMotorID = 42;
4949

5050
// If using a REV ThroughBore as your absolute encoder, set this value to true
5151
public static final boolean kUseThroughBore = false;
@@ -66,14 +66,14 @@ public record PivotGains(
6666
public static final double kStatusSignalUpdateFrequencyHz = 100.0;
6767

6868
// Soft positions limits to prevent the arm from breaking itself
69-
public static final Rotation2d kUpperPositionLimit = new Rotation2d();
70-
public static final Rotation2d kLowerPositionLimit = new Rotation2d();
69+
public static final Rotation2d kUpperPositionLimit = Rotation2d.fromDegrees(275.0);
70+
public static final Rotation2d kLowerPositionLimit = Rotation2d.fromDegrees(0.0);
7171

7272
// NOTE The configuration only needs to be applied to the leader-motor. The
7373
// follower-motor MUST obey the configuration of the leader-motor.
7474
public static final KrakenConfiguration kMotorConfiguration =
75-
new KrakenConfiguration(true, true, 0.0, 0.0, NeutralModeValue.Brake, false, 0.0, 0.0);
75+
new KrakenConfiguration(true, true, 80.0, 30.0, NeutralModeValue.Brake, false, 12.0, -12.0);
7676

7777
public static final PivotGains kPivotGains =
78-
new PivotGains(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
78+
new PivotGains(75.0, 0.0, 0.0, 0.24, 0.3, 0.0, 0.0, 10.0, 20.0);
7979
}

src/main/java/frc/robot/subsystems/pivot/PivotKrakenHardware.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static class PivotInputs {
4141
public double velocityRadsPerSec = 0.0;
4242
public double[] appliedVolts = new double[] {};
4343
public double[] supplyCurrentAmps = new double[] {};
44+
public double[] statorCurrentAmps = new double[] {};
4445
public double[] temperatureCelsius = new double[] {};
4546
}
4647

@@ -62,6 +63,7 @@ public static class PivotInputs {
6263
private StatusSignal<Double> velocityRotationsPerSec;
6364
private List<StatusSignal<Double>> appliedVolts;
6465
private List<StatusSignal<Double>> supplyCurrentAmps;
66+
private List<StatusSignal<Double>> statorCurrentAmps;
6567
private List<StatusSignal<Double>> temperatureCelsius;
6668

6769
// Control modes
@@ -92,6 +94,9 @@ public PivotKrakenHardware(KrakenConfiguration configuration) {
9294
motorConfiguration.Voltage.PeakForwardVoltage = configuration.kPeakForwardVoltage();
9395
motorConfiguration.Voltage.PeakReverseVoltage = configuration.kPeakReverseVoltage();
9496

97+
// Reset encoder on startup
98+
kLeadMotor.setPosition(0.0);
99+
95100
// Setup feedback sensor for position control
96101
if (PivotConstants.kUseThroughBore) {
97102
throughBoreEncoder = new DutyCycleEncoder(PivotConstants.kThroughBoreEncoderPort);
@@ -110,6 +115,7 @@ public PivotKrakenHardware(KrakenConfiguration configuration) {
110115
velocityRotationsPerSec = kLeadMotor.getVelocity();
111116
appliedVolts = List.of(kLeadMotor.getMotorVoltage(), kFollowMotor.getMotorVoltage());
112117
supplyCurrentAmps = List.of(kLeadMotor.getSupplyCurrent(), kFollowMotor.getSupplyCurrent());
118+
statorCurrentAmps = List.of(kLeadMotor.getStatorCurrent(), kFollowMotor.getStatorCurrent());
113119
temperatureCelsius = List.of(kLeadMotor.getDeviceTemp(), kFollowMotor.getDeviceTemp());
114120

115121
BaseStatusSignal.setUpdateFrequencyForAll(
@@ -120,6 +126,8 @@ public PivotKrakenHardware(KrakenConfiguration configuration) {
120126
appliedVolts.get(1),
121127
supplyCurrentAmps.get(0),
122128
supplyCurrentAmps.get(1),
129+
statorCurrentAmps.get(0),
130+
statorCurrentAmps.get(1),
123131
temperatureCelsius.get(0),
124132
temperatureCelsius.get(1));
125133

@@ -141,11 +149,15 @@ public void updateInputs(PivotInputs inputs) {
141149
velocityRotationsPerSec,
142150
appliedVolts.get(0),
143151
supplyCurrentAmps.get(0),
152+
statorCurrentAmps.get(0),
144153
temperatureCelsius.get(0))
145154
.isOK();
146155
inputs.followerMotorConnected =
147156
BaseStatusSignal.refreshAll(
148-
appliedVolts.get(1), supplyCurrentAmps.get(1), temperatureCelsius.get(1))
157+
appliedVolts.get(1),
158+
supplyCurrentAmps.get(1),
159+
statorCurrentAmps.get(1),
160+
temperatureCelsius.get(1))
149161
.isOK();
150162

151163
inputs.inteneralPosition =
@@ -156,6 +168,8 @@ public void updateInputs(PivotInputs inputs) {
156168
appliedVolts.stream().mapToDouble(StatusSignal::getValueAsDouble).toArray();
157169
inputs.supplyCurrentAmps =
158170
supplyCurrentAmps.stream().mapToDouble(StatusSignal::getValueAsDouble).toArray();
171+
inputs.statorCurrentAmps =
172+
statorCurrentAmps.stream().mapToDouble(StatusSignal::getValueAsDouble).toArray();
159173
inputs.temperatureCelsius =
160174
temperatureCelsius.stream().mapToDouble(StatusSignal::getValueAsDouble).toArray();
161175
}
@@ -176,7 +190,8 @@ public void setVoltage(double voltage) {
176190
* @param feedforwardOutput Feedforward that will also be applied to the control effort
177191
*/
178192
public void setPosition(double positionRads) {
179-
kLeadMotor.setControl(kPositionVoltage.withPosition(positionRads).withSlot(0));
193+
kLeadMotor.setControl(
194+
kPositionVoltage.withPosition(Units.radiansToRotations(positionRads)).withSlot(0));
180195
}
181196

182197
/** Sets the motor control to neutral, then switching to the default neutral control mode */
@@ -220,7 +235,13 @@ public void setMotionMagicConstraints(double maxVelocity, double maxAcceleration
220235

221236
motionMagicConfiguration.MotionMagicCruiseVelocity = maxVelocity;
222237
motionMagicConfiguration.MotionMagicAcceleration = maxAcceleration;
238+
motionMagicConfiguration.MotionMagicJerk = 10.0 * maxAcceleration;
223239

224240
kLeadMotor.getConfigurator().apply(motionMagicConfiguration);
225241
}
242+
243+
/** Reset the relative encoder used for position calculations */
244+
public void resetEncoder() {
245+
kLeadMotor.setPosition(0.0);
246+
}
226247
}

0 commit comments

Comments
 (0)