Author Topic: ping with axon  (Read 1651 times)

0 Members and 1 Guest are viewing this topic.

Offline pk44Topic starter

  • Jr. Member
  • **
  • Posts: 22
  • Helpful? 0
ping with axon
« on: November 08, 2011, 03:31:59 PM »
I am trying to get the ping sensor to work with the axon using my own code because the official ping code is still in beta. I want to read the pulse from the sensor with a arduino-style pulsein() function and then convert that into range data. while writing my code i found that the actual timer functions in the source code differ from what is in the documentation on the axon web page. I do not think that my digital input or output is working because the activity light on the sensor never blinks. So there are a number of things that could be wrong with my code, but i was hoping someone could help me figure this out, because the only sensors readily available to me are ping ultrasonic sensors. here is my code:

Code: [Select]
#include "SoR_Utils.h"
#include "sensors.c"
#include "rprintf.h"
#include "uart4.h"

int pulsein(char port, int pin, int hl)
{
    if(hl == 0)
    {
        if(port == 'A')
        {
            while(bit_is_set(PINA, pin))
            {
            }
            init_timer1(TIMER_CLK_1);
            while(bit_is_clear(PINA, pin))
            {
            }
            int pulse = get_timer1_overflow()*255+TCNT1;
            reset_timer1();
            return pulse;
        }
        else if(port == 'E')
        {
            while(bit_is_set(PINE, pin))
            {
            }
            init_timer1(TIMER_CLK_1);
            while(bit_is_clear(PINE, pin))
            {
            }
            int pulse = get_timer1_overflow()*255+TCNT1;
            reset_timer1();
            return pulse;
        }
        else if(port == 'H')
        {
            while(bit_is_set(PINH, pin))
            {
            }
            init_timer1(TIMER_CLK_1);
            while(bit_is_clear(PINH, pin))
            {
            }
            int pulse = get_timer1_overflow()*255+TCNT1;
            reset_timer1();
            return pulse;
        }
        else if(port == 'C')
        {
            while(bit_is_set(PINC, pin))
            {
            }
            init_timer1(TIMER_CLK_1);
            while(bit_is_clear(PINC, pin))
            {
            }
            int pulse = get_timer1_overflow()*255+TCNT1;
            reset_timer1();
            return pulse;
        }
    }
    else if(hl == 1)
    {
        if(port == 'A')
        {
            while(bit_is_clear(PINA, pin))
            {
            }
            init_timer1(TIMER_CLK_1);
            while(bit_is_set(PINA, pin))
            {
            }
            int pulse = get_timer1_overflow()*255+TCNT1;
            reset_timer1();
            return pulse;
        }
        else if(port == 'E')
        {
            while(bit_is_clear(PINE, pin))
            {
            }
            init_timer1(TIMER_CLK_1);
            while(bit_is_set(PINE, pin))
            {
            }
            int pulse = get_timer1_overflow()*255+TCNT1;
            reset_timer1();
            return pulse;
        }
        else if(port == 'H')
        {
            while(bit_is_clear(PINH, pin))
            {
            }
            init_timer1(TIMER_CLK_1);
            while(bit_is_set(PINH, pin))
            {
            }
            int pulse = get_timer1_overflow()*255+TCNT1;
            reset_timer1();
            return pulse;
        }
        else if(port == 'C')
        {
            while(bit_is_clear(PINC, pin))
            {
            }
            init_timer1(TIMER_CLK_1);
            while(bit_is_set(PINC, pin))
            {
            }
            int pulse = get_timer1_overflow()*255+TCNT1;
            reset_timer1();
            return pulse;
        }
    }
}
//only attach ping to pin 5
int readpingcm(char port)
{
    long duration;
    long cm;
    if(port == 'A')
    {
        //output
        DDRA = _BV(PA5);
        PORT_OFF(PORTA, 5);
        delay_us(2);
        PORT_ON(PORTA, 5);
        delay_us(5);
        PORT_OFF(PORTA, 5);
        //input
        cbi(DDRA, PA5);
        duration = pulsein('A', 5, 1);
        cm = duration/29/2;
        return cm;
    }
    else if(port == 'E')
    {
        //output
        DDRE = _BV(PE5);
        PORT_OFF(PORTE, 5);
        delay_us(2);
        PORT_ON(PORTE, 5);
        delay_us(5);
        PORT_OFF(PORTE, 5);
        //input
        cbi(DDRE, PE5);
        duration = pulsein('A', 5, 1);
        cm = duration/29/2;
        return cm;
    }
    else if(port == 'H')
    {
        //output
        DDRH = _BV(PH5);
        PORT_OFF(PORTH, 5);
        delay_us(2);
        PORT_ON(PORTH, 5);
        delay_us(5);
        PORT_OFF(PORTH, 5);
        //input
        cbi(DDRH, PH5);
        duration = pulsein('A', 5, 1);
        cm = duration/29/2;
        return cm;
    }
    else if(port == 'C')
    {
        //output
        DDRC = _BV(PC5);
        PORT_OFF(PORTC, 5);
        delay_us(2);
        PORT_ON(PORTC, 5);
        delay_us(5);
        PORT_OFF(PORTC, 5);
        //input
        cbi(DDRC, PC5);
        duration = pulsein('A', 5, 1);
        cm = duration/29/2;
        return cm;
    }
}
int main(void)
{
    uartInit();
        uartSetBaudRate(1, 115200);
    //initialize analog to digital converter
    a2dInit();
    //configure ADC scaling
    a2dSetPrescaler(ADC_PRESCALE_DIV32);
    //configure ADC reference voltage
    a2dSetReference(ADC_REFERENCE_AVCC);

    while(1)
    {
        int range = readpingcm('A');
        rprintfInit(uart1SendByte);
        rprintf("%d",range);
    }
    return 0;
}

thanks for any help!

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: ping with axon
« Reply #1 on: November 08, 2011, 03:58:39 PM »
I've gotten them to work with code much like that at http://www.arduino.cc/en/Tutorial/UltrasoundSensor To do it without looping you'll probably need to use the timer's input capture facility.

One of the problems I had with it was that it needs some time to startup. I can't recall how much it took but I think it was a few milliseconds before the first reading.

I haven't used one for a while now, and this is all straight from memory (which isn't working as well as it used to), so take it for what it's worth.

Joe

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: ping with axon
« Reply #2 on: November 08, 2011, 07:18:14 PM »
WebbotLib supports it as well (http://www.webbot.org.uk) and automatically takes cares of the one-off start up delay as well as the receommended delay between reads to avoid ghost echoes.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline pk44Topic starter

  • Jr. Member
  • **
  • Posts: 22
  • Helpful? 0
Re: ping with axon
« Reply #3 on: November 08, 2011, 09:46:27 PM »
actually, i might not need this anymore. I managed to get arduino running on the axon using the arduino_extras files on avr-developers.org, i skipped the bootloader and used an avr-ispmkii and avrdude. The axon has the same pinout as the arduino mega so i used that as a reference and got it communicating with my computer over Serial1(uart1). I would still like to get my code working though, just to finish it...

Offline pk44Topic starter

  • Jr. Member
  • **
  • Posts: 22
  • Helpful? 0
Re: ping with axon
« Reply #4 on: November 08, 2011, 09:47:47 PM »
sorry, avr-developers.com, not .org. and thanks for your suggestions!

 


data_list