Author Topic: Sharp IR object avoidance(code)  (Read 2282 times)

0 Members and 1 Guest are viewing this topic.

Offline greasemonkey94Topic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 1
Sharp IR object avoidance(code)
« on: November 15, 2010, 09:08:20 AM »
This is actually most of bukowski's modified code with some of my own additions.
Please check to see if there are any problems.
When i object is centered i'm assuming it means that the object is right in front of the robot,but in the code,if this happens it is instructed to drive straight ahead,this is good for robot fighting competitions but for obstacle avoidance i instructed it to turn right.
i may be completely wrong about the previous problem so please clarify..

original code--
Quote
http://s236.photobucket.com/albums/ff16/robvandiver/?action=view&current=121107_1201.flv

http://s236.photobucket.com/albums/ff16/robvandiver/?action=view&current=121107_1157.flv

Hope those links work.

Ok, so this is $50 robot with the sharp IR rangefinder upgrade version 2: the object avoider.
Still working out some kinks, like I have to get the rangefinder with the longer range, and finding "the perfect range" to work with. Right now Im still autocalibrating it every time I turn it on. Im sure the code could be better, I just changed a couple of lines, but not too bad for someone that knows nothing about anything!

Code:

/****************************************************************************
*
*   Copyright (c) 2007 www.societyofrobots.com
*   (please link back if you use this code!)
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License version 2 as
*   published by the Free Software Foundation.
*
*   Alternatively, this software may be distributed under the terms of BSD
*   license.
*
*   $50 Robot with Sharp IR using Stampy Technology v1, May 19th, 2007
*   Simple case-based method for a robot to do edge detection.
*
*
****************************************************************************/

//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)
      
         robot_turn_right();
   

      //object is centered
      else
         robot_go_straight();

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

   return 0;
   }


/*********************COMMAND LIST*************************

delay_cycles(cycles);
Delays - you can make your robot wait for a certain amount of time with this function.
Put the number of computational cycles to delay in the ().
23 cycles is about .992 milliseconds
to calculate: 23/.992*(time in milliseconds to delay) = cycles
Check servo datasheet where it says: 'Direction: Clockwise/Pulse Traveling 1500 to 1900usec'

servo_left(speed); and servo_right(speed);
Commands your servos to rotate at a certain speed.
Vary speed (which represents a delay in cycles) from 20 to 50.
Left is for port D0 and right is for port D1.

robot_turn_left(); and robot_turn_right(); and robot_go_straight();
Dont want to deal with speed?
Just call this function and it will 'just work.'

LED_on(); and LED_off();
Turns on and off your LED. The LED is on port D4.
By bringing port D4 low, you are turning on the LED.


variable=a2dConvert8bit(pin);
Reads analog pin. For example, set 'pin' to 5 to read PC5.
'variable' will store the value.

***********************************************************/


anyways, any suggestions for how I could make it work better or anything I did wrong are welcome!






new code




Code: [Select]
/****************************************************************************
*
*   Copyright (c) 2007 [url=http://www.societyofrobots.com]www.societyofrobots.com[/url]
*   (please link back if you use this code!)
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License version 2 as
*   published by the Free Software Foundation.
*
*   Alternatively, this software may be distributed under the terms of BSD
*   license.
*
* $50 Robot with Sharp IR using Stampy Technology v1, May 19th, 2007
* Simple case-based method for a robot to do edge detection.
*
*
****************************************************************************/

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

robot_turn_right();
                else if ((sharp_IR_reading < scan_thresh)
                        robot_turn_left();



//object is centered
else
robot_turn_right();

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

return 0;
}


/*********************COMMAND LIST*************************

delay_cycles(cycles);
Delays - you can make your robot wait for a certain amount of time with this function.
Put the number of computational cycles to delay in the ().
23 cycles is about .992 milliseconds
to calculate: 23/.992*(time in milliseconds to delay) = cycles
Check servo datasheet where it says: 'Direction: Clockwise/Pulse Traveling 1500 to 1900usec'

servo_left(speed); and servo_right(speed);
Commands your servos to rotate at a certain speed.
Vary speed (which represents a delay in cycles) from 20 to 50.
Left is for port D0 and right is for port D1.

robot_turn_left(); and robot_turn_right(); and robot_go_straight();
Dont want to deal with speed?
Just call this function and it will 'just work.'

LED_on(); and LED_off();
Turns on and off your LED. The LED is on port D4.
By bringing port D4 low, you are turning on the LED.


variable=a2dConvert8bit(pin);
Reads analog pin. For example, set 'pin' to 5 to read PC5.
'variable' will store the value.

***********************************************************/






If anyone has already built their bot, could you program the above code into the bot and see if it works(if you can :D)
« Last Edit: November 15, 2010, 09:19:33 AM by greasemonkey94 »

Offline ballbreaker

  • Full Member
  • ***
  • Posts: 78
  • Helpful? 1
Re: Sharp IR object avoidance(code)
« Reply #1 on: November 15, 2010, 09:37:40 AM »
judging by the code without testing it (because i wrote a similar one ) i think it will work
For Those About To Rock, We Salute You!