Author Topic: The digital input of a Sharp IR rangefinder  (Read 969 times)

0 Members and 1 Guest are viewing this topic.

Offline Choco_ligerTopic starter

  • Full Member
  • ***
  • Posts: 81
  • Helpful? 3
The digital input of a Sharp IR rangefinder
« on: July 13, 2010, 03:24:41 AM »
Hi everybody,

With my $50 robot, I want to get it to do wall avoidance with the Sharp IR.

So I wanted to know what does the IC read as a number from the sharp IR?

And how can I use this number to relate to distance in centimeters?




Offline billhowl

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 32
  • Your success is in your hands.
Re: The digital input of a Sharp IR rangefinder
« Reply #1 on: July 13, 2010, 03:41:53 AM »
Please read here
http://www.societyofrobots.com/robot_50_robot_sharpIR.shtml

the sharp IR will read by the 'analog input'

Offline Choco_ligerTopic starter

  • Full Member
  • ***
  • Posts: 81
  • Helpful? 3
Re: The digital input of a Sharp IR rangefinder
« Reply #2 on: July 13, 2010, 03:44:49 AM »
But what number will I get in the program?

Or will the number I get be the voltage?


« Last Edit: July 13, 2010, 03:54:23 AM by Choco_liger »

Offline Alfa_Zulu

  • Full Member
  • ***
  • Posts: 92
  • Helpful? 1
Re: The digital input of a Sharp IR rangefinder
« Reply #3 on: July 13, 2010, 05:14:36 AM »
if its an analogue sensor it will give it as a voltage i beleive, well it outputs a changing voltage, if you don't know an equation for it already measure its voltage at know distances and you can use that graph to find an equation that you can use to find the distance in relation to the output voltage. although this is just how i understand it and what I was told

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: The digital input of a Sharp IR rangefinder
« Reply #4 on: July 13, 2010, 05:35:07 AM »
But what number will I get in the program?

Or will the number I get be the voltage?




If you are using a 10 bit ADC, and at 5 volts, low voltage at 0 volts would be zero, and high voltage would be 1024. 2.5 volts would be 512, etc etc. 1024 is the max because that is 2^10

Offline billhowl

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 32
  • Your success is in your hands.
Re: The digital input of a Sharp IR rangefinder
« Reply #5 on: July 13, 2010, 06:28:52 AM »
What number you get will base number of bits you use in your ADC, by default AVR use 10bits will be 0 to 1023, and the $50 robot use 8bits will be 0 to 255.

As for how this numbers to relate to distance in centimeters will base the the datasheet of the Sharp IR range finder that you use. Please read the site for more info
 
http://www.danshope.com/blog/2009/06/sharp-ir-rangefinders-and-arduino-make.html
http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/
« Last Edit: July 13, 2010, 06:31:50 AM by billhowl »

Offline Choco_ligerTopic starter

  • Full Member
  • ***
  • Posts: 81
  • Helpful? 3
Re: The digital input of a Sharp IR rangefinder
« Reply #6 on: July 13, 2010, 08:05:42 PM »
Thanks guys, I think I know what to do now.

Offline Choco_ligerTopic starter

  • Full Member
  • ***
  • Posts: 81
  • Helpful? 3
Re: The digital input of a Sharp IR rangefinder
« Reply #7 on: July 13, 2010, 09:32:28 PM »
Okay sorry for double-posting but I tries the number 64 (equivalent to 40cm) in my code, but my code or IC can't read it for some reason. Is it my code?

It just drives straight so I'm assuming that it can't read the Sharp IR

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
void robot_movement_decision(void);
void robot_scan_position_reset(void);
/************************* DECLARING VARIABLES *****************************/
/**/                                                                     /**/
/**///global variables                                                   /**/
/**/int sharp_IR_reading = 0;                                            /**/
/**/int scan_angle = 45;//position of scanner, in units of servo command /**/
/**/                                                                     /**/
/*********************** END OF DECLARING VARIABLES ************************/


/*************************** START OF THE CODE *****************************/

int main(void) //the main function of code
{
 LED_on(); //turn LED on. Pretty self-explanatory
 robot_scan_position_reset(); //sets the servo to the center
 delay_cycles(42000);
 

  while(1) //loop forever
    {
    sharp_IR_reading = a2dConvert8bit(5); //gets input from sharp IR

//it will go into this loop and it will scan right until it reaches the end   
    while(scan_angle >= 30 && scan_angle < 60)
      {
  delay_cycles(500);
      scan_angle = scan_angle + 1; //move one degree right
      servo_scan(scan_angle); //implements this to a movement of the servo

        if(sharp_IR_reading > 64) //if it's less than about 40cm
        {
        robot_movement_decision(); //decision of which movement
        }
        else
        {
        robot_go_straight(); //if a obstacle is not within 20cm move straight
        }
//end of this loop
      }

//it will go into this loop and it will scan right until it reaches the end   
    while(scan_angle <= 60 && scan_angle > 30)
      {
  delay_cycles(500);
      scan_angle = scan_angle - 1; //move one degree left
      servo_scan(scan_angle); //implements this to a movement of the servo

        if(sharp_IR_reading > 64) //if it's less than about 40cm
        {
        robot_movement_decision(); //decision of which movement
        }
        else
        {
        robot_go_straight(); //if a obstacle is not within 20cm move straight
        }
     //end of this loop
      }

    }
  return 0; //end of code
}

