Society of Robots - Robot Forum
Software => Software => Topic started by: Conscripted on July 02, 2009, 12:42:47 PM
-
I'm working on getting the sharp ir upgrade for the $50 dollar robot working. Things aren't behaving like I think they should. I've got another post that explains the problem. Right now I'm trying to figure the code out. Can some one explain what this piece of code does?
//this function causes scanning servo to center on edge of object
void scan(void)
{
//lower (-) goes right
//higher (+) goes left
//30 far right, 50 straight, 56 far left (until it jams)
/*psuedocode
object is detected
scan right object detected
object not detected
scan left until object detected*/
sharp_IR_reading=a2dConvert8bit(3);
if (sharp_IR_reading > scan_thresh)//object detected
{
if (scan_angle>41) //overflow protection
scan_angle-=2;//scan right
}
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;
}
//servo scan code
servo_scan(scan_angle);
}
I know the code is commented and what it's supposed to do but I can't follow the program. I would like to understand what it is doing and why. Anyone willing to break this down line by line for me? It would be a great help
Conscripted
-
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.
-
Thank you. That was more detailed then I expected. The part I was having trouble with was
{
if (scan_angle>41) //overflow protection
scan_angle-=2;//scan right
}
I could not figure out what "scan_angle-=2" was for. I tried google but didn't find anything.
Thanks again.
Conscripted
-
You can read this for more info on shorthand
http://www2.its.strath.ac.uk/courses/c/subsection3_7_2.html (http://www2.its.strath.ac.uk/courses/c/subsection3_7_2.html)
-
Yeah but, why exactly 41? and why do we need to substract it!
{
if (scan_angle>41) //overflow protection
scan_angle-=2;//scan right
}
And what happens if the object is detected and it is smaller then 41? the robot goes straight?
-
That was really old code I wrote ages ago . . .
41 was the value I sent to the servo which made it rotate to the angle I subjectively decided was optimal.
If the object is detected the robot goes straight. If not, it spins in a circle looking for an object.