Author Topic: Modified 50$ sharp IR code.  (Read 1933 times)

0 Members and 1 Guest are viewing this topic.

Offline ballbreakerTopic starter

  • Full Member
  • ***
  • Posts: 78
  • Helpful? 1
Modified 50$ sharp IR code.
« on: July 20, 2010, 06:59:07 AM »
I've modified the 50$ with sharp ir source code but because i don't have much experience in c, and i don't have robot to test it on yet can someone check my code to see if there are errors?

the robot should avoid obstacles. when it's close to an object and the sensor is right turn right when sensor is left turn left and when sensor is straight turn around

else if object is away from object go straight.

Code: [Select]
//SoR Include
#include "SoR_Utils.h" //includes all the technical stuff

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


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



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);
}


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

int main(void)
{
//LED_on();

initialize();

delay_cycles(42000);//two second wait delay

/*********ADD YOUR CODE BELOW THIS LINE **********/

//find thresholds
autocalibrate();

//LED_off();


while(1)
{
scan();

if (sharp_IR_reading < scan_thresh && scan_angle =< 42)
robot_turn_right();

    else if (sharp_IR_reading < scan_thresh && scan_angle => 55)
    robot_turn_left();


        else if (sharp_IR_reading => scan_thresh)
    robot_go_straight();

else if (sharp_IR_reading < scan_thresh && scan_angle >42 && >55)
robot_turn_right();
robot_turn_right();


delay_cycles(400);//a small delay to prevent crazy oscillations
}
/*********ADD YOUR CODE ABOVE THIS LINE **********/

return 0;
}
For Those About To Rock, We Salute You!

Offline Choco_liger

  • Full Member
  • ***
  • Posts: 81
  • Helpful? 3
Re: Modified 50$ sharp IR code.
« Reply #1 on: July 20, 2010, 05:38:06 PM »
Are you sure your not trying to follow the object?

Because if your sensor is left and something is detected that is further away than the thresh, it will go towards it.

Oh remember that when you do
Code: [Select]
sharp_IR_reading < scan_thresh it's saying if the reading of the sharp_IR is a greater distance than the distance of the thresh.

This is because the sharp IR has a weird output, where the further the distance, the less the number.

Offline ballbreakerTopic starter

  • Full Member
  • ***
  • Posts: 78
  • Helpful? 1
Re: Modified 50$ sharp IR code.
« Reply #2 on: July 21, 2010, 07:15:01 AM »
Quote
the reading of the sharp_IR is a greater distance than the distance of the thresh.
This is because the sharp IR has a weird output, where the further the distance, the less the number.

i didn't knew thank you

so if i reverse the < and> will it work? or should i do more modifications?
For Those About To Rock, We Salute You!

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: Modified 50$ sharp IR code.
« Reply #3 on: July 21, 2010, 07:58:43 AM »
There is a graph in the Sharp sensor data sheet showing output voltage verse distance. Take a look.