First of all, you need to know the variables been used
//global variables
int sharp_IR_reading=0;
int scan_thresh=0;//threshold value of scanning sensor
int scan_angle=30;//position of scanner, in units of servo command
int max_scan_angle=56;//maximum position scanner can rotate to (57)
sharp_IR_reading is store the value of the current IR sensor reading
scan_angle is store the value of delay units for the servo command
max_scan_angle is store the maximum position value of delay units for the servo command
in SoR_Utils.h
//************DELAY FUNCTIONS************
//wait for X amount of cycles (23 cycles is about .992 milliseconds)
//to calculate: 23/.992*(time in milliseconds) = number of cycles
//or (number of cycles)*.992/23 = time in milliseconds
void delay_cycles(unsigned long int cycles)
{
while(cycles > 0)
cycles--;
}
//***************************************
so for value of 30 is about 1.29 milliseconds and for value of 56 is about 2.415 milliseconds
Next the variable scan_thresh is store the value of the object that you place in front of your IR sensor when you turn on the $50 robot.
//automatically calculates threshold value before run
void autocalibrate(void)
{
scan_thresh=a2dConvert8bit(3);//sensor reading
}
sharp_IR_reading=a2dConvert8bit(3);
a2dConvert8bit(3) is a function define in a2d.c, it read analog input pin 3 and convert to 8 bits which have values of 0 to 255
this value will store in the sharp_IR_reading.
if (sharp_IR_reading > scan_thresh)//object detected
This line of code is to check the value of sharp_IR_reading have greater than the value in variable scan_thresh mean that the object had detected.
{
if (scan_angle>41) //overflow protection
scan_angle-=2;//scan right
}
These line only execute when the statements (sharp_IR_reading > scan_thresh) is TRUE
This line of code is to check the value of scan_angle greater than 41 then scan_angle will be subtracts by 2
else //object not detected
{
if (scan_angle<=max_scan_angle) //maximum servo angle
scan_angle+=2; //scan left
//else //if scanned all the way, this forces it to start over
// scan_angle=30;
}
These line only execute when the statements (sharp_IR_reading > scan_thresh) is FALSE
This line of code is to check the value of scan_angle less than or equal to max_scan_angle then will add 2 to scan_angle
//servo scan code
servo_scan(scan_angle);
This line of code is call the function to move the scanner servo with the scan_angle position.
hope now you can understand the sharp ir code better.