@@ -18,6 +18,7 @@ import edu.wpi.first.math.estimator.SwerveDrivePoseEstimator
1818import edu.wpi.first.math.geometry.Pose2d
1919import edu.wpi.first.math.geometry.Rotation2d
2020import edu.wpi.first.math.geometry.Transform3d
21+ import edu.wpi.first.math.geometry.Translation3d
2122import edu.wpi.first.math.numbers.N1
2223import edu.wpi.first.math.numbers.N3
2324import edu.wpi.first.networktables.NetworkTableInstance
@@ -104,6 +105,7 @@ class LimelightPoseProvider(
104105 private var hbSubscriber = table.getDoubleTopic(" hb" ).subscribe(0.0 )
105106 private var txSubscriber = table.getDoubleTopic(" tx" ).subscribe(0.0 )
106107 private var tySubscriber = table.getDoubleTopic(" ty" ).subscribe(0.0 )
108+ private var tvSubscriber = table.getIntegerTopic(" tv" ).subscribe(0 )
107109 private var megatag1Subscriber = table.getDoubleArrayTopic(" botpose_wpiblue" ).subscribe(doubleArrayOf())
108110 private var megatag2Subscriber =
109111 table.getDoubleArrayTopic(" botpose_orb_wpiblue" ).subscribe(doubleArrayOf())
@@ -220,6 +222,30 @@ class LimelightPoseProvider(
220222 measurements + = measurement
221223 }
222224
225+ if (Robot .isDisabled) { // we don't care about cropping when disabled, seed pose estimator
226+ LimelightHelpers .setPipelineIndex(name, CLOSE_PIPELINE )
227+ LimelightHelpers .setCropWindow(name, - 1.0 , 1.0 , - 1.0 , 1.0 )
228+ }
229+ if (tvSubscriber.get().toInt() == 0 ) {
230+ // No target, enable search mode
231+ LimelightHelpers .setPipelineIndex(name, SEARCH_PIPELINE )
232+ LimelightHelpers .setCropWindow(name, - 1.0 , 1.0 , - 1.0 , 1.0 )
233+ } else {
234+ // Target found, set correct pipeline and search window
235+ val pose = LimelightHelpers .getTargetPose_RobotSpace(name)
236+ val distance = Translation3d (
237+ pose[0 ], // x (forward/backward)
238+ pose[1 ], // y (left/right)
239+ pose[2 ] // z (up/down)
240+ ).norm.meters
241+
242+ if (distance > DISABLE_DOWNSCALE_DISTANCE ) {
243+ LimelightHelpers .setPipelineIndex(name, FAR_PIPELINE )
244+ } else {
245+ LimelightHelpers .setPipelineIndex(name, CLOSE_PIPELINE )
246+ }
247+ }
248+
223249
224250 return measurements
225251 }
@@ -256,6 +282,8 @@ class LimelightPoseProvider(
256282 */
257283 private val MAX_SINGLE_TAG_DISTANCE = 3 .meters
258284
285+ private val DISABLE_DOWNSCALE_DISTANCE = 1.0 .meters
286+
259287 /* *
260288 * The acceptable ambiguity for a single-tag reading on MegaTag v1.
261289 */
@@ -265,6 +293,13 @@ class LimelightPoseProvider(
265293 * The amount of time (in robot ticks) an update before considering the camera to be disconnected.
266294 */
267295 private const val CONNECTED_TIMEOUT = 250.0
296+
297+ // 2d tracking, search for targets
298+ private const val SEARCH_PIPELINE = 0
299+ // 3d, downscaled
300+ private const val CLOSE_PIPELINE = 1
301+ // 3d, no downscale
302+ private const val FAR_PIPELINE = 2
268303 }
269304}
270305
0 commit comments