Files
2024AcrossTheRidgebotiverse/src/main/java/frc4388/robot/subsystems/Limelight.java
T

82 lines
2.3 KiB
Java
Raw Normal View History

2024-01-08 11:38:08 -07: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 edu.wpi.first.math.geometry.Pose2d;
2024-02-15 15:38:47 -07:00
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.networktables.NetworkTableInstance;
2024-02-16 12:59:22 -07:00
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.DriverStation.Alliance;
2024-01-08 11:38:08 -07:00
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
2024-02-16 12:59:22 -07:00
import frc4388.robot.Constants.VisionConstants;
2024-02-15 15:38:47 -07:00
// Look at vvv for networktables stuff
// https://docs.limelightvision.io/docs/docs-limelight/apis/complete-networktables-api#apriltag-and-3d-data
2024-02-15 15:38:47 -07:00
public class Limelight extends SubsystemBase {
2024-02-16 12:59:22 -07:00
// [X, Y, Z, Roll, Pitch, Yaw]
2024-02-15 15:38:47 -07:00
private double[] cameraPose;
2024-02-16 12:59:22 -07:00
private boolean isTag;
2024-02-16 12:59:22 -07:00
private Pose2d pose;
private boolean isNearSpeaker;
public boolean getIsTag() {
return isTag;
}
private void update() {
2024-03-11 15:44:32 -06:00
SmartDashboard.putBoolean("Apriltag", isTag);
2024-02-16 12:59:22 -07:00
if(!isTag){
return;
}
2024-02-16 12:59:22 -07:00
double x = cameraPose[0];
double y = cameraPose[1];
double yaw = cameraPose[5];
2024-02-15 15:38:47 -07:00
Rotation2d rot = Rotation2d.fromDegrees(yaw);
2024-02-16 12:59:22 -07:00
pose = new Pose2d(x, y, rot);
boolean isRed = DriverStation.getAlliance().get() == Alliance.Red;
double distance;
if(isRed){
distance = pose.getTranslation().getDistance(VisionConstants.RedSpeakerCenter);
}else{
distance = pose.getTranslation().getDistance(VisionConstants.BlueSpeakerCenter);
}
isNearSpeaker = distance <= VisionConstants.SpeakerBubbleDistance;
2024-02-16 17:04:46 -07:00
2024-03-15 14:39:35 -06:00
//SmartDashboard.putBoolean("nearSpeaker", isNearSpeaker);
//SmartDashboard.putNumber("speakerDistance", distance);
2024-02-16 12:59:22 -07:00
}
public Pose2d getPose() {
return pose;
}
public boolean isNearSpeaker() {
return isNearSpeaker;
}
@Override
2024-02-15 15:38:47 -07:00
public void periodic() {
// This method will be called once per scheduler run
2024-02-16 12:59:22 -07:00
isTag = NetworkTableInstance.getDefault().getTable("limelight").getEntry("tv").getDouble(0.0) == 1.0;
2024-02-16 12:59:22 -07:00
double[] newPose = NetworkTableInstance.getDefault().getTable("limelight").getEntry("botpose").getDoubleArray(new double[6]);
if(newPose != cameraPose){
cameraPose = newPose;
update();
}
2024-02-15 15:38:47 -07:00
}
}