Squirrels have fuzzy tails.
0 Members and 1 Guest are viewing this topic.
1 //SoR Include2 #include "SoR_Utils.h" //includes all the technical stuff34 //global variables5 int sharp_IR_reading=0;67 int scan_thresh=0;//threshold value of scanning sensor89 int scan_angle=30;//position of scanner, in units of servo command10 int max_scan_angle=56;//maximum position scanner can rotate to (57)111213 //this function causes scanning servo to center on edge of object14 void scan(void)15 {16 //lower (-) goes right17 //higher (+) goes left18 //30 far right, 50 straight, 56 far left (until it jams)1920 /*psuedocode21 object is detected22 scan right object detected23 object not detected24 scan left until object detected*/2526 sharp_IR_reading=a2dConvert8bit(3);2728 if (sharp_IR_reading > scan_thresh)//object detected29 {30 if (scan_angle>41) //overflow protection31 scan_angle-=2;//scan right32 }33 else //object not detected34 {35 if (scan_angle<=max_scan_angle) //maximum servo angle36 scan_angle+=2; //scan left37 //else //if scanned all the way, this forces it to start over38 // scan_angle=30;39 }4041 //servo scan code42 servo_scan(scan_angle);43 }444546 //automatically calculates threshold value before run47 void autocalibrate(void)48 {49 scan_thresh=a2dConvert8bit(3);//sensor reading50 }5152 int main(void)53 {54 //LED_on();5556 initialize();57 58 delay_cycles(42000);//two second wait delay5960 /*********ADD YOUR CODE BELOW THIS LINE **********/6162 //find thresholds63 autocalibrate();6465 //LED_off();666768 while(1)69 {70 scan();71 72 //object on left73 if(scan_angle > 57)74 robot_turn_left();7576 //object on right77 else if(scan_angle < 41)78 robot_turn_right();7980 //object is centered81 else82 robot_go_straight();8384 delay_cycles(400);//a small delay to prevent crazy oscillations85 }86 /*********ADD YOUR CODE ABOVE THIS LINE **********/8788 return 0;89 }
void servo_scan(signed long int speed) { PORT_ON(PORTD, 2); delay_cycles(speed); PORT_OFF(PORTD, 2);//keep off delay_cycles(200); }