FWIW, here's the code we were running earlier today to extract the vision data for target tracking.
in vision.java (autogenerated subsystem from RobotBuilder):
Code:
// create a new constructor
public Vision() {
// Get a pointer to the networkTable. "StrongholdContours" is the name we entered into the publish box in GRIP
table = NetworkTable.getTable("GRIP/StrongholdContours");
}
and here's the actual function. We had some issues with null sets (if no contours found) and hit some race conditions where the two array reads got different sizes because of an intervening update and some noise. I hacked around those for the moment, but will have to figure something a little better. But, anyway... this did produce the right X values for left-right tracking. All the other reads were pretty much the same. the xRes variable was the pixel resolution in the X direction, which was 320 for us.
Code:
public double getRawTargetXpos() {
double xPos; // return value
// 1) Get the current list of targets found. There might be more than one visible at a time
// first, get the vision system data for the target
double[] defaultValue = new double[0]; // set up a default value in case the table isn't published yet
// Get all needed table items at the same time, in case the table updates between reads.
// (could end up with different array sizes)
double[] targetX = table.getNumberArray("centerX", defaultValue);
double[] areas = table.getNumberArray("area", defaultValue);
if (targetX.length != areas.length) {
// here the table updated in the middle; we'll have to punt
System.out.println("NetworkTable udpated in the middle of getRawTargetXpos; returning first valid entry");
if (targetX.length==0)
{
// we didn't find ANY object. Return a perfectly centered answer so that the system doesn't
// try to adapt
xPos = xRes/2;
}
else xPos = targetX[0];
return xPos;
}
// For initial debug, just print out the table so we can see what's going on
/*
System.out.print("centerX: ");
for (double xval : targetX) { // for each target found,
System.out.print(xval + " ");
}
System.out.println();
*/
// 2) Choose the one that has the largest area. This is PROBABLY the closest target (and most in-line)
// Don't want to choose the one closest to the center because that might actually be the target
// for a different face that's very oblique to our robot position.
int largestIdx = 0;
if (targetX.length > 1) {
double largest = 0;
for (int c = 0; c < areas.length; c++) {
if (areas[c] > largest){
largest = areas[c];
largestIdx = c;
}
}
}
if (targetX.length==0)
{
// we didn't find ANY object. Return a perfectly centered answer so that the system doesn't
// try to adapt
xPos = xRes/2;
}
else xPos = targetX[largestIdx];
return xPos;
}