@@ -43,6 +43,14 @@ public class VisionSubsystem extends MeasurableSubsystem {
4343 VisionConstants .kCam5Pose .getTranslation ()
4444 };
4545
46+ private double [] camHeights = {
47+ camPositions [0 ].getZ (),
48+ camPositions [1 ].getZ (),
49+ camPositions [2 ].getZ (),
50+ camPositions [3 ].getZ (),
51+ camPositions [4 ].getZ ()
52+ };
53+
4654 // Array of camera rotations
4755 private Rotation3d [] camRotations = {
4856 VisionConstants .kCam1Pose .getRotation (),
@@ -292,42 +300,65 @@ private double getStdDevFactor(double distance, int numTags, String camName) {
292300 private Pose3d getCloserPose (Pose3d pose1 , Pose3d pose2 , double rotation ) {
293301 /*Which pose rotation is closer to our gyro. We subtract the absolute value of the gyro
294302 from the rotation of the pose and compare the two*/
295- double pose1Error =
296- FastMath .abs (
297- Rotation2d .fromRadians (rotation )
298- .minus (pose1 .getRotation ().toRotation2d ())
299- .getRadians ());
300- double pose2Error =
301- FastMath .abs (
302- Rotation2d .fromRadians (rotation )
303- .minus (pose2 .getRotation ().toRotation2d ())
304- .getRadians ());
305303
304+ double pose1Error = 2767 ;
305+ double pose2Error = 2767 ;
306+ if (pose1 != null ) {
307+ pose1Error =
308+ FastMath .abs (
309+ Rotation2d .fromRadians (rotation )
310+ .minus (pose1 .getRotation ().toRotation2d ())
311+ .getRadians ());
312+ }
313+ if (pose2 != null ) {
314+ pose2Error =
315+ FastMath .abs (
316+ Rotation2d .fromRadians (rotation )
317+ .minus (pose2 .getRotation ().toRotation2d ())
318+ .getRadians ());
319+ }
306320 Logger .recordOutput ("Vision/Pose 1 Yaw Error" , pose1Error );
307321 Logger .recordOutput ("Vision/Pose 2 Yaw Error" , pose2Error );
308322 Logger .recordOutput ("Vision/Historical yaw" , rotation );
309323
310- if (pose1Error < pose2Error ) {
311- if (pose1Error > VisionConstants .kYawErrorThreshold ) {}
324+ if (pose1Error > VisionConstants .kYawErrorThreshold ) {
325+ pose1 = null ;
326+ }
327+ if (pose2Error > VisionConstants .kYawErrorThreshold ) {
328+ pose2 = null ;
329+ }
330+
331+ if (pose1 == null && pose2 == null ) {
332+ return null ;
333+ }
312334
335+ if (pose1Error < pose2Error ) {
313336 return pose1 ;
314337 } else {
315338 return pose2 ;
316339 }
317340 }
318341
319342 private Pose3d getCorrectPose (Pose3d pose1 , Pose3d pose2 , double time , int camIndex ) {
320- // double dist1 = Math.abs(camHeights[camIndex] - pose1.getZ());
321- // double dist2 = Math.abs(camHeights[camIndex] - pose2.getZ());
343+ double dist1 = Math .abs (camHeights [camIndex ] - pose1 .getZ ());
344+ double dist2 = Math .abs (camHeights [camIndex ] - pose2 .getZ ());
345+
322346 // // This filters out results by seeing if they are close to the right height
323- // if (dist1 < dist2 && dist1 < 0.5) {
324- // return pose1;
325- // }
326- // if (dist2 < dist1 && dist2 < 0.5) {
327- // return pose2;
328- // }
347+ if (dist1 > VisionConstants .kCamErrorZThreshold ) {
348+ pose1 = null ;
349+ }
350+ if (dist2 > VisionConstants .kCamErrorZThreshold ) {
351+ pose2 = null ;
352+ }
353+
329354 // If we don't have enough data in the gyro buffer we default to returning a pose
330- if (gyroBuffer .size () < VisionConstants .kCircularBufferSize ) return pose1 ;
355+ if (gyroBuffer .size () < VisionConstants .kCircularBufferSize ) {
356+ if (pose1 != null ) {
357+ return pose1 ;
358+ } else {
359+ return pose2 ;
360+ }
361+ }
331362
332363 // See what pose is closer the the gyro at the time of the photo's capture.
333364 double rotation =
@@ -437,12 +468,17 @@ public void periodic() {
437468 Logger .recordOutput ("Vision/Raw Camera 2 " + camNames [idx ], cam2Pose );
438469
439470 cameraPose = getCorrectPose (cam1Pose , cam2Pose , result .getTimeStamp (), idx );
471+
472+ if (cameraPose == null ) {
473+ continue ;
474+ }
475+
476+ cameraRotation = cameraPose .getRotation ();
440477 robotTranslation =
441478 cameraPose
442479 .getTranslation ()
443- .minus (camPositions [idx ].rotateBy (cameraPose . getRotation () ))
480+ .minus (camPositions [idx ].rotateBy (cameraRotation ))
444481 .rotateBy (camRotations [idx ]);
445- cameraRotation = cameraPose .getRotation ();
446482 }
447483 Pose2d robotPose =
448484 new Pose2d (robotTranslation .toTranslation2d (), cameraRotation .toRotation2d ());
0 commit comments