I have been trying to write a program that gives me the position of an object detected by the Vision Sensor however when using an instruction like
printf("%.2f", Vision10.objects.centerY);
however I get the error
“No member named centerY in 'vex::safearray<vex::vision::object, 16>”
Any help is appreciated.
vision::objects
is an array that contains the largest objects. What you are probably looking for is vision::largestObject
.
printf("%.2f", Vision10.largestObject.centerY);
Another method of doing this is to access the first element of the array
printf("%.2f", Vision10.objects[0].centerY);
Thank you, the code you gave me did not work until I tweaked it to make the output a double as shown below
printf(“%.2f”, (double)Vision10.largestObject.centerY);
and another instruction in the program has shown that the sensor is working as intended however the location text output rarely occurs and when it does, it is random.
Could you share more of the code surrounding the printf
? It’s hard to tell what the exact cause of the issue is.
Some possibilities to consider:
- You probably want to call this in a loop so the value keeps updating
- You might need to flush the stream so the output shows up. This can be done by adding a
\n
after the%.2f
. - Make sure that your brain is connected to your computer through the USB cable. You should be able to do it with a controller, but you will get a better connection with the brain.
Here is some of the code as well as some of the output
Code:
#include “vex.h”
using namespace vex;
event checkRed = event();
event checkBlue = event();
event checkGreen = event();
void hasBlueCallback() {
Brain.Screen.setFont(mono40);
Brain.Screen.clearLine(1, black);
Brain.Screen.setCursor(Brain.Screen.row(), 1);
Brain.Screen.setCursor(1, 1);
Controller1.Screen.clearScreen();
Controller1.Screen.setCursor(1,1);
Vision10.takeSnapshot(Vision10__SIG_1);
if (Vision10.objectCount > 0) {
Brain.Screen.print(“I see you”);
Controller1.Screen.print(“I see you”);
printf(“CenterX:”);
//printf(“%.2f”, (double)Vision10.objects[0].centerX);
printf(“%.2f”, (double)Vision10.largestObject.centerX);
printf(“CenterY”);
//printf(“%.2f”, (double)Vision10.objects[0].centerY);
printf(“%.2f”, (double)Vision10.largestObject.centerY);
//this_thread::sleep_for(10);
} else {
Brain.Screen.print(“No Blue Object”);
Controller1.Screen.print(“No Blue Object”);
}
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
checkBlue(hasBlueCallback);
while (true) {
checkBlue.broadcastAndWait();
wait(1, seconds);
}
}
Output:
CenterX:145.00CenterY132.00CenterX:145.00CenterY133.00CenterX:145.00CenterY133.00CenterX:145.00CenterY132.00CenterX:143.00CenterY134.00CenterX:143.00CenterY135.00CenterX:222.00CenterY112.00CenterX:253.00CenterY140.00CenterX:47.00CenterY123.00CenterX:105.00CenterY127.00CenterX:105.00CenterY128.00CenterX:105.00CenterY129.00CenterX:105.00CenterY129.00CenterX:106.00CenterY127.00CenterX:264.00CenterY131.00CenterX:176.00CenterY99.00CenterX:38.00Cente
rY118.00CenterX:11.00CenterY158.00CenterX:10.00CenterY83.00CenterX:12.00CenterY99.00CenterX:9.00CenterY100.00CenterX:11.00CenterY100.00CenterX:12.00CenterY97.00CenterX:10.00CenterY106.00CenterX:10.00CenterY106.00CenterX:11.00CenterY159.00CenterX:11.00CenterY90.00CenterX:12.00CenterY99.00CenterX:11.00CenterY95.00CenterX:11.00CenterY90.00CenterX:11.00CenterY89.00CenterX:12.00CenterY95.00CenterX:11.00CenterY100.00CenterX:12.00CenterY100.00CenterX:
11.00CenterY91.00CenterX:11.00CenterY97.00CenterX:12.00CenterY92.00CenterX:11.00CenterY88.00CenterX:11.00CenterY88.00CenterX:11.
I cannot check if there is a difference with using wired mode for a few days but I will see if there is a difference when I can, thanks for the support.
(PS: cant get the code to indent properly, sorry if that makes it harder to read)