Author Topic: Sonar Readings fluctuation  (Read 1288 times)

0 Members and 1 Guest are viewing this topic.

Offline dualTopic starter

  • Jr. Member
  • **
  • Posts: 43
  • Helpful? 0
Sonar Readings fluctuation
« on: September 29, 2009, 12:55:55 PM »
Hi, I have three Maxbotix LZ1 sonars installed in front of my robot that I use for avoiding obstacles. I also have an LCD that prints out the ADC values read from each of the sonars. The problem is (it may or may not be all three of my sensors malfunctioning) the readings fluctuate so much.

I have actually implemented a software averaging Nth order filter but it still fluctuates from very low to high even if I leave the sonars stationary, say from 10 to 255. I use AT90USB646 running at 16Mhz no prescale coded in AVRStudio 4.15. I have the ADC prescaled by 128 using the AVCC as reference and using all 10-bits thinking that with a slow reading ADC and using a 4th order Averaging filter would mean much less noise.

Here are my codes:

Code: [Select]

void ADC_INIT(void)
{
  ADCSRA |= (1 << ADPS0)|(1 << ADPS1)|(1<<ADPS2);
//ADCSRA |= (1<<ADLAR);
ADMUX |= (1 << REFS0);   
ADCSRA |= (1 << ADEN);

}


void readfrontsonar(void)
{
//int i = 1;
ADMUX &=~0x0f;
//ADMUX |= (port << MUX0);
ADMUX |= (1<<MUX0);
//ADMUX |= (1<<MUX1);
//ADMUX |= (1<<MUX2);
ADCSRA |= (1<<ADSC);
while (ADCSRA & (1<<ADSC));

frontsonarsum = frontsonarsum - frontsonarfilt + ADCW;
frontsonarfilt = frontsonarsum >> 5;
frontsonar = frontsonarfilt;
}


void readleftsonar(void)
{
ADMUX &=~0x0f;
//ADMUX |= (port << MUX0);
//ADMUX |= (1<<MUX0);
//ADMUX |= (1<<MUX1);
ADMUX |= (1<<MUX2);
ADCSRA |= (1<<ADSC);
while (ADCSRA & (1<<ADSC));

leftsonarsum = leftsonarsum - leftsonarfilt + ADCW;
leftsonarfilt = leftsonarsum >> 5;
leftsonar = leftsonarfilt;

}

void readrightsonar(void)
{
ADMUX &=~0x0f;
//ADMUX |= (port << MUX0);
ADMUX |= (1<<MUX0);
//ADMUX |= (1<<MUX1);
ADMUX |= (1<<MUX2);
ADCSRA |= (1<<ADSC);
while (ADCSRA & (1<<ADSC));

rightsonarsum = rightsonarsum - rightsonarfilt + ADCW;
rightsonarfilt = rightsonarsum >> 5;
rightsonar = rightsonarfilt;


}

///In Main///

while(1)
{
readfrontsonar();
readleftsonar();
readrightsonar();

LCD_COMMAND(LCD_CLEAR_HOME); // Clear LCD screen, send cursor to start
sprintf(out,"%d %d %d",leftsonar, frontsonar, rightsonar);
LCD_STRING(out);
_delay_ms(100);
}

All of the variables that I use is global and int.

Offline msprague

  • Jr. Member
  • **
  • Posts: 35
  • Helpful? 1
Re: Sonar Readings fluctuation
« Reply #1 on: September 29, 2009, 01:44:06 PM »
There is some helpful information here:
http://www.maxbotix.com/MaxSonar-EZ1__FAQ.html
Especially question #6 may apply.  Are all three sensors "free running"?  That would cause the behavior you are experiencing.

 


Get Your Ad Here