77
88package org .carlmontrobotics .lib199 ;
99
10- import edu .wpi .first .networktables .NetworkTableInstance ;
10+ import java .util .function .Consumer ;
11+
12+ import com .fasterxml .jackson .databind .ObjectMapper ;
13+
1114import edu .wpi .first .math .controller .PIDController ;
15+ import edu .wpi .first .math .geometry .Pose3d ;
16+ import edu .wpi .first .math .geometry .Rotation3d ;
17+ import edu .wpi .first .networktables .NetworkTableEntry ;
18+ import edu .wpi .first .networktables .NetworkTableInstance ;
1219import edu .wpi .first .wpilibj .smartdashboard .SmartDashboard ;
1320
1421public class Limelight {
1522
23+ public static final ObjectMapper JSON_MAPPER = new ObjectMapper ();
24+
1625 public enum Mode {
1726 DIST , STEER , TARGET
1827 }
@@ -70,7 +79,7 @@ public Limelight(String ntName, double period) {
7079 */
7180 public double determineMountingAngle (double distance , double cameraHeight , double objectHeight ) {
7281 // NOTE: ty may be negative.
73- tyDeg = NetworkTableInstance . getDefault (). getTable ( config . ntName ). getEntry ("ty" ).getDouble (0.0 );
82+ tyDeg = getNTEntry ("ty" ).getDouble (0.0 );
7483 mountingAngleDeg = Math .atan ((cameraHeight - objectHeight ) / distance ) * 180 / Math .PI - tyDeg ;
7584 return mountingAngleDeg ;
7685 }
@@ -84,9 +93,9 @@ public double determineMountingAngle(double distance, double cameraHeight, doubl
8493 */
8594 public double [] determineObjectDist (double cameraHeight , double objectHeight , double cameraAngle ) {
8695 // angle b/t where limelight is looking, and target in x direction
87- txDeg = NetworkTableInstance . getDefault (). getTable ( config . ntName ). getEntry ("tx" ).getDouble (0.0 );
96+ txDeg = getNTEntry ("tx" ).getDouble (0.0 );
8897 // angle b/t where limelight is looking, and target in y direction
89- tyDeg = NetworkTableInstance . getDefault (). getTable ( config . ntName ). getEntry ("ty" ).getDouble (0.0 );
98+ tyDeg = getNTEntry ("ty" ).getDouble (0.0 );
9099 // angle b/t limelight's looking line and flat
91100 mountingAngleDeg = cameraAngle ;
92101 // diff b/t height of camera and height of target
@@ -108,8 +117,8 @@ public double[] determineObjectDist(double cameraHeight, double objectHeight, do
108117 // Adjusts the distance between a vision target and the robot. Uses basic PID
109118 // with the ty value from the network table.
110119 public double distanceAssist () {
111- tv = NetworkTableInstance . getDefault (). getTable ( config . ntName ). getEntry ("tv" ).getDouble (0.0 );
112- ta = NetworkTableInstance . getDefault (). getTable ( config . ntName ). getEntry ("ta" ).getDouble (0.0 );
120+ tv = getNTEntry ("tv" ).getDouble (0.0 );
121+ ta = getNTEntry ("ta" ).getDouble (0.0 );
113122 putValue ("tyDeg" , tyDeg );
114123 double adjustment = 0.0 ;
115124 double area_threshold = config .areaThresholdPercentage ;
@@ -125,9 +134,9 @@ public double distanceAssist() {
125134 // Adjusts the angle facing a vision target. Uses basic PID with the tx value
126135 // from the network table.
127136 public double steeringAssist () {
128- tv = NetworkTableInstance . getDefault (). getTable ( config . ntName ). getEntry ("tv" ).getDouble (0.0 );
129- txDeg = NetworkTableInstance . getDefault (). getTable ( config . ntName ). getEntry ("tx" ).getDouble (0.0 );
130- ta = NetworkTableInstance . getDefault (). getTable ( config . ntName ). getEntry ("ta" ).getDouble (0.0 );
137+ tv = getNTEntry ("tv" ).getDouble (0.0 );
138+ txDeg = getNTEntry ("tx" ).getDouble (0.0 );
139+ ta = getNTEntry ("ta" ).getDouble (0.0 );
131140 putValue ("txDeg" , txDeg );
132141 putValue ("tv" , tv );
133142
@@ -182,7 +191,37 @@ public TurnDirection getIdleTurnDirection() {
182191 public void setIdleTurnDirection (TurnDirection direction ) {
183192 idleTurnDirection = direction ;
184193 }
185-
194+
195+ /**
196+ * Get the JSON dump from the limelight. This method returns via a callback because of the high latency observed in JSON parsing.
197+ * Keep in mind that the callback will be called asynchronously.
198+ *
199+ * @param onSuccess The callback to run if the JSON dump is successful
200+ * @param onFailure The callback to run if an error occurs
201+ */
202+ public void getJsonDump (Consumer <LimelightJsonDump > onSuccess , Consumer <Exception > onFailure ) {
203+ new Thread (() -> {
204+ try {
205+ onSuccess .accept (JSON_MAPPER .readValue (JSON_MAPPER .readTree (getNTEntry ("json" ).getString (null )).elements ().next ().toString (), LimelightJsonDump .class ));
206+ } catch (Exception e ) {
207+ onFailure .accept (e );
208+ }
209+ }).start ();
210+ }
211+
212+ public Pose3d getTransform (Transform transform ) {
213+ double [] rawData = getNTEntry (transform .name ().toLowerCase ()).getDoubleArray (new double [6 ]);
214+ return new Pose3d (rawData [0 ], rawData [1 ], rawData [2 ], new Rotation3d (Math .toRadians (rawData [3 ]), Math .toRadians (rawData [4 ]), Math .toRadians (rawData [5 ])));
215+ }
216+
217+ public NetworkTableEntry getNTEntry (String key ) {
218+ return NetworkTableInstance .getDefault ().getTable (config .ntName ).getEntry (key );
219+ }
220+
221+ public boolean hasTarget () {
222+ return getNTEntry ("tv" ).getDouble (0.0 ) == 1.0 ;
223+ }
224+
186225 public static class Config {
187226 /**
188227 * Limelight name (if using more than one Limelight)
@@ -227,4 +266,8 @@ private TurnDirection(int sign) {
227266 this .sign = sign ;
228267 }
229268 }
269+
270+ public static enum Transform {
271+ BOTPOSE , BOTPOSE_WPIBLUE , BOTPOSE_WPIRED , CAMERAPOSE_TARGETSPACE , TARGETPOSE_CAMERASPACE , TARGETPOSE_ROBOTSPACE , BOTPOSE_TARGETSPACE ;
272+ }
230273}
0 commit comments