Author Topic: Trying to understand the sharp ir code  (Read 1882 times)

0 Members and 1 Guest are viewing this topic.

Offline ConscriptedTopic starter

  • Robot Overlord
  • ****
  • Posts: 289
  • Helpful? 10
Trying to understand the sharp ir code
« 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?

Code: [Select]
//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

Offline billhowl

  • Supreme Robot
  • *****
  • Posts: 375
  • Helpful? 32
  • Your success is in your hands.
Re: Trying to understand the sharp ir code
« Reply #1 on: July 03, 2009, 11:35:13 PM »
First of all, you need to know the variables been used



Code: [Select]
//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
Code: [Select]
//************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.

Code: [Select]
//automatically calculates threshold value before run
void autocalibrate(void)
{
scan_thresh=a2dConvert8bit(3);//sensor reading
}


Code: [Select]
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.

Code: [Select]
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.
Code: [Select]
{
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
Code: [Select]

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
Code: [Select]

//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.



Offline ConscriptedTopic starter

  • Robot Overlord
  • ****
  • Posts: 289
  • Helpful? 10
Re: Trying to understand the sharp ir code
« Reply #2 on: July 04, 2009, 06:26:34 AM »
Thank you. That was more detailed then I expected. The part I was having trouble with was
Code: [Select]
{
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

Offline billhowl

  • Supreme Robot
  • *****
  • Posts: 375
  • Helpful? 32
  • Your success is in your hands.
Re: Trying to understand the sharp ir code
« Reply #3 on: July 04, 2009, 08:31:18 AM »
You can read this for more info on shorthand
http://www2.its.strath.ac.uk/courses/c/subsection3_7_2.html

Offline SUTO the G

  • Jr. Member
  • **
  • Posts: 42
  • Helpful? 0
Re: Trying to understand the sharp ir code
« Reply #4 on: February 28, 2013, 12:40:03 PM »
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?

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,632
  • Helpful? 169
    • Society of Robots
Re: Trying to understand the sharp ir code
« Reply #5 on: March 02, 2013, 08:13:43 PM »
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.

 


Get Your Ad Here