Skip to content

Commit c15b36c

Browse files
committed
Flywheel calculated shot edits
1 parent bba9879 commit c15b36c

4 files changed

Lines changed: 39 additions & 8 deletions

File tree

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ dependencies {
7474

7575
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.1'
7676
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
77+
78+
implementation 'org.apache.commons:commons-math3:3.6.1'
7779
}
7880

7981
test {

src/main/java/frc/robot/commands/DrivetrainCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,25 +113,25 @@ public void execute() {
113113
.withRotationalRate(
114114
-rightX.getAsDouble()
115115
* MaxAngularRate)); // Drive counterclockwise with negative X
116-
System.out.println("TELEOP");
116+
// System.out.println("TELEOP");
117117
break;
118118

119119
// (Needs check) Mode for when shots are from a still position
120120
case STILL_SHOT:
121121
// make X with wheels, set wheels to brake mode
122122
applyRequest.ModuleStates = states;
123123
subsystem.setControl(applyRequest);
124-
System.out.println("Drivetrain: Still Shot Configuration");
124+
// System.out.println("Drivetrain: Still Shot Configuration");
125125
break;
126126

127127
// (Incomplete) Mode for moving shots
128128
case SOTM:
129129
// shoot on the move, reference Mechanical Advantage build log
130-
System.out.println("Drivetrain: SOTM Drive");
130+
// System.out.println("Drivetrain: SOTM Drive");
131131
break;
132132

133133
case AUTO_ALIGN_HUB:
134-
System.out.println("AUTO ALIGN");
134+
// System.out.println("AUTO ALIGN");
135135
// Recompute the desired heading toward the hub each loop
136136
m_targetAngle =
137137
ShotCalculator.getRotationTowardsHub(

src/main/java/frc/robot/commands/FlywheelCommand.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ public void execute() {
5959
// Spins flywheels at estimated speed given distance from hub
6060
case CALCULATED_SHOT:
6161
isOn = "true";
62-
6362
subsystem.shoot(
6463
ShotCalculator.calculateFlywheelShot(drivetrain.getState().Pose.getTranslation()));
65-
// System.out.println("Flywheel: Calculated Shot");
64+
System.out.println(
65+
"Flywheel: Calculated Shot"
66+
+ ShotCalculator.calculateFlywheelShot(
67+
drivetrain.getState().Pose.getTranslation()));
6668
break;
6769

6870
// Determines if robot should be prepared to shoot (if during active period or 5 seconds

src/main/java/frc/robot/helpers/ShotCalculator.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,25 @@
77
import edu.wpi.first.wpilibj.DriverStation;
88
import frc.robot.config.FlywheelConfig;
99
import java.util.Optional;
10+
import org.apache.commons.math3.stat.*;
11+
import org.apache.commons.math3.stat.regression.SimpleRegression;
1012

1113
public class ShotCalculator {
1214
/* Calculation Methods */
15+
// Calculate variable shot
1316
public static double calculateFlywheelShot(Translation2d robotPosition) {
1417
// 1. Calculate distance between goal and robot position
1518
double distanceToGoal = calculateHubPosition().getDistance(robotPosition);
16-
// 2. Retrieve correlating RPS from map
19+
// 2. Retrieve correlating RPS from map based off of distance
20+
if (distanceToGoal > Units.inchesToMeters(87) || distanceToGoal < Units.inchesToMeters(43)) {
21+
System.out.println(getRPM(distanceToGoal));
22+
return getRPM(distanceToGoal);
23+
}
24+
System.out.println(DISTANCE_TO_FLYWHEEL.get(distanceToGoal));
1725
return DISTANCE_TO_FLYWHEEL.get(distanceToGoal);
1826
}
1927

28+
// Determine if flywheel should be running before active period and run at mid level speed
2029
public static double calculateFlywheelDefaultShot(Translation2d robotPosition) {
2130
// 1. Determine if within shooter run period (five seconds before or during active period)
2231
if (shouldRunShooter(5)) {
@@ -144,8 +153,26 @@ public static boolean shouldRunShooter(double graceSeconds) {
144153
DISTANCE_TO_FLYWHEEL.put(Units.inchesToMeters(141), 70.0);
145154
DISTANCE_TO_FLYWHEEL.put(Units.inchesToMeters(90), 60.0);
146155
DISTANCE_TO_FLYWHEEL.put(Units.inchesToMeters(61), 50.0);
147-
DISTANCE_TO_FLYWHEEL.put(Units.inchesToMeters(43), 40.0);
156+
DISTANCE_TO_FLYWHEEL.put(Units.inchesToMeters(43), 47.0);
148157
DISTANCE_TO_FLYWHEEL.put(Units.inchesToMeters(74), 50.0);
149158
DISTANCE_TO_FLYWHEEL.put(Units.inchesToMeters(134), 65.0);
150159
}
160+
161+
// Distance (m) -> Speed (RPS)
162+
private static final SimpleRegression regression = new SimpleRegression();
163+
164+
// Set inputs
165+
static {
166+
regression.addData(Units.inchesToMeters(174), 87.0);
167+
regression.addData(Units.inchesToMeters(141), 70.0);
168+
regression.addData(Units.inchesToMeters(90), 60.0);
169+
regression.addData(Units.inchesToMeters(61), 50.0);
170+
regression.addData(Units.inchesToMeters(43), 47.0);
171+
regression.addData(Units.inchesToMeters(74), 50.0);
172+
regression.addData(Units.inchesToMeters(134), 65.0);
173+
}
174+
175+
public static double getRPM(double distance) {
176+
return regression.getSlope() * distance + regression.getIntercept();
177+
}
151178
}

0 commit comments

Comments
 (0)