go away spammer

Author Topic: $50 robot using PING))) Ultrasonic Sensor  (Read 18408 times)

0 Members and 1 Guest are viewing this topic.

Offline Commanderbob

  • Robot Overlord
  • ****
  • Posts: 146
  • Helpful? 2
  • Embeddedmicro.com
    • Embedded Micro
Re: $50 robot using PING))) Ultrasonic Sensor
« Reply #30 on: July 16, 2008, 11:45:34 PM »
OK, you could use timer 1 with port c but it won't be quite as good as the capture unit. I am just glad to help as the first time I had to get this working took a few weeks. I notice with my code it can sometimes get stuck. I added a piece that checks the timer to see if it is over the max. If it is the function returns 0xFFFF. another function will keep trying until it gets a valid reading. This hardly happens but if it does and the time out feature is not there the system gets stuck. Just a word of warning. You should not have a problem as you are reading the pin and not waiting for a capture.
Justin

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: $50 robot using PING))) Ultrasonic Sensor
« Reply #31 on: July 17, 2008, 10:41:12 AM »
cant wait for the entire PING function to be finished . When do you think you'll have the code fully done ( using PortB )  ?
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline Commanderbob

  • Robot Overlord
  • ****
  • Posts: 146
  • Helpful? 2
  • Embeddedmicro.com
    • Embedded Micro
Re: $50 robot using PING))) Ultrasonic Sensor
« Reply #32 on: July 17, 2008, 12:01:28 PM »
I already have it done! For the ATmega128. It may work with out any changes for the $50 robot. A lot of the AVR stuff is compatible.
Justin

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: $50 robot using PING))) Ultrasonic Sensor
« Reply #33 on: July 17, 2008, 12:12:08 PM »
I already have it done! For the ATmega128. It may work with out any changes for the $50 robot. A lot of the AVR stuff is compatible.
Justin

yep just found it before

thanks for uploading it
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline Commanderbob

  • Robot Overlord
  • ****
  • Posts: 146
  • Helpful? 2
  • Embeddedmicro.com
    • Embedded Micro
Re: $50 robot using PING))) Ultrasonic Sensor
« Reply #34 on: July 17, 2008, 12:20:55 PM »
Here is the new improved one with error checking! All you need to do is change the define lines to fit your AVR.
Code: [Select]
#define ULTRA_SONIC_MASK (1<<4)

#define ULTRA_SONIC_OUT (DDRD|=ULTRA_SONIC_MASK)
#define ULTRA_SONIC_IN (DDRD&=~ULTRA_SONIC_MASK)
#define ULTRA_SONIC_SET (PORTD|=ULTRA_SONIC_MASK)
#define ULTRA_SONIC_RESET (PORTD&=~ULTRA_SONIC_MASK)
#define ULTRA_SONIC_CAP_RISING (TCCR1B|=(1<<ICES1))
#define ULTRA_SONIC_CAP_FALLING (TCCR1B&=~(1<<ICES1))
#define ULTRA_SONIC_IS_LOW (!(PIND&ULTRA_SONIC_MASK))
#define ULTRA_SONIC_IS_HIGH (PIND&ULTRA_SONIC_MASK)

void PINT_INIT (void)//call FIRST sets up timer 1 for capture, keeps output compare pins digital I/O
{
    TCCR1A=0;
    TCCR1B=2;
}

uint16_t get_ultra_sonic (void)
{
    uint16_t fcap=0,scap=0,dist=0;//fcap stores first capture, scap stores second
    ULTRA_SONIC_OUT;//set pin to output
    ULTRA_SONIC_SET;//set pin to high
    _delay_us (10);//delay for a pulse
    ULTRA_SONIC_RESET;//set pin low
    ULTRA_SONIC_IN;//set pin to input
    Clear_ICF1;//clear the capture flag
    TCNT1=0;//set timer 1 to 0 to prevent overflow
    ULTRA_SONIC_CAP_RISING;//capture the rising edge of the pulse
    while (ICF1_Empty)//wait for capture
    {
        if (TCNT1>50000) return 0xFFFF;
    }
    Clear_ICF1;//clear the capture flag
    fcap=ICR1;//save the captured value
    ULTRA_SONIC_CAP_FALLING;//capture the falling edge of the pulse
    while (ICF1_Empty)//wait for capture
    {
        if (TCNT1>50000) return 0xFFFF;
    }
    Clear_ICF1;//clear the capture flag
    scap=ICR1;//save the captured value
    dist=scap-fcap;//subtract the timer values to find the pulse lenght
    dist*=0.0850725;//convert to mm
    return dist;
}
uint16_t ultra_sonic (void)//call this function for error checking!
{
    uint16_t dist;
    dist=0xFFFF;
    while (dist==0xFFFF)
    {
        dist=get_ultra_sonic();
        _delay_ms(10);//prevent echos if there was an error.
    }
    return dist;
}
« Last Edit: July 17, 2008, 12:24:00 PM by Commanderbob »

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: $50 robot using PING))) Ultrasonic Sensor
« Reply #35 on: July 17, 2008, 06:07:04 PM »
I have a member tutorial up now (under construction)

http://www.societyofrobots.com/member_tutorials/node/174

Offline Commanderbob

  • Robot Overlord
  • ****
  • Posts: 146
  • Helpful? 2
  • Embeddedmicro.com
    • Embedded Micro
Re: $50 robot using PING))) Ultrasonic Sensor
« Reply #36 on: July 17, 2008, 06:19:09 PM »
You should also put in about timer1 and the input capture unit once you try it. Also put up how you set up timer0 a lot of people don't know how.
Justin

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: $50 robot using PING))) Ultrasonic Sensor
« Reply #37 on: July 17, 2008, 06:25:11 PM »
yeah definitely!  thanks for the help!

Offline Commanderbob

  • Robot Overlord
  • ****
  • Posts: 146
  • Helpful? 2
  • Embeddedmicro.com
    • Embedded Micro
Re: $50 robot using PING))) Ultrasonic Sensor
« Reply #38 on: July 19, 2008, 08:39:19 AM »
Quote
Though only tested on an ATmega8 and ATmega168 is should port over to any AVR 8-bit RISC microcontroller
I used it with an ATmega128. ;)
Justin

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: $50 robot using PING))) Ultrasonic Sensor
« Reply #39 on: July 19, 2008, 10:53:56 AM »
updated  ;)

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: $50 robot using PING))) Ultrasonic Sensor
« Reply #40 on: September 05, 2008, 11:09:17 PM »
posting for reference:


Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
    • Ro-Bot-X Designs
Re: $50 robot using PING))) Ultrasonic Sensor
« Reply #41 on: September 06, 2008, 06:28:38 AM »
This is what I was trying to do for my balancing $50 robot, but failed, because of lack of understanding how low level C works. Pomprocker, this is the help I needed, to write the specific code for reading the Ping sensor in the bare C language. Easy for me to do in Basic and Arduino, hard as hell to do in C. As soon as I will get a little free time to work on the robot, I'll update the tutorial. Thanks.
Check out the uBotino robot controller!

 


Get Your Ad Here