Skip to content

Commit 66edf39

Browse files
committed
Refactor vision config to use Factors interface
Introduced a Factors interface with ObjFactors and StdDevFactors implementations to generalize camera configuration parameters. Updated Vision, VisionConstants, and object detection logic to use the new factors structure, enabling more flexible and descriptive configuration for both standard deviation and object confidence weights.
1 parent 9c1d37e commit 66edf39

File tree

4 files changed

+65
-16
lines changed

4 files changed

+65
-16
lines changed

src/main/java/org/steelhawks/subsystems/vision/Vision.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ public void periodic() {
200200
angularStdDev *= ANGULAR_STD_DEV_MEGATAG2_FACTOR;
201201
}
202202
if (cameraIndex < VisionConstants.getCameraConfig().length) {
203-
linearStdDev *= getCameraConfig()[cameraIndex].stdDevFactors();
204-
angularStdDev *= getCameraConfig()[cameraIndex].stdDevFactors();
203+
linearStdDev *= getCameraConfig()[cameraIndex].factors().getFactors()[0];
204+
angularStdDev *= getCameraConfig()[cameraIndex].factors().getFactors()[1];
205205
}
206206
if (useQuestNav && !Robot.isFirstRun()) {
207207
assert questNav != null;

src/main/java/org/steelhawks/subsystems/vision/VisionConstants.java

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,60 @@ public class VisionConstants {
2828
6, 7, 8, 9, 10, 11, 18, 19, 20, 21, 22
2929
};
3030

31+
public interface Factors {
32+
default Double[] getFactors() {
33+
return null;
34+
}
35+
36+
class ObjFactors implements Factors {
37+
private final Double[] factors;
38+
39+
/**
40+
* Only used for Limelight, to calculate confidence score.
41+
*
42+
* @param w1 How much the angle matters to confidence
43+
* @param w2 How much the shape matters to confidence
44+
* @param w3 How much the area matters to confidence
45+
*/
46+
public ObjFactors(double w1, double w2, double w3) {
47+
factors = new Double[] { w1, w2, w3 };
48+
}
49+
50+
public ObjFactors() {
51+
this(0.0, 0.0, 0.0);
52+
}
53+
54+
@Override
55+
public Double[] getFactors() {
56+
return factors;
57+
}
58+
}
59+
60+
class StdDevFactors implements Factors {
61+
private final Double[] factors;
62+
63+
public StdDevFactors(double stdDevLinear, double stdDevAngular) {
64+
factors = new Double[] {
65+
stdDevLinear, stdDevAngular
66+
};
67+
}
68+
69+
public StdDevFactors(double stdDevLinear) {
70+
this(stdDevLinear, stdDevLinear);
71+
}
72+
73+
@Override
74+
public Double[] getFactors() {
75+
return factors;
76+
}
77+
}
78+
}
79+
3180
/**
32-
* @param stdDevFactors Standard deviation multipliers for each camera (Adjust to trust some cameras more than others)
81+
* @param factors Standard deviation multipliers for each camera (Adjust to trust some cameras more than others) or for calculating object confidence
3382
*/
3483
public record CameraConfig(
35-
String name, Transform3d robotToCamera, double stdDevFactors, VisionConstants.CameraConfig.CameraType cameraType) {
84+
String name, Transform3d robotToCamera, Factors factors, VisionConstants.CameraConfig.CameraType cameraType) {
3685
public enum CameraType {
3786
LIMELIGHT,
3887
PHOTON,
@@ -55,7 +104,7 @@ public enum CameraType {
55104
Units.degreesToRadians(0.058),
56105
Units.degreesToRadians(-15),
57106
Units.degreesToRadians(-15))),
58-
1.0,
107+
new Factors.StdDevFactors(1.0),
59108
CameraType.PHOTON
60109
),
61110

@@ -75,7 +124,7 @@ public enum CameraType {
75124
Units.degreesToRadians(0),
76125
Units.degreesToRadians(-15),
77126
Units.degreesToRadians(30))),
78-
1.0,
127+
new Factors.StdDevFactors(1.0),
79128
CameraType.PHOTON
80129
),
81130

@@ -93,7 +142,7 @@ public enum CameraType {
93142
Units.degreesToRadians(0),
94143
Units.degreesToRadians(0),
95144
Units.degreesToRadians(-120))),
96-
8.0,
145+
new Factors.StdDevFactors(8.0),
97146
CameraType.PHOTON
98147
),
99148

@@ -111,7 +160,7 @@ public enum CameraType {
111160
Units.degreesToRadians(0),
112161
Units.degreesToRadians(0),
113162
Units.degreesToRadians(90))),
114-
9.0,
163+
new Factors.StdDevFactors(9.0),
115164
CameraType.PHOTON
116165
)
117166
};
@@ -120,7 +169,7 @@ public enum CameraType {
120169
new CameraConfig(
121170
"limelight",
122171
Constants.fromOnshapeCoordinates(4.469, 9.261, 21.578, 15.0, -20.0, 0.0),
123-
0.0,
172+
new Factors.ObjFactors(0.5, 0.3, 0.2),
124173
CameraType.LIMELIGHT
125174
)
126175
};
@@ -129,7 +178,7 @@ public enum CameraType {
129178
new CameraConfig(
130179
"limelight-coral",
131180
new Transform3d(),
132-
3.0,
181+
new Factors.StdDevFactors(3.0),
133182
CameraType.LIMELIGHT
134183
)
135184
};
@@ -138,13 +187,13 @@ public enum CameraType {
138187
new CameraConfig(
139188
"limelight-shooter",
140189
new Transform3d(),
141-
1.2,
190+
new Factors.StdDevFactors(1.2),
142191
CameraType.LIMELIGHT
143192
),
144193
new CameraConfig(
145194
"limelight",
146195
new Transform3d(),
147-
1.3,
196+
new Factors.StdDevFactors(1.3),
148197
CameraType.LIMELIGHT
149198
)
150199
};

src/main/java/org/steelhawks/subsystems/vision/objdetect/ObjectVision.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public static double calcConfidence(ObjectVisionIO.DetectionInfo d, int cameraIn
6767
double txAvg = (d.tx()[0] + d.tx()[1] + d.tx()[2] + d.tx()[3]) / 4.0;
6868
double angleScore = Math.cos(txAvg); // roughly favors smaller offsets
6969

70-
double w1 = 0.5; // how much the area matters to confidence
71-
double w2 = 0.3; // how much the shape matters to confidence
72-
double w3 = 0.2; // how much the angle matters to confidence
70+
double w1 = VisionConstants.getCameraConfig()[cameraIndex].factors().getFactors()[0];
71+
double w2 = VisionConstants.getCameraConfig()[cameraIndex].factors().getFactors()[1];
72+
double w3 = VisionConstants.getCameraConfig()[cameraIndex].factors().getFactors()[2];
7373

7474
return MathUtil.clamp(Constants.loggedValue(logName + "w1", w1 * areaScore) +
7575
Constants.loggedValue(logName + "w2", w2 * ratio) +

src/main/java/org/steelhawks/subsystems/vision/objdetect/ObjectVisionIOLimelight.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void updateInputs(ObjectVisionIOInputs inputs) {
4646
new ObjectObservation(
4747
camIndex,
4848
new DetectionInfo(detection),
49-
0.0, // TODO 0.0 for limelight, photon vision might just give you a confidence value in code but im not sure confirm and fix
49+
0.0, // 0.0 because limelight doesn't give you a confidence score, we calculate it ourselves
5050
timestamp // timestamp in seconds
5151
)
5252
);

0 commit comments

Comments
 (0)