Skip to content

Commit 74eed28

Browse files
committed
Unknown shot added
1 parent bc09164 commit 74eed28

File tree

4 files changed

+107
-11
lines changed

4 files changed

+107
-11
lines changed

src/main/java/frc/robot/Robot.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public void robotPeriodic() {
9494
// robot's periodic
9595
// block in order for anything in the Command-based framework to work.
9696
CommandScheduler.getInstance().run();
97+
mRobotContainer.enableLights();
9798
}
9899

99100
/** This function is called once each time the robot enters Disabled mode. */
@@ -117,7 +118,7 @@ public void disabledPeriodic() {
117118
}
118119
}
119120

120-
mRobotContainer.disableLights();
121+
mRobotContainer.enableLights();
121122
mRobotContainer.displaySwitch();
122123
}
123124

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import frc.robot.shooter.commands.SetShooterState;
7474
import frc.robot.shooter.commands.StopShooter;
7575
import frc.robot.shooter.commands.StopTrigger;
76+
import frc.robot.shooter.commands.VisionShooter;
7677
import frc.robot.status.Status;
7778
import frc.robot.status.commands.IdleCommand;
7879
import frc.robot.status.commands.SetColor;
@@ -333,13 +334,17 @@ public void configureButtonBindings() {
333334
.alongWith(new IdleCommand()));
334335

335336
JoystickButton xBoxX = new JoystickButton(operator, X_BOX_X);
336-
xBoxX.whenHeld(new PresetFlywheelController(mShooter, "TLR")
337-
// .alongWith(new TurnOnLEDs(mLimelight))
338-
.alongWith(new XBoxButtonCommand(X_BOX_X))); // tarmac, lower goal, rear shot
337+
// xBoxX.whenHeld(new PresetFlywheelController(mShooter, "TLR")
338+
// // .alongWith(new TurnOnLEDs(mLimelight))
339+
// .alongWith(new XBoxButtonCommand(X_BOX_X))); // tarmac, lower goal, rear shot
339340

340-
xBoxX.whenReleased(new StopShooter(mShooter)
341-
.alongWith(new TurnOffLEDs(mLimelight))
342-
.alongWith(new IdleCommand()));
341+
// xBoxX.whenReleased(new StopShooter(mShooter)
342+
// .alongWith(new TurnOffLEDs(mLimelight))
343+
// .alongWith(new IdleCommand()));
344+
345+
xBoxX.whenHeld(new VisionShooter(mShooter, mLimelight));
346+
347+
xBoxX.whenReleased(new StopShooter(mShooter));
343348

344349
JoystickButton xBoxY = new JoystickButton(operator, X_BOX_Y);
345350
xBoxY.whenHeld(new PresetFlywheelController(mShooter, "TUR")
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package frc.robot.shooter.commands;
2+
3+
import edu.wpi.first.wpilibj.Notifier;
4+
import edu.wpi.first.wpilibj.Timer;
5+
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
6+
import edu.wpi.first.wpilibj2.command.CommandBase;
7+
import frc.lib.control.DCMotor;
8+
import frc.lib.control.PIDController;
9+
import frc.robot.shooter.Shooter;
10+
import frc.robot.vision.Limelight;
11+
12+
public class VisionShooter extends CommandBase {
13+
14+
Shooter shooter;
15+
Limelight limelight;
16+
double lastPosition, lastTime, hoodAngle;
17+
double rpm;
18+
private Timer timer = new Timer();
19+
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);
22+
boolean closeToTarget = false;
23+
double velocity = 0;
24+
25+
public VisionShooter(Shooter shooter, Limelight limelight) {
26+
this.shooter = shooter;
27+
this.limelight = limelight;
28+
lastPosition = shooter.getEncoderPosition();
29+
lastTime = 0;
30+
timer.reset();
31+
timer.start();
32+
addRequirements(shooter);
33+
}
34+
35+
@Override
36+
public void initialize() {
37+
38+
closeToTarget = false;
39+
velocity = 0;
40+
lastPosition = shooter.getEncoderPosition();
41+
timer.reset();
42+
lastTime = 0;
43+
// timer.start();
44+
controller.startPeriodic(0.02);
45+
}
46+
47+
void controller() {
48+
double time = timer.get();
49+
double position = shooter.getEncoderPosition();
50+
double dt = time - lastTime;
51+
if (dt < 0.0001) {
52+
dt = 0.001;
53+
}
54+
velocity = (position - lastPosition) / (dt) * 60;
55+
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;
59+
60+
shooter.setHoodPosition(hoodAngle);
61+
flywheelController.setReference(rpm);
62+
SmartDashboard.putNumber("Estimated velocity", velocity);
63+
64+
double voltage = flywheel.solveFeedforward(rpm, 0) + flywheelController.calculate(velocity, dt);
65+
// double voltage = flywheel.solveFeedforward(rpm, 0);
66+
67+
shooter.setShooterVoltage(voltage);
68+
69+
lastPosition = position;
70+
lastTime = time;
71+
}
72+
73+
@Override
74+
public void end(boolean interrupted) {
75+
controller.stop();
76+
closeToTarget= false;
77+
velocity = 0;
78+
}
79+
80+
@Override
81+
public boolean isFinished() {
82+
return false;
83+
}
84+
}

src/main/java/frc/robot/vision/Limelight.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import edu.wpi.first.networktables.EntryListenerFlags;
88
import edu.wpi.first.wpilibj.Timer;
9+
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
910

1011
import static edu.wpi.first.networktables.NetworkTableInstance.getDefault;
1112

@@ -14,13 +15,18 @@
1415
public class Limelight extends SubsystemBase {
1516
private volatile VisionState state = new VisionState(0,0,0);
1617

18+
double h = 2.1;
19+
double theta = 45;
20+
1721
public Limelight() {
1822
getDefault().getTable("limelight").getEntry("tx").addListener(event -> {
1923
if (getDefault().getTable("limelight").getEntry("tv").getDouble(0) == 1) {
2024
double xOffset = getDefault().getTable("limelight").getEntry("tx").getDouble(0);
2125
double yOffset = getDefault().getTable("limelight").getEntry("ty").getDouble(0);
26+
double distance = h/(Math.tan(Math.toRadians(yOffset+theta))*Math.cos(Math.toRadians(xOffset)));
27+
SmartDashboard.putNumber("Distance", distance);
2228
double latency = getDefault().getTable("limelight").getEntry("tl").getDouble(0) / 1000.0 + 0.011;
23-
state = new VisionState(xOffset, yOffset, Timer.getFPGATimestamp() - latency);
29+
state = new VisionState(xOffset, distance, Timer.getFPGATimestamp() - latency);
2430
}
2531
}, EntryListenerFlags.kUpdate);
2632
}
@@ -43,12 +49,12 @@ public void turnOnLEDs() {
4349

4450
public static class VisionState {
4551
public final double xOffset;
46-
public final double yOffset;
52+
public final double distance;
4753
public final double timestamp;
4854

49-
public VisionState(double xOffset, double yOffset, double timestamp) {
55+
public VisionState(double xOffset, double distance, double timestamp) {
5056
this.xOffset = xOffset;
51-
this.yOffset = yOffset;
57+
this.distance = distance;
5258
this.timestamp = timestamp;
5359
}
5460
}

0 commit comments

Comments
 (0)