Skip to content

Commit 78886b0

Browse files
committed
untested vision adjustments
1 parent 2d38ce9 commit 78886b0

File tree

3 files changed

+65
-28
lines changed

3 files changed

+65
-28
lines changed

src/main/java/frc/robot/constants/BiscuitConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
public class BiscuitConstants {
2626

27-
public static final double kZero = .36;
27+
public static final double kZero = .37;
2828
public static final double kTicksPerRot = 160;
2929
public static final int talonID = 25;
3030
public static final double kCloseEnough = 0.05;

src/main/java/frc/robot/constants/VisionConstants.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ public final class VisionConstants {
6161
public static final double FOV75YUYVSingleTagCoeff = 22.0 / 100.0;
6262
public static final double FOV75YUYVBaseTrust = 3.0;
6363

64-
// Gyro error scaling
65-
public static final double kYawErrorThreshold = Units.degreesToRadians(45);
64+
// Gyro error
65+
public static final double kYawErrorThreshold = Units.degreesToRadians(30);
66+
public static final double kCamErrorZThreshold = 0.3;
6667

6768
// Constants for cameras
6869
public static final int kNumCams = 5;
@@ -94,14 +95,14 @@ public final class VisionConstants {
9495
public static final int kCircularBufferSize = 1000;
9596
// Poses
9697
public static final Pose3d kCam1Pose =
97-
new Pose3d(new Translation3d(0.28, 0.02, 0.30), new Rotation3d());
98+
new Pose3d(new Translation3d(0.28, 0.03, 0.30), new Rotation3d());
9899

99100
public static final Pose3d kCam2Pose =
100101
new Pose3d(
101102
new Translation3d(0.236, -0.108, 0.932),
102103
new Rotation3d(0, Units.degreesToRadians(-10.0), Units.degreesToRadians(22.5)));
103104
public static final Pose3d kCam3Pose =
104-
new Pose3d(new Translation3d(0.09, -0.31, 0.36), new Rotation3d());
105+
new Pose3d(new Translation3d(0.12, -0.28, 0.30), new Rotation3d());
105106
public static final Pose3d kCam4Pose =
106107
new Pose3d(
107108
new Translation3d(0.20, 0.035, 0.94),

src/main/java/frc/robot/subsystems/vision/VisionSubsystem.java

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)