Hello! So I’m working on this side project, which involves using the vision sensor and allowing the user to lock onto a specific object it sees and pathfind to it, and I have the pathfinding down but it’s only for the largest object. To get started I looked in the Vex API and found there’s an array of every object the vision sensor sees, “Dang that’s perfect!” I said and started trying to print data from it using this code block:
while (true) {
Brain.Screen.clearScreen();
Brain.Screen.setCursor(0,0);
Vision.takeSnapshot(Vision__GREEN_TB);
for (int i = 0; i <= Vision.objectCount; i++) {
Brain.Screen.print("Object %d: ", Vision.objects[i].id);
Brain.Screen.print(“X: %.f, Y: %.f, Width: %.f, Height: %.f”,
Vision.objects[i].centerX,
Vision.objects[i].centerY,
Vision.objects[i].width,
Vision.objects[i].height);
Brain.Screen.newLine();
}
wait(80, sec);
}
But when I place two objects in front of the vision sensor that it can detect (I used the devices page to visually confirm its detecting them), It prints out 3 lines on the brain all with 0 for every single data value except width which fluctuates between 0 and 3.
Am I using this right? I’m not very fluent in arrays but I’m pretty sure that’s how they work, I’ve tried tinkering with the vision sensors object class but nothing seems to expressly connect together.
I appreciate any help!
I have done this before and I can pull up some sample code I used last year for finding the closest object if you want it. The first issue I see it the wait statement is almost a minute and a half. That should probably be about 20msec. Not sure how fast the vision sensor detects stuff. Were you checking the built-in configurations or your custom vision configuration?
Anything helps! My pathfinding program runs off a priority system based on the objects position on the screen, I was hoping if I could pick apart the array in a way that I could select an object on the controller it would pathfind to that instead of the largest object. If it helps with an understanding here’s my current pathfinding code:
So yeah, ignore that, the brain was being a little finicky with printing the text out and so I resorted to some dramatic solutions lmao, it should be like 50 - 80 msec that way I can read the text on the screen before it disappears.
Judging by the amount of inactivity in this thread, this question might be too ambiguous or people might not really know the answer to it. So to poke this thread and give some more information here’s the link to the “array” in the Vex API page: VexAPI which relates to what I’m trying to use.
If no progress is made in like a week then I’ll just abandon it until some epic person unearths this thread and replies with some magnificent answer that shatters my understanding of vexcode (I dunno man i’m desperate )
I read your code earlier and I didn’t see any problems, but I read it again and I think I see what might be wrong. For one, when you loop through the array you are using a <= to check if you reached the end, but you should be using <. This is because arrays start counting at 0, so an array with length 4 will have things in it at 0, 1, 2, and 3, and won’t have anything at 4. Secondly, you’re using %.f in the print to tell it where to put the numbers, but this has a period in it which is messing it up. Instead you should use %f (no period), or %i or %d because the values on vision sensor objects are be integers.
I appreciate your feedback! So I took you’re advice and changed everything to this block of code:
while (true) {
Brain.Screen.clearScreen();
Brain.Screen.setCursor(0,0);
Vision.takeSnapshot(Vision__GREEN_TB);
for (int i = -1; i < Vision.objectCount; i++) {
Brain.Screen.print("Object %d: ", Vision.objects[i].id);
Brain.Screen.print("X: %d, Y: %d, Width: %d, Height: %d",
Vision.objects[i].centerX,
Vision.objects[i].centerY,
Vision.objects[i].width,
Vision.objects[i].height);
Brain.Screen.newLine();
}
wait(80, msec);
}
So I made the integer for the for statement -1 because when it was 0 it would only print out the objects data if there was a second object in front of the vision sensor, with that adjustment it now prints out both objects and there data accordingly. I wish the objects ID wasn’t just three constantly between them all, that way the code would be easier for my project.
Not a good idea, arrays start at index 0. It just happens that the vision object array is a special class called a safearray that filters out bad indexes and returns a dummy object.
perhaps have a look at this old code, it iterates through vision objects and displays them.
(it was written for VCS but APIs are the same)