Color sorting

2 replies [Last post]
TaosHigh
Title: NooBot
Joined: 04/09/2015
Posts:
BotPoints: 9
User offline. Last seen 2 years 28 weeks ago.

Having an issue while color sorting. I have a function that basically spins a conveyor belt and then checks the color of the pom in the box. I run this function in a loop in main. It runs once just fine, and works with either color. However, it only runs once. And then the robot stops. It never even reaches the return at the end of main.

Here is the code:

void sort_color(){

ao();
camera_open();
camera_update();
if (get_object_count(0)>0){ //if red
enable_servos;
set_servo_position(3, 360); //sweeps to left
set_servo_position(3, 1040);
msleep(1000);

}else if(get_object_count(1)>0){ //if green
enable_servos();
set_servo_position(3, 1600); //sweeps to right
msleep(1000);
set_servo_position(3, 1040);
msleep(1000);
}else{ //if no pom
mav(0, -1000);
msleep(1200); //spin more to try to find a pom

}
}

Any help would be greatly appreciated. Thanks! :)

dark_knight1234
Title: NooBot
Joined: 04/06/2015
Posts:
BotPoints: 16
User offline. Last seen 2 years 35 weeks ago.

in your program try adding a timed while loop use the code
double start = seconds();
// While the current time minus the starting time is less than 5 seconds
while(seconds() - start < 5.00)
{
// do something
}

this will keep on running your program till the time is up.....

DavidMutchler
Title: NooBot+
Joined: 06/17/2009
Posts:
BotPoints: 24
User offline. Last seen 46 weeks 5 days ago.

If I understand you right, your main looks something like this:


int main() {
    while (some stopping condition) {
        sort_color();
    }
    return 0;
}

Is that right? If so, then your sort_camera function should definitely NOT include the statement:


    camera_open();

Opening the camera should happen only ONCE in the program's run. I am unsure what happens if you attempt to open it again, but I would not be surprised to discover that the program hangs on the second attempt.

Additionally, your call to enable_servos omits the parentheses. It should be:

    enable_servos();

and it should also NOT be in the sort_color function, since you want it to happen only ONCE during the program's run.

Additionally, it looks like you omitted an msleep after the first set_servo_position call (doing two in a row without a sleep in between is pointless).

Fix those and then ask again (and submit your entire program, including main) if that does not work.