/****************************** END OF THE CODE ****************************/

/*********************** FUNCTION FOR MOVEMENT DECISION ********************/
/**/void robot_movement_decision(void)                                   /**/
/**/{                                                                    /**/                                                 
/**/      //Robot going right                                            /**/
/**/      if(scan_angle > 30 && scan_angle < 40)                         /**/
/**/      {                                                              /**/
/**/      robot_turn_right();                                            /**/
/**/      }                                                              /**/
/**/      //Robot going back                                             /**/
/**/      if(scan_angle > 40 && scan_angle < 50)                         /**/
/**/      {                                                              /**/
/**/      robot_go_back();                                               /**/
/**/      }                                                              /**/
/**/      //Robot going left                                             /**/
/**/      if(scan_angle > 50 && scan_angle < 60)                         /**/
/**/      {                                                              /**/
/**/      robot_turn_left();                                             /**/
/**/      }                                                              /**/
/**/      //Ending of function                                           /**/                                                   
/**/}                                                                    /**/
/*********************** FUNCTION FOR MOVEMENT DECISION ********************/

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

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


And the Header.

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.
*
* SoR Utilities v1, March 10th, 2007
*
****************************************************************************/

//AVR includes
#include <avr/io.h>     // include I/O definitions (port names, pin names, etc)
#include <avr/interrupt.h> // include interrupt support

//AVRlib includes
#include "global.h" // include global settings
//#include "buffer.h" // include buffer function library
//#include "uart.h" // include uart function library
//#include "rprintf.h" // include printf function library
//#include "timer.h" // include timer function library (timing, PWM, etc)
#include "a2d.h" // include A/D converter function library

//define port functions; example: PORT_ON( PORTD, 6);
#define PORT_ON( port_letter, number ) port_letter |= (1<<number)
#define PORT_OFF( port_letter, number ) port_letter &= ~(1<<number)
#define PORT_ALL_ON( port_letter, number ) port_letter |= (number)
#define PORT_ALL_OFF( port_letter, number ) port_letter &= ~(number)
#define FLIP_PORT( port_letter, number ) port_letter ^= (1<<number)
#define PORT_IS_ON( port_letter, number ) ( port_letter & (1<<number) )
#define PORT_IS_OFF( port_letter, number ) !( port_letter & (1<<number) )


//************CONFIGURE PORTS************
//configure ports for input or output - specific to ATmega8
void configure_ports(void)
{
DDRC = 0x00;  //configure all C ports for input
PORTC = 0x00; //make sure pull-up resistors are turned off
DDRD = 0xFF;  //configure all D ports for output
}
//***************************************

//************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--;
}
//***************************************

//*********SIMPLIFIED FUNCTIONS**********
//functions to make coding easier for a beginner
//but could cause port mixup confusion for intermediate users

void LED_on(void)
{
PORT_OFF(PORTD, 4);//turn LED on
}
void LED_off(void)
{
PORT_ON(PORTD, 4);//turn LED off
}
void servo_left(signed long int speed)
{
PORT_ON(PORTD, 0);
delay_cycles(speed);
PORT_OFF(PORTD, 0);//keep off
delay_cycles(200);
}
void servo_right(signed long int speed)
{
PORT_ON(PORTD, 1);
delay_cycles(speed);
PORT_OFF(PORTD, 1);//keep off
delay_cycles(200);
}
void servo_scan(signed long int speed)
{
PORT_ON(PORTD, 2);
delay_cycles(speed);
PORT_OFF(PORTD, 2);//keep off
delay_cycles(200);
}
// Usually in stead of 20 is 25 and 30 is 44.
void robot_turn_left(void)
{
servo_left(20);
servo_right(20);
}
void robot_turn_right(void)
{
servo_left(30);
servo_right(30);
}
void robot_go_straight(void)
{
servo_left(30);
servo_right(20);
}
void robot_go_back(void)
    {
servo_left(20);
servo_right(30);
}
void robot_scan_position_reset(void)
    {
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
servo_scan(45);
}



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

//*************INITIALIZATIONS***********
void initialize(void)
{
//other stuff Im experimenting with for SoR
//uartInit();  // initialize the UART (serial port)
//uartSetBaudRate(9600);// set the baud rate of the UART for our debug/reporting output
//rprintfInit(uartSendByte);// initialize rprintf system

//timerInit(); // initialize the timer system

configure_ports(); // configure which ports are analog, digital, etc.
a2dInit(); // initialize analog to digital converter (ADC)
a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling
a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage

//rprintf("Initialization Complete\r\n");
}
//****************************************

 


Get Your Ad Here

data_list