Skip to content

Commit 2ee09f9

Browse files
committed
2022_03_10 vision shot and flywheel controller
1 parent 2731492 commit 2ee09f9

File tree

6 files changed

+64
-28
lines changed

6 files changed

+64
-28
lines changed

.vscode/launch.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
{
8+
"type": "java",
9+
"name": "Launch InterpolationTable",
10+
"request": "launch",
11+
"mainClass": "frc.lib.util.InterpolationTable",
12+
"projectName": "RapidReact"
13+
},
714
{
815
"type": "wpilib",
916
"name": "WPILib Desktop Debug",

src/main/java/frc/lib/util/InterpolationTable.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55

66
public class InterpolationTable {
77

8+
public static void main(String... args) {
9+
InterpolationTable table = new InterpolationTable(new double[][]{
10+
{1.7, 1800, 78.25},
11+
{2.4, 1875, 73.4},
12+
{2.95, 1980, 73.25}
13+
});
14+
System.out.println(Arrays.toString(table.sample(2)));
15+
}
16+
817
private static final Comparator<double[]> INPUT_SORTER = (point1, point2) -> {return point1[0] > point2[0] ? 1 : -1;};
918

1019
private final double[][] referencePoints;
@@ -16,14 +25,22 @@ public InterpolationTable(double[][] referencePoints) {
1625
propertyCount = referencePoints[0].length - 1;
1726
}
1827

19-
public double[] sample(double input) throws IllegalArgumentException {
28+
public double[] sample(double input) {
2029
int leftBoundIndex = 0;
2130
int rightBoundIndex = referencePoints.length - 1;
2231
if (input < referencePoints[leftBoundIndex][0]) {
23-
throw new IllegalArgumentException();
32+
double[] output = new double[propertyCount];
33+
for (int i = 0; i < propertyCount; i++) {
34+
output[i] = referencePoints[leftBoundIndex][1 + i];
35+
}
36+
return output;
2437
}
2538
if (input > referencePoints[rightBoundIndex][0]) {
26-
throw new IllegalArgumentException();
39+
double[] output = new double[propertyCount];
40+
for (int i = 0; i < propertyCount; i++) {
41+
output[i] = referencePoints[rightBoundIndex][1 + i];
42+
}
43+
return output;
2744
}
2845
boolean found = false;
2946
while (!found) {

src/main/java/frc/robot/shooter/Shooter.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import edu.wpi.first.wpilibj.CounterBase.EncodingType;
1818
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
1919
import edu.wpi.first.wpilibj2.command.SubsystemBase;
20+
import frc.lib.util.InterpolationTable;
2021
import frc.robot.Constants.ElectricalConstants;
2122
import frc.robot.Constants.ShooterConstants;
2223

@@ -25,6 +26,12 @@ public class Shooter extends SubsystemBase {
2526
public static boolean UP = true;
2627
public static boolean DOWN = false;
2728

29+
public static final InterpolationTable INTERPOLATION_TABLE = new InterpolationTable(new double[][]{
30+
{1.7, 1800, 78.25},
31+
{2.4, 1875, 73.25},
32+
{2.95, 1980, 73.25}
33+
});
34+
2835
private boolean hoodDirection;
2936
int highCurrentCount = 0;
3037
int lowCurrentCount = 0;
@@ -74,6 +81,8 @@ public Shooter() {
7481
hoodEncoder = hoodMotor.getEncoder();
7582
masterEncoder = shooterLeader.getEncoder();
7683

84+
shooterLeader.setOpenLoopRampRate(0.1);
85+
7786
hoodEncoder.setPositionConversionFactor(ShooterConstants.kHoodGearingRatio);
7887

7988
hoodController = hoodMotor.getPIDController();
@@ -173,7 +182,8 @@ public double getEncoderPosition() {
173182

174183
public void setHoodPosition(double degrees) {
175184
// degrees = Math.min(Math.max(degrees, ShooterConstants.kHoodMinAngle), ShooterConstants.kHoodMaxAngle);
176-
hoodController.setReference(degrees, ControlType.kPosition, 0, 0.0, ArbFFUnits.kPercentOut);
185+
double reference = Math.max(60, Math.min(100, degrees));
186+
hoodController.setReference(reference, ControlType.kPosition, 0, 0.0, ArbFFUnits.kPercentOut);
177187
}
178188

179189
public void setShooterVelocity(double velocity) {
@@ -234,4 +244,21 @@ private void runTrigger() {
234244
triggerMotor.stopMotor();
235245
}
236246
}
247+
248+
public static class ShooterState {
249+
public final double hoodAngle;
250+
public final double velocity;
251+
public ShooterState(double hoodAngle, double velocity) {
252+
this.hoodAngle = hoodAngle;
253+
this.velocity = velocity;
254+
}
255+
}
256+
257+
public static double lookupVelocity(double distance) {
258+
return INTERPOLATION_TABLE.sample(distance)[0];
259+
}
260+
261+
public static double lookupHoodAngle(double distance) {
262+
return INTERPOLATION_TABLE.sample(distance)[1];
263+
}
237264
}

src/main/java/frc/robot/shooter/ShooterTable.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/main/java/frc/robot/shooter/commands/FlywheelController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public class FlywheelController extends CommandBase {
1919
double rpm;
2020
private Timer timer = new Timer();
2121
Notifier controller = new Notifier(this::controller);
22-
private DCMotor flywheel = new DCMotor(0.002081897, 0, 0.317466);
23-
private PIDController flywheelController = new PIDController(0.02, 0, 0);
22+
private DCMotor flywheel = new DCMotor(0.002081897, 0, 0.37);
23+
private PIDController flywheelController = new PIDController(0.0175, 0, 0);
2424
// private PIDController flywheelController = new PIDController(0.05, 0, 0);
2525
boolean closeToTarget = false;
2626
double velocity = 0;
@@ -63,6 +63,8 @@ void controller() {
6363
double voltage = flywheel.solveFeedforward(rpm, 0) + flywheelController.calculate(velocity, dt);
6464
// double voltage = flywheel.solveFeedforward(rpm, 0);
6565

66+
voltage = Math.min(voltage, 11.5);
67+
6668
shooter.setShooterVoltage(voltage);
6769

6870
lastPosition = position;

src/main/java/frc/robot/shooter/commands/VisionShooter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public class VisionShooter extends CommandBase {
1717
double rpm;
1818
private Timer timer = new Timer();
1919
Notifier controller = new Notifier(this::controller);
20-
private DCMotor flywheel = new DCMotor(0.002081897, 0, 0.317466);
21-
private PIDController flywheelController = new PIDController(0.02, 0, 0);
20+
private DCMotor flywheel = new DCMotor(0.002081897, 0, 0.302);
21+
private PIDController flywheelController = new PIDController(0.0175, 0, 0);
2222
boolean closeToTarget = false;
2323
double velocity = 0;
2424

@@ -53,9 +53,9 @@ void controller() {
5353
}
5454
velocity = (position - lastPosition) / (dt) * 60;
5555

56-
double distancePercent = (limelight.getState().distance - 1.7) / (2.9 - 1.7);
57-
double rpm = distancePercent * (180) + 1800;
58-
double hoodAngle = distancePercent * -5 + 78.25;
56+
double distance = limelight.getState().distance;
57+
double rpm = Shooter.lookupVelocity(distance);
58+
double hoodAngle = Shooter.lookupHoodAngle(distance);
5959

6060
shooter.setHoodPosition(hoodAngle);
6161
flywheelController.setReference(rpm);

0 commit comments

Comments
 (0)