void followLine(int distSensorID, int wallBump, int turnAwayRotation, int wallDistance, int walkDistance, int speed, float timeOut){ int turnInRotation = - turnAwayRotation; int walld,lastWalld; int position; float time; printf("Following line.\n"); reset_encoder(SHAFT_LEFT); reset_encoder(SHAFT_RIGHT); wheelLeft(speed); wheelRight(speed); lastWalld=walld=analog(distSensorID); time=seconds(); while((read_encoder(SHAFT_LEFT)+read_encoder(SHAFT_RIGHT) < walkDistance) && (seconds()-time < timeOut)){ walld=analog(distSensorID); readColor(); position = (colorBeneath[LIGHT_SENSOR_LEFT]==myColor)*4 + (colorBeneath[LIGHT_SENSOR_MID]==myColor)*2 + (colorBeneath[LIGHT_SENSOR_RIGHT]==myColor); /*printf("pos=%d ",position); */ /* Cases go here */ if(digital(wallBump)){ printf("banged to the wall!\n"); veer(-turnAwayRotation*WHEEL_VEER*3); }else if(position==6){ printf("veer left\n"); veer(-WHEEL_VEER); }else if(position==4){ printf("veer very left\n"); veer(-2*WHEEL_VEER); }else if(position==3){ printf("veer right\n"); veer(WHEEL_VEER); }else if(position==1){ printf("veer very right\n"); veer(2*WHEEL_VEER); }else if(position==5){ /* Something is going wrong */ printf("Something is wrong here!!!\n"); sleep(CHECK_LINE_LOOP_WAIT); }else if(position==0){ /* Very far off the line, check distance */ if(walld < wallDistance){ /* Distance is too far */ if(walld <= lastWalld){ /* is going out -> turn to the wall*/ printf("veer to wall\n"); veer(turnAwayRotation*WHEEL_VEER*2); }else{ printf("going straight to LINE\n"); sleep(CHECK_LINE_LOOP_WAIT); } }else{ /* Distance is too short */ if(walld >= lastWalld){ /* is going in */ printf("veer out from wall\n"); veer(-turnAwayRotation*WHEEL_VEER*2); }else{ printf("going straight out from wall to LINE\n"); sleep(CHECK_LINE_LOOP_WAIT); } } }else{ /* Otherwise, keep going straight */ printf("going straight\n"); sleep(CHECK_LINE_LOOP_WAIT); } lastWalld=walld; } /* while */ ao(); } void veer(int angle){ servo(SERVO_WHEEL,FRONT_WHEEL_STRAIGHT + angle); sleep(VEER_WAIT); servo(SERVO_WHEEL,FRONT_WHEEL_STRAIGHT); sleep(FRONT_WHEEL_WAIT); }