Skip to content

Commit dd2af02

Browse files
committed
Read frame count in same array as camera data
1 parent b0c6a03 commit dd2af02

File tree

1 file changed

+34
-39
lines changed

1 file changed

+34
-39
lines changed

src/main/java/org/mayheminc/robot2020/subsystems/Targeting.java

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class Targeting extends SubsystemBase {
5151
private double[] m_target_array;
5252
private int m_priorFrameCount;
5353
private double m_priorFrameTime;
54-
private double[] ARRAY_OF_NEG_ONE = { -1.0 };
54+
private final double[] ARRAY_OF_NEG_ONE = { -1.0 };
5555

5656
private double m_bestY = 0.0;
5757
private double m_bestX = 0.0;
@@ -65,65 +65,60 @@ public void periodic() {
6565

6666
// TODO: make an updateSmartDashboard() method in Targeting for optimization
6767
public void update() {
68-
// perform periodic update functions for the targeting capability
69-
int latestFrameCount = (int) SmartDashboard.getNumber("frameCount", -1.0 /* default to -1 */);
70-
if (latestFrameCount < 0) {
71-
// an invalid number for latestFrameCount, display warning light
72-
SmartDashboard.putBoolean("visionOK", false);
73-
SmartDashboard.putString("visionOkDebug", "No Data");
74-
} else if (latestFrameCount == m_priorFrameCount) {
75-
// have not received a new frame. If more than 1 second has elapsed since
76-
// prior new frame, display a warning light on the SmartDashboard
77-
if (Timer.getFPGATimestamp() > m_priorFrameTime + 1.0) {
78-
SmartDashboard.putBoolean("visionOK", false);
79-
SmartDashboard.putString("visionOkDebug", "Stale Data");
80-
} else {
81-
// else, have an old frame, but it's not too old yet, so do nothing
82-
}
83-
} else {
84-
// have received a new frame, save the time and update m_priorFrameCount
85-
m_priorFrameTime = Timer.getFPGATimestamp();
86-
m_priorFrameCount = latestFrameCount;
87-
SmartDashboard.putBoolean("visionOK", true);
88-
SmartDashboard.putString("visionOkDebug", "Good Data");
89-
}
68+
// get the latest output from the targeting camera
69+
m_target_array = SmartDashboard.getNumberArray("target", ARRAY_OF_NEG_ONE);
9070

9171
// Update all of the targeting information, as follows:
9272
// 1 - Determine if we have any valid data in the array.
9373
// If not, set the "error" to zero, so that the robot thinks
9474
// it is on target.
9575
// 2 - Use the target to compute the needed information
9676

97-
// get the latest output from the targeting camera
98-
m_target_array = SmartDashboard.getNumberArray("target", ARRAY_OF_NEG_ONE);
99-
10077
if (m_target_array == null || m_target_array.length == 0) {
101-
// this means the key is found, but is empty
78+
// This means that the key was set, but to an empty array
79+
SmartDashboard.putBoolean("visionOK", false);
80+
SmartDashboard.putString("visionOkDebug", "Bad Data");
10281
m_bestX = 0.0;
10382
m_bestY = 0.0;
10483
m_tilt = 0.0;
10584
m_area = 0.0;
10685
// m_desiredAzimuth = RobotContainer.turret.getAzimuthForCapturedImage();
10786
} else if (m_target_array[0] < 0.0) {
108-
// this means the array has no valid data. Set m_xError = 0.0
87+
// This means the array was not retrieved (and the default value was returned)
88+
// Display warning light
89+
SmartDashboard.putBoolean("visionOK", false);
90+
SmartDashboard.putString("visionOkDebug", "No Data");
10991
m_bestX = 0.0;
11092
m_bestY = 0.0;
11193
m_tilt = 0.0;
11294
m_area = 0.0;
11395
// m_desiredAzimuth = RobotContainer.turret.getAzimuthForCapturedImage();
11496
} else {
11597
// We have a valid data array.
116-
// There are three different situations:
117-
// a - We want the left-most target
118-
// b - We want the "centered" target
119-
// c - We want the right-most target
120-
121-
// Handle each of them separately;
122-
// we need the results in "bestXError" and "bestY"
123-
m_bestX = m_target_array[0]; // get the x-value
124-
m_bestY = m_target_array[1]; // get the y-value
125-
m_tilt = m_target_array[2];
126-
m_area = m_target_array[3];
98+
final int latestFrameCount = (int) m_target_array[0];
99+
100+
// perform periodic update functions for the targeting capability
101+
if (latestFrameCount == m_priorFrameCount) {
102+
// have not received a new frame. If more than 1 second has elapsed since
103+
// prior new frame, display a warning light on the SmartDashboard
104+
if (Timer.getFPGATimestamp() > m_priorFrameTime + 1.0) {
105+
SmartDashboard.putBoolean("visionOK", false);
106+
SmartDashboard.putString("visionOkDebug", "Stale Data");
107+
} else {
108+
// else, have an old frame, but it's not too old yet, so do nothing
109+
}
110+
} else {
111+
// have received a new frame, save the time and update m_priorFrameCount
112+
m_priorFrameTime = Timer.getFPGATimestamp();
113+
m_priorFrameCount = latestFrameCount;
114+
SmartDashboard.putBoolean("visionOK", true);
115+
SmartDashboard.putString("visionOkDebug", "Good Data");
116+
}
117+
118+
m_bestX = m_target_array[1]; // get the x-value
119+
m_bestY = m_target_array[2]; // get the y-value
120+
m_tilt = m_target_array[3];
121+
m_area = m_target_array[4];
127122

128123
m_desiredAzimuth = findDesiredAzimuth(m_bestX, m_bestY, m_tilt, m_area);
129124
m_desiredHood = getHoodFromY();

0 commit comments

Comments
 (0)