void findBall(int color) { camera_update(); display_clear(); int count = 1; int objNum = get_object_count(color); while(objNum == 0) { camera_update(); objNum = get_object_count(color); } printf("%d time finding a ball", count); point2 objCen = get_object_center(color, 0); int errorX = 0, errorY = 0; errorX = OFFSET_X - objCen.x; errorY = OFFSET_Y - objCen.y; while(!(a_button_clicked())) { if(BALL_NUM_BOX >= 2) break; if(errorX > -4 && errorX < 4 && errorY > -4 && errorY < 4) break; int turnLM = -1 * errorX * P_X + errorY * P_Y; int turnRM = errorX * P_X + errorY * P_Y; if(turnLM > -1 * MINS && turnLM < 0) turnLM = -1 * MINS; if(turnLM > 0 && turnLM < MINS) turnLM = MINS; if(turnRM > -1 * MINS && turnRM < 0) turnRM = -1 * MINS; if(turnRM > 0 && turnRM < MINS) turnRM = MINS; motor(LM, turnLM); motor(RM, turnRM); camera_update(); int objArea = get_object_area(color, 0); while(objArea < 200) { ao(); camera_update(); objArea = get_object_area(color, 0); } count++; display_clear(); printf("%d time finding a ball ", count); printf("%d NUM", BALL_NUM_BOX); objCen = get_object_center(color, 0); errorX = OFFSET_X - objCen.x; errorY = OFFSET_Y - objCen.y; } ao(); catchBalls(color); }
//routine to use the camera and see blob position based on standard create start routine void calibrateCamera() { int channel = 0; while (a_button_clicked() == 0) { msleep(2000); camera_update(); printf ("=> object count %d \n", get_object_count(channel)); int i = 0; int shelfPlaces[8] = {0,0,0,0,0,0,0,0}; while (i < get_object_count(channel)) { printf("obj %d, ctr loc, area, shelf plcmt: %d, %d, %d\n", i, get_object_center(channel, i).x, get_object_area(channel, i), getShelfPlacement(get_object_center(channel, i).x)); shelfPlaces[getShelfPlacement(get_object_center(channel, i).x)] += get_object_area(channel, i); i++; } printf("==========================\n"); printf("=RESULTS =\n"); printf("==========================\n"); i = 1; while (i < 8) { printf ("shelfPlace : %d, total cube area : %d\n", i, shelfPlaces[i]); i++; } printf("==========================\n"); printf("click b button for new camera capture, a to exit.\n"); while (b_button_clicked() == 0 && a_button_clicked() == 0 ) { msleep(25); } } printf("done with calibration function.\n"); }
int check_stopping_event(int stopping_event) { if (stopping_event == STOP_BY_ET) { if (analog_et(ET_SENSOR) < 300) { return 1; } } else if (stopping_event == STOP_BY_TOPHAT) { display_printf(0, 0, "%4i", analog(L_TOPHAT)); if (analog(L_TOPHAT) > 750 || analog(R_TOPHAT) > 750) { return 1; } } else if (stopping_event == STOP_BY_CAMERA) { camera_update(); if (get_object_count(0) < 1) { display_printf(0, 0, "No Object Found"); return 0; } if (get_object_area(0, 0) > PINGPONG_THRESHOLD) { display_clear(); display_printf(0, 0, "Object Seen!"); if (get_object_center(0, 0).x > 90 && get_object_center(0, 0).x < 110) { display_printf(0, 1, "Object centered"); return 1; } } } else { printf("Stopping type is not defined!\n"); return -1; } return 0; }
int check(int color){ int otherColor = 1 - color; camera_update(); camera_update(); camera_update(); camera_update(); camera_update(); camera_update(); camera_update(); camera_update(); if(get_object_area(color, 0) > 1700){ set_servo_position(ASV, ASV_BACK); msleep(400); // throw ball back into box set_servo_position(ASV, ASV_CHECK); set_servo_position(SSV, SSV_GUARD); msleep(300); if(check(color)){ set_servo_position(SSV, SSV_BACK); set_servo_position(ASV, ASV_BACK); msleep(400); }else{ set_servo_position(SSV, SSV_BACK); set_servo_position(ASV, ASV_BACK); msleep(300); }// avoid ball stuck in claw return 1; }else if(get_object_area(otherColor, 0) > 1500){ set_servo_position(SSV, SSV_PUSH); msleep(400); set_servo_position(SSV, SSV_BACK); msleep(400); set_servo_position(ASV, ASV_BACK); return 0; }// wrong ball is pushed out else{ set_servo_position(ASV, ASV_BACK); return 2; }// no ball is in claw }
// check the ball: int checkBalls(int color){ camera_open(); camera_update(); if ( get_object_area(color, 0) > 3000 ){ return 0; } return 1; }
int main() { printf("hello\n"); camera_open(LOW_RES); camera_update(); printf("Area: %i\n", get_object_area(0, 0)); CvMat* x; Mat y; return 1; }
inline void blob_update() { // set config file camera_update(); //update green state current.green.x = get_object_center(0 , 0).x; current.green.y = get_object_center(0 , 0).y; current.green.size = get_object_area(0 , 0); //update orange state current.orange.x = get_object_center(1 , 0).x; current.orange.y = get_object_center(1 , 0).y; current.orange.size = get_object_area(1 , 0); //update red state current.red.x = get_object_center(2 , 0).x; current.red.y = get_object_center(2 , 0).y; current.red.size = get_object_area(2 , 0); // update botguy state current.botguy.x = get_object_center(3 , 0).x; current.botguy.y = get_object_center(3 , 0).y; current.botguy.size = get_object_area(3 , 0); // set config file camera_update(); // update pink booster state current.pink.x = get_object_center(0 , 0).x; current.pink.y = get_object_center(0 , 0).y; current.pink.size = get_object_area(0 , 0); // update teal booster state current.teal.x = get_object_center(1 , 0).x; current.teal.y = get_object_center(1 , 0).y; current.teal.size = get_object_area(1 , 0); }
int check(int color) { int recheck = 0; int i = 0; for (i = 0; i < 8; i++) { camera_update(); if(get_object_area(color, 0) > 1200) recheck++; } //display_clear(); //printf("%d recheck", recheck); //while(!(c_button_clicked())); if(recheck >= 1) return 1; else return 0; }
int main(){ camera_open(LOW_RES); camera_load_config("color.config"); while(!(a_button_clicked())){ display_clear(); camera_update(); int objNum = get_object_count(GREEN); if(objNum == 0) printf("NO SUCH OBJECT!!!!!!!!"); else if(objNum > 0){ point2 objCen = get_object_center(GREEN, 0); int objArea = get_object_area(GREEN, 0); printf("X:\t%d\nY:\t%d\n", objCen.x, objCen.y); printf("Area:\t%d\n", objArea); } msleep(100); } return 0; }
int main(){ armUp(); clawOpen(); camera_open(LOW_RES); double start_wait=seconds(); while((seconds()-start_wait)<=25){//wait for poms or for 25 seconds int i=0; while(i<10){//picks latest image from the buffer camera_update(); i++; } if(get_object_count(chan)>0){ break; }else{ stop(0.1); } } while((seconds()-start_wait)<=60){ int area=get_object_area(chan, 0); if(area>=600){ int i=0; while(i<10){ camera_update(); i++; } int x=get_object_center(chan, 0).x; if(x<65){ rightF(0.1, 100, 80); }else{ leftF(0.1, 100, 80); } }else{ break; } } armDown(); clawClose(); armUp(); }
int main() { printf("version 1.9.5\n"); shut_down_in(0.010*1000); int start=seconds();//starting time for two minutes enable_servo(0); set_servo_position(0,2047); straight(1.8, 170);//gets in front of transport rightC(0.28, 100); leftC(0.28, -100); straight(1.65, 170); leftC(0.44, 100); printf("completed first dead reckoning\n"); set_analog_pullup(ETport, 0); //until distance is 5 centimeter /* int claw_threshold = 13; while(analog10(ETport)>claw_threshold){ back(0.1, -10); msleep(1000); printf("move once\n"); printf("%i\n", analog10(ETport)); }*/ back(0.25, -20);//pushes against transport msleep(2000); set_servo_position(0, 500);//puts claw on transport msleep(1000); printf("done with attaching to transport\n"); while(digital(8)==0)//goes forward with transport until it hits the wall { straight(0.1, 100); msleep(10); } printf("at wall\n"); back(0.25, -50);//backs up from wall to deposit transport set_servo_position(0, 2047);//raises claw msleep(1500); while(digital(8)==0){//goes forward until it hits wall for second time straight(0.1, 100); msleep(10); } printf("at wall 2\n"); back(0.35, -100);//goes back and turns left in order to be oriented with left inner wall leftC(0.6, 142); int bump_counter=0; while(digital(8)==0){//hits side wall straight(0.1, 100); msleep(10); } bump_counter++;//has one bump int i=0; while(i<10){//if hits side wall, pause stop(0.2); i++; } back(0.25, -100); leftC(0.46, 100); camera_open(LOW_RES); armUp(); clawOpen(); double start_wait=seconds(); while((seconds()-start_wait)<=25){//wait for poms or for 25 seconds int i=0; while(i<10){//picks latest image from the buffer camera_update(); i++; } if(get_object_count(chan)>0){//if camera sees objects, skip the 25 seconds and go onto picking up stuff break; }else{//if camera doesn't see any objects, keep waiting until 25 seconds is up stop(0.1); } } int x=get_object_center(chan, 0).x;//declares global unchanging variable for the x location of the largest object while((seconds()-start)<=120){//while there is still time left int area=get_object_area(chan, 0);//creates local changing variable. this is th area of the largest object camera sees if(area<=600){//if the object is small enough(far enough), navigate towards object. 600 is threshold int i=0; while(i<10){//buffer updating camera_update(); i++; } navigate_poms(x);//similar to line followig function; gets to poms. } armDown(); clawClose(); armUp(); /* leftF(0.5, 100, 80);//turn and go forward until transport is contacted while(digital(8)==0){ straight(0.1, 100);*/ } /* back(1.5, 100); leftC(0.44, 100); while(digital(8)==0 && digital(9)==1){ straight(0.1, 100); msleep(10); if(digital(9)==0){ rightF(0.1, 100, 40); left(0.1, 100); msleep(10); } } straight(2, 200); rightC(0.44, 100); straight(1, 80); rightC(0.92, 100); while(analog10(ETport<=600)){ msleep(10); } msleep(10000); straight(1, 80); leftC(0.44, 100); straight(2, 200); */ return 0; }
void findBall(){ display_clear(); int temptLMSpeed = LM_MINS; // changeable minimum speed for left motor int temptRMSpeed = RM_MINS; // changeable minimum speed for right morot int lastLMSpeed, lastRMSpeed; // motor speed int lastLMPos, lastRMPos; // motor positions int errorX = 0, errorY = 0; // difference of current ball coordinate and target coordinate int objArea; // area of the largest object in sight point2 objCen; // object center coordinate camera_update(); int objNum = get_object_count(GREEN); while(objNum == 0){ camera_update(); objNum = get_object_count(GREEN); } // find a ball in sight objCen = get_object_center(GREEN, 0); errorX = OFFSET_X - objCen.x; errorY = OFFSET_Y - objCen.y; while(!(a_button_clicked())){ camera_update(); objArea = get_object_area(GREEN, 0); while(objArea < 200){ ao(); camera_update(); objArea = get_object_area(GREEN, 0); } // make sure really a ball is in sight objCen = get_object_center(GREEN, 0); errorX = OFFSET_X - objCen.x; errorY = OFFSET_Y - objCen.y; // find differences between coordinates if(errorX > -3 && errorX < 2 && errorY > -3 && errorY < 2) break; int turnLM = -1 * errorX * P_X + errorY * P_Y; int turnRM = errorX * P_X + errorY * P_Y; // turn for LM, RM if(turnLM == lastLMSpeed && lastLMPos == get_motor_position_counter(LM)) temptLMSpeed = temptLMSpeed + 1; if(turnLM == lastLMSpeed && lastLMPos != get_motor_position_counter(LM)) temptLMSpeed = temptLMSpeed - 1; if(turnLM == -1 * lastLMSpeed && lastLMPos != get_motor_position_counter(LM)) temptLMSpeed = temptLMSpeed - 2; if(turnRM == lastRMSpeed && lastRMPos == get_motor_position_counter(RM)) temptRMSpeed = temptRMSpeed + 1; if(turnRM == lastRMSpeed && lastRMPos != get_motor_position_counter(RM)) temptRMSpeed = temptRMSpeed - 1; if(turnRM == -1 * lastRMSpeed && lastRMPos != get_motor_position_counter(RM)) temptRMSpeed = temptRMSpeed - 2; lastLMSpeed = turnLM; lastLMPos = get_motor_position_counter(LM); lastRMSpeed = turnRM; lastRMPos = get_motor_position_counter(RM); // check if speed is too high or low, thus make modification to minimum speed if(turnLM > -1 * temptLMSpeed && turnLM < 0) turnLM = -1 * temptLMSpeed; if(turnLM > 0 && turnLM < temptLMSpeed) turnLM = temptLMSpeed; if(turnRM > -1 * temptRMSpeed && turnRM < 0) turnRM = -1 * temptRMSpeed; if(turnRM > 0 && turnRM < temptRMSpeed) turnRM = temptRMSpeed; // set minimum speed: avoid unmoving motor(LM, turnLM); motor(RM, turnRM); // move } ao(); catchBall(GREEN); // stop and catch }
int main() { msleep(2500); set_analog_pullup(ET_s , 0); extra_buttons_show(1); // show three extra buttons set_a_button_text("COORDS"); // set the text of various buttons set_b_button_text("POM SIZE"); set_c_button_text("BOTGUY SIZE"); set_x_button_text("CUBE SIZE"); lego.left.port = 0; // set motor ports lego.right.port = 2; camera_open(LOW_RES); camera_update(); while (a_button() == 0) // press the a button to set the coordinates { camera_update(); target.green.x = get_object_center(0 , 0).x; // sets target coordinates (x) target.green.y = get_object_center(0 , 0).y; // sets target coordinates (y) printf("(%d , %d)\n" , target.green.x , target.green.y); msleep(10); } while (b_button() == 0) { camera_update(); target.green.size = get_object_area(0 , 0); target.orange.size = get_object_area(1 , 0); printf("Orange Size = %d" , target.orange.size); printf(" Green Size = %d\n" , target.green.size); } enable_servo(arm_servo); enable_servo(push_servo); enable_servo(basket_servo); set_servo_position(arm_servo , ARM_UP); set_servo_position(push_servo , P_DOWN); set_servo_position(basket_servo , B_UP); printf("(%d , %d)\n" , target.green.x , target.green.y); while(a_button() == 0) { printf("%d , %d , %d\n" , get_left() , get_middle() , get_right()); } while (1) // line follow until poms are seen { blob_update(); t_line_follow(); if (current.orange.size > target.orange.size && current.green.size > target.green.size) break; } blob_update(); get_pom(); // pick up a pom pom_push(); // push it into the basket while (1) // turn to next pom { blob_update(); mav(lego.left.port , 300); mav(lego.right.port , -300); msleep(10); if (current.green.size > target.green.size) break; } blob_update(); get_pom(); // pick up pom pom_push(); // push it into the basket avoid_cubeguy(); // avoid the cube or botguy avoid_booster(); while (1) { blob_update(); t_line_follow(); if (current.orange.size > target.orange.size && current.green.size > target.green.size) break; } blob_update(); pom_push(); while (1) { blob_update(); mav(lego.left.port , 300); mav(lego.right.port , -300); msleep(10); if (current.green.size > target.green.size) break; } blob_update(); get_pom(); pom_push(); avoid_cubeguy(); int start_time = seconds(); int t; while (1) { t_line_follow(); if (seconds() < start_time + t) break; } while (1) { } }
void sort_balls(void) { start_spinner(); while (!side_button()) { msleep(50); camera_update(); camera_update(); if (get_object_count(kCameraChannelOrange) > 0 && get_object_confidence(kCameraChannelOrange, 0) >= 0.95 && get_object_area(kCameraChannelOrange, 0) > 500) { stop_spinner(); set_servo_position(kServoPortSorter, kServoPositionSorterOrange); msleep(200); set_servo_position(kServoPortSorter, kServoPositionSorterCenter); msleep(200); start_spinner(); } else if (get_object_count(kCameraChannelGreen) > 0 && get_object_confidence(kCameraChannelGreen, 0) >= 0.95 && get_object_area(kCameraChannelGreen, 0) > 500) { stop_spinner(); set_servo_position(kServoPortSorter, kServoPositionSorterGreen); msleep(200); set_servo_position(kServoPortSorter, kServoPositionSorterCenter); msleep(200); start_spinner(); } } alloff(); }
int main(int argc, char *argv[]) { int blob_count = 0, i = 0; int main_channel = 2; /* red - botguy */ double time_of_snapshot = 0.0; double start = 0.0; printf("args: %d\n", argc); if (argc > 1) { if (argv[1][0] == '0') main_channel = 0; else if (argv[1][0] == '1') main_channel = 1; else if (argv[1][0] == '2') main_channel = 2; else if (argv[1][0] == '3') main_channel = 3; } for (i = 0; i < argc; i++) { printf("arg[%d] = %s\n", i, argv[i]); } printf("\n"); int area[NUM_TRACKING]; point2 center[NUM_TRACKING]; rectangle blob[NUM_TRACKING]; //int area1 = 0, areaX = 0; //point2 center; init_grid(); //wait_for_light(0); camera_open(); printf("Num channels: %d", get_channel_count()); camera_update(); time_of_snapshot = seconds(); msleep(300); start = seconds(); do { // Initialize memset(center, 0, sizeof(center)); memset(area, 0, sizeof(area)); clear_grid(); // Begin // Do not do anything with the camera until enough time has passed if (seconds() - time_of_snapshot >= 0.1) { camera_update(); time_of_snapshot = seconds(); blob_count = get_object_count(main_channel); //sprintf(msg, "#:%2d", blob_count); diag(); printf("#:%-2d ", blob_count); if (blob_count > count_of(area)) { blob_count = count_of(area); } if (blob_count > 0) { for (i = 0; i < blob_count; ++i) { area[i] = get_object_area(main_channel, i); center[i] = get_object_center(main_channel, i); blob[i] = get_object_bbox(main_channel, i); plot(&blob[i], i + '0'); } for (i = 0; i < blob_count; ++i) { printf(" %3d", blob[i].width * blob[i].height); } for (; i < count_of(blob); ++i) { printf(" "); } printf(" "); for (i = 0; i < blob_count; ++i) { printf(" %2dx%2d", blob[i].width, blob[i].height); } //show_xy(center[0].x, center[0].y); } //printf(" |%3d %4d <-> %4d %3d| %s\n", delta_left, left, right, delta_right, message); printf("%s\n", message); message[0] = 0; diag(); show_grid(); } //printf("The time is: %f %f\n", seconds(), seconds() - start); } while (seconds() - start <= 4.0); camera_close(); return 0; }