VCS Vision Sensor Help

#1

So I’ve been trying to write some code for vision and it hasn’t exactly worked. I use VCS and write in Vex C++, looking for some help.

Here is my code so far, wrote in a regular file for testing.
void visionS() {
double screenMiddle = (Vision1.largestObject.centerX-160);
double speed = (screenMiddle * 0.2431);
Left.spin(directionType::fwd,speed,velocityUnits::rpm);
Right.spin(directionType::rev,speed,velocityUnits::rpm);
Vision1.takeSnapshot(REDF);

}
int main() {
while (1==1){
Vision1.setLedColor(255,0,255);
Vision1.takeSnapshot(REDF);
if (Vision1.largestObject.exists==0){
Left.spin(directionType::fwd,10,velocityUnits::rpm);
Right.spin(directionType::rev,10,velocityUnits::rpm);
Vision1.takeSnapshot(REDF);
Vision1.setLedColor(255,0,0);
}
else if (Vision1.largestObject.exists==1){
Vision1.setLedColor(0,255,0);
visionS();
Vision1.takeSnapshot(REDF);
}
else {
Vision1.takeSnapshot(REDF);
}
}
}

0 Likes

#2

For future reference, this is how you should post code on the forum:
Tutorial

1 Like

#3

Does anyone know what is wrong with this code?

int main() { 
    while (true) {
    if (VisionSensor.largestObject.exists && VisionSensor.largestObject.width>5) {      
        Brain.Screen.setPenColor(vex::color::white);
        Brain.Screen.setFillColor(vex::color::green);
        Brain.Screen.drawRectangle(0,0, 480, 240);
        Brain.Screen.setCursor(2,6);
        Brain.Screen.setFont(vex::fontType::mono40);
        Brain.Screen.print("Forward");
        LeftMotor.spin(vex::directionType::fwd);
        RightMotor.spin(vex::directionType::fwd);
    }
    	else  {
            VisionSensor.takeSnapshot(2);
    if (VisionSensor.largestObject.exists && VisionSensor.largestObject.width>5) {
        Brain.Screen.setPenColor(vex::color::black);
        Brain.Screen.setFillColor(vex::color::yellow);
        Brain.Screen.drawRectangle(0,0, 480, 240);
        Brain.Screen.setCursor(2,6);
        Brain.Screen.setFont(vex::fontType::mono40);
        Brain.Screen.print("Right");
        LeftMotor.spin(vex::directionType::fwd);
        RightMotor.spin(vex::directionType::rev);
    }
    	else {
            VisionSensor.takeSnapshot(3);
    if (VisionSensor.largestObject.exists && VisionSensor.largestObject.width>5) {
        Brain.Screen.setPenColor(vex::color::white);
        Brain.Screen.setFillColor(vex::color::red);
        Brain.Screen.drawRectangle(0,0, 480, 240);
        Brain.Screen.setCursor(2,6);
        Brain.Screen.setFont(vex::fontType::mono40);
        Brain.Screen.print("Left");
        LeftMotor.spin(vex::directionType::rev);
        RightMotor.spin(vex::directionType::fwd);
    }
    	else {
            LeftMotor.stop();
            RightMotor.stop();
            Brain.Screen.clearScreen();
        }
        }
        } 
    task::sleep(100);
}
}

0 Likes

#4

Please respond a soon a s possible Thanks

0 Likes

#5

Yes. It’s in someone else’s old thread and you haven’t told us anything about what’s wrong with it. Better etiquette for things like this is to start your own thread. The formatting (of the code, not the post) could use some love, too, as the indentation scheme is inconsistent. Generally you want to indent one tab further after every { and indent one tab less before every }.

Looking at the actual code, though, at least one thing wrong with it is you are trying to use vision sensor results before taking any snapshots. There are snapshots in the else blocks, though, so by the time it gets back around to the first conditional you will have random data in the vision sensor results from either signature 2 or signature 3.

4 Likes