Files
2023WayOfTheRobot/src/main/java/frc4388/robot/subsystems/Limelight.java
T

115 lines
3.5 KiB
Java
Raw Normal View History

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;
import java.util.List;
import org.opencv.core.Point;
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-15 13:50:35 -06:00
import org.photonvision.targeting.TargetCorner;
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;
import frc4388.utility.AbhiIsADumbass;
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;
private boolean isConnected = false;
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
isConnected = cam.isConnected();
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-15 14:50:30 -06:00
public ArrayList<Point> getTargetPoints() throws AbhiIsADumbass {
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();
if (!result.hasTargets()) throw new AbhiIsADumbass();
2023-03-15 14:50:30 -06:00
ArrayList<Point> points = new ArrayList<>(2);
2023-03-15 13:50:35 -06:00
2023-03-15 14:50:30 -06:00
for(PhotonTrackedTarget target : result.getTargets()){
List<TargetCorner> corners = target.getDetectedCorners();
double sumX = 0.0;
double sumY = 0.0;
double mx = 0.0;
double my = 0.0;
for (TargetCorner c : corners) {
sumX += c.x;
sumY += c.y;
}
mx = sumX / 4.0;
my = sumY / 4.0;
2023-03-15 13:50:35 -06:00
2023-03-15 14:50:30 -06:00
points.add(new Point(mx, my));
}
return points;
}
private double getPointAngle(Point point) {
return (VisionConstants.LIME_VIXELS - point.y) * (VisionConstants.V_FOV / VisionConstants.LIME_VIXELS);
}
2023-03-15 14:55:18 -06:00
public double getHorizontalDistanceToTarget(boolean high) throws AbhiIsADumbass {
2023-03-15 14:50:30 -06:00
ArrayList<Point> targetPoints = getTargetPoints();
2023-03-15 16:46:06 -06:00
if (targetPoints == null) return -1;
2023-03-15 14:50:30 -06:00
2023-03-15 16:46:06 -06:00
// Point highPoint = targetPoints.get(0).y <= targetPoints.get(1).y ? targetPoints.get(0) : targetPoints.get(1);
// Point midPoint = targetPoints.get(0).y >= targetPoints.get(1).y ? targetPoints.get(0) : targetPoints.get(1);
2023-03-15 14:50:30 -06:00
2023-03-15 16:46:06 -06:00
Point tapePoint = targetPoints.get(0);//high ? highPoint : midPoint;
double tapeHeight = VisionConstants.MID_TAPE_HEIGHT;//high ? VisionConstants.HIGH_TAPE_HEIGHT : VisionConstants.MID_TAPE_HEIGHT;
2023-03-15 14:50:30 -06:00
double theta = VisionConstants.LIME_ANGLE + getPointAngle(tapePoint);
double effectiveTapeHeight = tapeHeight - VisionConstants.LIME_HEIGHT;
double distanceToTape = effectiveTapeHeight / Math.sin(Math.toRadians(theta));
2023-03-15 14:55:18 -06:00
double horizontalDistanceToTarget = Math.sqrt(Math.pow(distanceToTape, 2) - Math.pow(effectiveTapeHeight, 2));
2023-03-15 14:50:30 -06:00
2023-03-15 14:55:18 -06:00
return horizontalDistanceToTarget;
2023-03-15 13:50:35 -06:00
}
@Override
public void periodic() {
// This method will be called once per scheduler run
2023-03-15 16:46:06 -06:00
try {
System.out.println(getHorizontalDistanceToTarget(false));
} catch(AbhiIsADumbass abhiIsADumbass) {
abhiIsADumbass.printStackTrace();
}
2023-03-15 13:50:35 -06:00
}
}