2023-03-15 13:50:35 -06:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
|
|
|
|
package frc4388.robot.subsystems;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2023-03-18 14:41:01 -06:00
|
|
|
import java.util.List;
|
2023-03-15 13:50:35 -06:00
|
|
|
|
|
|
|
|
import org.photonvision.PhotonCamera;
|
|
|
|
|
import org.photonvision.common.hardware.VisionLEDMode;
|
|
|
|
|
import org.photonvision.targeting.PhotonPipelineResult;
|
2023-03-15 14:50:30 -06:00
|
|
|
import org.photonvision.targeting.PhotonTrackedTarget;
|
2023-03-18 14:41:01 -06:00
|
|
|
import org.photonvision.targeting.TargetCorner;
|
2023-03-15 13:50:35 -06:00
|
|
|
|
2023-03-15 16:46:06 -06:00
|
|
|
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
2023-03-15 13:50:35 -06:00
|
|
|
import edu.wpi.first.wpilibj2.command.SubsystemBase;
|
|
|
|
|
import frc4388.robot.Constants.VisionConstants;
|
|
|
|
|
|
|
|
|
|
public class Limelight extends SubsystemBase {
|
|
|
|
|
private PhotonCamera cam;
|
2023-03-15 15:25:06 -06:00
|
|
|
|
2023-03-15 16:46:06 -06:00
|
|
|
private boolean lightOn;
|
|
|
|
|
|
2023-03-15 15:25:06 -06:00
|
|
|
/** Creates a new Limelight. */
|
2023-03-15 13:50:35 -06:00
|
|
|
public Limelight() {
|
|
|
|
|
cam = new PhotonCamera(VisionConstants.NAME);
|
2023-03-15 16:46:06 -06:00
|
|
|
cam.setDriverMode(false);
|
2023-03-15 13:50:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setLEDs(boolean on) {
|
2023-03-15 16:46:06 -06:00
|
|
|
lightOn = on;
|
|
|
|
|
cam.setLED(lightOn ? VisionLEDMode.kOn : VisionLEDMode.kOff);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void toggleLEDs() {
|
|
|
|
|
lightOn = !lightOn;
|
|
|
|
|
cam.setLED(lightOn ? VisionLEDMode.kOn : VisionLEDMode.kOff);
|
2023-03-15 13:50:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setDriverMode(boolean driverMode) {
|
|
|
|
|
cam.setDriverMode(driverMode);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 16:25:26 -06:00
|
|
|
public void setToLimePipeline() {
|
|
|
|
|
cam.setPipelineIndex(1);
|
2023-03-17 15:14:53 -06:00
|
|
|
setLEDs(true);
|
2023-03-16 16:25:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setToAprilPipeline() {
|
|
|
|
|
cam.setPipelineIndex(0);
|
2023-03-17 15:14:53 -06:00
|
|
|
setLEDs(false);
|
2023-03-16 16:25:26 -06:00
|
|
|
}
|
|
|
|
|
|
2023-03-16 23:08:08 -06:00
|
|
|
public PhotonTrackedTarget getAprilPoint() {
|
2023-03-15 16:46:06 -06:00
|
|
|
if (!cam.isConnected()) return null;
|
|
|
|
|
|
2023-03-15 13:50:35 -06:00
|
|
|
PhotonPipelineResult result = cam.getLatestResult();
|
|
|
|
|
|
2023-03-16 16:25:26 -06:00
|
|
|
if (!result.hasTargets()) return null;
|
2023-03-15 13:50:35 -06:00
|
|
|
|
2023-03-16 23:08:08 -06:00
|
|
|
return result.getBestTarget();
|
2023-03-15 14:50:30 -06:00
|
|
|
}
|
|
|
|
|
|
2023-03-18 14:41:01 -06:00
|
|
|
private List<TargetCorner> getAprilCorners() {
|
|
|
|
|
if (!cam.isConnected()) return null;
|
|
|
|
|
|
|
|
|
|
PhotonPipelineResult result = cam.getLatestResult();
|
|
|
|
|
|
|
|
|
|
if (!result.hasTargets()) return null;
|
|
|
|
|
|
|
|
|
|
return result.getBestTarget().getDetectedCorners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getAprilSkew() {
|
|
|
|
|
List<TargetCorner> corners = getAprilCorners();
|
|
|
|
|
ArrayList<TargetCorner> bottomSide = getAprilBottomSide(corners);
|
|
|
|
|
|
|
|
|
|
if (bottomSide == null) return 0;
|
|
|
|
|
|
|
|
|
|
TargetCorner bottomRight = bottomSide.get(0).x > bottomSide.get(1).x ? bottomSide.get(0) : bottomSide.get(1);
|
|
|
|
|
TargetCorner bottomLeft = bottomRight.x == bottomSide.get(0).x ? bottomSide.get(1) : bottomSide.get(0);
|
|
|
|
|
|
|
|
|
|
return bottomLeft.y - bottomRight.y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ArrayList<TargetCorner> getAprilBottomSide(List<TargetCorner> box) {
|
|
|
|
|
if (box == null) return null;
|
|
|
|
|
|
|
|
|
|
ArrayList<TargetCorner> bottomSide = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
TargetCorner l1 = new TargetCorner(-1, -1);
|
|
|
|
|
TargetCorner l2 = new TargetCorner(-1, -1);
|
|
|
|
|
|
|
|
|
|
for (TargetCorner c : box) {
|
|
|
|
|
if (c.y > l1.y) l1 = c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (TargetCorner c : box) {
|
|
|
|
|
if (c.y == l1.y) continue;
|
|
|
|
|
if (c.y > l2.y) l2 = c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bottomSide.add(l1);
|
|
|
|
|
bottomSide.add(l2);
|
|
|
|
|
|
|
|
|
|
return bottomSide;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-17 15:14:53 -06:00
|
|
|
public double getDistanceToApril() {
|
2023-03-16 23:08:08 -06:00
|
|
|
PhotonTrackedTarget aprilPoint = getAprilPoint();
|
|
|
|
|
if (aprilPoint == null) return -1;
|
2023-03-16 16:25:26 -06:00
|
|
|
|
2023-03-16 23:08:08 -06:00
|
|
|
double aprilHeight = VisionConstants.APRIL_HEIGHT - VisionConstants.LIME_HEIGHT;
|
|
|
|
|
double theta = 35.0 + aprilPoint.getPitch();
|
2023-03-16 16:25:26 -06:00
|
|
|
|
2023-03-16 23:08:08 -06:00
|
|
|
double distanceToApril = aprilHeight / Math.tan(Math.toRadians(theta));
|
|
|
|
|
return distanceToApril;
|
2023-03-16 16:25:26 -06:00
|
|
|
}
|
|
|
|
|
|
2023-03-16 23:08:08 -06:00
|
|
|
public PhotonTrackedTarget getLowestTape() {
|
2023-03-16 19:10:26 -06:00
|
|
|
if (!cam.isConnected()) return null;
|
|
|
|
|
|
|
|
|
|
PhotonPipelineResult result = cam.getLatestResult();
|
|
|
|
|
|
|
|
|
|
if (!result.hasTargets()) return null;
|
|
|
|
|
|
|
|
|
|
ArrayList<PhotonTrackedTarget> points = (ArrayList<PhotonTrackedTarget>) result.getTargets();
|
|
|
|
|
|
|
|
|
|
PhotonTrackedTarget lowest = points.get(0);
|
|
|
|
|
for (PhotonTrackedTarget point : points) {
|
|
|
|
|
if (point.getPitch() < lowest.getPitch()) {
|
|
|
|
|
lowest = point;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lowest;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-17 15:14:53 -06:00
|
|
|
public double getDistanceToTape() {
|
2023-03-16 23:08:08 -06:00
|
|
|
PhotonTrackedTarget tapePoint = getLowestTape();
|
|
|
|
|
if (tapePoint == null) return -1;
|
2023-03-15 14:50:30 -06:00
|
|
|
|
2023-03-16 23:08:08 -06:00
|
|
|
double tapeHeight = VisionConstants.MID_TAPE_HEIGHT - VisionConstants.LIME_HEIGHT;
|
2023-03-16 16:25:26 -06:00
|
|
|
double theta = 35.0 + tapePoint.getPitch();
|
2023-03-15 14:50:30 -06:00
|
|
|
|
2023-03-16 23:08:08 -06:00
|
|
|
double distanceToTape = tapeHeight / Math.tan(Math.toRadians(theta));
|
|
|
|
|
return distanceToTape;
|
2023-03-15 13:50:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void periodic() {
|
2023-03-18 14:41:01 -06:00
|
|
|
SmartDashboard.putNumber("April Skew", getAprilSkew());
|
2023-03-15 13:50:35 -06:00
|
|
|
}
|
|
|
|
|
}
|