-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLimelight.java
95 lines (72 loc) · 3.01 KB
/
Limelight.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package frc.robot.subsystems;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableEntry;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Constants;
public class Limelight extends SubsystemBase {
NetworkTable networkTable;
private NetworkTableEntry targetEntry;
private NetworkTableEntry horizontalAngleOffSet;
private NetworkTableEntry verticalAngleOffSet;
private NetworkTableEntry ta;
private NetworkTableEntry ledMode;
private boolean target;
private double x;
private double y;
private double area;
public Limelight() {
networkTable = NetworkTableInstance.getDefault().getTable("Limelight");
targetEntry = networkTable.getEntry("tv");// Whether the limelight has any valid targets (0 or 1)
horizontalAngleOffSet = networkTable.getEntry("tx"); // Horizontal Offset From Crosshair To Target (-27 degrees
// to 27 degrees)
verticalAngleOffSet = networkTable.getEntry("ty"); // Vertical Offset From Crosshair To Target (-20.5 degrees to
// 20.5 degrees)
ta = networkTable.getEntry("ta"); // Target Area (0% of image to 100% of image
ledMode = networkTable.getEntry("ledMode");
}
/**
* @return a horizontal angle from -27 to 27 between the target and the camera
*/
public double getHorizontalAngle() {
return horizontalAngleOffSet.getDouble(0);
}
/** @return a vertical angle from -27 to 27 between the target and the camera */
public double getVerticalAngle() {
return verticalAngleOffSet.getDouble(0);
}
/** @return If the limelight is connected */
public boolean connected() {
return networkTable.getEntry("tx").exists();
}
/** @return If the limelight has found a target */
public boolean canSeeTarget() {
return targetEntry.getNumber(0).intValue() > 0;
}
/** @return If the limelight is on target */
public boolean onTarget() {
return canSeeTarget() && getHorizontalAngle() < Constants.APPROXIMATE_ANGLE;
}
// returns the size of the target
public double getTargetArea() {
return ta.getNumber(0).doubleValue();
}
public double calculateDistance() {
return (Constants.PORT_HEIGHT - Constants.HEIGHT_ABOVE_GROUND)
/ Math.tan(Math.toRadians(y) + Math.toRadians(Constants.MOUNTED_ANGLE));
}
public void turnOn() {
ledMode.setDouble(3);
}
public void turnOff() {
ledMode.setDouble(1);
}
public void periodic() {
target = targetEntry.getBoolean(false);
x = horizontalAngleOffSet.getDouble(0.0);
y = verticalAngleOffSet.getDouble(0.0);
area = ta.getDouble(0.0);
SmartDashboard.putNumber("Horizontal Error", x);
}
}